Sha256: 04a76db734bbb5dd7695ea2fe71b272da1d906e5b0e0d52f670c1dcd8d6cf0f6

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

class G5Authenticatable::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

  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 has_global_role?
      G5Authenticatable::BasePolicy.new(user, G5Updatable::Client).has_global_role?
    end
  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?
    super_admin? || admin? || editor? || viewer?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
g5_authenticatable-0.9.1.pre.2 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-0.8.1.pre app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-0.8.0 app/policies/g5_authenticatable/base_policy.rb
g5_authenticatable-0.8.0.beta1 app/policies/g5_authenticatable/base_policy.rb