grant 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ module Grant
2
+ module Status
3
+
4
+ def grant_disabled?
5
+ Thread.current[:grant_disabled] == true
6
+ end
7
+
8
+ def grant_enabled?
9
+ Thread.current[:grant_disabled] == false
10
+ end
11
+
12
+ def disable_grant
13
+ Thread.current[:grant_disabled] = true
14
+ end
15
+
16
+ def enable_grant
17
+ Thread.current[:grant_disabled] = false
18
+ end
19
+
20
+ def without_grant
21
+ previously_disabled = grant_disabled?
22
+
23
+ begin
24
+ disable_grant
25
+ result = yield if block_given?
26
+ ensure
27
+ enable_grant unless previously_disabled
28
+ end
29
+
30
+ result
31
+ end
32
+
33
+ def with_grant
34
+ previously_disabled = grant_disabled?
35
+
36
+ begin
37
+ enable_grant
38
+ result = yield if block_given?
39
+ ensure
40
+ disable_grant if previously_disabled
41
+ end
42
+
43
+ result
44
+ end
45
+
46
+ def do_as(user)
47
+ previous_user = Grant::User.current_user
48
+
49
+ begin
50
+ Grant::User.current_user = user
51
+ result = yield if block_given?
52
+ ensure
53
+ Grant::User.current_user = previous_user
54
+ end
55
+
56
+ result
57
+ end
58
+
59
+ end
60
+ end
@@ -1,16 +1,15 @@
1
1
  module Grant
2
2
  module User
3
+
3
4
  def current_user
4
- Thread.current[@@current_user_symbol]
5
+ Thread.current[:grant_user]
5
6
  end
6
7
 
7
8
  def current_user=(user)
8
- Thread.current[@@current_user_symbol] = user
9
+ Thread.current[:grant_user] = user
9
10
  end
10
-
11
+
11
12
  module_function :current_user, :current_user=
12
13
 
13
- private
14
- @@current_user_symbol = :grant_current_user_symbol
15
14
  end
