Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harry Brundage
committed
Aug 9, 2010
1 parent
9cd08d7
commit c1b39c2
Showing
4 changed files
with
57 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Thwart | ||
module Enforcer | ||
def thwart_access(resource) | ||
raise ArgumentError, "Thwart needs a current_user method to enforce permissions." unless self.respond_to?(:current_user) | ||
raise ArgumentError, "Thwart needs the params hash to have an [:action] to enforce." if params.nil? || params[:action].nil? | ||
raise ArgumentError, "Unknown action #{params[:action]} to enforce" unless Thwart::Actions.has_can?(params[:action]) | ||
|
||
unless Thwart.query(current_user, resource, params[:action]) | ||
raise Thwart::NoPermissionError, "User #{current_user} doesn't have permission to #{params[:action]} #{resource}." | ||
else | ||
true | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
|
||
describe Thwart::Enforcer do | ||
context "access enforcment" do | ||
it "should need the current user method defined on the controller" do | ||
lambda { instance_with_module(Thwart::Enforcer).thwart_access }.should raise_error(ArgumentError) | ||
end | ||
it "should need the params hash to have an action key" do | ||
class_with_module(Thwart::Enforcer) | ||
lambda { @controller.thwart_access }.should raise_error(ArgumentError) | ||
end | ||
it "should need the params[:actions] to be a recognized action" | ||
it "should thwart access by raising an error if the user doesn't have permission" | ||
it "should return true if the user does have permission" | ||
end | ||
end |