rolify 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +12 -0
- data/README.rdoc +15 -4
- data/lib/rolify/role.rb +47 -7
- data/lib/rolify/version.rb +1 -1
- data/spec/rolify/role_spec.rb +97 -8
- data/spec/support/data.rb +1 -0
- metadata +3 -2
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# 0.3
|
2
|
+
* multiple roles check:
|
3
|
+
* ``has_all_roles?`` returns true if the user has ALL the roles in arguments
|
4
|
+
* ``has_any_role?`` returns true if the user has ANY the roles in arguments
|
5
|
+
|
6
|
+
# 0.2
|
7
|
+
* fixed the generator to include the lib
|
8
|
+
* fixed the migration file with missing polymorphic field
|
9
|
+
* added some examples in documentation
|
10
|
+
|
11
|
+
# 0.1
|
12
|
+
* first release
|
data/README.rdoc
CHANGED
@@ -57,27 +57,38 @@ That's it !
|
|
57
57
|
|
58
58
|
=== 4. Check roles
|
59
59
|
|
60
|
-
To check if a user has a global role
|
60
|
+
To check if a user has a global role:
|
61
61
|
|
62
62
|
user = User.find(1)
|
63
|
+
user.has_role "admin" # sets a global role
|
63
64
|
user.has_role? "admin"
|
64
65
|
=> true
|
65
66
|
|
66
|
-
To check if a user has a role scoped to a resource
|
67
|
+
To check if a user has a role scoped to a resource:
|
67
68
|
|
68
69
|
user = User.find(2)
|
70
|
+
user.has_role "moderator", Forum.first # sets a role scoped to a resource
|
69
71
|
user.has_role? "moderator", Forum.first
|
70
72
|
=> true
|
71
73
|
user.has_role? "moderator", Forum.last
|
72
74
|
=> false
|
73
75
|
|
76
|
+
A global role overrides resource role request:
|
77
|
+
|
78
|
+
user = User.find(3)
|
79
|
+
user.has_role "moderator" # sets a global role
|
80
|
+
user.has_role? "moderator", Forum.first
|
81
|
+
=> true
|
82
|
+
user.has_role? "moderator", Forum.last
|
83
|
+
=> true
|
84
|
+
|
74
85
|
== Questions or Problems?
|
75
86
|
|
76
|
-
If you have any
|
87
|
+
If you have any issue or feature request with/for rolify, please add an {issue on GitHub}[https://github.com/EppO/rolify/issues] or fork the project and send a pull request.
|
77
88
|
|
78
89
|
== TODO
|
79
90
|
|
80
91
|
* put syntactic sugar:
|
81
|
-
*
|
92
|
+
* <tt>is_admin?</tt> and <tt>is_admin_of?(resource)</tt> like shortcuts
|
82
93
|
* multiple role names query at the same time
|
83
94
|
* write a tutorial showing how to use rolify with CanCan and devise
|
data/lib/rolify/role.rb
CHANGED
@@ -7,16 +7,56 @@ module Rolify
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def has_role?(role, resource = nil)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
query, values = build_query(role, resource)
|
11
|
+
self.roles.where(*query, *values).size > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_all_roles?(*args)
|
15
|
+
conditions = []
|
16
|
+
values = []
|
17
|
+
args.each do |arg|
|
18
|
+
if arg.is_a? Hash
|
19
|
+
return false if !self.has_role?(arg[:name], arg[:resource])
|
20
|
+
elsif arg.is_a? String
|
21
|
+
return false if !self.has_role?(arg)
|
22
|
+
else
|
23
|
+
raise ArgumentError, "Invalid argument type: only hash or string allowed"
|
24
|
+
end
|
16
25
|
end
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_any_role?(*args)
|
30
|
+
conditions = []
|
31
|
+
values = []
|
32
|
+
args.each do |arg|
|
33
|
+
if arg.is_a? Hash
|
34
|
+
a, v = build_query(arg[:name], arg[:resource])
|
35
|
+
elsif arg.is_a? String
|
36
|
+
a, v = build_query(arg)
|
37
|
+
else
|
38
|
+
raise ArgumentError, "Invalid argument type: only hash or string allowed"
|
39
|
+
end
|
40
|
+
conditions << a
|
41
|
+
values += v
|
42
|
+
end
|
43
|
+
self.roles.where([ conditions.join(' OR '), *values ]).size > 0
|
17
44
|
end
|
18
45
|
|
19
46
|
def roles_name
|
20
47
|
self.roles.select(:name).map { |r| r.name }
|
21
48
|
end
|
22
|
-
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def build_query(role, resource = nil)
|
53
|
+
query = "((name = ?) AND (resource_type IS NULL) AND (resource_id IS NULL))"
|
54
|
+
values = [ role ]
|
55
|
+
if resource
|
56
|
+
query.insert(0, "(")
|
57
|
+
query += " OR ((name = ?) AND (resource_type = ?) AND (resource_id = ?)))"
|
58
|
+
values << role << resource.class.name << resource.id
|
59
|
+
end
|
60
|
+
[ [ query ], values]
|
61
|
+
end
|
62
|
+
end
|
data/lib/rolify/version.rb
CHANGED
data/spec/rolify/role_spec.rb
CHANGED
@@ -1,11 +1,37 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Rolify do
|
4
|
+
context "in a Instance level" do
|
5
|
+
before(:all) do
|
6
|
+
@admin = User.first
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should respond to has_role method" do
|
10
|
+
@admin.should respond_to(:has_role).with(1).arguments
|
11
|
+
@admin.should respond_to(:has_role).with(2).arguments
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should respond to has_role? method" do
|
15
|
+
@admin.should respond_to(:has_role?).with(1).arguments
|
16
|
+
@admin.should respond_to(:has_role?).with(2).arguments
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should respond to has_all_roles? method" do
|
20
|
+
@admin.should respond_to(:has_all_roles?)
|
21
|
+
@admin.should respond_to(:has_all_roles?)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should respond to has_any_role? method" do
|
25
|
+
@admin.should respond_to(:has_any_role?)
|
26
|
+
@admin.should respond_to(:has_any_role?)
|
27
|
+
end
|
28
|
+
end
|
4
29
|
|
5
30
|
context "with a global role" do
|
6
31
|
before(:all) do
|
7
32
|
@admin = User.first
|
8
33
|
@admin.has_role "admin"
|
34
|
+
@admin.has_role "staff"
|
9
35
|
end
|
10
36
|
|
11
37
|
it "should set a global role" do
|
@@ -41,16 +67,32 @@ describe "Rolify::Role" do
|
|
41
67
|
@admin.has_role?("dummy").should be(false)
|
42
68
|
@admin.has_role?("dumber", Forum.first).should be(false)
|
43
69
|
end
|
70
|
+
|
71
|
+
it "should check if user has all of a global roles set" do
|
72
|
+
@admin.has_role?("staff").should be(true)
|
73
|
+
@admin.has_all_roles?("admin", "staff").should be(true)
|
74
|
+
@admin.has_all_roles?("admin", "dummy").should be(false)
|
75
|
+
@admin.has_all_roles?("dummy", "dumber").should be(false)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should check if user has any of a global roles set" do
|
79
|
+
@admin.has_any_role?("admin", "staff").should be(true)
|
80
|
+
@admin.has_any_role?("admin", "moderator").should be(true)
|
81
|
+
@admin.has_any_role?("dummy", "dumber").should be(false)
|
82
|
+
end
|
44
83
|
end
|
45
84
|
|
46
85
|
context "with a scoped role" do
|
47
86
|
before(:all) do
|
48
|
-
@moderator = User.
|
87
|
+
@moderator = User.find(2)
|
49
88
|
@moderator.has_role "moderator", Forum.first
|
50
89
|
end
|
51
90
|
|
52
91
|
it "should set a scoped role" do
|
53
|
-
@moderator
|
92
|
+
expect { @moderator.has_role "visitor", Forum.last }.to change{ Role.count }.by(1)
|
93
|
+
supermodo = Role.last
|
94
|
+
supermodo.name.should eq("visitor")
|
95
|
+
supermodo.resource.should eq(Forum.last)
|
54
96
|
end
|
55
97
|
|
56
98
|
it "should not create another role if already existing" do
|
@@ -59,10 +101,7 @@ describe "Rolify::Role" do
|
|
59
101
|
end
|
60
102
|
|
61
103
|
it "should get a scoped role" do
|
62
|
-
|
63
|
-
supermodo = Role.last
|
64
|
-
supermodo.name.should eq("supermodo")
|
65
|
-
supermodo.resource.should eq(Forum.last)
|
104
|
+
@moderator.has_role?("moderator", Forum.first).should be(true)
|
66
105
|
end
|
67
106
|
|
68
107
|
it "should not get a global role" do
|
@@ -83,5 +122,55 @@ describe "Rolify::Role" do
|
|
83
122
|
@moderator.has_role?("dummy", Forum.last).should be(false)
|
84
123
|
@moderator.has_role?("dumber").should be(false)
|
85
124
|
end
|
125
|
+
|
126
|
+
it "should check if user has all of a scoped roles set" do
|
127
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
128
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
129
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
130
|
+
{ :name => "dummy", :resource => Forum.last }).should be(false)
|
131
|
+
@moderator.has_all_roles?({ :name => "dummy", :resource => Forum.first },
|
132
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should check if user has any of a scoped roles set" do
|
136
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
137
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
138
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
139
|
+
{ :name => "dummy", :resource => Forum.last }).should be(true)
|
140
|
+
@moderator.has_any_role?( { :name => "dummy", :resource => Forum.first },
|
141
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "with different roles" do
|
146
|
+
before(:all) do
|
147
|
+
@user = User.last
|
148
|
+
@user.has_role "admin"
|
149
|
+
@user.has_role "moderator", Forum.first
|
150
|
+
@user.has_role "visitor", Forum.last
|
151
|
+
@user.has_role "anonymous"
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should get a global role" do
|
155
|
+
@user.has_role?("admin").should be(true)
|
156
|
+
@user.has_role?("anonymous").should be(true)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should get a scoped role" do
|
160
|
+
@user.has_role?("moderator", Forum.first).should be(true)
|
161
|
+
@user.has_role?("visitor", Forum.last).should be(true)
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should check if user has all of a mix of global and scoped roles set" do
|
165
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
|
166
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.last }).should be(false)
|
167
|
+
@user.has_all_roles?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should check if user has any of a mix of global and scoped roles set" do
|
171
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.first }).should be(true)
|
172
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }).should be(true)
|
173
|
+
@user.has_any_role?("dummy", { :name => "dumber", :resource => Forum.last }).should be(false)
|
174
|
+
end
|
86
175
|
end
|
87
|
-
end
|
176
|
+
end
|
data/spec/support/data.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rolify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Florent Monbillard
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-07 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ extra_rdoc_files: []
|
|
46
46
|
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
+
- CHANGELOG.rdoc
|
49
50
|
- Gemfile
|
50
51
|
- LICENSE
|
51
52
|
- README.rdoc
|