user_access.rb 2.6 KB
Newer Older
1
module Gitlab
2
  class UserAccess
3
    extend Gitlab::Cache::RequestCache
4

5
    request_cache_key do
6 7 8
      [user&.id, project&.id]
    end

9 10 11 12 13 14 15 16
    attr_reader :user, :project

    def initialize(user, project: nil)
      @user = user
      @project = project
    end

    def can_do_action?(action)
17
      return false unless can_access_git?
18

19 20 21 22 23 24 25 26 27
      @permission_cache ||= {}
      @permission_cache[action] ||= user.can?(action, project)
    end

    def cannot_do_action?(action)
      !can_do_action?(action)
    end

    def allowed?
28
      return false unless can_access_git?
29

J
Jacob Vosmaer 已提交
30 31
      if user.requires_ldap_check? && user.try_obtain_ldap_lease
        return false unless Gitlab::LDAP::Access.allowed?(user)
32 33 34 35
      end

      true
    end
36

37
    request_cache def can_create_tag?(ref)
38 39
      return false unless can_access_git?

40 41
      if protected?(ProtectedTag, project, ref)
        protected_tag_accessible_to?(ref, action: :create)
42 43 44 45 46
      else
        user.can?(:push_code, project)
      end
    end

47
    request_cache def can_delete_branch?(ref)
48 49
      return false unless can_access_git?

50
      if protected?(ProtectedBranch, project, ref)
51 52 53 54 55 56
        user.can?(:delete_protected_branch, project)
      else
        user.can?(:push_code, project)
      end
    end

57
    def can_update_branch?(ref)
58 59 60
      can_push_to_branch?(ref) || can_merge_to_branch?(ref)
    end

61
    request_cache def can_push_to_branch?(ref)
62
      return false unless can_access_git?
63

64
      if protected?(ProtectedBranch, project, ref)
65 66
        return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)

67
        protected_branch_accessible_to?(ref, action: :push)
68 69 70 71 72
      else
        user.can?(:push_code, project)
      end
    end

73
    request_cache def can_merge_to_branch?(ref)
74
      return false unless can_access_git?
75

76 77
      if protected?(ProtectedBranch, project, ref)
        protected_branch_accessible_to?(ref, action: :merge)
78 79 80 81 82 83
      else
        user.can?(:push_code, project)
      end
    end

    def can_read_project?
84
      return false unless can_access_git?
85 86 87

      user.can?(:read_project, project)
    end
88 89 90

    private

91 92
    def can_access_git?
      user && user.can?(:access_git)
93
    end
94 95 96

    def protected_branch_accessible_to?(ref, action:)
      ProtectedBranch.protected_ref_accessible_to?(
97 98
        ref, user,
        action: action,
99 100 101 102 103
        protected_refs: project.protected_branches)
    end

    def protected_tag_accessible_to?(ref, action:)
      ProtectedTag.protected_ref_accessible_to?(
104 105
        ref, user,
        action: action,
106 107 108 109 110 111
        protected_refs: project.protected_tags)
    end

    request_cache def protected?(kind, project, ref)
      kind.protected?(project, ref)
    end
112 113
  end
end