active_restrictors 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/Gemfile.lock +1 -7
- data/README.rdoc +19 -19
- data/gemfiles/activerecord3_0.gemfile +10 -0
- data/gemfiles/activerecord3_1.gemfile +10 -0
- data/gemfiles/activerecord3_1.gemfile.lock +44 -0
- data/gemfiles/activerecord3_2.gemfile +10 -0
- data/lib/active_restrictors/active_restrictor.rb +14 -12
- data/lib/active_restrictors/version.rb +1 -1
- data/spec/model_definitions.rb +1 -3
- data/spec/setup.rb +4 -5
- metadata +8 -4
data/CHANGELOG.rdoc
CHANGED
data/Gemfile.lock
CHANGED
@@ -10,24 +10,18 @@ GEM
|
|
10
10
|
activesupport (= 3.1.3)
|
11
11
|
arel (~> 2.2.1)
|
12
12
|
tzinfo (~> 0.3.29)
|
13
|
-
activerecord-jdbc-adapter (1.2.1)
|
14
|
-
activerecord-jdbcsqlite3-adapter (1.2.1)
|
15
|
-
activerecord-jdbc-adapter (~> 1.2.1)
|
16
|
-
jdbc-sqlite3 (~> 3.7.2)
|
17
13
|
activesupport (3.1.3)
|
18
14
|
multi_json (~> 1.0)
|
19
15
|
arel (2.2.1)
|
20
16
|
builder (3.0.0)
|
21
17
|
i18n (0.6.0)
|
22
|
-
|
23
|
-
minitest (2.10.0)
|
18
|
+
minitest (2.10.1)
|
24
19
|
multi_json (1.0.4)
|
25
20
|
rake (0.9.2.2)
|
26
21
|
sqlite3 (1.3.5)
|
27
22
|
tzinfo (0.3.31)
|
28
23
|
|
29
24
|
PLATFORMS
|
30
|
-
java
|
31
25
|
ruby
|
32
26
|
|
33
27
|
DEPENDENCIES
|
data/README.rdoc
CHANGED
@@ -9,26 +9,26 @@ Chainable ActiveRecord restrictions.
|
|
9
9
|
Restrictions are made via join tables between two models and a User object. Imagine these models:
|
10
10
|
|
11
11
|
+-------+ +---------------+ +------------+ +--------------+ +------+
|
12
|
-
|
|
12
|
+
| Asset |<*-----|AssetPermission|<-----| Permission |--->|UserPermission|-----*>| User |
|
13
13
|
+-------+ +---------------+ +------------+ +--------------+ +------+
|
14
14
|
|
15
15
|
Our model definitions would look something like:
|
16
16
|
|
17
17
|
class Permission < ActiveRecord::Base
|
18
|
-
has_many :
|
19
|
-
has_many :
|
18
|
+
has_many :asset_permissions, :dependent => :destroy
|
19
|
+
has_many :assets, :through => :asset_permissions
|
20
20
|
has_many :user_permissions, :dependent => :destroy
|
21
21
|
has_many :users, :through => :user_permissions
|
22
22
|
end
|
23
23
|
|
24
|
-
class
|
25
|
-
belongs_to :
|
24
|
+
class AssetPermission < ActiveRecord::Base
|
25
|
+
belongs_to :asset
|
26
26
|
belongs_to :permission
|
27
27
|
end
|
28
28
|
|
29
|
-
class
|
30
|
-
has_many :
|
31
|
-
has_many :permissions, :through => :
|
29
|
+
class Asset < ActiveRecord::Base
|
30
|
+
has_many :asset_permissions, :dependent => :destroy
|
31
|
+
has_many :permissions, :through => :asset_permissions
|
32
32
|
end
|
33
33
|
|
34
34
|
class UserPermission < ActiveRecord::Base
|
@@ -41,14 +41,14 @@ Our model definitions would look something like:
|
|
41
41
|
has_many :users, :through => :user_permissions
|
42
42
|
end
|
43
43
|
|
44
|
-
Now, suppose a User should only be allowed to to see
|
44
|
+
Now, suppose a User should only be allowed to to see an Asset instance if the Asset instance and the User both have the same permission assigned to them. We modify Asset like so:
|
45
45
|
|
46
|
-
class
|
46
|
+
class Asset < ActiveRecord::Base
|
47
47
|
...
|
48
48
|
include ActiveRestrictor
|
49
49
|
|
50
50
|
add_restrictor(:permissions,
|
51
|
-
:enabled => lambda{ User.current_user.
|
51
|
+
:enabled => lambda{ User.current_user.assets_enabled? },
|
52
52
|
:views => {
|
53
53
|
:value => :name,
|
54
54
|
:multiple => true,
|
@@ -63,16 +63,16 @@ A quick overview of what these options are doing.
|
|
63
63
|
* :enabled -> Restrictor is applied/not applied. This can be a static value or it can be a callable block to allow dynamic enabling
|
64
64
|
* :value -> This is the attribute on the Permission model that is displayed to the user
|
65
65
|
* :multiple -> Allows multiple Permissions to be applied on the restriction
|
66
|
-
* :default_view_all -> If
|
66
|
+
* :default_view_all -> If Asset has no Permissions applied, it is viewable by all
|
67
67
|
* :user_values_only -> Only Permissions assigned to the user will be viewable in edit mode
|
68
68
|
|
69
69
|
With the inclusion of the restrictor, we now have two new methods available. The first is on User instances:
|
70
70
|
|
71
|
-
User.first.
|
71
|
+
User.first.allowed_assets -> Returns scoping of Assets the given user instance has access to
|
72
72
|
|
73
|
-
The second is on
|
73
|
+
The second is on Asset instances:
|
74
74
|
|
75
|
-
|
75
|
+
Asset.first.allowed_users -> Returns scoping of the Users allowed to acces this instance
|
76
76
|
|
77
77
|
== View Helpers
|
78
78
|
|
@@ -81,20 +81,20 @@ The second is on Fubar instances:
|
|
81
81
|
%table
|
82
82
|
%tr
|
83
83
|
%td= 'Name'
|
84
|
-
%td= @
|
85
|
-
- display_full_restictors(@
|
84
|
+
%td= @asset.name
|
85
|
+
- display_full_restictors(@asset).each do |pair|
|
86
86
|
%tr
|
87
87
|
%td= "#{pair.first}:"
|
88
88
|
%td= pair.last
|
89
89
|
|
90
90
|
=== Edit
|
91
91
|
|
92
|
-
- form_for(@
|
92
|
+
- form_for(@asset) do |f|
|
93
93
|
%table
|
94
94
|
%tr
|
95
95
|
%td= 'Name:'
|
96
96
|
%td= f.text_field :name
|
97
|
-
- edit_full_restrictors(@
|
97
|
+
- edit_full_restrictors(@asset, f).each do |pair|
|
98
98
|
%tr
|
99
99
|
%td= "#{pair.first}:"
|
100
100
|
%td= pair.last
|
@@ -0,0 +1,10 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem 'activerecord', '~> 3.0.0'
|
4
|
+
gem 'rake'
|
5
|
+
gem 'minitest'
|
6
|
+
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
7
|
+
gem "jdbc-sqlite3", :platform => :jruby
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
|
9
|
+
|
10
|
+
gemspec :path => '../'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem 'activerecord', '~> 3.1.0'
|
4
|
+
gem 'rake'
|
5
|
+
gem 'minitest'
|
6
|
+
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
7
|
+
gem "jdbc-sqlite3", :platform => :jruby
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
|
9
|
+
|
10
|
+
gemspec :path => '../'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /home/croberts/Projects/active_restrictors
|
3
|
+
specs:
|
4
|
+
active_restrictors (0.2.0)
|
5
|
+
activerecord (~> 3.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.1.3)
|
11
|
+
activesupport (= 3.1.3)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activerecord (3.1.3)
|
15
|
+
activemodel (= 3.1.3)
|
16
|
+
activesupport (= 3.1.3)
|
17
|
+
arel (~> 2.2.1)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activerecord-jdbc-adapter (1.2.1)
|
20
|
+
activerecord-jdbcsqlite3-adapter (1.2.1)
|
21
|
+
activerecord-jdbc-adapter (~> 1.2.1)
|
22
|
+
jdbc-sqlite3 (~> 3.7.2)
|
23
|
+
activesupport (3.1.3)
|
24
|
+
multi_json (~> 1.0)
|
25
|
+
arel (2.2.1)
|
26
|
+
builder (3.0.0)
|
27
|
+
i18n (0.6.0)
|
28
|
+
jdbc-sqlite3 (3.7.2)
|
29
|
+
minitest (2.11.0)
|
30
|
+
multi_json (1.0.4)
|
31
|
+
rake (0.9.2.2)
|
32
|
+
tzinfo (0.3.31)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
java
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
active_restrictors!
|
39
|
+
activerecord (~> 3.1.0)
|
40
|
+
activerecord-jdbcsqlite3-adapter
|
41
|
+
jdbc-sqlite3
|
42
|
+
minitest
|
43
|
+
rake
|
44
|
+
sqlite3
|
@@ -0,0 +1,10 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem 'activerecord', '~> 3.2.0'
|
4
|
+
gem 'rake'
|
5
|
+
gem 'minitest'
|
6
|
+
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
|
7
|
+
gem "jdbc-sqlite3", :platform => :jruby
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
|
9
|
+
|
10
|
+
gemspec :path => '../'
|
@@ -23,20 +23,22 @@ module ActiveRestrictor
|
|
23
23
|
# Implicit type is run directly against the source model. (ex: Fubar.includes(:feebar).where(:feebars => {:user_id => User.current_user.id}))
|
24
24
|
# Full is a full restrictor using join tables and provides view helpers for management
|
25
25
|
def add_restrictor(name, opts={})
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
if(new_opts[:
|
34
|
-
new_opts[:
|
35
|
-
|
36
|
-
|
26
|
+
if(table_exists?)
|
27
|
+
self.restrictors ||= []
|
28
|
+
new_opts = {:name => name, :enabled => true, :type => :full, :scope => self.scoped, :default_allowed_all => false}.merge(opts)
|
29
|
+
new_opts[:views] ||= {}
|
30
|
+
new_opts = map_deprecated_hash(new_opts)
|
31
|
+
new_opts[:views][:id] ||= :id
|
32
|
+
new_opts[:class] = restrictor_class(new_opts)
|
33
|
+
if(new_opts[:type] == :full && new_opts[:views][:value].blank?)
|
34
|
+
if(new_opts[:class].column_names.include?('name'))
|
35
|
+
new_opts[:views][:value] = :name
|
36
|
+
else
|
37
|
+
raise 'Value must be defined for association to generate views'
|
38
|
+
end
|
37
39
|
end
|
40
|
+
self.restrictors.push(new_opts)
|
38
41
|
end
|
39
|
-
self.restrictors.push(new_opts)
|
40
42
|
end
|
41
43
|
|
42
44
|
# TODO: Add in proper mapping plus scope building
|
data/spec/model_definitions.rb
CHANGED
data/spec/setup.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
2
1
|
require 'rubygems'
|
3
2
|
require 'bundler/setup'
|
4
|
-
|
5
|
-
if(RUBY_PLATFORM == 'java')
|
6
|
-
require 'jdbc/sqlite3'
|
7
|
-
else
|
3
|
+
unless(RUBY_PLATFORM == 'java')
|
8
4
|
require 'sqlite3'
|
9
5
|
end
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/migration'
|
8
|
+
require 'benchmark'
|
10
9
|
require 'active_restrictors'
|
11
10
|
require 'minitest/autorun'
|
12
11
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_restrictors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Roberts
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-27 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -43,6 +43,10 @@ extra_rdoc_files:
|
|
43
43
|
- README.rdoc
|
44
44
|
- CHANGELOG.rdoc
|
45
45
|
files:
|
46
|
+
- gemfiles/activerecord3_2.gemfile
|
47
|
+
- gemfiles/activerecord3_1.gemfile.lock
|
48
|
+
- gemfiles/activerecord3_0.gemfile
|
49
|
+
- gemfiles/activerecord3_1.gemfile
|
46
50
|
- Gemfile.lock
|
47
51
|
- spec/restrictor_spec.rb
|
48
52
|
- spec/setup.rb
|