Sha256: 4494b6e7e411a22c413df8aa4dffc4a52937163c5bb005b0c65bfc284e77c178

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 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?
      user.roles.global.exists?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
g5_authenticatable-1.1.2.rc.5 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-1.1.2.rc.4 app/policies/g5_authenticatable/base_policy.rb