debian-mirror-gitlab/lib/gitlab/user_access.rb

76 lines
1.7 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Gitlab
2016-08-24 12:49:21 +05:30
class UserAccess
attr_reader :user, :project
def initialize(user, project: nil)
@user = user
@project = project
end
def can_do_action?(action)
2017-08-17 22:00:37 +05:30
return false unless can_access_git?
2016-08-24 12:49:21 +05:30
@permission_cache ||= {}
@permission_cache[action] ||= user.can?(action, project)
end
def cannot_do_action?(action)
!can_do_action?(action)
end
def allowed?
2017-08-17 22:00:37 +05:30
return false unless can_access_git?
2014-09-02 18:07:02 +05:30
2016-04-02 18:10:28 +05:30
if user.requires_ldap_check? && user.try_obtain_ldap_lease
2014-09-02 18:07:02 +05:30
return false unless Gitlab::LDAP::Access.allowed?(user)
end
true
end
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
def can_create_tag?(ref)
return false unless can_access_git?
if ProtectedTag.protected?(project, ref)
project.protected_tags.protected_ref_accessible_to?(ref, user, action: :create)
else
user.can?(:push_code, project)
end
end
2016-08-24 12:49:21 +05:30
def can_push_to_branch?(ref)
2017-08-17 22:00:37 +05:30
return false unless can_access_git?
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
if ProtectedBranch.protected?(project, ref)
2016-09-13 17:45:13 +05:30
return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)
2017-08-17 22:00:37 +05:30
project.protected_branches.protected_ref_accessible_to?(ref, user, action: :push)
2016-08-24 12:49:21 +05:30
else
user.can?(:push_code, project)
end
end
def can_merge_to_branch?(ref)
2017-08-17 22:00:37 +05:30
return false unless can_access_git?
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
if ProtectedBranch.protected?(project, ref)
project.protected_branches.protected_ref_accessible_to?(ref, user, action: :merge)
2016-08-24 12:49:21 +05:30
else
user.can?(:push_code, project)
end
end
def can_read_project?
2017-08-17 22:00:37 +05:30
return false unless can_access_git?
2016-08-24 12:49:21 +05:30
user.can?(:read_project, project)
end
2017-08-17 22:00:37 +05:30
private
def can_access_git?
user && user.can?(:access_git)
end
2014-09-02 18:07:02 +05:30
end
end