2017-06-13 05:32:21 -04:00
|
|
|
module Users
|
|
|
|
# Service for creating a new user.
|
|
|
|
class UpdateService < BaseService
|
|
|
|
def initialize(current_user, user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@user = user
|
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2017-06-14 05:35:58 -04:00
|
|
|
def execute(skip_authorization: false, &block)
|
|
|
|
assign_attributes(skip_authorization, &block)
|
2017-06-13 05:32:21 -04:00
|
|
|
|
2017-06-14 06:39:58 -04:00
|
|
|
if @user.save || !@user.changed?
|
2017-06-13 05:32:21 -04:00
|
|
|
success
|
|
|
|
else
|
2017-06-14 06:39:58 -04:00
|
|
|
error("User could not be updated #{@user.errors.full_messages.uniq.join('. ')}" )
|
2017-06-13 05:32:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-14 05:35:58 -04:00
|
|
|
def execute!(skip_authorization: false, &block)
|
|
|
|
assign_attributes(skip_authorization, &block)
|
|
|
|
|
2017-06-14 06:39:58 -04:00
|
|
|
@user.save! if @user.changed?
|
2017-06-14 05:35:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assign_attributes(skip_authorization, &block)
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_update_user?
|
|
|
|
|
|
|
|
yield(@user) if block_given?
|
|
|
|
|
|
|
|
@user.assign_attributes(params) if params.any?
|
|
|
|
end
|
|
|
|
|
2017-06-13 05:32:21 -04:00
|
|
|
def can_update_user?
|
|
|
|
current_user == @user || current_user&.admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|