thwart 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +6 -0
- data/Rakefile +2 -3
- data/VERSION +1 -1
- data/lib/thwart.rb +11 -8
- data/lib/thwart/action_group_builder.rb +0 -1
- data/lib/thwart/actions_store.rb +2 -1
- data/lib/thwart/canable.rb +6 -6
- data/lib/thwart/dsl.rb +2 -3
- data/lib/thwart/enforcer.rb +10 -5
- data/lib/thwart/rails.rb +5 -0
- data/lib/thwart/resource.rb +10 -1
- data/lib/thwart/role.rb +16 -6
- data/lib/thwart/role_builder.rb +1 -1
- data/lib/thwart/role_registry.rb +27 -17
- data/spec/actions_store_spec.rb +3 -3
- data/spec/dsl_spec.rb +1 -1
- data/spec/enforcer_spec.rb +36 -8
- data/spec/resource_spec.rb +1 -1
- data/spec/role_registry_spec.rb +12 -24
- data/spec/role_spec.rb +26 -1
- data/spec/spec_helper.rb +10 -4
- data/spec/thwart_spec.rb +2 -4
- data/thwart.gemspec +4 -3
- metadata +5 -5
- data/examples/testing.rb +0 -21
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
-
|
3
|
+
require 'bundler'
|
4
4
|
begin
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
@@ -10,8 +10,7 @@ begin
|
|
10
10
|
gem.email = "harry@skylightlabs.ca"
|
11
11
|
gem.homepage = "http://github.com/hornairs/thwart"
|
12
12
|
gem.authors = ["Harry Brundage"]
|
13
|
-
gem.
|
14
|
-
gem.add_dependency "activesupport", ">= 3.0.rc1"
|
13
|
+
gem.add_bundler_dependencies
|
15
14
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
15
|
end
|
17
16
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/thwart.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support'
|
2
|
+
require 'active_support/ruby/shim'
|
2
3
|
require 'active_support/callbacks'
|
3
4
|
require 'active_support/core_ext/module/attribute_accessors'
|
4
5
|
require "active_support/core_ext/module/delegation"
|
@@ -30,23 +31,23 @@ module Thwart
|
|
30
31
|
# autoload :Dsl, 'thwart/dsl'
|
31
32
|
|
32
33
|
# The default can => able methods for CRUD
|
33
|
-
CrudActions = {:create => :creatable, :show => :
|
34
|
+
CrudActions = {:create => :creatable, :show => :showable, :update => :updatable, :destroy => :destroyable}
|
34
35
|
|
35
36
|
Actions = ActionsStore.new
|
37
|
+
Actionables = ActionGroupBuilder.new(Actions)
|
36
38
|
Roles = RoleRegistry.new
|
37
39
|
|
38
40
|
class << self
|
39
|
-
attr_reader :
|
40
|
-
attr_accessor :default_query_response, :role_registry, :actor_must_play_role, :all_classes_are_resources
|
41
|
+
attr_reader :role_dsl
|
42
|
+
attr_accessor :default_query_response, :role_registry, :actor_must_play_role, :all_classes_are_resources, :log_query_path, :last_query_path
|
41
43
|
delegate :create_action, :to => "Thwart::Actions"
|
42
|
-
delegate :create_action_group, :to =>
|
44
|
+
delegate :create_action_group, :to => "Thwart::Actionables"
|
43
45
|
delegate :create_role, :to => :role_dsl
|
44
46
|
delegate :query, :to => "Thwart::Roles"
|
45
47
|
|
46
48
|
def configure(&block)
|
47
49
|
# Create builder DSLs for this configuration block
|
48
|
-
@
|
49
|
-
@role_dsl = RoleBuilder.new(@actionables_dsl)
|
50
|
+
@role_dsl = RoleBuilder.new(Actionables)
|
50
51
|
Roles.monitor_builder(@role_dsl)
|
51
52
|
|
52
53
|
# Configure
|
@@ -55,13 +56,15 @@ module Thwart
|
|
55
56
|
dsl.evaluate(self, &block)
|
56
57
|
|
57
58
|
# Unset and stop monitoring builder DSLs so they can be GC'd
|
58
|
-
@actionables_dsl = nil
|
59
59
|
@role_dsl = nil
|
60
60
|
Roles.monitor_builder(nil)
|
61
61
|
self
|
62
62
|
end
|
63
63
|
end
|
64
|
+
self.log_query_path = false
|
64
65
|
|
65
66
|
class MissingAttributeError < StandardError; end
|
66
67
|
class NoPermissionError < StandardError; end
|
67
|
-
end
|
68
|
+
end
|
69
|
+
|
70
|
+
require 'thwart/rails' if defined?(Rails)
|
@@ -37,7 +37,6 @@ module Thwart
|
|
37
37
|
|
38
38
|
return name.map{|n| resolve_action_group(n)}.flatten.uniq if name.respond_to?(:map)
|
39
39
|
return self.actionables[name].flatten.uniq if self.actionables.include?(name)
|
40
|
-
puts self.actionables
|
41
40
|
raise Thwart::ActionOrGroupNotFoundError, "Action or group #{name} could not be found!"
|
42
41
|
end
|
43
42
|
|
data/lib/thwart/actions_store.rb
CHANGED
@@ -29,7 +29,8 @@ module Thwart
|
|
29
29
|
#
|
30
30
|
# @param [Symbol] able_method The name of the [action-able]_by? method.
|
31
31
|
def can_from_able(able)
|
32
|
-
self.actions.
|
32
|
+
pair = self.actions.find {|k, v| v == able}
|
33
|
+
pair.first if pair
|
33
34
|
end
|
34
35
|
|
35
36
|
# Adds an action to actions and the correct methods to can and able modules.
|
data/lib/thwart/canable.rb
CHANGED
@@ -2,12 +2,12 @@ module Thwart
|
|
2
2
|
# Module in which the can_[action]? methods hang out
|
3
3
|
module Cans
|
4
4
|
|
5
|
-
def respond_to?(
|
6
|
-
return true if Thwart::Actions.find_can(
|
5
|
+
def respond_to?(*args)
|
6
|
+
return true if Thwart::Actions.find_can(args.first) != false
|
7
7
|
super
|
8
8
|
end
|
9
9
|
|
10
|
-
def method_missing(name, *args)
|
10
|
+
def method_missing(name, *args, &block)
|
11
11
|
can = Thwart::Actions.find_can(name)
|
12
12
|
return Thwart.query(self, args.first, can) if args.length == 1 && !!can
|
13
13
|
super
|
@@ -16,12 +16,12 @@ module Thwart
|
|
16
16
|
|
17
17
|
# Module in which the [action]able_by? methods hang out
|
18
18
|
module Ables
|
19
|
-
def respond_to?(
|
20
|
-
return true if Thwart::Actions.find_able(
|
19
|
+
def respond_to?(*args)
|
20
|
+
return true if Thwart::Actions.find_able(args.first) != false
|
21
21
|
super
|
22
22
|
end
|
23
23
|
|
24
|
-
def method_missing(name, *args)
|
24
|
+
def method_missing(name, *args, &block)
|
25
25
|
able = Thwart::Actions.find_able(name)
|
26
26
|
return Thwart.query(args.first, self, Thwart::Actions.can_from_able(able)) if args.length == 1 && !!able
|
27
27
|
super
|
data/lib/thwart/dsl.rb
CHANGED
@@ -13,16 +13,15 @@ module Thwart
|
|
13
13
|
def evaluate(a_target, &block)
|
14
14
|
self.target = a_target
|
15
15
|
self.method_map = target.public_methods.inject({}) do |acc, m|
|
16
|
-
key = m.to_s.gsub(
|
16
|
+
key = m.to_s.gsub(/=$/, "").to_sym
|
17
17
|
acc[key] = m if acc[key].nil? || m != key
|
18
18
|
acc
|
19
19
|
end.merge(self.extra_methods)
|
20
|
-
|
21
20
|
self.instance_eval(&block)
|
22
21
|
self.target
|
23
22
|
end
|
24
23
|
|
25
|
-
def respond_to?(name)
|
24
|
+
def respond_to?(name, other = false)
|
26
25
|
if @all
|
27
26
|
return target.respond_to?(name)
|
28
27
|
else
|
data/lib/thwart/enforcer.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
module Thwart
|
2
2
|
module Enforcer
|
3
|
-
def thwart_access(resource)
|
3
|
+
def thwart_access(resource, action = nil)
|
4
|
+
if action.blank?
|
5
|
+
raise ArgumentError, "thwart_access needs an action or the params hash to have an [:action] to enforce." if !self.respond_to?(:params) || !self.params.respond_to?(:[]) || self.params[:action].nil?
|
6
|
+
action = params[:action]
|
7
|
+
end
|
8
|
+
action = action.to_sym
|
4
9
|
raise ArgumentError, "Thwart needs a current_user method to enforce permissions." unless self.respond_to?(:current_user)
|
5
|
-
|
6
|
-
raise ArgumentError, "Unknown action #{
|
10
|
+
|
11
|
+
raise ArgumentError, "Unknown action #{action} to enforce" unless Thwart::Actions.has_can?(action)
|
7
12
|
|
8
|
-
unless Thwart.query(current_user, resource,
|
9
|
-
raise Thwart::NoPermissionError, "User #{current_user} doesn't have permission to #{
|
13
|
+
unless Thwart.query(current_user, resource, action)
|
14
|
+
raise Thwart::NoPermissionError, "User #{current_user} doesn't have permission to #{action} #{resource}."
|
10
15
|
else
|
11
16
|
true
|
12
17
|
end
|
data/lib/thwart/rails.rb
ADDED
data/lib/thwart/resource.rb
CHANGED
@@ -6,11 +6,20 @@ module Thwart::Resource
|
|
6
6
|
base.extend ClassMethods
|
7
7
|
end
|
8
8
|
|
9
|
+
def thwart_name
|
10
|
+
self.class.thwart_name
|
11
|
+
end
|
12
|
+
|
9
13
|
module ClassMethods
|
10
14
|
attr_accessor :thwart_name
|
11
15
|
|
16
|
+
def thwart_name
|
17
|
+
return nil unless @thwarted
|
18
|
+
return @thwart_name unless @thwart_name.nil?
|
19
|
+
ActiveSupport::Inflector.singularize(self.table_name).to_sym if self.respond_to?(:table_name)
|
20
|
+
end
|
12
21
|
def thwart_access(&block)
|
13
|
-
|
22
|
+
@thwarted = true
|
14
23
|
|
15
24
|
# Set up DSL using dsl helper
|
16
25
|
if block_given?
|
data/lib/thwart/role.rb
CHANGED
@@ -21,10 +21,9 @@ module Thwart
|
|
21
21
|
def query(actor, resource, action)
|
22
22
|
@query_result_found = false
|
23
23
|
resp = nil
|
24
|
-
|
25
24
|
if self.responses.has_key?(action)
|
26
25
|
# Find the resource scope response if it exists {:view => {:foo => bool}}
|
27
|
-
resp = self.resource_response(self.responses[action], resource) if !found?
|
26
|
+
resp = self.resource_response(self.responses[action], self.find_resource_name(resource)) if !found?
|
28
27
|
# Find the action scope response if it exists {:view => bool}
|
29
28
|
resp = self.action_response(action) if !found?
|
30
29
|
end
|
@@ -34,14 +33,14 @@ module Thwart
|
|
34
33
|
# Call it if it is a proc
|
35
34
|
resp = resp.call(actor, resource, action) if resp.respond_to?(:call)
|
36
35
|
|
37
|
-
|
36
|
+
resp
|
38
37
|
end
|
39
38
|
|
40
|
-
def resource_response(resources,
|
39
|
+
def resource_response(resources, name)
|
41
40
|
# Return the resource scoped response if it exists
|
42
41
|
if resources.respond_to?(:[]) && resources.respond_to?(:include?)
|
43
|
-
if resources.include?(
|
44
|
-
return found!(resources[
|
42
|
+
if resources.include?(name)
|
43
|
+
return found!(resources[name])
|
45
44
|
elsif resources.include?(:_other)
|
46
45
|
return found!(resources[:_other])
|
47
46
|
end
|
@@ -58,6 +57,17 @@ module Thwart
|
|
58
57
|
nil
|
59
58
|
end
|
60
59
|
|
60
|
+
def find_resource_name(resource)
|
61
|
+
return resource if resource.is_a?(Symbol)
|
62
|
+
r ||= resource.thwart_name if resource.respond_to?(:thwart_name)
|
63
|
+
if resource.class != Class
|
64
|
+
r ||= resource.class.thwart_name if resource.class.respond_to?(:thwart_name)
|
65
|
+
r ||= resource.class.name.downcase if Thwart.all_classes_are_resources
|
66
|
+
end
|
67
|
+
r = r.to_sym if r.respond_to?(:to_sym)
|
68
|
+
r
|
69
|
+
end
|
70
|
+
|
61
71
|
private
|
62
72
|
|
63
73
|
def found!(response)
|
data/lib/thwart/role_builder.rb
CHANGED
data/lib/thwart/role_registry.rb
CHANGED
@@ -13,19 +13,36 @@ module Thwart
|
|
13
13
|
@roles << role
|
14
14
|
end
|
15
15
|
|
16
|
-
def query(actor, resource, action)
|
16
|
+
def query(actor, resource, action)
|
17
17
|
role = self.find_actor_role(actor)
|
18
|
-
|
18
|
+
if Thwart.log_query_path
|
19
|
+
Thwart.last_query_path = []
|
20
|
+
Thwart.last_query_path.push({:actor => actor, :resource => resource, :action => action})
|
21
|
+
name = resource.thwart_name if resource.respond_to?(:thwart_name)
|
22
|
+
name ||= resource
|
23
|
+
Thwart.last_query_path.push({:actor_role => role.name, :resource_name => resource})
|
24
|
+
end
|
25
|
+
|
19
26
|
if role.nil? || !self.has_role?(role)
|
20
27
|
raise MissingRoleError, "Role #{role} could not be found in the registry!" if Thwart.actor_must_play_role
|
21
28
|
else
|
22
29
|
q = [role]
|
23
30
|
while r = q.shift
|
24
31
|
resp = r.query(actor, resource, action)
|
25
|
-
|
32
|
+
|
33
|
+
if Thwart.log_query_path
|
34
|
+
Thwart.last_query_path.push("Querying #{r.name}")
|
35
|
+
Thwart.last_query_path.push(r)
|
36
|
+
Thwart.last_query_path.push("Response: #{resp}")
|
37
|
+
end
|
38
|
+
|
39
|
+
if resp != nil
|
26
40
|
return resp # positive/negative response from the role, a rule governs the role on this query
|
27
41
|
else
|
28
|
-
q = q | r.parents
|
42
|
+
q = q | r.parents.map do |a|
|
43
|
+
a = self.find_role(a) if a.is_a?(Symbol)
|
44
|
+
a
|
45
|
+
end # add this roles parents to the query queue
|
29
46
|
end
|
30
47
|
end
|
31
48
|
end
|
@@ -39,19 +56,8 @@ module Thwart
|
|
39
56
|
|
40
57
|
def find_actor_role(actor)
|
41
58
|
r = actor.thwart_role if actor.respond_to?(:thwart_role)
|
42
|
-
r
|
43
|
-
if r.is_a?(Symbol)
|
44
|
-
r = self.roles.find {|a| a.name == r}
|
45
|
-
end
|
46
|
-
r
|
47
|
-
end
|
48
|
-
|
49
|
-
def find_resource_identifier(resource)
|
50
|
-
r ||= resource.thwart_name if resource.respond_to?(:thwart_name)
|
51
|
-
if resource.class != Class
|
52
|
-
r ||= resource.class.thwart_name if resource.class.respond_to?(:thwart_name)
|
53
|
-
r ||= resource.class.name.downcase.to_sym if Thwart.all_classes_are_resources
|
54
|
-
end
|
59
|
+
r = r.to_sym if r.respond_to?(:to_sym)
|
60
|
+
r = find_role(r) if r.is_a?(Symbol)
|
55
61
|
r
|
56
62
|
end
|
57
63
|
|
@@ -71,5 +77,9 @@ module Thwart
|
|
71
77
|
end
|
72
78
|
@role_creator = role_creator
|
73
79
|
end
|
80
|
+
|
81
|
+
def find_role(name)
|
82
|
+
self.roles.find {|a| a.name == name}
|
83
|
+
end
|
74
84
|
end
|
75
85
|
end
|
data/spec/actions_store_spec.rb
CHANGED
@@ -50,13 +50,13 @@ describe Thwart::ActionsStore do
|
|
50
50
|
it "should have C for create" do
|
51
51
|
@actions.has_can?(:create).should == true
|
52
52
|
end
|
53
|
-
it "should have R for
|
54
|
-
@actions.has_can?(:
|
53
|
+
it "should have R for .. er ... show" do
|
54
|
+
@actions.has_can?(:show).should == true
|
55
55
|
end
|
56
56
|
it "should have U for update" do
|
57
57
|
@actions.has_can?(:update).should == true
|
58
58
|
end
|
59
|
-
it "should have D for
|
59
|
+
it "should have D for destroy" do
|
60
60
|
@actions.has_can?(:destroy).should == true
|
61
61
|
end
|
62
62
|
end
|
data/spec/dsl_spec.rb
CHANGED
data/spec/enforcer_spec.rb
CHANGED
@@ -1,17 +1,45 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
3
|
+
def enforced_controller(&block)
|
4
|
+
class_with_module(Thwart::Enforcer, &block)
|
5
|
+
end
|
4
6
|
describe Thwart::Enforcer do
|
7
|
+
before do
|
8
|
+
Thwart::Actions.stub(:has_can? => true)
|
9
|
+
@controller_klass = enforced_controller do
|
10
|
+
def current_user
|
11
|
+
:user
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@r = double("resource")
|
15
|
+
end
|
16
|
+
|
5
17
|
context "access enforcment" do
|
6
18
|
it "should need the current user method defined on the controller" do
|
7
|
-
lambda {
|
19
|
+
lambda { enforced_controller.new.thwart_access }.should raise_error(ArgumentError)
|
20
|
+
end
|
21
|
+
it "should need either the params hash or be passed action key" do
|
22
|
+
i = @controller_klass.new
|
23
|
+
lambda { i.thwart_access(@r) }.should raise_error(ArgumentError)
|
24
|
+
lambda { i.thwart_access(@r, :some_action) }.should_not raise_error(ArgumentError)
|
25
|
+
|
26
|
+
i2 = @controller_klass.new
|
27
|
+
i2.should_receive(:params).at_least(:once).and_return({:action => :an_action})
|
28
|
+
lambda { i2.thwart_access(@r) }.should_not raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should thwart access by raising an error if the user doesn't have permission" do
|
32
|
+
Thwart.stub(:query => false)
|
33
|
+
lambda { @controller_klass.new.thwart_access(@r, :an_action) }.should raise_error(Thwart::NoPermissionError)
|
34
|
+
end
|
35
|
+
it "should return true if the user does have permission" do
|
36
|
+
Thwart.stub(:query => true)
|
37
|
+
lambda { @controller_klass.new.thwart_access(@r, :an_action) }.should_not raise_error(Thwart::NoPermissionError)
|
8
38
|
end
|
9
|
-
it "should
|
10
|
-
|
11
|
-
lambda { @
|
39
|
+
it "should convert string actions" do
|
40
|
+
Thwart.stub(:query => true)
|
41
|
+
lambda { @controller_klass.new.thwart_access(@r, "an_action") }.should_not raise_error(Thwart::NoPermissionError)
|
12
42
|
end
|
13
|
-
|
14
|
-
it "should thwart access by raising an error if the user doesn't have permission"
|
15
|
-
it "should return true if the user does have permission"
|
43
|
+
|
16
44
|
end
|
17
45
|
end
|
data/spec/resource_spec.rb
CHANGED
data/spec/role_registry_spec.rb
CHANGED
@@ -49,35 +49,15 @@ describe Thwart::RoleRegistry do
|
|
49
49
|
actor = double("Actor", :thwart_role => :role1)
|
50
50
|
@registry.find_actor_role(actor).should == @role
|
51
51
|
end
|
52
|
+
it "should convert strings to roles" do
|
53
|
+
actor = double("Actor", :thwart_role => "role1")
|
54
|
+
@registry.find_actor_role(actor).should == @role
|
55
|
+
end
|
52
56
|
it "should find nil for symbols pointing to non registered roles " do
|
53
57
|
actor = double("Actor", :thwart_role => :role2)
|
54
58
|
@registry.find_actor_role(actor).should == nil
|
55
59
|
end
|
56
60
|
end
|
57
|
-
context "resource finding" do
|
58
|
-
it "should find nil for nil" do
|
59
|
-
@registry.find_resource_identifier(nil).should == nil
|
60
|
-
end
|
61
|
-
it "should find using the thwart_name attribute" do
|
62
|
-
resource = double("Resource", :thwart_name => :balls)
|
63
|
-
@registry.find_resource_identifier(resource).should == :balls
|
64
|
-
end
|
65
|
-
it "should find using the class thwart_name attribute" do
|
66
|
-
klass = Class.new do
|
67
|
-
def thwart_name
|
68
|
-
:balls
|
69
|
-
end
|
70
|
-
end
|
71
|
-
@registry.find_resource_identifier(klass.new).should == :balls
|
72
|
-
end
|
73
|
-
it "should find using the class name if the gem wide setting is set" do
|
74
|
-
Thwart.all_classes_are_resources = true
|
75
|
-
class Bollocks; end
|
76
|
-
@registry.find_resource_identifier(Bollocks.new).should == :bollocks
|
77
|
-
Thwart.all_classes_are_resources = false
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
61
|
context "querying" do
|
82
62
|
it "should return the default if the role can't be found and the gem wide setting is set" do
|
83
63
|
Thwart.actor_must_play_role = false
|
@@ -110,6 +90,14 @@ describe Thwart::RoleRegistry do
|
|
110
90
|
@registry.query(actor_with_role(@role1), nil, nil).should == false
|
111
91
|
end
|
112
92
|
|
93
|
+
it "should log the query path if the gem wide setting is set" do
|
94
|
+
Thwart.stub(:log_query_path => true)
|
95
|
+
a = actor_with_role(@role1)
|
96
|
+
@registry.query(a, nil, nil)
|
97
|
+
Thwart.last_query_path.should == [
|
98
|
+
{:actor => a, :resource => nil, :action => nil}, {:actor_role => :role1, :resource_name => nil}, "Querying role1", @role1, "Response: false"]
|
99
|
+
end
|
100
|
+
|
113
101
|
it "should query the parents in a breadth first order if the role query is unsuccessful" do
|
114
102
|
@role3.should_receive(:parents)
|
115
103
|
@registry.query(actor_with_role(@role3), nil, nil).should == true # this ensures role2 is checked before role1
|
data/spec/role_spec.rb
CHANGED
@@ -24,7 +24,32 @@ describe Thwart::Role do
|
|
24
24
|
@role.parents += [:a, :c]
|
25
25
|
@role.parents.should == [:a, :b, :c]
|
26
26
|
end
|
27
|
-
|
27
|
+
context "resource finding" do
|
28
|
+
before do
|
29
|
+
@role = instance_with_role_definition
|
30
|
+
end
|
31
|
+
it "should find nil for nil" do
|
32
|
+
@role.find_resource_name(nil).should == nil
|
33
|
+
end
|
34
|
+
it "should find using the thwart_name attribute" do
|
35
|
+
resource = double("Resource", :thwart_name => :balls)
|
36
|
+
@role.find_resource_name(resource).should == :balls
|
37
|
+
end
|
38
|
+
it "should find using the class thwart_name attribute" do
|
39
|
+
klass = Class.new do
|
40
|
+
def thwart_name
|
41
|
+
"balls"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
@role.find_resource_name(klass.new).should == :balls
|
45
|
+
end
|
46
|
+
it "should find using the class name if the gem wide setting is set" do
|
47
|
+
Thwart.all_classes_are_resources = true
|
48
|
+
class Bollocks; end
|
49
|
+
@role.find_resource_name(Bollocks.new).should == :bollocks
|
50
|
+
Thwart.all_classes_are_resources = false
|
51
|
+
end
|
52
|
+
end
|
28
53
|
context "with simple responses set" do
|
29
54
|
before do
|
30
55
|
@role = instance_with_role_definition do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup
|
4
|
+
|
1
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
7
|
require 'thwart'
|
@@ -32,14 +36,16 @@ def generic_model(name=nil, &block)
|
|
32
36
|
klass
|
33
37
|
end
|
34
38
|
|
35
|
-
def class_with_module(mod)
|
36
|
-
Class.new do
|
39
|
+
def class_with_module(mod, &block)
|
40
|
+
klass = Class.new do
|
37
41
|
include mod
|
38
42
|
end
|
43
|
+
klass.class_eval(&block) if block_given?
|
44
|
+
klass
|
39
45
|
end
|
40
46
|
|
41
|
-
def instance_with_module(mod)
|
42
|
-
class_with_module(mod).new
|
47
|
+
def instance_with_module(mod, &block)
|
48
|
+
class_with_module(mod, &block).new
|
43
49
|
end
|
44
50
|
|
45
51
|
def instance_with_role_definition(&block)
|
data/spec/thwart_spec.rb
CHANGED
@@ -45,10 +45,8 @@ describe Thwart do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
it "should create new action groups" do
|
48
|
-
|
49
|
-
Thwart::
|
50
|
-
actionables.should_receive(:create_action_group).with(:manage)
|
51
|
-
actionables.should_receive(:create_action_group).with(:inspect, [])
|
48
|
+
Thwart::Actionables.should_receive(:create_action_group).with(:manage)
|
49
|
+
Thwart::Actionables.should_receive(:create_action_group).with(:inspect, [])
|
52
50
|
Thwart.configure do
|
53
51
|
action_group :manage
|
54
52
|
action_group :inspect, []
|
data/thwart.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{thwart}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Harry Brundage"]
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"Gemfile",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
@@ -33,6 +34,7 @@ Gem::Specification.new do |s|
|
|
33
34
|
"lib/thwart/canable.rb",
|
34
35
|
"lib/thwart/dsl.rb",
|
35
36
|
"lib/thwart/enforcer.rb",
|
37
|
+
"lib/thwart/rails.rb",
|
36
38
|
"lib/thwart/resource.rb",
|
37
39
|
"lib/thwart/role.rb",
|
38
40
|
"lib/thwart/role_builder.rb",
|
@@ -70,8 +72,7 @@ Gem::Specification.new do |s|
|
|
70
72
|
"spec/spec_helper.rb",
|
71
73
|
"spec/thwart_spec.rb",
|
72
74
|
"examples/a_complete_example.rb",
|
73
|
-
"examples/example_helper.rb"
|
74
|
-
"examples/testing.rb"
|
75
|
+
"examples/example_helper.rb"
|
75
76
|
]
|
76
77
|
|
77
78
|
if s.respond_to? :specification_version then
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thwart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Harry Brundage
|
@@ -78,6 +78,7 @@ extra_rdoc_files:
|
|
78
78
|
files:
|
79
79
|
- .document
|
80
80
|
- .gitignore
|
81
|
+
- Gemfile
|
81
82
|
- LICENSE
|
82
83
|
- README.rdoc
|
83
84
|
- Rakefile
|
@@ -92,6 +93,7 @@ files:
|
|
92
93
|
- lib/thwart/canable.rb
|
93
94
|
- lib/thwart/dsl.rb
|
94
95
|
- lib/thwart/enforcer.rb
|
96
|
+
- lib/thwart/rails.rb
|
95
97
|
- lib/thwart/resource.rb
|
96
98
|
- lib/thwart/role.rb
|
97
99
|
- lib/thwart/role_builder.rb
|
@@ -109,7 +111,6 @@ files:
|
|
109
111
|
- spec/spec_helper.rb
|
110
112
|
- spec/thwart_spec.rb
|
111
113
|
- thwart.gemspec
|
112
|
-
- examples/testing.rb
|
113
114
|
has_rdoc: true
|
114
115
|
homepage: http://github.com/hornairs/thwart
|
115
116
|
licenses: []
|
@@ -159,4 +160,3 @@ test_files:
|
|
159
160
|
- spec/thwart_spec.rb
|
160
161
|
- examples/a_complete_example.rb
|
161
162
|
- examples/example_helper.rb
|
162
|
-
- examples/testing.rb
|
data/examples/testing.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
#require File.expand_path(File.dirname(__FILE__) + '/example_helper')
|
2
|
-
|
3
|
-
|
4
|
-
class User
|
5
|
-
# include Thwart::Actor
|
6
|
-
# thwart_access do
|
7
|
-
# role_method :role
|
8
|
-
# end
|
9
|
-
attr_accessor :name, :role
|
10
|
-
def initialize(n, r)
|
11
|
-
self.name = n
|
12
|
-
self.role = r
|
13
|
-
end
|
14
|
-
end
|
15
|
-
user = User.new('Bob', :employee)
|
16
|
-
map = user.public_methods.inject({}) do |acc, m|
|
17
|
-
debugger if m.to_s.gsub(/=$/, "") == ""
|
18
|
-
key = m.to_s.gsub(/=$/, "").to_sym
|
19
|
-
acc[key] = m if acc[key].nil? || m != key
|
20
|
-
acc
|
21
|
-
end
|