summaryrefslogtreecommitdiff
path: root/app/services/users
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-05-19 15:44:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-05-19 15:44:42 +0000
commit4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch)
tree5423a1c7516cffe36384133ade12572cf709398d /app/services/users
parente570267f2f6b326480d284e0164a6464ba4081bc (diff)
downloadgitlab-ce-4555e1b21c365ed8303ffb7a3325d773c9b8bf31.tar.gz
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'app/services/users')
-rw-r--r--app/services/users/activity_service.rb2
-rw-r--r--app/services/users/approve_service.rb2
-rw-r--r--app/services/users/ban_service.rb25
-rw-r--r--app/services/users/block_service.rb2
-rw-r--r--app/services/users/build_service.rb22
-rw-r--r--app/services/users/create_service.rb2
-rw-r--r--app/services/users/destroy_service.rb4
-rw-r--r--app/services/users/migrate_to_ghost_user_service.rb2
-rw-r--r--app/services/users/registrations_build_service.rb19
-rw-r--r--app/services/users/reject_service.rb2
-rw-r--r--app/services/users/update_assigned_open_issue_count_service.rb33
-rw-r--r--app/services/users/update_canonical_email_service.rb2
-rw-r--r--app/services/users/update_service.rb4
-rw-r--r--app/services/users/update_todo_count_cache_service.rb29
-rw-r--r--app/services/users/upsert_credit_card_validation_service.rb20
15 files changed, 141 insertions, 29 deletions
diff --git a/app/services/users/activity_service.rb b/app/services/users/activity_service.rb
index 64844a3f002..c89a286cc8b 100644
--- a/app/services/users/activity_service.rb
+++ b/app/services/users/activity_service.rb
@@ -38,4 +38,4 @@ module Users
end
end
-Users::ActivityService.prepend_ee_mod
+Users::ActivityService.prepend_mod
diff --git a/app/services/users/approve_service.rb b/app/services/users/approve_service.rb
index fea7fc55d90..15486ddcd43 100644
--- a/app/services/users/approve_service.rb
+++ b/app/services/users/approve_service.rb
@@ -47,4 +47,4 @@ module Users
end
end
-Users::ApproveService.prepend_if_ee('EE::Users::ApproveService')
+Users::ApproveService.prepend_mod_with('Users::ApproveService')
diff --git a/app/services/users/ban_service.rb b/app/services/users/ban_service.rb
new file mode 100644
index 00000000000..247ed14966b
--- /dev/null
+++ b/app/services/users/ban_service.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Users
+ class BanService < BaseService
+ def initialize(current_user)
+ @current_user = current_user
+ end
+
+ def execute(user)
+ if user.ban
+ log_event(user)
+ success
+ else
+ messages = user.errors.full_messages
+ error(messages.uniq.join('. '))
+ end
+ end
+
+ private
+
+ def log_event(user)
+ Gitlab::AppLogger.info(message: "User banned", user: "#{user.username}", email: "#{user.email}", banned_by: "#{current_user.username}", ip_address: "#{current_user.current_sign_in_ip}")
+ end
+ end
+end
diff --git a/app/services/users/block_service.rb b/app/services/users/block_service.rb
index 8513664ee85..37921c477b4 100644
--- a/app/services/users/block_service.rb
+++ b/app/services/users/block_service.rb
@@ -26,4 +26,4 @@ module Users
end
end
-Users::BlockService.prepend_if_ee('EE::Users::BlockService')
+Users::BlockService.prepend_mod_with('Users::BlockService')
diff --git a/app/services/users/build_service.rb b/app/services/users/build_service.rb
index b3b172f9df2..649cf281ab0 100644
--- a/app/services/users/build_service.rb
+++ b/app/services/users/build_service.rb
@@ -14,9 +14,11 @@ module Users
end
def execute(skip_authorization: false)
+ @skip_authorization = skip_authorization
+
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_create_user?
- user_params = build_user_params(skip_authorization: skip_authorization)
+ user_params = build_user_params
user = User.new(user_params)
if current_user&.admin?
@@ -37,6 +39,8 @@ module Users
private
+ attr_reader :skip_authorization
+
def identity_attributes
[:extern_uid, :provider]
end
@@ -102,7 +106,7 @@ module Users
]
end
- def build_user_params(skip_authorization:)
+ def build_user_params
if current_user&.admin?
user_params = params.slice(*admin_create_params)
@@ -111,10 +115,10 @@ module Users
end
else
allowed_signup_params = signup_params
- allowed_signup_params << :skip_confirmation if skip_authorization
+ allowed_signup_params << :skip_confirmation if allow_caller_to_request_skip_confirmation?
user_params = params.slice(*allowed_signup_params)
- if user_params[:skip_confirmation].nil?
+ if assign_skip_confirmation_from_settings?(user_params)
user_params[:skip_confirmation] = skip_user_confirmation_email_from_setting
end
@@ -136,6 +140,14 @@ module Users
user_params
end
+ def allow_caller_to_request_skip_confirmation?
+ skip_authorization
+ end
+
+ def assign_skip_confirmation_from_settings?(user_params)
+ user_params[:skip_confirmation].nil?
+ end
+
def skip_user_confirmation_email_from_setting
!Gitlab::CurrentSettings.send_user_confirmation_email
end
@@ -150,4 +162,4 @@ module Users
end
end
-Users::BuildService.prepend_if_ee('EE::Users::BuildService')
+Users::BuildService.prepend_mod_with('Users::BuildService')
diff --git a/app/services/users/create_service.rb b/app/services/users/create_service.rb
index ec8b3cea664..757ebd783ee 100644
--- a/app/services/users/create_service.rb
+++ b/app/services/users/create_service.rb
@@ -26,4 +26,4 @@ module Users
end
end
-Users::CreateService.prepend_if_ee('EE::Users::CreateService')
+Users::CreateService.prepend_mod_with('Users::CreateService')
diff --git a/app/services/users/destroy_service.rb b/app/services/users/destroy_service.rb
index 613d2e4ad82..4ec875098fa 100644
--- a/app/services/users/destroy_service.rb
+++ b/app/services/users/destroy_service.rb
@@ -31,7 +31,7 @@ module Users
end
if !delete_solo_owned_groups && user.solo_owned_groups.present?
- user.errors[:base] << 'You must transfer ownership or delete groups before you can remove user'
+ user.errors.add(:base, 'You must transfer ownership or delete groups before you can remove user')
return user
end
@@ -73,4 +73,4 @@ module Users
end
end
-Users::DestroyService.prepend_if_ee('EE::Users::DestroyService')
+Users::DestroyService.prepend_mod_with('Users::DestroyService')
diff --git a/app/services/users/migrate_to_ghost_user_service.rb b/app/services/users/migrate_to_ghost_user_service.rb
index 1b46edd4d7d..a471f55e644 100644
--- a/app/services/users/migrate_to_ghost_user_service.rb
+++ b/app/services/users/migrate_to_ghost_user_service.rb
@@ -93,4 +93,4 @@ module Users
end
end
-Users::MigrateToGhostUserService.prepend_if_ee('EE::Users::MigrateToGhostUserService')
+Users::MigrateToGhostUserService.prepend_mod_with('Users::MigrateToGhostUserService')
diff --git a/app/services/users/registrations_build_service.rb b/app/services/users/registrations_build_service.rb
new file mode 100644
index 00000000000..9d7bf0a7e18
--- /dev/null
+++ b/app/services/users/registrations_build_service.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Users
+ class RegistrationsBuildService < BuildService
+ extend ::Gitlab::Utils::Override
+
+ private
+
+ override :allow_caller_to_request_skip_confirmation?
+ def allow_caller_to_request_skip_confirmation?
+ true
+ end
+
+ override :assign_skip_confirmation_from_settings?
+ def assign_skip_confirmation_from_settings?(user_params)
+ user_params[:skip_confirmation].blank?
+ end
+ end
+end
diff --git a/app/services/users/reject_service.rb b/app/services/users/reject_service.rb
index 0e3eb3e5dde..833c30d9427 100644
--- a/app/services/users/reject_service.rb
+++ b/app/services/users/reject_service.rb
@@ -39,4 +39,4 @@ module Users
end
end
-Users::RejectService.prepend_if_ee('EE::Users::RejectService')
+Users::RejectService.prepend_mod_with('Users::RejectService')
diff --git a/app/services/users/update_assigned_open_issue_count_service.rb b/app/services/users/update_assigned_open_issue_count_service.rb
new file mode 100644
index 00000000000..2ed05853b2f
--- /dev/null
+++ b/app/services/users/update_assigned_open_issue_count_service.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Users
+ # Service class for calculating and caching the number of assigned open issues for a user.
+ class UpdateAssignedOpenIssueCountService
+ attr_accessor :target_user
+
+ def initialize(target_user:)
+ @target_user = target_user
+
+ raise ArgumentError, "Please provide a target user" unless target_user.is_a?(User)
+ end
+
+ def execute
+ value = calculate_count
+ Rails.cache.write(cache_key, value, expires_in: User::COUNT_CACHE_VALIDITY_PERIOD)
+
+ ServiceResponse.success(payload: { count: value })
+ rescue StandardError => e
+ ServiceResponse.error(message: e.message)
+ end
+
+ private
+
+ def cache_key
+ ['users', target_user.id, 'assigned_open_issues_count']
+ end
+
+ def calculate_count
+ IssuesFinder.new(target_user, assignee_id: target_user.id, state: 'opened', non_archived: true).execute.count
+ end
+ end
+end
diff --git a/app/services/users/update_canonical_email_service.rb b/app/services/users/update_canonical_email_service.rb
index e75452f60fd..c4b7a98f60b 100644
--- a/app/services/users/update_canonical_email_service.rb
+++ b/app/services/users/update_canonical_email_service.rb
@@ -7,7 +7,7 @@ module Users
INCLUDED_DOMAINS_PATTERN = [/gmail.com/].freeze
def initialize(user:)
- raise ArgumentError.new("Please provide a user") unless user.is_a?(User)
+ raise ArgumentError, "Please provide a user" unless user.is_a?(User)
@user = user
end
diff --git a/app/services/users/update_service.rb b/app/services/users/update_service.rb
index b69720eefd6..ff08c806319 100644
--- a/app/services/users/update_service.rb
+++ b/app/services/users/update_service.rb
@@ -34,7 +34,7 @@ module Users
def execute!(*args, &block)
result = execute(*args, &block)
- raise ActiveRecord::RecordInvalid.new(@user) unless result[:status] == :success
+ raise ActiveRecord::RecordInvalid, @user unless result[:status] == :success
true
end
@@ -96,4 +96,4 @@ module Users
end
end
-Users::UpdateService.prepend_if_ee('EE::Users::UpdateService')
+Users::UpdateService.prepend_mod_with('Users::UpdateService')
diff --git a/app/services/users/update_todo_count_cache_service.rb b/app/services/users/update_todo_count_cache_service.rb
index 03ab66bd64a..3407b22e355 100644
--- a/app/services/users/update_todo_count_cache_service.rb
+++ b/app/services/users/update_todo_count_cache_service.rb
@@ -4,31 +4,34 @@ module Users
class UpdateTodoCountCacheService < BaseService
QUERY_BATCH_SIZE = 10
- attr_reader :users
+ attr_reader :user_ids
- # users - An array of User objects
- def initialize(users)
- @users = users
+ # user_ids - An array of User IDs
+ def initialize(user_ids)
+ @user_ids = user_ids
end
def execute
- users.each_slice(QUERY_BATCH_SIZE) do |users_batch|
- todo_counts = Todo.for_user(users_batch).count_grouped_by_user_id_and_state
+ user_ids.each_slice(QUERY_BATCH_SIZE) do |user_ids_batch|
+ todo_counts = Todo.for_user(user_ids_batch).count_grouped_by_user_id_and_state
- users_batch.each do |user|
- update_count_cache(user, todo_counts, :done)
- update_count_cache(user, todo_counts, :pending)
+ user_ids_batch.each do |user_id|
+ update_count_cache(user_id, todo_counts, :done)
+ update_count_cache(user_id, todo_counts, :pending)
end
end
end
private
- def update_count_cache(user, todo_counts, state)
- count = todo_counts.fetch([user.id, state.to_s], 0)
- expiration_time = user.count_cache_validity_period
+ def update_count_cache(user_id, todo_counts, state)
+ count = todo_counts.fetch([user_id, state.to_s], 0)
- Rails.cache.write(['users', user.id, "todos_#{state}_count"], count, expires_in: expiration_time)
+ Rails.cache.write(
+ ['users', user_id, "todos_#{state}_count"],
+ count,
+ expires_in: User::COUNT_CACHE_VALIDITY_PERIOD
+ )
end
end
end
diff --git a/app/services/users/upsert_credit_card_validation_service.rb b/app/services/users/upsert_credit_card_validation_service.rb
new file mode 100644
index 00000000000..70a96b3ec6b
--- /dev/null
+++ b/app/services/users/upsert_credit_card_validation_service.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module Users
+ class UpsertCreditCardValidationService < BaseService
+ def initialize(params)
+ @params = params.to_h.with_indifferent_access
+ end
+
+ def execute
+ ::Users::CreditCardValidation.upsert(@params)
+
+ ServiceResponse.success(message: 'CreditCardValidation was set')
+ rescue ActiveRecord::InvalidForeignKey, ActiveRecord::NotNullViolation => e
+ ServiceResponse.error(message: "Could not set CreditCardValidation: #{e.message}")
+ rescue StandardError => e
+ Gitlab::ErrorTracking.track_exception(e, params: @params, class: self.class.to_s)
+ ServiceResponse.error(message: "Could not set CreditCardValidation: #{e.message}")
+ end
+ end
+end