2014-05-15 04:17:13 -04:00
|
|
|
module Gitlab
|
2016-07-18 04:16:56 -04:00
|
|
|
class UserAccess
|
|
|
|
attr_reader :user, :project
|
|
|
|
|
|
|
|
def initialize(user, project: nil)
|
|
|
|
@user = user
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_do_action?(action)
|
|
|
|
@permission_cache ||= {}
|
|
|
|
@permission_cache[action] ||= user.can?(action, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cannot_do_action?(action)
|
|
|
|
!can_do_action?(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?
|
|
|
|
return false if user.blank? || user.blocked?
|
2014-05-15 04:17:13 -04:00
|
|
|
|
2016-03-10 06:37:14 -05:00
|
|
|
if user.requires_ldap_check? && user.try_obtain_ldap_lease
|
|
|
|
return false unless Gitlab::LDAP::Access.allowed?(user)
|
2014-05-15 04:17:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2016-07-18 04:16:56 -04:00
|
|
|
|
|
|
|
def can_push_to_branch?(ref)
|
|
|
|
return false unless user
|
|
|
|
|
2016-07-08 02:15:02 -04:00
|
|
|
if project.protected_branch?(ref)
|
2016-08-01 11:48:15 -04:00
|
|
|
return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)
|
|
|
|
|
2016-08-16 02:30:56 -04:00
|
|
|
access_levels = project.protected_branches.matching(ref).map(&:push_access_levels).flatten
|
2016-07-08 02:15:02 -04:00
|
|
|
access_levels.any? { |access_level| access_level.check_access(user) }
|
2016-07-18 04:16:56 -04:00
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_merge_to_branch?(ref)
|
|
|
|
return false unless user
|
|
|
|
|
2016-07-08 02:15:02 -04:00
|
|
|
if project.protected_branch?(ref)
|
2016-08-16 02:30:56 -04:00
|
|
|
access_levels = project.protected_branches.matching(ref).map(&:merge_access_levels).flatten
|
2016-07-08 02:15:02 -04:00
|
|
|
access_levels.any? { |access_level| access_level.check_access(user) }
|
2016-07-18 04:16:56 -04:00
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read_project?
|
|
|
|
return false unless user
|
|
|
|
|
|
|
|
user.can?(:read_project, project)
|
|
|
|
end
|
2014-05-15 04:17:13 -04:00
|
|
|
end
|
|
|
|
end
|