Skip to content

Commit a900bc5

Browse files
committed
Update password through courses if boolean is set
1 parent e0cdbae commit a900bc5

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

app/controllers/api/v8/users_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ def set_password
230230
end
231231

232232
def maybe_update_password
233+
if @user.password_managed_by_courses_mooc_fi
234+
return @user.update_password_via_courses_mooc_fi(@user.email, user_params[:old_password], user_params[:password])
235+
end
236+
233237
if params[:old_password].present? && params[:password].present?
234238
if !@user.has_password?(params[:old_password])
235239
@user.errors.add(:old_password, 'incorrect')

app/controllers/settings_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def set_email
8686
end
8787

8888
def maybe_update_password(user, user_params)
89+
if user.password_managed_by_courses_mooc_fi
90+
return user.update_password_via_courses_mooc_fi(user.email, user_params[:old_password], user_params[:password])
91+
end
92+
8993
if user_params[:old_password].present? || user_params[:password].present?
9094
if !user.has_password?(user_params[:old_password])
9195
user.errors.add(:old_password, 'incorrect')

app/controllers/users_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def set_password
179179
end
180180

181181
def maybe_update_password(user, user_params)
182+
if user.password_managed_by_courses_mooc_fi
183+
return user.update_password_via_courses_mooc_fi(user.email, user_params[:old_password], user_params[:password])
184+
end
185+
182186
if user_params[:old_password].present? || user_params[:password].present?
183187
if !user.has_password?(user_params[:old_password])
184188
user.errors.add(:old_password, 'incorrect')

app/models/user.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def authenticate_via_courses_mooc_fi(email, submitted_password)
161161
)
162162

163163
data = JSON.parse(response.body)
164-
unless data["authenticated"] == true
164+
unless data['authenticated'] == true
165165
raise "Authentication via courses.mooc.fi failed for #{email}"
166166
end
167167

@@ -176,6 +176,34 @@ def authenticate_via_courses_mooc_fi(email, submitted_password)
176176
raise "Unexpected error while authenticating via courses.mooc.fi: #{e.message}"
177177
end
178178

179+
def update_password_via_courses_mooc_fi(email, old_password, new_password)
180+
update_url = SiteSetting.value('courses_mooc_fi_update_password_url')
181+
182+
response = RestClient.put(
183+
update_url,
184+
{
185+
email: email,
186+
old_password: old_password,
187+
new_password: new_password
188+
}.to_json,
189+
{ content_type: :json, accept: :json }
190+
)
191+
192+
data = JSON.parse(response.body)
193+
194+
unless data['updated'] == true
195+
raise "Updating password via courses.mooc.fi failed for #{email}"
196+
end
197+
198+
true
199+
rescue RestClient::ExceptionWithResponse => e
200+
Rails.logger.error("Updating password via courses.mooc.fi failed for #{email}: #{e.response}")
201+
false
202+
rescue => e
203+
Rails.logger.error("Unexpected error updating password via courses.mooc.fi for #{email}: #{e.message}")
204+
false
205+
end
206+
179207
def password_reset_key
180208
action_tokens.find { |t| t.action == 'reset_password' }
181209
end

0 commit comments

Comments
 (0)