Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions app/controllers/concerns/account_credentials_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# typed: strict
# frozen_string_literal: true

# The email and password fields of the account form, which both the user's own account page and
# the participant settings page submit.
module AccountCredentialsForm
extend T::Sig
extend ActiveSupport::Concern
extend T::Helpers
requires_ancestor { ApplicationController }

CREDENTIAL_FIELDS = T.let(%w[email email_repeat old_password password password_repeat].freeze, T::Array[String])

private
# Applies the submitted email and password fields to an existing account and returns whether
# a password change was requested.
sig { params(user: User, user_params: T.untyped).returns(T::Boolean) }
def apply_credentials(user, user_params)
if user.managed_externally?
refuse_managed_credential_changes(user, user_params)
return false
end

set_email(user, user_params)
maybe_update_password(user, user_params)
end

sig { params(user: User, user_params: T.untyped).void }
def set_email(user, user_params)
return if !user.new_record? && user_params[:email_repeat].blank?

if user_params[:email].blank?
user.errors.add(:email, 'needed')
elsif user_params[:email] != user_params[:email_repeat]
user.errors.add(:email_repeat, 'did not match')
else
user.email = user_params[:email].strip
end
end

# A managed account's form has no credential fields, so any submitted come from a stale page.
sig { params(user: User, user_params: T.untyped).void }
def refuse_managed_credential_changes(user, user_params)
return if CREDENTIAL_FIELDS.none? { |field| user_params[field].present? }

user.errors.add(:base, User::MANAGED_CREDENTIALS_MESSAGE)
end

sig { params(user: User, user_params: T.untyped).returns(T::Boolean) }
def maybe_update_password(user, user_params)
if user_params[:old_password].present? || user_params[:password].present?
if user.externally_managed_without_target?
user.errors.add(:base, 'This account is being migrated; password changes are temporarily unavailable.')
return false
end
if !user.has_password?(user_params[:old_password])
user.errors.add(:old_password, 'incorrect')
elsif user_params[:password] != user_params[:password_repeat]
user.errors.add(:password_repeat, 'did not match')
elsif user_params[:password].blank?
user.errors.add(:password, 'cannot be empty')
elsif user_params[:password].length > 1000
user.errors.add(:password, 'cannot be over 1000 characters')
else
user.password = user_params[:password]
end
true
else
false
end
end
end
52 changes: 3 additions & 49 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class SettingsController < ApplicationController
extend T::Sig
include AccountCredentialsForm
skip_authorization_check

before_action :set_user
Expand All @@ -20,13 +21,12 @@ def update
User.transaction do
email_before = user.email
@email_before = T.let(email_before, T.nilable(String))
set_email
password_changed = maybe_update_password(user, params[:user])
password_changed = apply_credentials(user, params.fetch(:user, {}))
set_user_fields

if user.errors.empty? && user.save
# Password changed locally: migrate the user to courses.mooc.fi
user.post_new_user_to_courses_mooc_fi(params[:user][:password]) if password_changed && !user.managed_externally?
user.post_new_user_to_courses_mooc_fi(params[:user][:password]) if password_changed
RecentlyChangedUserDetail.email_changed.create!(old_value: email_before, new_value: user.email, username: user.login, user_id: user.id) unless T.must(email_before.casecmp(user.email)).zero?
flash[:notice] = if password_changed
'Changes saved and password changed'
Expand Down Expand Up @@ -84,52 +84,6 @@ def set_user
@user = T.let(User.find(params[:participant_id]), T.nilable(User))
end

sig { void }
def set_email
user = T.must(@user)
user_params = params[:user]

return if !user.new_record? && user_params[:email_repeat].blank?

if user_params[:email].blank?
user.errors.add(:email, 'needed')
elsif user_params[:email] != user_params[:email_repeat]
user.errors.add(:email_repeat, 'did not match')
else
user.email = user_params[:email].strip
end
end

