Sha256: b279f9008d9244ed385025b7398ceb57daa85c7d20158c66e920b274d6e40247

Contents?: true

Size: 1.78 KB

Versions: 13

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module G5Authenticatable
  # Base class for all pundit authorization policies
  # Defaults to limiting every action to super admin users
  class BasePolicy
    attr_reader :user, :record

    def initialize(user, record = nil)
      @user = user
      @record = record
    end

    def index?
      super_admin?
    end

    def show?
      scope.where(id: record.id).exists?
    end

    def create?
      super_admin?
    end

    def new?
      create?
    end

    def update?
      super_admin?
    end

    def edit?
      update?
    end

    def destroy?
      super_admin?
    end

    def scope
      Pundit.policy_scope!(user, record.class)
    end

    # Base class for all authorization scopes
    class BaseScope
      attr_reader :user, :scope

      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      def resolve
        if user.has_role?(:super_admin)
          scope.all
        else
          scope.none
        end
      end

      def global_role?
        G5Authenticatable::BasePolicy.new(user, nil).global_role?
      end

      alias has_global_role? global_role?
    end

    def super_admin?
      user.present? && user.has_role?(:super_admin)
    end

    def admin?
      user.present? && user.has_role?(:admin)
    end

    def editor?
      user.present? && user.has_role?(:editor)
    end

    def viewer?
      user.present? && user.has_role?(:viewer)
    end

    def has_global_role?
      ActiveSupport::Deprecation.warn <<-DEPRECATION.strip_heredoc
        [G5Authenticatable] the `has_global_role?` method is deprecated and
        will be removed. Use `global_role?` instead.
      DEPRECATION
      global_role?
    end

    def global_role?
      super_admin? || admin? || editor? || viewer?
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
g5_authenticatable-1.1.4 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.4.rc.3 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.4.rc.2 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.4.rc.1 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.2 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.2.pre.1 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.1 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.0 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.0.0 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.0.0.pre.4 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.0.0.pre.3 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.0.0.pre.2 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.0.0.pre.1 app/policies/g5_authenticatable/base_policy.rb