diff --git a/app/controllers/api/v8/users_controller.rb b/app/controllers/api/v8/users_controller.rb index 2dfc8f52f..ec5300e34 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 bf98bfbeb..2d973091c 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 f6490f2e9..e75b78c30 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 937a9ee47..fc0b89d28 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 70f2b05e1..860a100da 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -237,6 +237,55 @@ def update_password_via_courses_mooc_fi(old_password, new_password) end end + def post_new_user_to_courses_mooc_fi(password) + Rails.logger.info("Posting new user #{self.email} to courses.mooc.fi") + 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.email}: #{data}") + raise "Creating user in courses.mooc.fi failed for user #{self.email}" + end + + unless data['password_set'] + Rails.logger.warn("Password was not set for user #{self.email} in courses.mooc.fi") + end + + Rails.logger.info("User #{self.email} successfully created in courses.mooc.fi") + true + + rescue Faraday::ClientError => e + Rails.logger.error( + "Creating user in courses.mooc.fi failed for user #{self.email}: #{e.response}" + ) + false + + rescue => e + Rails.logger.error( + "Unexpected error creating user in courses.mooc.fi for user #{self.email}: #{e.message}" + ) + false + end + end + def password_reset_key action_tokens.find { |t| t.action == 'reset_password' } end diff --git a/config/site.defaults.yml b/config/site.defaults.yml index 634499df6..36ea5a787 100644 --- a/config/site.defaults.yml +++ b/config/site.defaults.yml @@ -138,3 +138,8 @@ course_instruction_page: http://mooc.fi/courses/general/ohjelmointi/ # Teacher manual link teacher_manual_url: http://testmycode.github.io/tmc-server/usermanual/ + +# URLs for password management via courses.mooc.fi +courses_mooc_fi_auth_url: +courses_mooc_fi_update_password_url: +courses_mooc_fi_create_user_url: diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 190fc86e0..35ec59241 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -52,6 +52,8 @@ password: 'xoox', password_repeat: 'xoox' } + # Mock the courses.mooc.fi integration to avoid HTTP calls in tests + allow_any_instance_of(User).to receive(:post_new_user_to_courses_mooc_fi).and_return(true) end it 'should create a new user account' do