sig { params(user: User, user_params: T.untyped).returns(T::Boolean) }
def maybe_update_password(user, user_params)
if user_params[:old_password].present? || user_params[:password].present?
if user.managed_externally?
unless user.update_password_via_courses_mooc_fi(user_params[:old_password], user_params[:password])
user.errors.add(:password, 'could not be changed. Please check your current password and try again.')
return false
end
return true
elsif user.externally_managed_without_target?
user.errors.add(:base, 'This account is being migrated; password changes are temporarily unavailable.')
return false
end
if !user.has_password?(user_params[:old_password])
user.errors.add(:old_password, 'incorrect')
elsif user_params[:password] != user_params[:password_repeat]
user.errors.add(:password_repeat, 'did not match')
elsif user_params[:password].blank?
user.errors.add(:password, 'cannot be empty')
elsif user_params[:password].length > 1000
user.errors.add(:password, 'cannot be over 1000 characters')
else
user.password = user_params[:password]
end
true
else
false
end
end

sig { returns(T.nilable(T::Hash[T.untyped, T.untyped])) }
def set_user_fields
return if params[:user_field].nil?
Expand Down
54 changes: 4 additions & 50 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Presents the "register user" and "edit profile" views.
class UsersController < ApplicationController
extend T::Sig
include AccountCredentialsForm
skip_authorization_check
# Stale legacy deletion forms must still redirect, whatever the forgery protection strategy.
skip_forgery_protection only: :redirect_to_account_deletion
Expand Down Expand Up @@ -51,7 +52,7 @@ def create

@user.login = SecureRandom.uuid

set_email
set_email(@user, params[:user])
set_password
set_user_fields

Expand Down Expand Up @@ -87,13 +88,12 @@ def update
email_before = @user.email
@email_before = T.let(email_before, T.nilable(String))

set_email
password_changed = maybe_update_password(@user, params[:user])
password_changed = apply_credentials(@user, params.fetch(:user, {}))
set_user_fields

if @user.errors.empty? && @user.save
# Password changed locally: migrate the user to courses.mooc.fi
@user.post_new_user_to_courses_mooc_fi(params[:user][:password]) if password_changed && !@user.managed_externally?
@user.post_new_user_to_courses_mooc_fi(params[:user][:password]) if password_changed
RecentlyChangedUserDetail.email_changed.create!(old_value: email_before, new_value: @user.email, username: @user.login, user_id: @user.id) unless T.must(email_before.casecmp(@user.email)).zero?
flash[:notice] = if password_changed
'Changes saved and password changed'
Expand Down Expand Up @@ -140,22 +140,6 @@ def redirect_to_account_deletion
end

private
sig { void }
def set_email
user = T.must(@user)
user_params = params[:user]

return if !user.new_record? && user_params[:email_repeat].blank?

if user_params[:email].blank?
user.errors.add(:email, 'needed')
elsif user_params[:email] != user_params[:email_repeat]
user.errors.add(:email_repeat, 'did not match')
else
user.email = user_params[:email].strip
end
end

sig { void }
def set_password
user = T.must(@user)
Expand All @@ -171,36 +155,6 @@ def set_password
end
end

sig { params(user: User, user_params: T.untyped).returns(T::Boolean) }
def maybe_update_password(user, user_params)
if user_params[:old_password].present? || user_params[:password].present?
if user.managed_externally?
unless user.update_password_via_courses_mooc_fi(user_params[:old_password], user_params[:password])
user.errors.add(:password, 'could not be changed. Please check your current password and try again.')
return false
end
return true
elsif user.externally_managed_without_target?
user.errors.add(:base, 'This account is being migrated; password changes are temporarily unavailable.')
return false
end
if !user.has_password?(user_params[:old_password])
user.errors.add(:old_password, 'incorrect')
elsif user_params[:password] != user_params[:password_repeat]
user.errors.add(:password_repeat, 'did not match')
elsif user_params[:password].blank?
user.errors.add(:password, 'cannot be empty')
elsif user_params[:password].length > 1000
user.errors.add(:password, 'cannot be over 1000 characters')
else
user.password = user_params[:password]
end
true
else
false
end
end

