debian-mirror-gitlab/app/services/users/build_service.rb

166 lines
4.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Users
class BuildService < BaseService
2018-11-20 20:47:30 +05:30
delegate :user_default_internal_regex_enabled?,
:user_default_internal_regex_instance,
to: :'Gitlab::CurrentSettings.current_application_settings'
2019-09-30 21:07:59 +05:30
attr_reader :identity_params
2018-11-20 20:47:30 +05:30
2017-08-17 22:00:37 +05:30
def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
2019-09-30 21:07:59 +05:30
@identity_params = params.slice(*identity_attributes)
2017-08-17 22:00:37 +05:30
end
def execute(skip_authorization: false)
2021-06-08 01:23:25 +05:30
@skip_authorization = skip_authorization
2017-08-17 22:00:37 +05:30
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_create_user?
2021-06-08 01:23:25 +05:30
user_params = build_user_params
2017-08-17 22:00:37 +05:30
user = User.new(user_params)
if current_user&.admin?
@reset_token = user.generate_reset_token if params[:reset_password]
if user_params[:force_random_password]
2020-03-13 15:44:24 +05:30
random_password = User.random_password
2017-08-17 22:00:37 +05:30
user.password = user.password_confirmation = random_password
end
end
2020-04-08 14:13:33 +05:30
build_identity(user)
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
Users::UpdateCanonicalEmailService.new(user: user).execute
2017-08-17 22:00:37 +05:30
user
end
private
2021-06-08 01:23:25 +05:30
attr_reader :skip_authorization
2019-09-30 21:07:59 +05:30
def identity_attributes
2019-07-07 11:18:12 +05:30
[:extern_uid, :provider]
end
2020-04-08 14:13:33 +05:30
def build_identity(user)
return if identity_params.empty?
user.identities.build(identity_params)
end
2017-08-17 22:00:37 +05:30
def can_create_user?
2018-03-17 18:26:18 +05:30
(current_user.nil? && Gitlab::CurrentSettings.allow_signup?) || current_user&.admin?
2017-08-17 22:00:37 +05:30
end
# Allowed params for creating a user (admins only)
def admin_create_params
[
:access_level,
:admin,
:avatar,
:bio,
:can_create_group,
:color_scheme_id,
:email,
:external,
:force_random_password,
:hide_no_password,
:hide_no_ssh_key,
:linkedin,
:name,
:password,
:password_automatically_set,
:password_expires_at,
:projects_limit,
:remember_me,
:skip_confirmation,
:skype,
:theme_id,
:twitter,
:username,
2018-11-18 11:00:15 +05:30
:website_url,
2018-12-05 23:21:45 +05:30
:private_profile,
:organization,
:location,
2020-04-22 19:07:51 +05:30
:public_email,
2020-06-23 00:09:42 +05:30
:user_type,
2021-04-17 20:07:23 +05:30
:note,
:view_diffs_file_by_file
2017-08-17 22:00:37 +05:30
]
end
# Allowed params for user signup
def signup_params
[
:email,
:password_automatically_set,
:name,
2020-03-13 15:44:24 +05:30
:first_name,
:last_name,
2017-08-17 22:00:37 +05:30
:password,
2020-04-22 19:07:51 +05:30
:username,
:user_type
2017-08-17 22:00:37 +05:30
]
end
2021-06-08 01:23:25 +05:30
def build_user_params
2017-08-17 22:00:37 +05:30
if current_user&.admin?
user_params = params.slice(*admin_create_params)
if params[:reset_password]
user_params.merge!(force_random_password: true, password_expires_at: nil)
end
else
allowed_signup_params = signup_params
2021-06-08 01:23:25 +05:30
allowed_signup_params << :skip_confirmation if allow_caller_to_request_skip_confirmation?
2017-08-17 22:00:37 +05:30
user_params = params.slice(*allowed_signup_params)
2021-06-08 01:23:25 +05:30
if assign_skip_confirmation_from_settings?(user_params)
2017-08-17 22:00:37 +05:30
user_params[:skip_confirmation] = skip_user_confirmation_email_from_setting
end
2020-03-13 15:44:24 +05:30
fallback_name = "#{user_params[:first_name]} #{user_params[:last_name]}"
if user_params[:name].blank? && fallback_name.present?
user_params = user_params.merge(name: fallback_name)
end
2017-08-17 22:00:37 +05:30
end
2021-01-03 14:25:43 +05:30
user_params[:created_by_id] = current_user&.id
2019-02-15 15:39:39 +05:30
if user_default_internal_regex_enabled? && !user_params.key?(:external)
user_params[:external] = user_external?
end
2020-04-22 19:07:51 +05:30
user_params.delete(:user_type) unless project_bot?(user_params[:user_type])
2017-08-17 22:00:37 +05:30
user_params
end
2021-06-08 01:23:25 +05:30
def allow_caller_to_request_skip_confirmation?
skip_authorization
end
def assign_skip_confirmation_from_settings?(user_params)
user_params[:skip_confirmation].nil?
end
2017-08-17 22:00:37 +05:30
def skip_user_confirmation_email_from_setting
2018-03-17 18:26:18 +05:30
!Gitlab::CurrentSettings.send_user_confirmation_email
2017-08-17 22:00:37 +05:30
end
2018-11-20 20:47:30 +05:30
def user_external?
user_default_internal_regex_instance.match(params[:email]).nil?
end
2020-04-22 19:07:51 +05:30
def project_bot?(user_type)
user_type&.to_sym == :project_bot
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Users::BuildService.prepend_mod_with('Users::BuildService')