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)
|
2017-03-09 16:59:19 -05:00
|
|
|
return false unless can_access_git?
|
2016-11-16 09:07:04 -05:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
@permission_cache ||= {}
|
|
|
|
@permission_cache[action] ||= user.can?(action, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cannot_do_action?(action)
|
|
|
|
!can_do_action?(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?
|
2017-03-09 16:59:19 -05:00
|
|
|
return false unless can_access_git?
|
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
|
|
|
|
2017-04-03 21:05:42 -04:00
|
|
|
def can_create_tag?(ref)
|
2017-03-31 12:57:29 -04:00
|
|
|
return false unless can_access_git?
|
|
|
|
|
2017-04-03 13:59:58 -04:00
|
|
|
if ProtectedTag.protected?(project, ref)
|
2017-04-03 22:50:15 -04:00
|
|
|
project.protected_tags.protected_ref_accessible_to?(ref, user, action: :create)
|
2017-03-31 12:57:29 -04:00
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-08 03:41:58 -04:00
|
|
|
def can_delete_branch?(ref)
|
|
|
|
return false unless can_access_git?
|
|
|
|
|
|
|
|
if ProtectedBranch.protected?(project, ref)
|
|
|
|
user.can?(:delete_protected_branch, project)
|
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
def can_push_to_branch?(ref)
|
2017-03-09 16:59:19 -05:00
|
|
|
return false unless can_access_git?
|
2016-07-18 04:16:56 -04:00
|
|
|
|
2017-04-03 13:59:58 -04:00
|
|
|
if ProtectedBranch.protected?(project, ref)
|
2016-08-01 11:48:15 -04:00
|
|
|
return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)
|
|
|
|
|
2017-04-28 10:05:00 -04:00
|
|
|
project.protected_branches.protected_ref_accessible_to?(ref, user, action: :push)
|
2016-07-18 04:16:56 -04:00
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_merge_to_branch?(ref)
|
2017-03-09 16:59:19 -05:00
|
|
|
return false unless can_access_git?
|
2016-07-18 04:16:56 -04:00
|
|
|
|
2017-04-03 13:59:58 -04:00
|
|
|
if ProtectedBranch.protected?(project, ref)
|
2017-04-03 21:05:42 -04:00
|
|
|
project.protected_branches.protected_ref_accessible_to?(ref, user, action: :merge)
|
2016-07-18 04:16:56 -04:00
|
|
|
else
|
|
|
|
user.can?(:push_code, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read_project?
|
2017-03-09 16:59:19 -05:00
|
|
|
return false unless can_access_git?
|
2016-07-18 04:16:56 -04:00
|
|
|
|
|
|
|
user.can?(:read_project, project)
|
|
|
|
end
|
2016-11-16 09:07:04 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-09 16:59:19 -05:00
|
|
|
def can_access_git?
|
|
|
|
user && user.can?(:access_git)
|
2016-11-16 09:07:04 -05:00
|
|
|
end
|
2014-05-15 04:17:13 -04:00
|
|
|
end
|
|
|
|
end
|