diff --git a/app/controllers/api/v8/users_controller.rb b/app/controllers/api/v8/users_controller.rb index 2dfc8f52..ec5300e3 100644 --- a/app/controllers/api/v8/users_controller.rb +++ b/app/controllers/api/v8/users_controller.rb @@ -157,6 +157,12 @@ def create if @user.errors.empty? && @user.save # TODO: Whitelist origins UserMailer.email_confirmation(@user, params[:origin], params[:language]).deliver_now + + # Post the new user to courses.mooc.fi for password management + if params[:user][:password].present? + @user.post_new_user_to_courses_mooc_fi(params[:user][:password]) + end + render json: build_success_response(params[:include_id]) else errors = @user.errors diff --git a/app/controllers/points_controller.rb b/app/controllers/points_controller.rb index bf98bfbe..2d973091 100644 --- a/app/controllers/points_controller.rb +++ b/app/controllers/points_controller.rb @@ -47,13 +47,6 @@ def index end end - def refresh_gdocs - authorize! :refresh, @course - @sheetname = params[:id] - @course = Course.find(params[:course_id]) - @notifications = @course.refresh_gdocs_worksheet @sheetname - end - def show @sheetname = params[:id] @course = Course.find(params[:course_id]) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f6490f2e..e75b78c3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -45,6 +45,12 @@ def create if @user.errors.empty? && @user.save UserMailer.email_confirmation(@user).deliver_now + + # Post the new user to courses.mooc.fi for password management + if params[:user][:password].present? + @user.post_new_user_to_courses_mooc_fi(params[:user][:password]) + end + if @bare_layout render plain: '
User account created.
', layout: true else diff --git a/app/models/ability.rb b/app/models/ability.rb index 937a9ee4..fc0b89d2 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -20,9 +20,6 @@ def initialize(user) can :disable, Organization can :rerun, Submission - can :refresh_gdocs_spreadsheet, Course do |c| - c.spreadsheet_key.present? - end can :access, :pghero can :read_vm_log, Submission can :read, :instance_state diff --git a/app/models/user.rb b/app/models/user.rb index 70f2b05e..b8dd1704 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -237,6 +237,54 @@ def update_password_via_courses_mooc_fi(old_password, new_password) end end + def post_new_user_to_courses_mooc_fi(password) + create_url = SiteSetting.value('courses_mooc_fi_create_user_url') + + conn = Faraday.new do |f| + f.request :json + f.response :json + end + + begin + response = conn.post(create_url) do |req| + req.headers['Content-Type'] = 'application/json' + req.headers['Accept'] = 'application/json' + req.headers['Authorization'] = Rails.application.secrets.tmc_server_secret_for_communicating_to_secret_project + + req.body = { + upstream_id: id, + password: password, + } + end + + data = response.body + + unless data.is_a?(Hash) && data['user'].present? + Rails.logger.error("Creating user in courses.mooc.fi returned unexpected response for user #{self.id}: #{data}") + raise "Creating user in courses.mooc.fi failed for user #{self.id}" + end + + unless data['password_set'] + Rails.logger.warn("Password was not set for user #{self.id} in courses.mooc.fi") + end + + Rails.logger.info("User #{self.id} successfully created in courses.mooc.fi") + true + + rescue Faraday::ClientError => e + Rails.logger.error( + "Creating user in courses.mooc.fi failed for user #{self.id}: #{e.response}" + ) + false + + rescue => e + Rails.logger.error( + "Unexpected error creating user in courses.mooc.fi for user #{self.id}: #{e.message}" + ) + false + end + end + def password_reset_key action_tokens.find { |t| t.action == 'reset_password' } end