Skip to content
Open
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
6 changes: 6 additions & 0 deletions app/controllers/api/v8/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions app/controllers/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<div class="success" style="font-size: 14pt; margin: 10pt;">User account created.</div>', layout: true
else
Expand Down
3 changes: 0 additions & 3 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading