2018-07-18 12:03:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-13 04:47:52 -04:00
|
|
|
module Users
|
|
|
|
class BuildService < BaseService
|
2018-08-30 08:53:06 -04:00
|
|
|
delegate :user_default_internal_regex_enabled?,
|
|
|
|
:user_default_internal_regex_instance,
|
|
|
|
to: :'Gitlab::CurrentSettings.current_application_settings'
|
|
|
|
|
2017-04-13 04:47:52 -04:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2017-04-24 17:12:14 -04:00
|
|
|
def execute(skip_authorization: false)
|
|
|
|
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_create_user?
|
2017-04-13 04:47:52 -04:00
|
|
|
|
2017-04-26 03:54:33 -04:00
|
|
|
user_params = build_user_params(skip_authorization: skip_authorization)
|
|
|
|
user = User.new(user_params)
|
2017-04-13 04:47:52 -04:00
|
|
|
|
|
|
|
if current_user&.admin?
|
2017-04-26 03:54:33 -04:00
|
|
|
@reset_token = user.generate_reset_token if params[:reset_password]
|
2017-04-13 04:47:52 -04:00
|
|
|
|
2017-04-26 03:54:33 -04:00
|
|
|
if user_params[:force_random_password]
|
2017-04-13 04:47:52 -04:00
|
|
|
random_password = Devise.friendly_token.first(Devise.password_length.min)
|
|
|
|
user.password = user.password_confirmation = random_password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-02 09:33:50 -04:00
|
|
|
identity_attrs = params.slice(*identity_params)
|
2017-04-13 04:47:52 -04:00
|
|
|
|
2018-11-15 07:32:17 -05:00
|
|
|
unless identity_attrs.empty?
|
2017-04-13 04:47:52 -04:00
|
|
|
user.identities.build(identity_attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-04-02 09:33:50 -04:00
|
|
|
def identity_params
|
|
|
|
[:extern_uid, :provider]
|
|
|
|
end
|
|
|
|
|
2017-04-13 04:47:52 -04:00
|
|
|
def can_create_user?
|
2018-02-02 13:39:55 -05:00
|
|
|
(current_user.nil? && Gitlab::CurrentSettings.allow_signup?) || current_user&.admin?
|
2017-04-13 04:47:52 -04:00
|
|
|
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-07-24 08:46:19 -04:00
|
|
|
:website_url,
|
2018-09-26 11:25:31 -04:00
|
|
|
:private_profile,
|
|
|
|
:organization,
|
2018-09-26 11:27:26 -04:00
|
|
|
:location,
|
|
|
|
:public_email
|
2017-04-13 04:47:52 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Allowed params for user signup
|
|
|
|
def signup_params
|
|
|
|
[
|
|
|
|
:email,
|
|
|
|
:email_confirmation,
|
|
|
|
:password_automatically_set,
|
|
|
|
:name,
|
|
|
|
:password,
|
|
|
|
:username
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2017-04-25 15:51:36 -04:00
|
|
|
def build_user_params(skip_authorization:)
|
2017-04-13 04:47:52 -04:00
|
|
|
if current_user&.admin?
|
|
|
|
user_params = params.slice(*admin_create_params)
|
|
|
|
user_params[:created_by_id] = current_user&.id
|
|
|
|
|
|
|
|
if params[:reset_password]
|
|
|
|
user_params.merge!(force_random_password: true, password_expires_at: nil)
|
|
|
|
end
|
|
|
|
else
|
2017-04-26 03:54:33 -04:00
|
|
|
allowed_signup_params = signup_params
|
|
|
|
allowed_signup_params << :skip_confirmation if skip_authorization
|
|
|
|
|
|
|
|
user_params = params.slice(*allowed_signup_params)
|
|
|
|
if user_params[:skip_confirmation].nil?
|
|
|
|
user_params[:skip_confirmation] = skip_user_confirmation_email_from_setting
|
|
|
|
end
|
2017-04-13 04:47:52 -04:00
|
|
|
end
|
|
|
|
|
2018-10-22 06:37:05 -04:00
|
|
|
if user_default_internal_regex_enabled? && !user_params.key?(:external)
|
|
|
|
user_params[:external] = user_external?
|
|
|
|
end
|
|
|
|
|
2017-04-13 04:47:52 -04:00
|
|
|
user_params
|
|
|
|
end
|
2017-04-26 03:54:33 -04:00
|
|
|
|
|
|
|
def skip_user_confirmation_email_from_setting
|
2018-02-02 13:39:55 -05:00
|
|
|
!Gitlab::CurrentSettings.send_user_confirmation_email
|
2017-04-26 03:54:33 -04:00
|
|
|
end
|
2018-08-30 08:53:06 -04:00
|
|
|
|
|
|
|
def user_external?
|
|
|
|
user_default_internal_regex_instance.match(params[:email]).nil?
|
|
|
|
end
|
2017-04-13 04:47:52 -04:00
|
|
|
end
|
|
|
|
end
|