user_access.rb 1.36 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's avatar
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 33 34
      if project.protected_branch?(ref)
        access_levels = project.protected_branches.matching(ref).map(&:push_access_level)
        access_levels.any? { |access_level| access_level.check_access(user) }
35 36 37 38 39 40 41 42
      else
        user.can?(:push_code, project)
      end
    end

    def can_merge_to_branch?(ref)
      return false unless user

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

    def can_read_project?
      return false unless user

      user.can?(:read_project, project)
    end
56 57
  end
end