16
- end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Grant
2
- VERSION = "2.0.0"
3
- end
2
+ VERSION = "2.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'grant/config'
3
+
4
+ describe Grant::Config do
5
+
6
+ describe 'Configuration' do
7
+ it "should parse actions from a config array" do
8
+ config = Grant::Config.new(:create, 'update')
9
+ config.actions.should_not be_nil
10
+ config.actions.should have(2).items
11
+ config.actions.should =~ [:create, :update]
12
+ end
13
+ end
14
+
15
+ describe 'Configuration Validation' do
16
+ it "should raise a Grant::Error if no action is specified" do
17
+ lambda {
18
+ Grant::Config.new
19
+ }.should raise_error(Grant::Error)
20
+ end
21
+
22
+ it "should raise a Grant::Error if an invalid action is specified" do
23
+ lambda {
24
+ Grant::Config.new(:create, :view)
25
+ }.should raise_error(Grant::Error)
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'grant/grantable'
3
+ require 'grant/user'
4
+
5
+ describe Grant::Grantable do
6
+ before(:each) do
7
+ @user = User.create
8
+ Grant::User.current_user = @user
9
+ end
10
+
11
+ it 'should not restrict CRUD operations until the first grant method call' do
12
+ lambda {
13
+ m = Model.create
14
+ m = Model.find(m.id)
15
+ m.update_attributes(:name => 'new')
16
+ m.destroy
17
+ }.should_not raise_error
18
+ end
19
+
20
+ it 'should automatically include Grant::Status after the first grant method call' do
21
+ redefine_model { grant(:create) { true } }
22
+ Model.included_modules.should include(Grant::Status)
23
+ end
24
+
25
+ it 'should setup failing Grant::Grantor objects for create, find, update, and destroy callbacks when initialized' do
26
+ m = Model.create
27
+ Model.initialize_grant
28
+ lambda { Model.create }.should raise_error(Grant::Error)
29
+ lambda { Model.find(m.id) }.should raise_error(Grant::Error)
30
+ lambda { m.update_attributes(:name => 'new') }.should raise_error(Grant::Error)
31
+ lambda { m.destroy }.should raise_error(Grant::Error)
32
+ end
33
+
34
+ it 'should indicate whether Grant has been initialized' do
35
+ redefine_model
36
+ Model.should_not be_grant_initialized
37
+ Model.initialize_grant
38
+ Model.should be_grant_initialized
39
+ end
40
+
41
+ it 'should associate callbacks with active record create, find, update, and destroy callbacks' do
42
+ redefine_model do
43
+ grant(:create) { true }
44
+ grant(:find) { true }
45
+ grant(:update) { false }
46
+ grant(:destroy) { false }
47
+ end
48
+
49
+ m = Model.create
50
+ m = Model.find(m.id)
51
+ lambda { m.update_attributes(:name => 'new')}.should raise_error(Grant::Error)
52
+ lambda { m.destroy }.should raise_error(Grant::Error)
53
+ end
54
+
55
+ it 'should allow multiple actions to be specified in a grant statement' do
56
+ redefine_model
57
+ m = Model.create
58
+ redefine_model do
59
+ grant(:create, :find) { false }
60
+ grant(:update, :destroy) { true }
61
+ end
62
+
63
+ lambda { Model.find(m.id) }.should raise_error(Grant::Error)
64
+ lambda { Model.create }.should raise_error(Grant::Error)
65
+ m.update_attributes(:name => 'new')
66
+ m.destroy
67
+ end
68
+
69
+ it 'should allow callbacks to be redefined with subsequent grant statements' do
70
+ redefine_model do
71
+ grant(:create) { true }
72
+ grant(:create) { false }
73
+ end
74
+
75
+ lambda { Model.create }.should raise_error(Grant::Error)
76
+ end
77
+
78
+ it 'should provide callbacks with the user and model being protected' do
79
+ redefine_model do
80
+ grant(:create) do |user, model|
81
+ user.should == Grant::User.current_user
82
+ model.should_not == nil
83
+ true
84
+ end
85
+ end
86
+
87
+ Model.create
88
+ end
89
+
90
+ def redefine_model(&blk)
91
+ clazz = Class.new(ActiveRecord::Base, &blk)
92
+ Object.send :remove_const, 'Model'
93
+ Object.send :const_set, 'Model', clazz
94
+ end
95
+
96
+ class User < ActiveRecord::Base; end
97
+ class Model < ActiveRecord::Base; end
98
+
99
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'ostruct'
3
+ require 'grant/grantor'
4
+
5
+ describe Grant::Grantor do
6
+
7
+ describe '#initialize' do
8
+ it 'should define a before_create callback method when passed create as an argument' do
9
+ Grant::Grantor.new(:create).should respond_to(:before_create)
10
+ end
11
+ it 'should define an after_find callback method when passed find as an argument' do
12
+ Grant::Grantor.new(:find).should respond_to(:after_find)
13
+ end
14
+ it 'should define a before_update callback method when passed update as an argument' do
15
+ Grant::Grantor.new(:update).should respond_to(:before_update)
16
+ end
17
+ it 'should define a before_destroy callback method when passed destroy as an argument' do
18
+ Grant::Grantor.new(:destroy).should respond_to(:before_destroy)
19
+ end
20
+ end
21
+
22
+ describe '#error' do
23
+ it 'should raise a nicely formatted error detailing the user and model objects' do
24
+ user = OpenStruct.new(:id => 1)
25
+ model = OpenStruct.new(:id => 2)
26
+ action = :create
27
+
28
+ begin
29
+ Grant::Grantor.new(:create).error(user, action, model)
30
+ rescue => ex
31
+ ex.message.should include("#{user.class.name}:#{user.id}")
32
+ ex.message.should include("#{model.class.name}:#{model.id}")
33
+ ex.message.should include(action.to_s)
34
+ else
35
+ fail "should have received an exception"
36
+ end
37
+ end
38
+ end
39
+
40
+ end
@@ -1,45 +1,10 @@
1
1
  require 'rspec'
2
+ require 'grant'
2
3
 
3
4
  # Requires supporting files with custom matchers and macros, etc,
4
5
  # in ./support/ and its subdirectories.
5
6
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
6
7
 
7
8
  RSpec.configure do |config|
