-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Pundit for authorization + let there always be a model (#180)
- Loading branch information
1 parent
b06229c
commit f16c720
Showing
12 changed files
with
43 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %> < ::<%= class_name %> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,20 @@ | ||
require "pundit" | ||
require "godmin/authorization/policy" | ||
require "godmin/authorization/policy_finder" | ||
|
||
module Godmin | ||
module Authorization | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :policy | ||
include Pundit | ||
|
||
rescue_from NotAuthorizedError do | ||
included do | ||
rescue_from Pundit::NotAuthorizedError do | ||
render plain: "You are not authorized to do this", status: 403, layout: "godmin/login" | ||
end | ||
end | ||
|
||
def authorize(record, query = nil) | ||
policy = policy(record) | ||
|
||
unless policy.public_send(query || action_name + "?") | ||
fail NotAuthorizedError | ||
end | ||
def pundit_user | ||
admin_user | ||
end | ||
|
||
def policy(record) | ||
policies[record] ||= PolicyFinder.find(record, engine_wrapper.namespace).new(admin_user, record) | ||
end | ||
|
||
def policies | ||
@_policies ||= {} | ||
end | ||
|
||
class NotAuthorizedError < StandardError; end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.