BigW Consortium Gitlab

user_access.rb 1.47 KB
Newer Older
1
module Gitlab
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  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?
21

Jacob Vosmaer committed
22 23
      if user.requires_ldap_check? && user.try_obtain_ldap_lease
        return false unless Gitlab::LDAP::Access.allowed?(user)
24 25 26 27
      end

      true
    end
28 29 30 31

    def can_push_to_branch?(ref)
      return false unless user

32
      if project.protected_branch?(ref)
33 34
        return true if project.empty_repo? && project.user_can_push_to_empty_repo?(user)

35
        access_levels = project.protected_branches.matching(ref).map(&:push_access_levels).flatten
36
        access_levels.any? { |access_level| access_level.check_access(user) }
37 38 39 40 41 42 43 44
      else
        user.can?(:push_code, project)
      end
    end

    def can_merge_to_branch?(ref)
      return false unless user

45
      if project.protected_branch?(ref)
46
        access_levels = project.protected_branches.matching(ref).map(&:merge_access_levels).flatten
47
        access_levels.any? { |access_level| access_level.check_access(user) }
48 49 50 51 52 53 54 55 56 57
      else
        user.can?(:push_code, project)
      end
    end

    def can_read_project?
      return false unless user

      user.can?(:read_project, project)
    end
58 59
  end
end