8
- # If you're not using ActiveRecord you should remove these
9
- # lines, delete config/database.yml and disable :active_record
10
- # in your config/boot.rb
11
- # config.use_transactional_fixtures = true
12
- # config.use_instantiated_fixtures = false
13
-
14
- # == Fixtures
15
- #
16
- # You can declare fixtures for each example_group like this:
17
- # describe "...." do
18
- # fixtures :table_a, :table_b
19
- #
20
- # Alternatively, if you prefer to declare them only once, you can
21
- # do so right here. Just uncomment the next line and replace the fixture
22
- # names with your fixtures.
23
- #
24
- # config.global_fixtures = :all
25
- #
26
- # If you declare global fixtures, be aware that they will be declared
27
- # for all of your examples, even those that don't use them.
28
- #
29
- # You can also declare which fixtures to use (for example fixtures for test/fixtures):
30
- #
31
- # config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
32
- #
33
- # == Mock Framework
34
- #
35
- # RSpec uses it's own mocking framework by default. If you prefer to
36
- # use mocha, flexmock or RR, uncomment the appropriate line:
37
- #
38
- # config.mock_with :mocha
39
- # config.mock_with :flexmock
40
- # config.mock_with :rr
41
- #
42
- # == Notes
43
- #
44
- # For more information take a look at Spec::Runner::Configuration and Spec::Runner
9
+ config.include TransactionalSpecs
45
10
  end
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'grant/status'
3
+
4
+ describe Grant::Status do
5
+ it "should be enabled if set to enabled" do
6
+ obj = Class.new { include Grant::Status }.new
7
+ obj.enable_grant
8
+ obj.should be_grant_enabled
9
+ obj.should_not be_grant_disabled
10
+ end
11
+
12
+ it "should be disabled if set to disabled" do
13
+ obj = Class.new { include Grant::Status }.new
14
+ obj.disable_grant
15
+ obj.should_not be_grant_enabled
16
+ obj.should be_grant_disabled
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ require 'active_support/core_ext'
2
+ require 'active_record'
3
+
4
+ tmpdir = File.join(File.dirname(__FILE__), '..', '..', 'tmp')
5
+ FileUtils.mkdir(tmpdir) unless File.exist?(tmpdir)
6
+ test_db = File.join(tmpdir, 'test.db')
7
+
8
+ connection_spec = {
9
+ :adapter => 'sqlite3',
10
+ :database => test_db
11
+ }
12
+
13
+ # Delete any existing instance of the test database
14
+ FileUtils.rm test_db, :force => true
15
+
16
+ # Create a new test database
17
+ ActiveRecord::Base.establish_connection(connection_spec)
18
+
19
+ # ActiveRecord::Base.connection.initialize_schema_migrations_table
20
+
21
+ class CreateUser < ActiveRecord::Migration
22
+ def self.up
23
+ create_table :users, :force => true do |t|
24
+ t.column :username, :string
25
+ end
26
+ end
27
+
28
+ def self.down
29
+ drop_table :users
30
+ end
31
+ end
32
+
33
+ class CreateModel < ActiveRecord::Migration
34
+ def self.up
35
+ create_table :models, :force => true do |t|
36
+ t.column :name, :string
37
+ t.column :value, :string
38
+ end
39
+ end
40
+
41
+ def self.down
42
+ drop_table :models
43
+ end
44
+ end
45
+
46
+ CreateUser.up
47
+ CreateModel.up
48
+
@@ -0,0 +1,17 @@
1
+ module TransactionalSpecs
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ around(:each) do |spec|
6
+ ActiveRecord::Base.transaction do
7
+ begin
8
+ spec.call
9
+ ensure
10
+ raise ActiveRecord::Rollback
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -7,19 +7,19 @@ describe Grant::User do
7
7
  Grant::User.current_user = user
8
8
  Grant::User.current_user.should == user
9
9
  end
10
-
10
+
11
11
  it "should not return the same user from a different thread" do
12
12
  user = "user"
13
13
  user2 = "user2"
14
-
14
+
15
15
  Grant::User.current_user = user
16
-
16
+
17
17
  Thread.new do
18
18
  Grant::User.current_user.should be_nil
19
19
  Grant::User.current_user = user2
20
20
  Grant::User.current_user.should == user2
21
21
  end
