grant 2.0.0 → 2.0.1
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 +5 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +43 -0
- data/README.rdoc +15 -8
- data/Rakefile +33 -0
- data/grant.gemspec +23 -0
- data/init.rb +1 -0
- data/lib/grant.rb +21 -3
- data/lib/grant/config.rb +22 -0
- data/lib/grant/grantable.rb +41 -0
- data/lib/grant/grantor.rb +23 -0
- data/lib/grant/integration.rb +4 -46
- data/lib/grant/model_security.rb +4 -50
- data/lib/grant/spec_helpers.rb +5 -5
- data/lib/grant/status.rb +60 -0
- data/lib/grant/user.rb +5 -6
- data/lib/grant/version.rb +2 -2
- data/spec/config_spec.rb +29 -0
- data/spec/grantable_spec.rb +99 -0
- data/spec/grantor_spec.rb +40 -0
- data/spec/spec_helper.rb +2 -37
- data/spec/status_spec.rb +18 -0
- data/spec/support/db_setup.rb +48 -0
- data/spec/support/transactional_specs.rb +17 -0
- data/spec/user_spec.rb +5 -5
- metadata +72 -30
- data/lib/grant/config_parser.rb +0 -22
- data/lib/grant/thread_local.rb +0 -18
- data/lib/grant/thread_status.rb +0 -34
- data/spec/config_parser_spec.rb +0 -34
- data/spec/integration_spec.rb +0 -66
- data/spec/model_security_spec.rb +0 -88
- data/spec/thread_local_spec.rb +0 -14
- data/spec/thread_status_spec.rb +0 -16
data/lib/grant/config_parser.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module Grant
|
2
|
-
class ConfigParser
|
3
|
-
|
4
|
-
def self.extract_config(args)
|
5
|
-
normalize_config args
|
6
|
-
validate_config args
|
7
|
-
args
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
|
12
|
-
def self.normalize_config(actions)
|
13
|
-
actions.each_with_index { |item, index| actions[index] = item.to_sym unless item.kind_of? Symbol }
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.validate_config(actions)
|
17
|
-
raise Grant::Error.new("at least one :create, :find, :update, or :destroy action must be specified") if actions.empty?
|
18
|
-
raise Grant::Error.new(":create, :find, :update, and :destroy are the only valid actions") unless actions.all? { |a| [:create, :find, :update, :destroy].include? a }
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
data/lib/grant/thread_local.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Grant
|
2
|
-
class ThreadLocal
|
3
|
-
|
4
|
-
def initialize(initial_value)
|
5
|
-
@thread_symbol = "#{rand}#{Time.now.to_f}"
|
6
|
-
set initial_value
|
7
|
-
end
|
8
|
-
|
9
|
-
def set(value)
|
10
|
-
Thread.current[@thread_symbol] = value
|
11
|
-
end
|
12
|
-
|
13
|
-
def get
|
14
|
-
Thread.current[@thread_symbol]
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
data/lib/grant/thread_status.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'grant/thread_local'
|
2
|
-
|
3
|
-
module Grant
|
4
|
-
module ThreadStatus
|
5
|
-
|
6
|
-
def self.enabled?
|
7
|
-
status
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.disabled?
|
11
|
-
!status
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.enable
|
15
|
-
set_status true
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.disable
|
19
|
-
set_status false
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
def self.status
|
24
|
-
@status = Grant::ThreadLocal.new(true) if @status.nil?
|
25
|
-
@status.get
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.set_status(status)
|
29
|
-
@status = Grant::ThreadLocal.new(true) if @status.nil?
|
30
|
-
@status.set status
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
data/spec/config_parser_spec.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'grant'
|
3
|
-
|
4
|
-
describe Grant::ConfigParser do
|
5
|
-
|
6
|
-
describe 'Configuration' do
|
7
|
-
it "should parse actions from a config array" do
|
8
|
-
config = Grant::ConfigParser.extract_config([:create, 'update'])
|
9
|
-
config.should_not be_nil
|
10
|
-
config.should =~ [:create, :update]
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should parse actions" do
|
14
|
-
config = Grant::ConfigParser.extract_config([:create])
|
15
|
-
config.should_not be_nil
|
16
|
-
config.should =~ [:create]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe 'Configuration Validation' do
|
21
|
-
it "should raise a Grant::Error if no action is specified" do
|
22
|
-
lambda {
|
23
|
-
Grant::ConfigParser.instance_eval { validate_config([]) }
|
24
|
-
}.should raise_error(Grant::Error)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should raise a Grant::Error if an invalid action is specified" do
|
28
|
-
lambda {
|
29
|
-
Grant::ConfigParser.instance_eval { validate_config([:create, :udate]) }
|
30
|
-
}.should raise_error(Grant::Error)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
data/spec/integration_spec.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'grant'
|
3
|
-
|
4
|
-
describe Grant::Integration do
|
5
|
-
include Grant::Integration
|
6
|
-
|
7
|
-
it "should have the ability to disable Grant" do
|
8
|
-
disable_grant
|
9
|
-
grant_disabled?.should be_true
|
10
|
-
Grant::ThreadStatus.should be_disabled
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should have the ability to enable Grant" do
|
14
|
-
disable_grant
|
15
|
-
enable_grant
|
16
|
-
grant_enabled?.should be_true
|
17
|
-
Grant::ThreadStatus.should be_enabled
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should have the ability to check if grant is enabled" do
|
21
|
-
enable_grant
|
22
|
-
grant_enabled?.should be_true
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should have the ability to check if grant is disabled" do
|
26
|
-
disable_grant
|
27
|
-
grant_disabled?.should be_true
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should be able to execute a block of code with grant temporarily disabled but switched back to enabled afterwards" do
|
31
|
-
enable_grant
|
32
|
-
without_grant do
|
33
|
-
grant_disabled?.should be_true
|
34
|
-
Grant::ThreadStatus.should be_disabled
|
35
|
-
end
|
36
|
-
grant_enabled?.should be_true
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should be able to execute a block of code with grant disabled and remain disabled afterwards if it was beforehand" do
|
40
|
-
disable_grant
|
41
|
-
without_grant do
|
42
|
-
grant_disabled?.should be_true
|
43
|
-
Grant::ThreadStatus.should be_disabled
|
44
|
-
end
|
45
|
-
grant_disabled?.should be_true
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should be able to execute a block of code with grant temporarily enabled but switched back to disabled afterwards" do
|
49
|
-
disable_grant
|
50
|
-
with_grant do
|
51
|
-
grant_enabled?.should be_true
|
52
|
-
Grant::ThreadStatus.should be_enabled
|
53
|
-
end
|
54
|
-
grant_disabled?.should be_true
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should be able to execute a block of code with grant enabled and remain enabled afterwards if it was beforehand" do
|
58
|
-
enable_grant
|
59
|
-
with_grant do
|
60
|
-
grant_enabled?.should be_true
|
61
|
-
Grant::ThreadStatus.should be_enabled
|
62
|
-
end
|
63
|
-
grant_enabled?.should be_true
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
data/spec/model_security_spec.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'grant'
|
3
|
-
|
4
|
-
describe Grant::ModelSecurity do
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
Grant::User.current_user = (Class.new do
|
8
|
-
def id; 1 end
|
9
|
-
end).new
|
10
|
-
end
|
11
|
-
|
12
|
-
describe 'module include' do
|
13
|
-
it 'should establish failing ActiveRecord callbacks for before_create, before_update, before_destroy, and after_find when included' do
|
14
|
-
verify_standard_callbacks(new_model_class.new)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#grant' do
|
19
|
-
it 'should allow after_find callback to succeed when granted' do
|
20
|
-
c = new_model_class.instance_eval { grant(:find) { true }; self }
|
21
|
-
verify_standard_callbacks(c.new, :find)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should allow before_create callback to succeed when granted' do
|
25
|
-
c = new_model_class.instance_eval { grant(:create) { true }; self }
|
26
|
-
verify_standard_callbacks(c.new, :create)
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'should allow before_update callback to succeed when granted' do
|
30
|
-
c = new_model_class.instance_eval { grant(:update) { true }; self }
|
31
|
-
verify_standard_callbacks(c.new, :update)
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should allow before_destroy callback to succeed when granted' do
|
35
|
-
c = new_model_class.instance_eval { grant(:destroy) { true }; self }
|
36
|
-
verify_standard_callbacks(c.new, :destroy)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should allow multiple callbacks to be specified with one grant statment' do
|
40
|
-
c = new_model_class.instance_eval { grant(:create, :update) { true }; self }
|
41
|
-
verify_standard_callbacks(c.new, :create, :update)
|
42
|
-
|
43
|
-
c = new_model_class.instance_eval { grant(:create, :update, :destroy) { true }; self }
|
44
|
-
verify_standard_callbacks(c.new, :create, :update, :destroy)
|
45
|
-
|
46
|
-
c = new_model_class.instance_eval { grant(:create, :update, :destroy, :find) { true }; self }
|
47
|
-
verify_standard_callbacks(c.new, :create, :update, :destroy, :find)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def verify_standard_callbacks(instance, *succeeding_callbacks)
|
52
|
-
verify_callbacks([:create, :update, :destroy, :find], instance, nil, succeeding_callbacks)
|
53
|
-
end
|
54
|
-
|
55
|
-
def verify_callbacks(all_actions, instance, associated_model, succeeding_callbacks)
|
56
|
-
all_actions.each do |action|
|
57
|
-
expectation = succeeding_callbacks.include?(action) ? :should_not : :should
|
58
|
-
lambda { instance.send(action, associated_model) }.send(expectation, raise_error(Grant::Error))
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def new_model_class
|
63
|
-
Class.new(ActiveRecordMock) do
|
64
|
-
include Grant::ModelSecurity
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
class ActiveRecordMock
|
69
|
-
def id; 1 end
|
70
|
-
|
71
|
-
def self.before_create(method)
|
72
|
-
define_method(:create) { send method }
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.before_update(method)
|
76
|
-
define_method(:update) { send method }
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.before_destroy(method)
|
80
|
-
define_method(:destroy) { send method }
|
81
|
-
end
|
82
|
-
|
83
|
-
def self.after_find(method)
|
84
|
-
define_method(:find) { send method }
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
data/spec/thread_local_spec.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'grant/thread_local'
|
3
|
-
|
4
|
-
describe Grant::ThreadLocal do
|
5
|
-
it "should properly set and get thread-local variables" do
|
6
|
-
val = "val"
|
7
|
-
tl = Grant::ThreadLocal.new(val)
|
8
|
-
tl.get.should == val
|
9
|
-
|
10
|
-
val2 = "val2"
|
11
|
-
tl.set val2
|
12
|
-
tl.get.should == val2
|
13
|
-
end
|
14
|
-
end
|
data/spec/thread_status_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'grant'
|
3
|
-
|
4
|
-
describe Grant::ThreadStatus do
|
5
|
-
it "should be enabled if set to enabled" do
|
6
|
-
Grant::ThreadStatus.enable
|
7
|
-
Grant::ThreadStatus.should be_enabled
|
8
|
-
Grant::ThreadStatus.should_not be_disabled
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should be disabled if set to disabled" do
|
12
|
-
Grant::ThreadStatus.disable
|
13
|
-
Grant::ThreadStatus.should_not be_enabled
|
14
|
-
Grant::ThreadStatus.should be_disabled
|
15
|
-
end
|
16
|
-
end
|