base_policy.rb 1.39 KB
Newer Older
1 2
# frozen_string_literal: true

3
require_dependency 'declarative_policy'
4

5 6 7
class BasePolicy < DeclarativePolicy::Base
  desc "User is an instance admin"
  with_options scope: :user, score: 0
8 9 10 11 12 13 14
  condition(:admin) do
    if Feature.enabled?(:user_mode_in_session)
      Gitlab::Auth::CurrentUserMode.new(@user).admin_mode?
    else
      @user&.admin?
    end
  end
15

16 17 18 19
  desc "User is blocked"
  with_options scope: :user, score: 0
  condition(:blocked) { @user&.blocked? }

20 21 22 23
  desc "User has access to all private groups & projects"
  with_options scope: :user, score: 0
  condition(:full_private_access) { @user&.full_private_access? }

24 25
  with_options scope: :user, score: 0
  condition(:external_user) { @user.nil? || @user.external? }
26

27 28
  with_options scope: :user, score: 0
  condition(:can_create_group) { @user&.can_create_group }
29

30 31
  desc "The application is restricted from public visibility"
  condition(:restricted_public_level, scope: :global) do
32
    Gitlab::CurrentSettings.current_application_settings.restricted_visibility_levels.include?(Gitlab::VisibilityLevel::PUBLIC)
33 34
  end

35 36 37 38 39 40 41 42
  condition(:external_authorization_enabled, scope: :global, score: 0) do
    ::Gitlab::ExternalAuthorization.perform_check?
  end

  rule { external_authorization_enabled & ~full_private_access }.policy do
    prevent :read_cross_project
  end

43
  rule { default }.enable :read_cross_project
44
end
45

46
BasePolicy.prepend_if_ee('EE::BasePolicy')