Skip to content

Commit a0af56b

Browse files
committed
Post new users to courses and move password management
1 parent 04178b9 commit a0af56b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

app/controllers/api/v8/users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def create
157157
if @user.errors.empty? && @user.save
158158
# TODO: Whitelist origins
159159
UserMailer.email_confirmation(@user, params[:origin], params[:language]).deliver_now
160+
161+
# Post the new user to courses.mooc.fi for password management
162+
if params[:user][:password].present?
163+
@user.post_new_user_to_courses_mooc_fi(params[:user][:password])
164+
end
165+
160166
render json: build_success_response(params[:include_id])
161167
else
162168
errors = @user.errors

app/controllers/users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def create
4545

4646
if @user.errors.empty? && @user.save
4747
UserMailer.email_confirmation(@user).deliver_now
48+
49+
# Post the new user to courses.mooc.fi for password management
50+
if params[:user][:password].present?
51+
@user.post_new_user_to_courses_mooc_fi(params[:user][:password])
52+
end
53+
4854
if @bare_layout
4955
render plain: '<div class="success" style="font-size: 14pt; margin: 10pt;">User account created.</div>', layout: true
5056
else

app/models/user.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,54 @@ def update_password_via_courses_mooc_fi(old_password, new_password)
237237
end
238238
end
239239

240+
def post_new_user_to_courses_mooc_fi(password)
241+
create_url = SiteSetting.value('courses_mooc_fi_create_user_url')
242+
243+
conn = Faraday.new do |f|
244+
f.request :json
245+
f.response :json
246+
end
247+
248+
begin
249+
response = conn.post(create_url) do |req|
250+
req.headers['Content-Type'] = 'application/json'
251+
req.headers['Accept'] = 'application/json'
252+
req.headers['Authorization'] = Rails.application.secrets.tmc_server_secret_for_communicating_to_secret_project
253+
254+
req.body = {
255+
upstream_id: id,
256+
password: password,
257+
}
258+
end
259+
260+
data = response.body
261+
262+
unless data.is_a?(Hash) && data['user'].present?
263+
Rails.logger.error("Creating user in courses.mooc.fi returned unexpected response for user #{self.id}: #{data}")
264+
raise "Creating user in courses.mooc.fi failed for user #{self.id}"
265+
end
266+
267+
unless data['password_set']
268+
Rails.logger.warn("Password was not set for user #{self.id} in courses.mooc.fi")
269+
end
270+
271+
Rails.logger.info("User #{self.id} successfully created in courses.mooc.fi")
272+
true
273+
274+
rescue Faraday::ClientError => e
275+
Rails.logger.error(
276+
"Creating user in courses.mooc.fi failed for user #{self.id}: #{e.response}"
277+
)
278+
false
279+
280+
rescue => e
281+
Rails.logger.error(
282+
"Unexpected error creating user in courses.mooc.fi for user #{self.id}: #{e.message}"
283+
)
284+
false
285+
end
286+
end
287+
240288
def password_reset_key
241289
action_tokens.find { |t| t.action == 'reset_password' }
242290
end

0 commit comments

Comments
 (0)