22
-
22
+
23
23
  Grant::User.current_user.should == user
24
24
  end
25
- end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grant
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
4
+ hash: 13
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 0
10
- version: 2.0.0
9
+ - 1
10
+ version: 2.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Kunkle
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-05 00:00:00 -05:00
19
+ date: 2011-03-21 00:00:00 -04:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -25,14 +25,48 @@ dependencies:
25
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
26
  none: false
27
27
  requirements:
28
- - - ">="
28
+ - - "="
29
29
  - !ruby/object:Gem::Version
30
- hash: 3
30
+ hash: 27
31
31
  segments:
32
+ - 2
33
+ - 5
32
34
  - 0
33
- version: "0"
35
+ version: 2.5.0
34
36
  type: :development
35
37
  version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3-ruby
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ hash: 29
47
+ segments:
48
+ - 1
49
+ - 3
50
+ - 3
51
+ version: 1.3.3
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: activerecord
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">"
61
+ - !ruby/object:Gem::Version
62
+ hash: 7
63
+ segments:
64
+ - 3
65
+ - 0
66
+ - 0
67
+ version: 3.0.0
68
+ type: :development
69
+ version_requirements: *id003
36
70
  description: Grant is a Ruby gem and Rails plugin that forces you to make explicit security decisions about the operations performed on your ActiveRecord models.
37
71
  email:
38
72
  executables: []
@@ -42,23 +76,32 @@ extensions: []
42
76
  extra_rdoc_files: []
43
77
 
44
78
  files:
45
- - lib/grant/config_parser.rb
79
+ - .gitignore
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE
84
+ - README.rdoc
85
+ - Rakefile
86
+ - grant.gemspec
87
+ - init.rb
88
+ - lib/grant.rb
89
+ - lib/grant/config.rb
90
+ - lib/grant/grantable.rb
91
+ - lib/grant/grantor.rb
46
92
  - lib/grant/integration.rb
47
93
  - lib/grant/model_security.rb
48
94
  - lib/grant/spec_helpers.rb
49
- - lib/grant/thread_local.rb
50
- - lib/grant/thread_status.rb
95
+ - lib/grant/status.rb
51
96
  - lib/grant/user.rb
52
97
  - lib/grant/version.rb
53
- - lib/grant.rb
54
- - LICENSE
55
- - README.rdoc
56
- - spec/config_parser_spec.rb
57
- - spec/integration_spec.rb
58
- - spec/model_security_spec.rb
98
+ - spec/config_spec.rb
99
+ - spec/grantable_spec.rb
100
+ - spec/grantor_spec.rb
59
101
  - spec/spec_helper.rb
60
- - spec/thread_local_spec.rb
61
- - spec/thread_status_spec.rb
102
+ - spec/status_spec.rb
103
+ - spec/support/db_setup.rb
104
+ - spec/support/transactional_specs.rb
62
105
  - spec/user_spec.rb
63
106
  has_rdoc: true
64
107
  homepage: http://github.com/nearinfinity/grant
@@ -83,24 +126,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
126
  requirements:
84
127
  - - ">="
85
128
  - !ruby/object:Gem::Version
86
- hash: 23
129
+ hash: 3
87
130
  segments:
88
- - 1
89
- - 3
90
- - 6
91
- version: 1.3.6
131
+ - 0
132
+ version: "0"
92
133
  requirements: []
93
134
 
94
135
  rubyforge_project:
95
- rubygems_version: 1.3.7
136
+ rubygems_version: 1.4.2
96
137
  signing_key:
97
138
  specification_version: 3
98
139
  summary: Conscious security constraints for your ActiveRecord model objects
99
140
  test_files:
100
- - spec/config_parser_spec.rb
101
- - spec/integration_spec.rb
102
- - spec/model_security_spec.rb
141
+ - spec/config_spec.rb
142
+ - spec/grantable_spec.rb
143
+ - spec/grantor_spec.rb
103
144
  - spec/spec_helper.rb
104
- - spec/thread_local_spec.rb
105
- - spec/thread_status_spec.rb
145
+ - spec/status_spec.rb
146
+ - spec/support/db_setup.rb
147
+ - spec/support/transactional_specs.rb
106
148
  - spec/user_spec.rb