sig { returns(T.nilable(T::Hash[T.untyped, T.untyped])) }
def set_user_fields
return if params[:user_field].nil?
Expand Down
2 changes: 2 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def initialize(user, auth_source: nil)
can :update, User do |u|
u == user
end
# Its owner changes a managed account's email and password on courses.mooc.fi.
cannot :update_credentials, User, &:managed_externally?

cannot :read, Course
can :read, Course do |c|
Expand Down
30 changes: 29 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class User < ApplicationRecord

COURSES_MOOC_FI_USER_ID_FORMAT = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/

MANAGED_CREDENTIALS_MESSAGE = 'The email address and password of this account are changed on courses.mooc.fi.'

# Lookups by courses.mooc.fi id (token authentication) match exactly, so store the canonical form.
normalizes :courses_mooc_fi_user_id, with: ->(id) { id.strip.downcase }

Expand Down Expand Up @@ -501,10 +503,36 @@ def courses_mooc_fi_profile_url
courses_mooc_fi_url("/manage/users/#{courses_mooc_fi_user_id}")
end

# This user's summary page in the mooc.fi admin tools. Nil when mooc.fi isn't configured.
sig { returns(T.nilable(String)) }
def moocfi_admin_url
base_url = User.moocfi_base_url
"#{base_url}/admin/users/#{id}/summary" if base_url
end

# courses.mooc.fi's root URL without a trailing slash. Nil when it isn't configured.
sig { returns(T.nilable(String)) }
def self.courses_mooc_fi_base_url
SiteSetting.value('courses_mooc_fi_base_url').to_s.chomp('/').presence
site_base_url('courses_mooc_fi_base_url')
end

# mooc.fi's root URL without a trailing slash. Nil when it isn't configured.
sig { returns(T.nilable(String)) }
def self.moocfi_base_url
site_base_url('moocfi_base_url')
end

# The site setting `key` as a root URL without a trailing slash. Nil when it isn't configured.
sig { params(key: String).returns(T.nilable(String)) }
private_class_method def self.site_base_url(key)
SiteSetting.value(key).to_s.strip.chomp('/').presence
end

# Where users of a managed account change its email and password. Nil when courses.mooc.fi isn't configured.
sig { returns(T.nilable(String)) }
def self.courses_mooc_fi_account_settings_url
base_url = courses_mooc_fi_base_url
"#{base_url}/user-settings/account" if base_url
end

# Where users delete their account, which also deletes it here. Nil when courses.mooc.fi isn't configured.
Expand Down
11 changes: 9 additions & 2 deletions app/views/participants/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@
end %>
<li><%= button_to 'Force migrate to courses.mooc.fi', force_migrate_to_courses_mooc_fi_participant_path, method: :post, class: 'btn btn-primary', data: { confirm: confirm_text } %> (shown to you because you're an admin)</li>
<% end %>
<% if current_user.administrator? && @user.courses_mooc_fi_profile_url %>
<li><%= link_to 'View on courses.mooc.fi', @user.courses_mooc_fi_profile_url, target: '_blank', rel: 'noopener', class: 'btn btn-primary' %> (shown to you because you're an admin)</li>
<% if current_user.administrator? %>
<% profile_url = @user.courses_mooc_fi_profile_url %>
<% moocfi_url = @user.moocfi_admin_url %>
<% if profile_url %>
<li><%= link_to 'View on courses.mooc.fi', profile_url, target: '_blank', rel: 'noopener', class: 'btn btn-primary' %> (shown to you because you're an admin)</li>
<% end %>
<% if moocfi_url %>
<li><%= link_to 'View on mooc.fi', moocfi_url, target: '_blank', rel: 'noopener', class: 'btn btn-primary' %> (shown to you because you're an admin)</li>
<% end %>
<% end %>
</ul>
</section>
Expand Down
65 changes: 0 additions & 65 deletions app/views/settings/_form.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/settings/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= render partial: 'participants/nav' %>

<%= render :partial => 'settings/form', :user => @user, locals: { hide_login: true } %>
<%= render 'users/form', hide_login: true %>

<%= render 'users/account_deletion', user: @user %>

Expand Down
Loading
Loading