2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-14 10:54:10 -04:00
|
|
|
class Member < ActiveRecord::Base
|
2017-11-29 10:30:17 -05:00
|
|
|
include AfterCommitQueue
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2016-06-01 12:03:51 -04:00
|
|
|
include Importable
|
2016-08-18 17:45:41 -04:00
|
|
|
include Expirable
|
2014-09-14 10:54:10 -04:00
|
|
|
include Gitlab::Access
|
2017-03-02 01:01:02 -05:00
|
|
|
include Presentable
|
2018-12-06 08:15:29 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2014-09-14 10:54:10 -04:00
|
|
|
|
2015-04-14 12:04:29 -04:00
|
|
|
attr_accessor :raw_invite_token
|
|
|
|
|
2015-04-10 08:46:09 -04:00
|
|
|
belongs_to :created_by, class_name: "User"
|
2014-09-14 10:54:10 -04:00
|
|
|
belongs_to :user
|
2017-06-02 08:29:30 -04:00
|
|
|
belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2014-09-14 10:54:10 -04:00
|
|
|
|
2017-02-22 17:35:08 -05:00
|
|
|
delegate :name, :username, :email, to: :user, prefix: true
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
validates :user, presence: true, unless: :invite?
|
2014-09-14 10:54:10 -04:00
|
|
|
validates :source, presence: true
|
2015-11-11 10:42:27 -05:00
|
|
|
validates :user_id, uniqueness: { scope: [:source_type, :source_id],
|
2015-04-10 09:09:37 -04:00
|
|
|
message: "already exists in source",
|
|
|
|
allow_nil: true }
|
2014-09-14 12:32:51 -04:00
|
|
|
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
|
2018-12-06 08:15:29 -05:00
|
|
|
validate :higher_access_level_than_group, unless: :importing?
|
2015-11-17 09:49:37 -05:00
|
|
|
validates :invite_email,
|
|
|
|
presence: {
|
|
|
|
if: :invite?
|
|
|
|
},
|
2016-02-09 11:59:47 -05:00
|
|
|
email: {
|
2015-11-17 09:49:37 -05:00
|
|
|
allow_nil: true
|
|
|
|
},
|
|
|
|
uniqueness: {
|
|
|
|
scope: [:source_type, :source_id],
|
|
|
|
allow_nil: true
|
|
|
|
}
|
2014-09-14 10:54:10 -04:00
|
|
|
|
2016-09-05 11:37:26 -04:00
|
|
|
# This scope encapsulates (most of) the conditions a row in the member table
|
|
|
|
# must satisfy if it is a valid permission. Of particular note:
|
|
|
|
#
|
|
|
|
# * Access requests must be excluded
|
|
|
|
# * Blocked users must be excluded
|
|
|
|
# * Invitations take effect immediately
|
|
|
|
# * expires_at is not implemented. A background worker purges expired rows
|
|
|
|
scope :active, -> do
|
|
|
|
is_external_invite = arel_table[:user_id].eq(nil).and(arel_table[:invite_token].not_eq(nil))
|
|
|
|
user_is_active = User.arel_table[:state].eq(:active)
|
|
|
|
|
2017-08-11 10:19:11 -04:00
|
|
|
user_ok = Arel::Nodes::Grouping.new(is_external_invite).or(user_is_active)
|
|
|
|
|
|
|
|
left_join_users
|
|
|
|
.where(user_ok)
|
|
|
|
.where(requested_at: nil)
|
|
|
|
.reorder(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Like active, but without invites. For when a User is required.
|
2018-03-21 12:14:32 -04:00
|
|
|
scope :active_without_invites_and_requests, -> do
|
2017-08-11 10:19:11 -04:00
|
|
|
left_join_users
|
|
|
|
.where(users: { state: 'active' })
|
2018-03-11 13:55:24 -04:00
|
|
|
.non_request
|
2017-08-11 10:19:11 -04:00
|
|
|
.reorder(nil)
|
2016-09-05 11:37:26 -04:00
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
scope :invite, -> { where.not(invite_token: nil) }
|
2016-06-02 12:05:06 -04:00
|
|
|
scope :non_invite, -> { where(invite_token: nil) }
|
2016-04-18 12:53:32 -04:00
|
|
|
scope :request, -> { where.not(requested_at: nil) }
|
2017-02-08 10:02:25 -05:00
|
|
|
scope :non_request, -> { where(requested_at: nil) }
|
2016-09-05 11:37:26 -04:00
|
|
|
|
|
|
|
scope :has_access, -> { active.where('access_level > 0') }
|
|
|
|
|
|
|
|
scope :guests, -> { active.where(access_level: GUEST) }
|
|
|
|
scope :reporters, -> { active.where(access_level: REPORTER) }
|
|
|
|
scope :developers, -> { active.where(access_level: DEVELOPER) }
|
2018-07-11 10:36:08 -04:00
|
|
|
scope :maintainers, -> { active.where(access_level: MAINTAINER) }
|
|
|
|
scope :masters, -> { maintainers } # @deprecated
|
2016-09-05 11:37:26 -04:00
|
|
|
scope :owners, -> { active.where(access_level: OWNER) }
|
2019-01-16 07:09:29 -05:00
|
|
|
scope :owners_and_maintainers, -> { active.where(access_level: [OWNER, MAINTAINER]) }
|
2018-07-11 10:36:08 -04:00
|
|
|
scope :owners_and_masters, -> { owners_and_maintainers } # @deprecated
|
2018-11-16 10:09:32 -05:00
|
|
|
scope :with_user, -> (user) { where(user: user) }
|
2014-09-14 12:32:51 -04:00
|
|
|
|
2016-11-18 12:50:29 -05:00
|
|
|
scope :order_name_asc, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.name', 'ASC')) }
|
|
|
|
scope :order_name_desc, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.name', 'DESC')) }
|
|
|
|
scope :order_recent_sign_in, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.last_sign_in_at', 'DESC')) }
|
|
|
|
scope :order_oldest_sign_in, -> { left_join_users.reorder(Gitlab::Database.nulls_last_order('users.last_sign_in_at', 'ASC')) }
|
2016-11-16 16:37:51 -05:00
|
|
|
|
2019-01-14 16:53:37 -05:00
|
|
|
scope :on_project_and_ancestors, ->(project) { where(source: [project] + project.ancestors) }
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
before_validation :generate_invite_token, on: :create, if: -> (member) { member.invite_email.present? }
|
2016-03-14 09:13:35 -04:00
|
|
|
|
2016-06-01 12:03:51 -04:00
|
|
|
after_create :send_invite, if: :invite?, unless: :importing?
|
2016-06-16 08:07:49 -04:00
|
|
|
after_create :send_request, if: :request?, unless: :importing?
|
|
|
|
after_create :create_notification_setting, unless: [:pending?, :importing?]
|
|
|
|
after_create :post_create_hook, unless: [:pending?, :importing?]
|
|
|
|
after_update :post_update_hook, unless: [:pending?, :importing?]
|
2018-03-07 14:54:28 -05:00
|
|
|
after_destroy :destroy_notification_setting
|
2016-03-14 09:13:35 -04:00
|
|
|
after_destroy :post_destroy_hook, unless: :pending?
|
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
|
|
|
after_commit :refresh_member_authorized_projects
|
2015-04-10 09:09:37 -04:00
|
|
|
|
2016-03-28 17:22:28 -04:00
|
|
|
default_value_for :notification_level, NotificationSetting.levels[:global]
|
|
|
|
|
2015-04-14 06:33:27 -04:00
|
|
|
class << self
|
2016-11-16 16:37:51 -05:00
|
|
|
def search(query)
|
|
|
|
joins(:user).merge(User.search(query))
|
|
|
|
end
|
|
|
|
|
2018-04-20 05:25:22 -04:00
|
|
|
def filter_by_2fa(value)
|
|
|
|
case value
|
|
|
|
when 'enabled'
|
2018-08-21 05:29:31 -04:00
|
|
|
left_join_users.merge(User.with_two_factor)
|
2018-04-20 05:25:22 -04:00
|
|
|
when 'disabled'
|
|
|
|
left_join_users.merge(User.without_two_factor)
|
|
|
|
else
|
|
|
|
all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-04 05:19:47 -04:00
|
|
|
def sort_by_attribute(method)
|
2016-11-16 16:37:51 -05:00
|
|
|
case method.to_s
|
2016-11-16 16:45:35 -05:00
|
|
|
when 'access_level_asc' then reorder(access_level: :asc)
|
|
|
|
when 'access_level_desc' then reorder(access_level: :desc)
|
2016-11-16 16:37:51 -05:00
|
|
|
when 'recent_sign_in' then order_recent_sign_in
|
|
|
|
when 'oldest_sign_in' then order_oldest_sign_in
|
|
|
|
when 'last_joined' then order_created_desc
|
|
|
|
when 'oldest_joined' then order_created_asc
|
|
|
|
else
|
|
|
|
order_by(method)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-18 12:50:29 -05:00
|
|
|
def left_join_users
|
|
|
|
users = User.arel_table
|
|
|
|
members = Member.arel_table
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
member_users = members.join(users, Arel::Nodes::OuterJoin)
|
|
|
|
.on(members[:user_id].eq(users[:id]))
|
|
|
|
.join_sources
|
2016-11-18 12:50:29 -05:00
|
|
|
|
|
|
|
joins(member_users)
|
|
|
|
end
|
|
|
|
|
2016-07-25 09:21:55 -04:00
|
|
|
def access_for_user_ids(user_ids)
|
2016-08-04 01:59:14 -04:00
|
|
|
where(user_id: user_ids).has_access.pluck(:user_id, :access_level).to_h
|
2016-07-25 09:21:55 -04:00
|
|
|
end
|
|
|
|
|
2015-04-14 06:33:27 -04:00
|
|
|
def find_by_invite_token(invite_token)
|
|
|
|
invite_token = Devise.token_generator.digest(self, :invite_token, invite_token)
|
|
|
|
find_by(invite_token: invite_token)
|
|
|
|
end
|
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def add_user(source, user, access_level, existing_members: nil, current_user: nil, expires_at: nil, ldap: false)
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-09-05 12:03:24 -04:00
|
|
|
# `user` can be either a User object, User ID or an email to be invited
|
|
|
|
member = retrieve_member(source, user, existing_members)
|
2016-09-16 11:54:21 -04:00
|
|
|
access_level = retrieve_access_level(access_level)
|
2015-10-30 15:55:19 -04:00
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
return member unless can_update_member?(current_user, member)
|
|
|
|
|
2018-11-15 10:09:27 -05:00
|
|
|
set_member_attributes(
|
|
|
|
member,
|
|
|
|
access_level,
|
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at,
|
|
|
|
ldap: ldap
|
|
|
|
)
|
2016-09-16 11:54:21 -04:00
|
|
|
|
|
|
|
if member.request?
|
2016-10-03 05:06:52 -04:00
|
|
|
::Members::ApproveAccessRequestService.new(
|
|
|
|
current_user,
|
|
|
|
access_level: access_level
|
2018-02-16 09:10:22 -05:00
|
|
|
).execute(
|
|
|
|
member,
|
|
|
|
skip_authorization: ldap,
|
|
|
|
skip_log_audit_event: ldap
|
|
|
|
)
|
2015-04-14 06:33:27 -04:00
|
|
|
else
|
2016-09-16 11:54:21 -04:00
|
|
|
member.save
|
2015-04-14 06:33:27 -04:00
|
|
|
end
|
2015-10-30 15:55:19 -04:00
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
member
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-09-16 11:54:21 -04:00
|
|
|
end
|
2015-04-14 06:33:27 -04:00
|
|
|
|
2018-11-15 10:09:27 -05:00
|
|
|
# Populates the attributes of a member.
|
|
|
|
#
|
|
|
|
# This logic resides in a separate method so that EE can extend this logic,
|
|
|
|
# without having to patch the `add_user` method directly.
|
|
|
|
def set_member_attributes(member, access_level, current_user: nil, expires_at: nil, ldap: false)
|
|
|
|
member.attributes = {
|
|
|
|
created_by: member.created_by || current_user,
|
|
|
|
access_level: access_level,
|
|
|
|
expires_at: expires_at
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-04-21 10:07:42 -04:00
|
|
|
def add_users(source, users, access_level, current_user: nil, expires_at: nil)
|
|
|
|
return [] unless users.present?
|
|
|
|
|
2017-09-05 12:03:24 -04:00
|
|
|
emails, users, existing_members = parse_users_list(source, users)
|
2017-04-28 04:50:11 -04:00
|
|
|
|
2017-04-21 10:07:42 -04:00
|
|
|
self.transaction do
|
2017-09-05 12:03:24 -04:00
|
|
|
(emails + users).map! do |user|
|
2017-04-21 10:07:42 -04:00
|
|
|
add_user(
|
|
|
|
source,
|
|
|
|
user,
|
|
|
|
access_level,
|
2017-09-05 12:03:24 -04:00
|
|
|
existing_members: existing_members,
|
2017-04-21 10:07:42 -04:00
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
def access_levels
|
|
|
|
Gitlab::Access.sym_options
|
2015-04-14 06:33:27 -04:00
|
|
|
end
|
2015-10-30 15:55:19 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-05 12:03:24 -04:00
|
|
|
def parse_users_list(source, list)
|
|
|
|
emails, user_ids, users = [], [], []
|
|
|
|
existing_members = {}
|
|
|
|
|
|
|
|
list.each do |item|
|
|
|
|
case item
|
|
|
|
when User
|
|
|
|
users << item
|
|
|
|
when Integer
|
|
|
|
user_ids << item
|
|
|
|
when /\A\d+\Z/
|
|
|
|
user_ids << item.to_i
|
|
|
|
when Devise.email_regexp
|
|
|
|
emails << item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if user_ids.present?
|
|
|
|
users.concat(User.where(id: user_ids))
|
|
|
|
existing_members = source.members_and_requesters.where(user_id: user_ids).index_by(&:user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
[emails, users, existing_members]
|
|
|
|
end
|
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
# This method is used to find users that have been entered into the "Add members" field.
|
|
|
|
# These can be the User objects directly, their IDs, their emails, or new emails to be invited.
|
|
|
|
def retrieve_user(user)
|
|
|
|
return user if user.is_a?(User)
|
|
|
|
|
|
|
|
User.find_by(id: user) || User.find_by(email: user) || user
|
|
|
|
end
|
|
|
|
|
2017-09-05 12:03:24 -04:00
|
|
|
def retrieve_member(source, user, existing_members)
|
|
|
|
user = retrieve_user(user)
|
|
|
|
|
|
|
|
if user.is_a?(User)
|
|
|
|
if existing_members
|
|
|
|
existing_members[user.id] || source.members.build(user_id: user.id)
|
|
|
|
else
|
|
|
|
source.members_and_requesters.find_or_initialize_by(user_id: user.id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
source.members.build(invite_email: user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 11:54:21 -04:00
|
|
|
def retrieve_access_level(access_level)
|
|
|
|
access_levels.fetch(access_level) { access_level.to_i }
|
|
|
|
end
|
|
|
|
|
2015-11-03 06:11:56 -05:00
|
|
|
def can_update_member?(current_user, member)
|
2015-11-17 09:49:37 -05:00
|
|
|
# There is no current user for bulk actions, in which case anything is allowed
|
2016-09-16 11:54:21 -04:00
|
|
|
!current_user || current_user.can?(:"update_#{member.type.underscore}", member)
|
2015-10-30 15:55:19 -04:00
|
|
|
end
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
def real_source_type
|
|
|
|
source_type
|
|
|
|
end
|
|
|
|
|
2017-06-02 10:13:10 -04:00
|
|
|
def access_field
|
|
|
|
access_level
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def invite?
|
|
|
|
self.invite_token.present?
|
|
|
|
end
|
|
|
|
|
2016-03-14 09:13:35 -04:00
|
|
|
def request?
|
2016-06-02 12:05:06 -04:00
|
|
|
requested_at.present?
|
2016-03-14 09:13:35 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
def pending?
|
|
|
|
invite? || request?
|
2015-04-10 09:09:37 -04:00
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def accept_request
|
2016-03-14 09:13:35 -04:00
|
|
|
return false unless request?
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
updated = self.update(requested_at: nil)
|
2016-04-18 12:53:32 -04:00
|
|
|
after_accept_request if updated
|
2016-03-14 09:13:35 -04:00
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
updated
|
2016-03-14 09:13:35 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def accept_invite!(new_user)
|
2015-04-10 09:22:31 -04:00
|
|
|
return false unless invite?
|
2015-11-11 10:42:27 -05:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
self.invite_token = nil
|
|
|
|
self.invite_accepted_at = Time.now.utc
|
|
|
|
|
|
|
|
self.user = new_user
|
|
|
|
|
|
|
|
saved = self.save
|
|
|
|
|
|
|
|
after_accept_invite if saved
|
|
|
|
|
|
|
|
saved
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:37:02 -04:00
|
|
|
def decline_invite!
|
|
|
|
return false unless invite?
|
|
|
|
|
|
|
|
destroyed = self.destroy
|
|
|
|
|
|
|
|
after_decline_invite if destroyed
|
|
|
|
|
|
|
|
destroyed
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def generate_invite_token
|
|
|
|
raw, enc = Devise.token_generator.generate(self.class, :invite_token)
|
|
|
|
@raw_invite_token = raw
|
|
|
|
self.invite_token = enc
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_invite_token!
|
|
|
|
generate_invite_token && save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resend_invite
|
|
|
|
return unless invite?
|
|
|
|
|
|
|
|
generate_invite_token! unless @raw_invite_token
|
|
|
|
|
|
|
|
send_invite
|
|
|
|
end
|
|
|
|
|
2016-03-28 12:25:57 -04:00
|
|
|
def create_notification_setting
|
2016-03-29 08:03:23 -04:00
|
|
|
user.notification_settings.find_or_create_for(source)
|
2016-03-28 12:25:57 -04:00
|
|
|
end
|
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
def destroy_notification_setting
|
|
|
|
notification_setting&.destroy
|
|
|
|
end
|
|
|
|
|
2016-03-29 07:37:43 -04:00
|
|
|
def notification_setting
|
2018-02-14 07:01:12 -05:00
|
|
|
@notification_setting ||= user&.notification_settings_for(source)
|
2016-03-28 14:31:36 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-08-07 20:36:35 -04:00
|
|
|
def notifiable?(type, opts = {})
|
2017-08-04 16:56:33 -04:00
|
|
|
# always notify when there isn't a user yet
|
|
|
|
return true if user.blank?
|
|
|
|
|
|
|
|
NotificationRecipientService.notifiable?(user, type, notifiable_options.merge(opts))
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-08-04 16:56:33 -04:00
|
|
|
|
2018-12-06 08:15:29 -05:00
|
|
|
# Find the user's group member with a highest access level
|
|
|
|
def highest_group_member
|
|
|
|
strong_memoize(:highest_group_member) do
|
|
|
|
next unless user_id && source&.ancestors&.any?
|
|
|
|
|
|
|
|
GroupMember.where(source: source.ancestors, user_id: user_id).order(:access_level).last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def send_invite
|
|
|
|
# override in subclass
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def send_request
|
2016-06-17 08:06:55 -04:00
|
|
|
notification_service.new_access_request(self)
|
2015-04-10 09:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_create_hook
|
|
|
|
system_hook_service.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_update_hook
|
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
|
|
|
# override in sub class
|
2015-04-10 09:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_destroy_hook
|
|
|
|
system_hook_service.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
|
|
|
# Refreshes authorizations of the current member.
|
|
|
|
#
|
|
|
|
# This method schedules a job using Sidekiq and as such **must not** be called
|
|
|
|
# in a transaction. Doing so can lead to the job running before the
|
|
|
|
# transaction has been committed, resulting in the job either throwing an
|
|
|
|
# error or not doing any meaningful work.
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2016-10-11 08:25:17 -04:00
|
|
|
def refresh_member_authorized_projects
|
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:
1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
transaction it was possible for a job to run/complete before a
COMMIT; resulting in it either producing an error, or producing no
new data.
2. When scheduling jobs the code would not wait until completion. This
could lead to a user creating a project and then immediately trying
to push to it. Usually this will work fine, but given enough load it
might take a few seconds before a user has access.
The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).
This commit changes two things to deal with this:
1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
by scheduling using Rails' after_commit hook instead of doing so in
an arbitrary method.
2. When scheduling jobs the calling thread now waits for all jobs to
complete.
Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:
* sidekiq-status
* sidekiq_status
No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.
Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.
This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.
Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.
The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.
For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.
In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-22 12:22:02 -05:00
|
|
|
# If user/source is being destroyed, project access are going to be
|
|
|
|
# destroyed eventually because of DB foreign keys, so we shouldn't bother
|
|
|
|
# with refreshing after each member is destroyed through association
|
2016-10-11 08:25:17 -04:00
|
|
|
return if destroyed_by_association.present?
|
|
|
|
|
|
|
|
UserProjectAccessChangedService.new(user_id).execute
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-10-11 08:25:17 -04:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def after_accept_invite
|
|
|
|
post_create_hook
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:37:02 -04:00
|
|
|
def after_decline_invite
|
|
|
|
# override in subclass
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def after_accept_request
|
2015-04-10 09:09:37 -04:00
|
|
|
post_create_hook
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2015-04-10 09:09:37 -04:00
|
|
|
def system_hook_service
|
|
|
|
SystemHooksService.new
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2015-04-10 09:09:37 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2015-04-10 09:09:37 -04:00
|
|
|
def notification_service
|
|
|
|
NotificationService.new
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-08-04 14:53:36 -04:00
|
|
|
|
2017-08-04 16:56:33 -04:00
|
|
|
def notifiable_options
|
|
|
|
{}
|
2017-08-04 14:53:36 -04:00
|
|
|
end
|
2018-12-06 08:15:29 -05:00
|
|
|
|
|
|
|
def higher_access_level_than_group
|
|
|
|
if highest_group_member && highest_group_member.access_level >= access_level
|
|
|
|
error_parameters = { access: highest_group_member.human_access, group_name: highest_group_member.group.name }
|
|
|
|
|
|
|
|
errors.add(:access_level, s_("should be higher than %{access} inherited membership from group %{group_name}") % error_parameters)
|
|
|
|
end
|
|
|
|
end
|
2014-09-14 10:54:10 -04:00
|
|
|
end
|