diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-06-26 11:42:44 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-06-26 11:42:44 +0000 |
commit | bff82b784bac7af8cf3a3d8b49fa10ebd1ef67f0 (patch) | |
tree | 2b294a4f5f947843ea6afdb8f31338e2901f5e8f /lib | |
parent | 739a03a825e5c04c7566bc3bb26314b049e403ec (diff) | |
parent | 4d70c479107fc502668a83fc369159062d8efbb8 (diff) | |
download | gitlab-ce-bff82b784bac7af8cf3a3d8b49fa10ebd1ef67f0.tar.gz |
Merge branch 'feature/add-new-services' into 'master'
Add additional user and email services
See merge request !12125
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/internal.rb | 7 | ||||
-rw-r--r-- | lib/api/notification_settings.rb | 5 | ||||
-rw-r--r-- | lib/api/users.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/ldap/access.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/o_auth/user.rb | 2 |
5 files changed, 22 insertions, 16 deletions
diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 479ee16a611..f1c79970ba4 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -132,8 +132,11 @@ module API return { success: false, message: 'Two-factor authentication is not enabled for this user' } end - codes = user.generate_otp_backup_codes! - user.save! + codes = nil + + ::Users::UpdateService.new(user).execute! do |user| + codes = user.generate_otp_backup_codes! + end { success: true, recovery_codes: codes } end diff --git a/lib/api/notification_settings.rb b/lib/api/notification_settings.rb index 992ea5dc24d..5d113c94b22 100644 --- a/lib/api/notification_settings.rb +++ b/lib/api/notification_settings.rb @@ -34,7 +34,10 @@ module API notification_setting.transaction do new_notification_email = params.delete(:notification_email) - current_user.update(notification_email: new_notification_email) if new_notification_email + if new_notification_email + ::Users::UpdateService.new(current_user, notification_email: new_notification_email).execute + end + notification_setting.update(declared_params(include_missing: false)) end rescue ArgumentError => e # catch level enum error diff --git a/lib/api/users.rb b/lib/api/users.rb index c10e3364382..f9555842daf 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -98,7 +98,7 @@ module API authenticated_as_admin! params = declared_params(include_missing: false) - user = ::Users::CreateService.new(current_user, params).execute + user = ::Users::CreateService.new(current_user, params).execute(skip_authorization: true) if user.persisted? present user, with: Entities::UserPublic @@ -156,7 +156,9 @@ module API user_params[:password_expires_at] = Time.now if user_params[:password].present? - if user.update_attributes(user_params.except(:extern_uid, :provider)) + result = ::Users::UpdateService.new(user, user_params.except(:extern_uid, :provider)).execute + + if result[:status] == :success present user, with: Entities::UserPublic else render_validation_error!(user) @@ -234,9 +236,9 @@ module API user = User.find_by(id: params.delete(:id)) not_found!('User') unless user - email = user.emails.new(declared_params(include_missing: false)) + email = Emails::CreateService.new(user, declared_params(include_missing: false)).execute - if email.save + if email.errors.blank? NotificationService.new.new_email(email) present email, with: Entities::Email else @@ -274,8 +276,7 @@ module API email = user.emails.find_by(id: params[:email_id]) not_found!('Email') unless email - email.destroy - user.update_secondary_emails! + Emails::DestroyService.new(user, email: email.email).execute end desc 'Delete a user. Available only for admins.' do @@ -487,9 +488,9 @@ module API requires :email, type: String, desc: 'The new email' end post "emails" do - email = current_user.emails.new(declared_params) + email = Emails::CreateService.new(current_user, declared_params).execute - if email.save + if email.errors.blank? NotificationService.new.new_email(email) present email, with: Entities::Email else @@ -505,8 +506,7 @@ module API email = current_user.emails.find_by(id: params[:email_id]) not_found!('Email') unless email - email.destroy - current_user.update_secondary_emails! + Emails::DestroyService.new(current_user, email: email.email).execute end desc 'Get a list of user activities' diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb index 54a5b1d31cd..8779577258b 100644 --- a/lib/gitlab/ldap/access.rb +++ b/lib/gitlab/ldap/access.rb @@ -16,8 +16,8 @@ module Gitlab def self.allowed?(user) self.open(user) do |access| if access.allowed? - user.last_credential_check_at = Time.now - user.save + Users::UpdateService.new(user, last_credential_check_a: Time.now).execute + true else false diff --git a/lib/gitlab/o_auth/user.rb b/lib/gitlab/o_auth/user.rb index 7307f8c2c87..b3f453e506d 100644 --- a/lib/gitlab/o_auth/user.rb +++ b/lib/gitlab/o_auth/user.rb @@ -32,7 +32,7 @@ module Gitlab block_after_save = needs_blocking? - gl_user.save! + Users::UpdateService.new(gl_user).execute! gl_user.block if block_after_save |