2011-10-08 17:36:38 -04:00
|
|
|
class Ability
|
2012-10-08 20:10:04 -04:00
|
|
|
class << self
|
2016-08-23 20:29:40 -04:00
|
|
|
# Given a list of users and a project this method returns the users that can
|
|
|
|
# read the given project.
|
|
|
|
def users_that_can_read_project(users, project)
|
|
|
|
if project.public?
|
|
|
|
users
|
|
|
|
else
|
|
|
|
users.select do |user|
|
|
|
|
if user.admin?
|
|
|
|
true
|
|
|
|
elsif project.internal? && !user.external?
|
|
|
|
true
|
|
|
|
elsif project.owner == user
|
|
|
|
true
|
|
|
|
elsif project.team.members.include?(user)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-08 13:07:15 -04:00
|
|
|
|
2017-01-05 08:36:06 -05:00
|
|
|
# Given a list of users and a snippet this method returns the users that can
|
|
|
|
# read the given snippet.
|
|
|
|
def users_that_can_read_personal_snippet(users, snippet)
|
|
|
|
case snippet.visibility_level
|
|
|
|
when Snippet::INTERNAL, Snippet::PUBLIC
|
|
|
|
users
|
|
|
|
when Snippet::PRIVATE
|
2017-01-20 05:28:40 -05:00
|
|
|
users.include?(snippet.author) ? [snippet.author] : []
|
2017-01-05 08:36:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-23 20:29:40 -04:00
|
|
|
# Returns an Array of Issues that can be read by the given user.
|
|
|
|
#
|
|
|
|
# issues - The issues to reduce down to those readable by the user.
|
|
|
|
# user - The User for which to check the issues
|
|
|
|
def issues_readable_by_user(issues, user = nil)
|
|
|
|
return issues if user && user.admin?
|
|
|
|
|
|
|
|
issues.select { |issue| issue.visible_to_user?(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: make this private and use the actual abilities stuff for this
|
|
|
|
def can_edit_note?(user, note)
|
|
|
|
return false if !note.editable? || !user.present?
|
|
|
|
return true if note.author == user || user.admin?
|
|
|
|
|
|
|
|
if note.project
|
|
|
|
max_access_level = note.project.team.max_member_access(user.id)
|
|
|
|
max_access_level >= Gitlab::Access::MASTER
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2016-08-08 13:07:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?(user, action, subject)
|
|
|
|
allowed(user, subject).include?(action)
|
|
|
|
end
|
|
|
|
|
2013-01-25 04:30:49 -05:00
|
|
|
def allowed(user, subject)
|
2016-08-08 13:07:15 -04:00
|
|
|
return uncached_allowed(user, subject) unless RequestStore.active?
|
|
|
|
|
|
|
|
user_key = user ? user.id : 'anonymous'
|
|
|
|
subject_key = subject ? "#{subject.class.name}/#{subject.id}" : 'global'
|
|
|
|
key = "/ability/#{user_key}/#{subject_key}"
|
2016-08-30 14:09:21 -04:00
|
|
|
RequestStore[key] ||= uncached_allowed(user, subject).freeze
|
2016-08-08 13:07:15 -04:00
|
|
|
end
|
|
|
|
|
2016-08-23 20:29:40 -04:00
|
|
|
private
|
|
|
|
|
2016-08-08 13:07:15 -04:00
|
|
|
def uncached_allowed(user, subject)
|
2016-08-18 13:39:49 -04:00
|
|
|
BasePolicy.class_for(subject).abilities(user, subject)
|
2016-08-04 10:00:31 -04:00
|
|
|
end
|
2011-10-17 06:39:03 -04:00
|
|
|
end
|
2011-10-08 17:36:38 -04:00
|
|
|
end
|