Sha256: 33440365b87f9137b088822b8f4822653c1209a90f968f0e236929fb66270ddb

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# Abstract base class for all policies
# @abstract
# @!attribute [r] user
#   @return [User] the current user
# @!attribute [r] record
#   @return [Object] some kind of model object, whose authorization you want to check
class Maestrano::Connector::Rails::ApplicationPolicy
  attr_reader :user, :record

  # Returns a new instance of {BasePolicy}
  # @param [User] user the current user
  # @param [Object] record some kind of model object, whose authorization you want to check
  # @return [ApplicationPolicy]
  def initialize(user, record)
    # Closed system: must be logged in to do anything
    raise Pundit::NotAuthorizedError, 'must be logged in' unless user
    @user = user
    @record = record
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    create?
  end

  def edit?
    update?
  end

  def destroy?
    false
  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_to_tenant
    end

    def scope_to_tenant
      scope.where(tenant: user)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
maestrano-connector-rails-2.3.2 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.3.1 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.3.0 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.2.1 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.2.0 app/policies/maestrano/connector/rails/application_policy.rb