user policies

This commit is contained in:
Mark Moser
2016-09-20 14:22:20 -05:00
parent 12c7e9e77c
commit ead9564fe8
8 changed files with 160 additions and 2 deletions

View File

@ -0,0 +1,60 @@
# frozen_string_literal: true
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "Must be logged in." unless user
@user = user
@record = record
end
def index?
false
end
def show?
scope.where(id: record.id).exists?
end
def view?
show?
end
def create?
false
end
def new?
create?
end
def update?
false
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
# This is a closed system.
raise Pundit::NotAuthorizedError, "No access to resource."
end
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
class UserPolicy < ApplicationPolicy
def view?
user.admin? && show?
end
def create?
user.admin?
end
def update?
user.admin?
end
class Scope < Scope
def resolve
return scope if user.admin?
raise Pundit::NotAuthorizedError, "No access to resource."
end
end
end