rolify 3.2.0 → 3.3.0.rc1
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/.travis.yml +2 -0
- data/README.md +9 -5
- data/lib/generators/rolify/role/role_generator.rb +4 -1
- data/lib/generators/rolify/role/templates/README +8 -6
- data/lib/generators/rolify/role/templates/README-active_record +2 -15
- data/lib/generators/rolify/role/templates/README-mongoid +2 -15
- data/lib/generators/rolify/role/templates/role-mongoid.rb +0 -2
- data/lib/rolify.rb +13 -10
- data/lib/rolify/adapters/active_record/resource_adapter.rb +1 -1
- data/lib/rolify/adapters/active_record/role_adapter.rb +5 -4
- data/lib/rolify/adapters/base.rb +1 -1
- data/lib/rolify/adapters/mongoid/resource_adapter.rb +2 -2
- data/lib/rolify/adapters/mongoid/role_adapter.rb +24 -14
- data/lib/rolify/finders.rb +17 -16
- data/lib/rolify/matchers.rb +13 -0
- data/lib/rolify/resource.rb +1 -1
- data/lib/rolify/version.rb +1 -1
- data/spec/generators/rolify/role/role_generator_spec.rb +0 -1
- data/spec/rolify/custom_spec.rb +1 -1
- data/spec/rolify/matchers_spec.rb +24 -0
- data/spec/rolify/namespace_spec.rb +27 -0
- data/spec/rolify/resource_spec.rb +36 -29
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +20 -0
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +2 -2
- data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +59 -59
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +2 -2
- data/spec/spec_helper.rb +2 -1
- data/spec/support/adapters/active_record.rb +40 -9
- data/spec/support/adapters/mongoid.rb +77 -20
- data/spec/support/data.rb +12 -15
- data/spec/support/schema.rb +24 -18
- metadata +83 -26
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Resourcify and rolify on the same model" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
reset_defaults
|
7
|
+
Role.delete_all
|
8
|
+
HumanResource.delete_all
|
9
|
+
end
|
10
|
+
|
11
|
+
let!(:user) { user = HumanResource.new login: 'Samer'; user.save; user }
|
12
|
+
|
13
|
+
it "should add the role to the user" do
|
14
|
+
expect { user.add_role :admin }.to change { user.roles.count }.by(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should create a role to the roles collection" do
|
18
|
+
expect { user.add_role :moderator }.to change { Role.count }.by(1)
|
19
|
+
end
|
20
|
+
end
|
@@ -3,8 +3,8 @@ shared_examples_for Rolify::Dynamic do
|
|
3
3
|
Rolify.dynamic_shortcuts = true
|
4
4
|
role_class.destroy_all
|
5
5
|
user_class.rolify :role_cname => role_class.to_s
|
6
|
-
Forum.resourcify :role_cname => role_class.to_s
|
7
|
-
Group.resourcify :role_cname => role_class.to_s
|
6
|
+
Forum.resourcify :roles, :role_cname => role_class.to_s
|
7
|
+
Group.resourcify :roles, :role_cname => role_class.to_s
|
8
8
|
end
|
9
9
|
|
10
10
|
context "using a global role" do
|
@@ -3,55 +3,55 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
3
3
|
context "removing a global role", :scope => :global do
|
4
4
|
context "being a global role of the user" do
|
5
5
|
it { expect { subject.remove_role("admin".send(param_method)) }.to change { subject.roles.size }.by(-1) }
|
6
|
-
|
7
|
-
it {
|
8
|
-
it {
|
9
|
-
it {
|
10
|
-
it {
|
11
|
-
it {
|
12
|
-
it {
|
6
|
+
|
7
|
+
it { should_not have_role("admin".send(param_method)) }
|
8
|
+
it { should have_role("staff".send(param_method)) }
|
9
|
+
it { should have_role("manager".send(param_method), Group) }
|
10
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
11
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
12
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
context "being a class scoped role to the user" do
|
16
16
|
it { expect { subject.remove_role("manager".send(param_method)) }.to change { subject.roles.size }.by(-1) }
|
17
|
-
|
18
|
-
it {
|
19
|
-
it {
|
20
|
-
it {
|
21
|
-
it {
|
22
|
-
it {
|
23
|
-
it {
|
17
|
+
|
18
|
+
it { should_not have_role("admin".send(param_method)) }
|
19
|
+
it { should have_role("staff".send(param_method)) }
|
20
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
21
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
22
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
23
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
context "being instance scoped roles to the user" do
|
27
27
|
it { expect { subject.remove_role("moderator".send(param_method)) }.to change { subject.roles.size }.by(-2) }
|
28
|
-
|
29
|
-
it {
|
30
|
-
it {
|
31
|
-
it {
|
32
|
-
it {
|
33
|
-
it {
|
34
|
-
it {
|
28
|
+
|
29
|
+
it { should_not have_role("admin".send(param_method)) }
|
30
|
+
it { should have_role("staff".send(param_method)) }
|
31
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
32
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
33
|
+
it { should_not have_role("moderator".send(param_method), Group.last) }
|
34
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
context "not being a role of the user" do
|
38
38
|
it { expect { subject.remove_role("superhero".send(param_method)) }.not_to change { subject.roles.size } }
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
context "used by another user" do
|
42
42
|
before do
|
43
43
|
user = user_class.last
|
44
44
|
user.add_role "staff".send(param_method)
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it { expect { subject.remove_role("staff".send(param_method)) }.not_to change { role_class.count } }
|
48
|
-
|
49
|
-
it {
|
50
|
-
it {
|
51
|
-
it {
|
52
|
-
it {
|
53
|
-
it {
|
54
|
-
it {
|
48
|
+
|
49
|
+
it { should_not have_role("admin".send(param_method)) }
|
50
|
+
it { should_not have_role("staff".send(param_method)) }
|
51
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
52
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
53
|
+
it { should_not have_role("moderator".send(param_method), Group.last) }
|
54
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
55
55
|
end
|
56
56
|
|
57
57
|
context "not used by anyone else" do
|
@@ -67,29 +67,29 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
67
67
|
context "being a global role of the user" do
|
68
68
|
it { expect { subject.remove_role("warrior".send(param_method), Forum) }.not_to change{ subject.roles.size } }
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
context "being a class scoped role to the user" do
|
72
72
|
it { expect { subject.remove_role("manager".send(param_method), Forum) }.to change{ subject.roles.size }.by(-1) }
|
73
|
-
|
74
|
-
it {
|
75
|
-
it {
|
76
|
-
it {
|
77
|
-
it {
|
78
|
-
it {
|
79
|
-
it {
|
73
|
+
|
74
|
+
it { should have_role("warrior") }
|
75
|
+
it { should_not have_role("manager", Forum) }
|
76
|
+
it { should have_role("player", Forum) }
|
77
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
78
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
79
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
context "being instance scoped role to the user" do
|
83
83
|
it { expect { subject.remove_role("moderator".send(param_method), Forum) }.to change { subject.roles.size }.by(-1) }
|
84
|
-
|
85
|
-
it {
|
86
|
-
it {
|
87
|
-
it {
|
88
|
-
it {
|
89
|
-
it {
|
90
|
-
it {
|
84
|
+
|
85
|
+
it { should have_role("warrior") }
|
86
|
+
it { should_not have_role("manager", Forum) }
|
87
|
+
it { should have_role("player", Forum) }
|
88
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
89
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
90
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
context "not being a role of the user" do
|
94
94
|
it { expect { subject.remove_role("manager".send(param_method), Group) }.not_to change { subject.roles.size } }
|
95
95
|
end
|
@@ -99,23 +99,23 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
99
99
|
context "being a global role of the user" do
|
100
100
|
it { expect { subject.remove_role("soldier".send(param_method), Group.first) }.not_to change { subject.roles.size } }
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
context "being a class scoped role to the user" do
|
104
104
|
it { expect { subject.remove_role("visitor".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
context "being instance scoped role to the user" do
|
108
108
|
it { expect { subject.remove_role("moderator".send(param_method), Forum.first) }.to change { subject.roles.size }.by(-1) }
|
109
|
-
|
110
|
-
it {
|
111
|
-
it {
|
112
|
-
it {
|
113
|
-
it {
|
109
|
+
|
110
|
+
it { should have_role("soldier") }
|
111
|
+
it { should have_role("visitor", Forum) }
|
112
|
+
it { should_not have_role("moderator", Forum.first) }
|
113
|
+
it { should have_role("anonymous", Forum.last) }
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
context "not being a role of the user" do
|
117
117
|
it { expect { subject.remove_role("anonymous".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
|
118
118
|
end
|
119
119
|
end
|
120
120
|
end
|
121
|
-
end
|
121
|
+
end
|
@@ -13,8 +13,8 @@ shared_examples_for Rolify::Role do
|
|
13
13
|
Rolify.dynamic_shortcuts = false
|
14
14
|
user_class.rolify :role_cname => role_class.to_s
|
15
15
|
role_class.destroy_all
|
16
|
-
Forum.resourcify :role_cname => role_class.to_s
|
17
|
-
Group.resourcify :role_cname => role_class.to_s
|
16
|
+
Forum.resourcify :roles, :role_cname => role_class.to_s
|
17
|
+
Group.resourcify :roles, :role_cname => role_class.to_s
|
18
18
|
end
|
19
19
|
|
20
20
|
context "in the Instance level " do
|
data/spec/spec_helper.rb
CHANGED
@@ -6,7 +6,7 @@ ActiveRecord::Base.extend Rolify
|
|
6
6
|
|
7
7
|
load File.dirname(__FILE__) + '/../schema.rb'
|
8
8
|
|
9
|
-
#
|
9
|
+
# Standard user and role classes
|
10
10
|
class User < ActiveRecord::Base
|
11
11
|
rolify
|
12
12
|
end
|
@@ -14,18 +14,17 @@ end
|
|
14
14
|
class Role < ActiveRecord::Base
|
15
15
|
has_and_belongs_to_many :users, :join_table => :users_roles
|
16
16
|
belongs_to :resource, :polymorphic => true
|
17
|
-
|
18
|
-
extend Rolify::Adapter::Scopes
|
19
|
-
end
|
20
17
|
|
21
|
-
|
22
|
-
#resourcify done during specs setup to be able to use custom user classes
|
18
|
+
extend Rolify::Adapter::Scopes
|
23
19
|
end
|
24
20
|
|
25
|
-
|
26
|
-
|
21
|
+
# Resourcifed and rolifed at the same time
|
22
|
+
class HumanResource < ActiveRecord::Base
|
23
|
+
resourcify :resources
|
24
|
+
rolify
|
27
25
|
end
|
28
26
|
|
27
|
+
# Custom role and class names
|
29
28
|
class Customer < ActiveRecord::Base
|
30
29
|
rolify :role_cname => "Privilege"
|
31
30
|
end
|
@@ -33,6 +32,38 @@ end
|
|
33
32
|
class Privilege < ActiveRecord::Base
|
34
33
|
has_and_belongs_to_many :customers, :join_table => :customers_privileges
|
35
34
|
belongs_to :resource, :polymorphic => true
|
36
|
-
|
35
|
+
|
37
36
|
extend Rolify::Adapter::Scopes
|
37
|
+
end
|
38
|
+
|
39
|
+
# Namespaced models
|
40
|
+
module Admin
|
41
|
+
def self.table_name_prefix
|
42
|
+
'admin_'
|
43
|
+
end
|
44
|
+
|
45
|
+
class Moderator < ActiveRecord::Base
|
46
|
+
rolify :role_cname => "Admin::Right"
|
47
|
+
end
|
48
|
+
|
49
|
+
class Right < ActiveRecord::Base
|
50
|
+
has_and_belongs_to_many :moderators, :class_name => "Admin::Moderator",:join_table => :admin_moderators_admin_rights
|
51
|
+
belongs_to :resource, :polymorphic => true
|
52
|
+
|
53
|
+
extend Rolify::Adapter::Scopes
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# Resources classes
|
59
|
+
class Forum < ActiveRecord::Base
|
60
|
+
#resourcify done during specs setup to be able to use custom user classes
|
61
|
+
end
|
62
|
+
|
63
|
+
class Group < ActiveRecord::Base
|
64
|
+
#resourcify done during specs setup to be able to use custom user classes
|
65
|
+
|
66
|
+
def subgroups
|
67
|
+
Group.where(:parent_id => id)
|
68
|
+
end
|
38
69
|
end
|
@@ -14,11 +14,11 @@ end
|
|
14
14
|
|
15
15
|
Rolify.use_mongoid
|
16
16
|
|
17
|
-
#
|
17
|
+
# Standard user and role classes
|
18
18
|
class User
|
19
19
|
include Mongoid::Document
|
20
20
|
rolify
|
21
|
-
|
21
|
+
|
22
22
|
field :login, :type => String
|
23
23
|
end
|
24
24
|
|
@@ -26,9 +26,8 @@ class Role
|
|
26
26
|
include Mongoid::Document
|
27
27
|
has_and_belongs_to_many :users
|
28
28
|
belongs_to :resource, :polymorphic => true
|
29
|
-
|
29
|
+
|
30
30
|
field :name, :type => String
|
31
|
-
index({ :name => 1 }, { :unique => true })
|
32
31
|
index(
|
33
32
|
{
|
34
33
|
:name => 1,
|
@@ -37,28 +36,41 @@ class Role
|
|
37
36
|
},
|
38
37
|
{ :unique => true }
|
39
38
|
)
|
40
|
-
|
39
|
+
|
41
40
|
scopify
|
42
41
|
end
|
43
42
|
|
44
|
-
|
43
|
+
# Resourcifed and rolifed at the same time
|
44
|
+
class HumanResource
|
45
45
|
include Mongoid::Document
|
46
|
-
|
47
|
-
|
48
|
-
field :name, :type => String
|
49
|
-
end
|
46
|
+
resourcify :resources
|
47
|
+
rolify
|
50
48
|
|
51
|
-
|
52
|
-
include Mongoid::Document
|
53
|
-
#resourcify done during specs setup to be able to use custom user classes
|
54
|
-
|
55
|
-
field :name, :type => String
|
49
|
+
field :login, :type => String
|
56
50
|
end
|
57
51
|
|
52
|
+
#class Power
|
53
|
+
# include Mongoid::Document
|
54
|
+
# has_and_belongs_to_many :human_resources
|
55
|
+
# belongs_to :resource, :polymorphic => true
|
56
|
+
# scopify
|
57
|
+
#
|
58
|
+
# field :name, :type => String
|
59
|
+
# index(
|
60
|
+
# {
|
61
|
+
# :name => 1,
|
62
|
+
# :resource_type => 1,
|
63
|
+
# :resource_id => 1
|
64
|
+
# },
|
65
|
+
# { :unique => true }
|
66
|
+
# )
|
67
|
+
#end
|
68
|
+
|
69
|
+
# Custom role and class names
|
58
70
|
class Customer
|
59
71
|
include Mongoid::Document
|
60
72
|
rolify :role_cname => "Privilege"
|
61
|
-
|
73
|
+
|
62
74
|
field :login, :type => String
|
63
75
|
end
|
64
76
|
|
@@ -66,9 +78,9 @@ class Privilege
|
|
66
78
|
include Mongoid::Document
|
67
79
|
has_and_belongs_to_many :customers
|
68
80
|
belongs_to :resource, :polymorphic => true
|
69
|
-
|
81
|
+
scopify
|
82
|
+
|
70
83
|
field :name, :type => String
|
71
|
-
index({ :name => 1 }, { :unique => true })
|
72
84
|
index(
|
73
85
|
{
|
74
86
|
:name => 1,
|
@@ -77,6 +89,51 @@ class Privilege
|
|
77
89
|
},
|
78
90
|
{ :unique => true }
|
79
91
|
)
|
80
|
-
|
81
|
-
|
92
|
+
end
|
93
|
+
|
94
|
+
# Namespaced models
|
95
|
+
module Admin
|
96
|
+
class Moderator
|
97
|
+
include Mongoid::Document
|
98
|
+
rolify :role_cname => "Admin::Right"
|
99
|
+
|
100
|
+
field :login, :type => String
|
101
|
+
end
|
102
|
+
|
103
|
+
class Right
|
104
|
+
include Mongoid::Document
|
105
|
+
has_and_belongs_to_many :moderators, :class_name => 'Admin::Moderator'
|
106
|
+
belongs_to :resource, :polymorphic => true
|
107
|
+
scopify
|
108
|
+
|
109
|
+
field :name, :type => String
|
110
|
+
index(
|
111
|
+
{
|
112
|
+
:name => 1,
|
113
|
+
:resource_type => 1,
|
114
|
+
:resource_id => 1
|
115
|
+
},
|
116
|
+
{ :unique => true }
|
117
|
+
)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Resources classes
|
122
|
+
class Forum
|
123
|
+
include Mongoid::Document
|
124
|
+
#resourcify done during specs setup to be able to use custom user classes
|
125
|
+
|
126
|
+
field :name, :type => String
|
127
|
+
end
|
128
|
+
|
129
|
+
class Group
|
130
|
+
include Mongoid::Document
|
131
|
+
#resourcify done during specs setup to be able to use custom user classes
|
132
|
+
|
133
|
+
field :name, :type => String
|
134
|
+
field :parent_id, :type => Integer
|
135
|
+
|
136
|
+
def subgroups
|
137
|
+
Group.in(:parent_id => _id)
|
138
|
+
end
|
82
139
|
end
|
data/spec/support/data.rb
CHANGED
@@ -1,20 +1,17 @@
|
|
1
|
-
User.destroy_all
|
2
|
-
Role.destroy_all
|
3
|
-
Forum.destroy_all
|
4
|
-
Group.destroy_all
|
5
|
-
Privilege.destroy_all
|
6
|
-
Customer.destroy_all
|
7
|
-
|
8
1
|
# Users
|
9
|
-
User.
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
[ User, Customer, Admin::Moderator ].each do |user|
|
3
|
+
user.destroy_all
|
4
|
+
|
5
|
+
user.create(:login => "admin")
|
6
|
+
user.create(:login => "moderator")
|
7
|
+
user.create(:login => "god")
|
8
|
+
user.create(:login => "zombie")
|
9
|
+
end
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
# Roles
|
12
|
+
[ Role, Privilege, Admin::Right ].each do |role|
|
13
|
+
role.destroy_all
|
14
|
+
end
|
18
15
|
|
19
16
|
# Resources
|
20
17
|
Forum.create(:name => "forum 1")
|