Sha256: a38e2b4aa68824b63e6aad57925033122ec844a9484a72b45d07cf721a286e09

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 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?
    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

15 entries across 15 versions & 1 rubygems

Version Path
itsf_backend-1.1.13 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.12 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.11 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.10 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.9 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.8 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.7 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.6 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.5 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.4 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.3 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.2 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.1 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.1.0 app/policies/itsf/backend/base_policy.rb
itsf_backend-1.0.1 app/policies/itsf/backend/base_policy.rb