user_access.rb 2.1 KB
Newer Older
1
module Gitlab
2 3 4 5 6 7 8 9 10
  class UserAccess
    attr_reader :user, :project

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

    def can_do_action?(action)
11
      return false unless can_access_git?
12

13 14 15 16 17 18 19 20 21
      @permission_cache ||= {}
      @permission_cache[action] ||= user.can?(action, project)
    end

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

    def allowed?
22
      return false unless can_access_git?
23

J
Jacob Vosmaer 已提交
24 25
      if user.requires_ldap_check? && user.try_obtain_ldap_lease
        return false unless Gitlab::LDAP::Access.allowed?(user)
26 27 28 29
      end

      true
    end
30

31
    def can_create_tag?(ref)
32 33
      return false unless can_access_git?

34
      if ProtectedTag.protected?(project, ref)
35
        project.protected_tags.protected_ref_accessible_to?(ref, user, action: :create)
36 37 38 39 40
      else
        user.can?(:push_code, project)
      end
    end

41 42 43 44 45 46 47 48 49 50
    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

51 52 53 54
    def can_push_or_merge_to_branch?(ref)
      can_push_to_branch?(ref) || can_merge_to_branch?(ref)
    end

55
    def can_push_to_branch?(ref)
56
      return false unless can_access_git?
57

58
      if ProtectedBranch.protected?(project, ref)
59 60
        return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)

61
        project.protected_branches.protected_ref_accessible_to?(ref, user, action: :push)
62 63 64 65 66 67
      else
        user.can?(:push_code, project)
      end
    end

    def can_merge_to_branch?(ref)
68
      return false unless can_access_git?
69

70
      if ProtectedBranch.protected?(project, ref)
71
        project.protected_branches.protected_ref_accessible_to?(ref, user, action: :merge)
72 73 74 75 76 77
      else
        user.can?(:push_code, project)
      end
    end

    def can_read_project?
78
      return false unless can_access_git?
79 80 81

      user.can?(:read_project, project)
    end
82 83 84

    private

85 86
    def can_access_git?
      user && user.can?(:access_git)
87
    end
88 89
  end
end