Sha256: a1b0140b00ac03df708978db4b3467b917b087ef43fa132c981303a32cfea659

Contents?: true

Size: 1.18 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
maestrano-connector-rails-2.3.6 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.3.5 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.3.4 app/policies/maestrano/connector/rails/application_policy.rb
maestrano-connector-rails-2.3.3 app/policies/maestrano/connector/rails/application_policy.rb