Sha256: b22e68a9a6a35312c4cdb0b70e96f28c7b7beb9006bb59146ff2dd698b55edf0

Contents?: true

Size: 1.29 KB

Versions: 31

Compression:

Stored size: 1.29 KB

Contents

module Itsf::Backend
  class BasePolicy
    attr_reader :user, :record

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

    def index?
      # false
      authorize_with_rbac(user, record, __method__)
    end

    def show?
      # scope.where(id: record.id).exists?
       authorize_with_rbac(user, record, __method__)
    end

    def create?
      # false
      authorize_with_rbac(user, record, __method__)
    end

    def new?
      create?
    end

    def update?
      # false
      authorize_with_rbac(user, record, __method__)
    end

    def edit?
      update?
    end

    def destroy?
      # false
      authorize_with_rbac(user, record, __method__)
    end

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

    class Scope
      attr_reader :user, :scope

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

      def resolve
        scope
      end
    end

    private

    def authorize_with_rbac(user, record, method_name)
      resource_name = record.class == Class ? record.name : record.class.name
      permission_identifier = "#{resource_name.underscore}/#{method_name.to_s.chomp('?')}"
      allowed = user.respond_to?(:allowed_to?) ? user.allowed_to?(permission_identifier) : true
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
itsf_backend-5.0.0.pre app/policies/itsf/backend/base_policy.rb
itsf_backend-4.2.3 app/policies/itsf/backend/base_policy.rb
itsf_backend-4.2.2 app/policies/itsf/backend/base_policy.rb
itsf_backend-4.2.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-4.2.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-4.0.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-4.0.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.1.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.1.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.7 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.6 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.5 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.4 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.3 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.2 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-3.0.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-2.2.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-2.2.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-2.1.0 app/policies/itsf/backend/base_policy.rb