rolify 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +25 -11
- data/Gemfile +0 -8
- data/README.rdoc +23 -12
- data/lib/rolify/role.rb +14 -8
- data/lib/rolify/version.rb +1 -1
- data/rolify.gemspec +6 -1
- data/spec/generators/rolify/role/role_generator_spec.rb +116 -0
- data/spec/rolify/role_spec.rb +406 -249
- data/spec/spec_helper.rb +2 -1
- data/spec/support/data.rb +10 -3
- data/spec/support/models.rb +3 -0
- data/spec/support/schema.rb +4 -0
- metadata +65 -13
- data/spec/spec.opts +0 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,4 +1,18 @@
|
|
1
|
-
= 1.
|
1
|
+
= 1.1 (Oct 14, 2011)
|
2
|
+
* added a spec to test the rails generator using ammeter gem
|
3
|
+
* Gemfile cleanup, moved all dependencies in gemspec instead
|
4
|
+
* edited the dependency to Rails 3.1 and newer, now that Rails 3.1 has been released
|
5
|
+
* new role scoping capabilities
|
6
|
+
* instance level : <tt>user.has_role "moderator", Forum.first</tt> (<em>already supported in previous release</em>). user has the moderator role only on that Forum in particular
|
7
|
+
* class level : <tt>user.has_role "moderator", Forum</tt>. User has the moderator role on all instances of Forum
|
8
|
+
* global level : <tt>user.has_role "moderator"</tt> (<em>already supported in previous release</em>). User has the moderator role globally (e.q. on all resources)
|
9
|
+
* new scoped query capabilities
|
10
|
+
* <tt>user.has_role? "moderator", Forum.first</tt> (<em>already supported in previous release</em>). asks if the user has the moderator role on Forum.first instance
|
11
|
+
* <tt>user.has_role? "moderator", Forum</tt>. asks if the user has the moderator role on all Forum instances
|
12
|
+
* <tt>user.has_role? "moderator"</tt> (<em>already supported in previous release</em>). asks if the user has the global moderator role
|
13
|
+
* <tt>user.has_role? "moderator", :any</tt>. asks if the user has at least one moderator role no matter the scope is (instance, class or global).
|
14
|
+
|
15
|
+
= 1.0 (Aug 25, 2011)
|
2
16
|
* added a new parameter to disable dynamic shortcut methods due to potential incompatibility with other gems using method_missing with the same pattern
|
3
17
|
* add <tt>Rolify.dynamic_shortcuts = false</tt> in the initializer file or
|
4
18
|
* use the generator command with a third parameter:
|
@@ -7,26 +21,26 @@
|
|
7
21
|
* code refactoring to do some speed improvements and code clean up
|
8
22
|
* added a lot of specs to improve tests coverage
|
9
23
|
* wrote a tutorial showing how to use rolify with CanCan and Devise
|
10
|
-
* rolify is now on travis-ci
|
24
|
+
* rolify is now on travis-ci to monitor build status
|
11
25
|
|
12
|
-
= 0.7
|
26
|
+
= 0.7 (June 20, 2011)
|
13
27
|
* added a method_missing to catch newly created role outside the current ruby process (i.e. dynamic shortcut methods are not defined within this process)
|
14
28
|
* dynamic shortcut is created on the fly in the method_missing to avoid extra method_missing for the same dynamic shortcut
|
15
29
|
* check if the role actually exists in the database before defining the new method
|
16
30
|
* first call is slower due to method_missing but next calls are fast
|
17
31
|
* avoid strange bugs when spawning many ruby processes as the dynamic shortcut methods were only defined in the process that used the <tt>has_role</tt> command
|
18
32
|
|
19
|
-
= 0.6
|
33
|
+
= 0.6 (June 19, 2011)
|
20
34
|
* custom User and Role class names support
|
21
35
|
* can now use other class names for Role and User classes
|
22
36
|
* fixed generators and templates
|
23
37
|
* join table is explicitly set to avoid alphabetical order issue
|
24
38
|
* created a new railtie to load the dynamic shortcuts at startup
|
25
39
|
|
26
|
-
= 0.5.1
|
27
|
-
* fixed a nasty typo on a variable name and added a spec to make
|
40
|
+
= 0.5.1 (June 07, 2011)
|
41
|
+
* fixed a nasty typo on a variable name and added a spec to make it never happen again
|
28
42
|
|
29
|
-
= 0.5
|
43
|
+
= 0.5 (June 07, 2011)
|
30
44
|
* dynamic shortcuts support
|
31
45
|
* creates automatically new methods upon new role creation (or at startup for a Rails app)
|
32
46
|
* <tt>has_role "admin"</tt> will create a method called <tt>is_admin?</tt>
|
@@ -34,21 +48,21 @@
|
|
34
48
|
* <tt>is_moderator_of?(resource)</tt>
|
35
49
|
* <tt>is_moderator?</tt>
|
36
50
|
|
37
|
-
= v0.4
|
51
|
+
= v0.4 (June 07, 2011)
|
38
52
|
* removing role support
|
39
53
|
* <tt>has_no_role</tt> removes a global role or a role scoped to a resource
|
40
54
|
* Please note that trying to remove a global role whereas the user a role with the same name on a resource will remove that scoped role
|
41
55
|
* Trying to remove a role scoped to a resource whereas the user has a global role won't remove it
|
42
56
|
|
43
|
-
= v0.3
|
57
|
+
= v0.3 (June 06, 2011)
|
44
58
|
* multiple roles check:
|
45
59
|
* <tt>has_all_roles?</tt> returns true if the user has ALL the roles in arguments
|
46
60
|
* <tt>has_any_role?</tt> returns true if the user has ANY the roles in arguments
|
47
61
|
|
48
|
-
= v0.2
|
62
|
+
= v0.2 (June 04, 2011)
|
49
63
|
* fixed the generator to include the lib
|
50
64
|
* fixed the migration file with missing polymorphic field
|
51
65
|
* added some examples in documentation
|
52
66
|
|
53
|
-
= v0.1
|
67
|
+
= v0.1 (June 04, 2011)
|
54
68
|
* first release
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= rolify
|
1
|
+
= rolify {<img src="https://secure.travis-ci.org/EppO/rolify.png">}[http://travis-ci.org/EppO/rolify]
|
2
2
|
Very simple Roles library without any authorization enforcement supporting scope on resource object.
|
3
3
|
|
4
4
|
Let's see an example:
|
@@ -10,7 +10,7 @@ This library was intended to be used with CanCan[https://github.com/ryanb/cancan
|
|
10
10
|
|
11
11
|
== Requirements
|
12
12
|
|
13
|
-
* >= Rails 3.1
|
13
|
+
* >= Rails 3.1
|
14
14
|
* ActiveRecord ORM
|
15
15
|
|
16
16
|
== Installation
|
@@ -47,11 +47,16 @@ To define a global role:
|
|
47
47
|
user = User.find(1)
|
48
48
|
user.has_role "admin"
|
49
49
|
|
50
|
-
To define a role scoped to a resource
|
50
|
+
To define a role scoped to a resource instance
|
51
51
|
|
52
52
|
user = User.find(2)
|
53
53
|
user.has_role "moderator", Forum.first
|
54
54
|
|
55
|
+
To define a role scoped to a resource class
|
56
|
+
|
57
|
+
user = User.find(3)
|
58
|
+
user.has_role "moderator", Forum
|
59
|
+
|
55
60
|
That's it !
|
56
61
|
|
57
62
|
=== 4. Check roles
|
@@ -63,18 +68,29 @@ To check if a user has a global role:
|
|
63
68
|
user.has_role? "admin"
|
64
69
|
=> true
|
65
70
|
|
66
|
-
To check if a user has a role scoped to a resource:
|
71
|
+
To check if a user has a role scoped to a resource instance:
|
67
72
|
|
68
73
|
user = User.find(2)
|
69
|
-
user.has_role "moderator", Forum.first # sets a role scoped to a resource
|
74
|
+
user.has_role "moderator", Forum.first # sets a role scoped to a resource instance
|
70
75
|
user.has_role? "moderator", Forum.first
|
71
76
|
=> true
|
72
77
|
user.has_role? "moderator", Forum.last
|
73
78
|
=> false
|
74
79
|
|
75
|
-
|
80
|
+
To check if a user has a role scoped to a resource class:
|
76
81
|
|
77
82
|
user = User.find(3)
|
83
|
+
user.has_role "moderator", Forum # sets a role scoped to a resource class
|
84
|
+
user.has_role? "moderator", Forum
|
85
|
+
=> true
|
86
|
+
user.has_role? "moderator", Forum.first
|
87
|
+
=> true
|
88
|
+
user.has_role? "moderator", Forum.last
|
89
|
+
=> true
|
90
|
+
|
91
|
+
A global role overrides resource role request:
|
92
|
+
|
93
|
+
user = User.find(4)
|
78
94
|
user.has_role "moderator" # sets a global role
|
79
95
|
user.has_role? "moderator", Forum.first
|
80
96
|
=> true
|
@@ -85,9 +101,4 @@ Please see on {the wiki}[https://github.com/EppO/rolify/wiki/Usage] for all the
|
|
85
101
|
|
86
102
|
== Questions or Problems?
|
87
103
|
|
88
|
-
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.
|
89
|
-
|
90
|
-
== TODO
|
91
|
-
|
92
|
-
* complete tests coverage
|
93
|
-
* performance enhancements
|
104
|
+
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.
|
data/lib/rolify/role.rb
CHANGED
@@ -28,9 +28,9 @@ module Rolify
|
|
28
28
|
module Roles
|
29
29
|
|
30
30
|
def has_role(role_name, resource = nil)
|
31
|
-
role = Rolify.role_cname.find_or_create_by_name_and_resource_type_and_resource_id(
|
32
|
-
|
33
|
-
|
31
|
+
role = Rolify.role_cname.find_or_create_by_name_and_resource_type_and_resource_id(:name => role_name,
|
32
|
+
:resource_type => (resource.is_a?(Class) ? resource.to_s : resource.class.name if resource),
|
33
|
+
:resource_id => (resource.id if resource && !resource.is_a?(Class)))
|
34
34
|
if !roles.include?(role)
|
35
35
|
self.class.define_dynamic_method(role_name, resource) if Rolify.dynamic_shortcuts
|
36
36
|
self.roles << role
|
@@ -62,8 +62,8 @@ module Rolify
|
|
62
62
|
|
63
63
|
def has_no_role(role_name, resource = nil)
|
64
64
|
role = self.roles.where( :name => role_name)
|
65
|
-
role = role.where(
|
66
|
-
|
65
|
+
role = role.where(:resource_type => (resource.is_a?(Class) ? resource.to_s : resource.class.name)) if resource
|
66
|
+
role = role.where(:resource_id => resource.id) if resource && !resource.is_a?(Class)
|
67
67
|
self.roles.delete(role) if role
|
68
68
|
end
|
69
69
|
|
@@ -102,12 +102,18 @@ module Rolify
|
|
102
102
|
end
|
103
103
|
|
104
104
|
def build_query(role, resource = nil)
|
105
|
+
return [ "name = ?", role] if resource == :any
|
105
106
|
query = "((name = ?) AND (resource_type IS NULL) AND (resource_id IS NULL))"
|
106
107
|
values = [ role ]
|
107
108
|
if resource
|
108
109
|
query.insert(0, "(")
|
109
|
-
query += " OR ((name = ?) AND (resource_type = ?) AND (resource_id
|
110
|
-
values << role << resource.
|
110
|
+
query += " OR ((name = ?) AND (resource_type = ?) AND (resource_id IS NULL))"
|
111
|
+
values << role << (resource.is_a?(Class) ? resource.to_s : resource.class.name)
|
112
|
+
if !resource.is_a? Class
|
113
|
+
query += " OR ((name = ?) AND (resource_type = ?) AND (resource_id = ?))"
|
114
|
+
values << role << resource.class.name << resource.id
|
115
|
+
end
|
116
|
+
query += ")"
|
111
117
|
end
|
112
118
|
[ [ query ], values]
|
113
119
|
end
|
@@ -135,4 +141,4 @@ module Rolify
|
|
135
141
|
end
|
136
142
|
end
|
137
143
|
end
|
138
|
-
end
|
144
|
+
end
|
data/lib/rolify/version.rb
CHANGED
data/rolify.gemspec
CHANGED
@@ -19,6 +19,11 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
+
s.add_dependency "activerecord", ">= 3.1.0"
|
23
|
+
|
22
24
|
s.add_development_dependency "sqlite3"
|
23
|
-
s.
|
25
|
+
s.add_development_dependency "ammeter"
|
26
|
+
s.add_development_dependency "rake"
|
27
|
+
s.add_development_dependency "rspec"
|
28
|
+
s.add_development_dependency "bundler"
|
24
29
|
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Generators are not automatically loaded by Rails
|
4
|
+
require 'generators/rolify/role/role_generator'
|
5
|
+
|
6
|
+
describe Rolify::Generators::RoleGenerator do
|
7
|
+
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
8
|
+
destination File.expand_path("../../../../../tmp", __FILE__)
|
9
|
+
before {
|
10
|
+
prepare_destination
|
11
|
+
}
|
12
|
+
|
13
|
+
describe 'no arguments' do
|
14
|
+
before(:all) { arguments [] }
|
15
|
+
|
16
|
+
before {
|
17
|
+
capture(:stdout) {
|
18
|
+
generator.create_file "app/models/user.rb" do
|
19
|
+
"class User < ActiveRecord::Base\nend"
|
20
|
+
end
|
21
|
+
}
|
22
|
+
run_generator
|
23
|
+
}
|
24
|
+
|
25
|
+
describe 'config/initializers/rolify.rb' do
|
26
|
+
subject { file('config/initializers/rolify.rb') }
|
27
|
+
it { should exist }
|
28
|
+
it { should contain "Rolify.user_cname = User" }
|
29
|
+
it { should contain "Rolify.role_cname = Role" }
|
30
|
+
it { should contain "Rolify.dynamic_shortcuts = true" }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'app/models/user.rb' do
|
34
|
+
subject { file('app/models/user.rb') }
|
35
|
+
it { should contain "include Rolify::Roles" }
|
36
|
+
it { should contain "extend Rolify::Reloaded" }
|
37
|
+
it { should contain "has_and_belongs_to_many :roles, :join_table => :users_roles" }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'migration file' do
|
41
|
+
subject { file('db/migrate/rolify_create_roles.rb') }
|
42
|
+
|
43
|
+
# should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
|
44
|
+
it { should be_a_migration }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'specifying user and role names' do
|
49
|
+
before(:all) { arguments %w(Rank Client) }
|
50
|
+
|
51
|
+
before {
|
52
|
+
capture(:stdout) {
|
53
|
+
generator.create_file "app/models/client.rb" do
|
54
|
+
"class Client < ActiveRecord::Base\nend"
|
55
|
+
end
|
56
|
+
}
|
57
|
+
run_generator
|
58
|
+
}
|
59
|
+
|
60
|
+
describe 'config/initializers/rolify.rb' do
|
61
|
+
subject { file('config/initializers/rolify.rb') }
|
62
|
+
it { should exist }
|
63
|
+
it { should contain "Rolify.user_cname = Client" }
|
64
|
+
it { should contain "Rolify.role_cname = Rank" }
|
65
|
+
it { should contain "Rolify.dynamic_shortcuts = true" }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'app/models/client.rb' do
|
69
|
+
subject { file('app/models/client.rb') }
|
70
|
+
it { should contain "include Rolify::Roles" }
|
71
|
+
it { should contain "extend Rolify::Reloaded" }
|
72
|
+
it { should contain "has_and_belongs_to_many :roles, :class_name => \"Rank\", :join_table => :clients_ranks" }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'migration file' do
|
76
|
+
subject { file('db/migrate/rolify_create_ranks.rb') }
|
77
|
+
|
78
|
+
# should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
|
79
|
+
it { should be_a_migration }
|
80
|
+
#it { should contain "create_table(:ranks)" }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'specifying no dynamic shortcuts' do
|
85
|
+
before(:all) { arguments [ "Role", "User", "--no-dynamic_shortcuts" ] }
|
86
|
+
|
87
|
+
before {
|
88
|
+
capture(:stdout) {
|
89
|
+
generator.create_file "app/models/user.rb" do
|
90
|
+
"class User < ActiveRecord::Base\nend"
|
91
|
+
end
|
92
|
+
}
|
93
|
+
run_generator
|
94
|
+
}
|
95
|
+
|
96
|
+
describe 'config/initializers/rolify.rb' do
|
97
|
+
subject { file('config/initializers/rolify.rb') }
|
98
|
+
it { should exist }
|
99
|
+
it { should contain "Rolify.dynamic_shortcuts = false" }
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'app/models/user.rb' do
|
103
|
+
subject { file('app/models/user.rb') }
|
104
|
+
it { should contain "include Rolify::Roles" }
|
105
|
+
it { should_not contain "extend Rolify::Reloaded" }
|
106
|
+
it { should contain "has_and_belongs_to_many :roles, :join_table => :users_roles" }
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'migration file' do
|
110
|
+
subject { file('db/migrate/rolify_create_roles.rb') }
|
111
|
+
|
112
|
+
# should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
|
113
|
+
it { should be_a_migration }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/spec/rolify/role_spec.rb
CHANGED
@@ -1,262 +1,419 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
shared_examples_for "Rolify module" do |dynamic|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@admin.should respond_to(:has_role).with(2).arguments
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should respond to has_role? method" do
|
21
|
-
@admin.should respond_to(:has_role?).with(1).arguments
|
22
|
-
@admin.should respond_to(:has_role?).with(2).arguments
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should respond to has_all_roles? method" do
|
26
|
-
@admin.should respond_to(:has_all_roles?)
|
27
|
-
@admin.should respond_to(:has_all_roles?)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should respond to has_any_role? method" do
|
31
|
-
@admin.should respond_to(:has_any_role?)
|
32
|
-
@admin.should respond_to(:has_any_role?)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should respond to has_no_role method" do
|
36
|
-
@admin.should respond_to(:has_no_role).with(1).arguments
|
37
|
-
@admin.should respond_to(:has_no_role).with(2).arguments
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should respond to dynamic methods", :if => dynamic do
|
41
|
-
@admin.should respond_to(:is_admin?).with(0).arguments
|
42
|
-
@admin.should respond_to(:is_moderator_of?).with(1).arguments
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should not respond to any unknown methods", :if => dynamic do
|
46
|
-
@admin.should_not respond_to(:is_god?)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should create a new dynamic method if role exists in database", :if => dynamic do
|
50
|
-
Rolify.role_cname.create(:name => "superman")
|
51
|
-
@admin.should respond_to(:is_superman?).with(0).arguments
|
52
|
-
@admin.is_superman?.should be(false)
|
53
|
-
Rolify.role_cname.create(:name => "batman", :resource => Forum.first)
|
54
|
-
@admin.should respond_to(:is_batman_of?).with(1).arguments
|
55
|
-
@admin.should respond_to(:is_batman?).with(0).arguments
|
56
|
-
@admin.is_batman_of?(Forum.first).should be(false)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should not have any dynamic methods if dynamic_shortcuts is disabled", :if => dynamic == false do
|
60
|
-
@admin.should_not respond_to(:is_admin?)
|
61
|
-
@admin.should_not respond_to(:is_moderator_of?)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context "with a global role" do
|
66
|
-
before(:all) do
|
67
|
-
@admin = Rolify.user_cname.first
|
68
|
-
@admin.has_role "admin"
|
69
|
-
@admin.has_role "staff"
|
70
|
-
@admin.has_role "moderator", Forum.first
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should set a global role" do
|
74
|
-
expect { @admin.has_role "superadmin" }.to change{ Rolify.role_cname.count }.by(1)
|
75
|
-
superadmin = Rolify.role_cname.last
|
76
|
-
superadmin.name.should eq("superadmin")
|
77
|
-
superadmin.resource.should be(nil)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should not create another role if already existing" do
|
81
|
-
expect { @admin.has_role "admin" }.not_to change{ Rolify.role_cname.count }
|
82
|
-
expect { @admin.has_role "admin" }.not_to change{ @admin.roles.size }
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should get a global role" do
|
86
|
-
@admin.has_role?("admin").should be(true)
|
87
|
-
end
|
4
|
+
before(:all) do
|
5
|
+
Rolify.user_cname = user_cname
|
6
|
+
Rolify.role_cname = role_cname
|
7
|
+
Rolify.dynamic_shortcuts = dynamic_shortcuts
|
8
|
+
Rolify.role_cname.destroy_all
|
9
|
+
end
|
10
|
+
|
11
|
+
context "in a Instance level" do
|
12
|
+
before(:all) do
|
13
|
+
@admin = Rolify.user_cname.first
|
14
|
+
@admin.has_role "admin"
|
15
|
+
@admin.has_role "moderator", Forum.first
|
16
|
+
end
|
88
17
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
it "should get any resource request" do
|
94
|
-
@admin.has_role?("admin", Forum.first).should be(true)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should not get another global role" do
|
98
|
-
Rolify.role_cname.create(:name => "global")
|
99
|
-
@admin.has_role?("global").should be(false)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should not get a scoped role" do
|
103
|
-
@admin.has_role?("moderator", Forum.last).should be(false)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should not get inexisting role" do
|
107
|
-
@admin.has_role?("dummy").should be(false)
|
108
|
-
@admin.has_role?("dumber", Forum.first).should be(false)
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should check if user has all of a global roles set" do
|
112
|
-
@admin.has_all_roles?("staff").should be(true)
|
113
|
-
@admin.has_all_roles?("admin", "staff").should be(true)
|
114
|
-
@admin.has_all_roles?("admin", "dummy").should be(false)
|
115
|
-
@admin.has_all_roles?("dummy", "dumber").should be(false)
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should check if user has any of a global roles set" do
|
119
|
-
@admin.has_any_role?("staff").should be(true)
|
120
|
-
@admin.has_any_role?("admin", "staff").should be(true)
|
121
|
-
@admin.has_any_role?("admin", "moderator").should be(true)
|
122
|
-
@admin.has_any_role?("dummy", "dumber").should be(false)
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should remove a global role of a user" do
|
126
|
-
expect { @admin.has_no_role("admin") }.to change{ @admin.roles.size }.by(-1)
|
127
|
-
@admin.has_role?("admin").should be(false)
|
128
|
-
@admin.has_role?("staff").should be(true)
|
129
|
-
@admin.has_role?("moderator", Forum.first).should be(true)
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should remove a scoped role of a user" do
|
133
|
-
expect { @admin.has_no_role("moderator") }.to change{ @admin.roles.size }.by(-1)
|
134
|
-
@admin.has_role?("staff").should be(true)
|
135
|
-
@admin.has_role?("moderator", Forum.first).should be(false)
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should not remove a another global role" do
|
139
|
-
expect { @admin.has_no_role("global") }.not_to change{ @admin.roles.size }
|
140
|
-
end
|
18
|
+
it "should respond to has_role method" do
|
19
|
+
@admin.should respond_to(:has_role).with(1).arguments
|
20
|
+
@admin.should respond_to(:has_role).with(2).arguments
|
141
21
|
end
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
22
|
+
|
23
|
+
it "should respond to has_role? method" do
|
24
|
+
@admin.should respond_to(:has_role?).with(1).arguments
|
25
|
+
@admin.should respond_to(:has_role?).with(2).arguments
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should respond to has_all_roles? method" do
|
29
|
+
@admin.should respond_to(:has_all_roles?)
|
30
|
+
@admin.should respond_to(:has_all_roles?)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should respond to has_any_role? method" do
|
34
|
+
@admin.should respond_to(:has_any_role?)
|
35
|
+
@admin.should respond_to(:has_any_role?)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should respond to has_no_role method" do
|
39
|
+
@admin.should respond_to(:has_no_role).with(1).arguments
|
40
|
+
@admin.should respond_to(:has_no_role).with(2).arguments
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should respond to dynamic methods", :if => dynamic do
|
44
|
+
@admin.should respond_to(:is_admin?).with(0).arguments
|
45
|
+
@admin.should respond_to(:is_moderator_of?).with(1).arguments
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not respond to any unknown methods", :if => dynamic do
|
49
|
+
@admin.should_not respond_to(:is_god?)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should create a new dynamic method if role exists in database", :if => dynamic do
|
53
|
+
Rolify.role_cname.create(:name => "superman")
|
54
|
+
@admin.should respond_to(:is_superman?).with(0).arguments
|
55
|
+
Rolify.role_cname.create(:name => "batman", :resource => Forum.first)
|
56
|
+
@admin.should respond_to(:is_batman_of?).with(1).arguments
|
57
|
+
@admin.should respond_to(:is_batman?).with(0).arguments
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should not have any dynamic methods if dynamic_shortcuts is disabled", :if => dynamic == false do
|
61
|
+
@admin.should_not respond_to(:is_admin?)
|
62
|
+
@admin.should_not respond_to(:is_moderator_of?)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "with a global role" do
|
67
|
+
before do
|
68
|
+
@admin = Rolify.user_cname.first
|
69
|
+
@admin.has_role "admin"
|
70
|
+
@admin.has_role "staff"
|
71
|
+
@admin.has_role "moderator", Forum.first
|
72
|
+
@admin.has_role "moderator", Forum.find(2)
|
73
|
+
@admin.has_role "manager", Group
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should set a global role" do
|
77
|
+
expect { @admin.has_role "superadmin" }.to change{ Rolify.role_cname.count }.by(1)
|
78
|
+
superadmin = Rolify.role_cname.last
|
79
|
+
superadmin.name.should eq("superadmin")
|
80
|
+
superadmin.resource.should be(nil)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should not create another role if already existing" do
|
84
|
+
expect { @admin.has_role "admin" }.not_to change{ Rolify.role_cname.count }
|
85
|
+
expect { @admin.has_role "admin" }.not_to change{ @admin.roles.size }
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should get a global role" do
|
89
|
+
@admin.has_role?("admin").should be(true)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should be able to use dynamic shortcut", :if => dynamic do
|
93
|
+
@admin.is_admin?.should be(true)
|
94
|
+
@admin.is_evil?.should be(false)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should get any resource request" do
|
98
|
+
@admin.has_role?("admin", Forum.first).should be(true)
|
99
|
+
@admin.has_role?("admin", Forum).should be(true)
|
100
|
+
@admin.has_role?("admin", :any).should be(true)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should not get another global role" do
|
104
|
+
Rolify.role_cname.create(:name => "global")
|
105
|
+
@admin.has_role?("global").should be(false)
|
106
|
+
@admin.has_role?("global", :any).should be(false)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should not get an instance scoped role" do
|
110
|
+
@admin.has_role?("moderator", Forum.last).should be(false)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should not get a class scoped role" do
|
114
|
+
@admin.has_role?("manager", Forum).should be(false)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should not get inexisting role" do
|
118
|
+
@admin.has_role?("dummy").should be(false)
|
119
|
+
@admin.has_role?("dumber", Forum.first).should be(false)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should check if user has all of a global roles set" do
|
123
|
+
@admin.has_all_roles?("staff").should be(true)
|
124
|
+
@admin.has_all_roles?("admin", "staff").should be(true)
|
125
|
+
@admin.has_all_roles?("admin", "dummy").should be(false)
|
126
|
+
@admin.has_all_roles?("dummy", "dumber").should be(false)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should check if user has any of a global roles set" do
|
130
|
+
@admin.has_any_role?("staff").should be(true)
|
131
|
+
@admin.has_any_role?("admin", "staff").should be(true)
|
132
|
+
@admin.has_any_role?("admin", "moderator").should be(true)
|
133
|
+
@admin.has_any_role?("dummy", "dumber").should be(false)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should remove a global role of a user" do
|
137
|
+
expect { @admin.has_no_role("admin") }.to change{ @admin.roles.size }.by(-1)
|
138
|
+
@admin.has_role?("admin").should be(false)
|
139
|
+
@admin.has_role?("staff").should be(true)
|
140
|
+
@admin.has_role?("moderator", Forum.first).should be(true)
|
141
|
+
@admin.has_role?("manager", Group).should be(true)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should remove a class scoped role of a user" do
|
145
|
+
expect { @admin.has_no_role("manager") }.to change{ @admin.roles.size }.by(-1)
|
146
|
+
@admin.has_role?("staff").should be(true)
|
147
|
+
@admin.has_role?("moderator", Forum.first).should be(true)
|
148
|
+
@admin.has_role?("manager", Group).should be(false)
|
224
149
|
end
|
225
|
-
|
226
|
-
context "with different roles" do
|
227
|
-
before(:all) do
|
228
|
-
@user = Rolify.user_cname.last
|
229
|
-
@user.has_role "admin"
|
230
|
-
@user.has_role "moderator", Forum.first
|
231
|
-
@user.has_role "visitor", Forum.last
|
232
|
-
@user.has_role "anonymous"
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should get a global role" do
|
236
|
-
@user.has_role?("admin").should be(true)
|
237
|
-
@user.has_role?("anonymous").should be(true)
|
238
|
-
end
|
239
150
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
151
|
+
it "should remove two instance scoped roles of a user" do
|
152
|
+
expect { @admin.has_no_role("moderator") }.to change{ @admin.roles.size }.by(-2)
|
153
|
+
@admin.has_role?("staff").should be(true)
|
154
|
+
@admin.has_role?("moderator", Forum.first).should be(false)
|
155
|
+
@admin.has_role?("moderator", Forum.find(2)).should be(false)
|
156
|
+
@admin.has_role?("manager", Group).should be(true)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should not remove another global role" do
|
160
|
+
expect { @admin.has_no_role("global") }.not_to change{ @admin.roles.size }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "with an instance scoped role" do
|
165
|
+
before do
|
166
|
+
@moderator = Rolify.user_cname.find(2)
|
167
|
+
@moderator.has_role "moderator", Forum.first
|
168
|
+
@moderator.has_role "soldier"
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should set an instance scoped role" do
|
172
|
+
expect { @moderator.has_role "visitor", Forum.last }.to change{ Rolify.role_cname.count }.by(1)
|
173
|
+
visitor = Rolify.role_cname.last
|
174
|
+
visitor.name.should eq("visitor")
|
175
|
+
visitor.resource.should eq(Forum.last)
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should not create another role if already existing" do
|
179
|
+
expect { @moderator.has_role "moderator", Forum.first }.not_to change{ Rolify.role_cname.count }
|
180
|
+
expect { @moderator.has_role "moderator", Forum.first }.not_to change{ @moderator.roles.size }
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should get an instance scoped role" do
|
184
|
+
@moderator.has_role?("moderator", Forum.first).should be(true)
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should get any of instance scoped role" do
|
188
|
+
@moderator.has_role?("moderator", :any).should be(true)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should not get an instance scoped role when asking for a global" do
|
192
|
+
@moderator.has_role?("moderator").should be(false)
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should not get an instance scoped role when asking for a class scoped" do
|
196
|
+
@moderator.has_role?("moderator", Forum).should be(false)
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should be able to use dynamic shortcut", :if => dynamic do
|
200
|
+
@moderator.is_moderator?.should be(false)
|
201
|
+
@moderator.is_moderator_of?(Forum.first).should be(true)
|
202
|
+
@moderator.is_moderator_of?(Forum.last).should be(false)
|
203
|
+
@moderator.is_moderator_of?(Forum).should be(false)
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should not get a global role" do
|
207
|
+
@moderator.has_role?("admin").should be(false)
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should not get the same role on another resource" do
|
211
|
+
Rolify.role_cname.create(:name => "moderator", :resource => Forum.last)
|
212
|
+
@moderator.has_role?("moderator", Forum.last).should be(false)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should not get the another role on the same resource" do
|
216
|
+
Rolify.role_cname.create(:name => "tourist", :resource => Forum.first)
|
217
|
+
@moderator.has_role?("tourist", Forum.first).should be(false)
|
218
|
+
@moderator.has_role?("tourist", :any).should be(false)
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should not get inexisting role" do
|
222
|
+
@moderator.has_role?("dummy", Forum.last).should be(false)
|
223
|
+
@moderator.has_role?("dumber").should be(false)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should check if user has all of a scoped roles set" do
|
227
|
+
@moderator.has_all_roles?({ :name => "visitor", :resource => Forum.last }).should be(true)
|
228
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
229
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
230
|
+
@moderator.has_all_roles?({ :name => "moderator", :resource => Forum.first },
|
231
|
+
{ :name => "dummy", :resource => Forum.last }).should be(false)
|
232
|
+
@moderator.has_all_roles?({ :name => "dummy", :resource => Forum.first },
|
233
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should check if user has any of a scoped roles set" do
|
237
|
+
@moderator.has_any_role?( { :name => "visitor", :resource => Forum.last }).should be(true)
|
238
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
239
|
+
{ :name => "visitor", :resource => Forum.last }).should be(true)
|
240
|
+
@moderator.has_any_role?( { :name => "moderator", :resource => Forum.first },
|
241
|
+
{ :name => "dummy", :resource => Forum.last }).should be(true)
|
242
|
+
@moderator.has_any_role?( { :name => "dummy", :resource => Forum.first },
|
243
|
+
{ :name => "dumber", :resource => Forum.last }).should be(false)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should not remove a global role of a user" do
|
247
|
+
expect { @moderator.has_no_role("soldier", Forum.first) }.not_to change{ @moderator.roles.size }
|
248
|
+
end
|
249
|
+
|
250
|
+
it "should remove a scoped role of a user" do
|
251
|
+
expect { @moderator.has_no_role("moderator", Forum.first) }.to change{ @moderator.roles.size }.by(-1)
|
252
|
+
@moderator.has_role?("moderator", Forum.first).should be(false)
|
253
|
+
@moderator.has_role?("soldier").should be(true)
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should not remove another scoped role" do
|
257
|
+
expect { @moderator.has_no_role("visitor", Forum.first) }.not_to change{ @moderator.roles.size }
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context "with a class scoped role" do
|
262
|
+
before do
|
263
|
+
@manager = Rolify.user_cname.find(3)
|
264
|
+
@manager.has_role "manager", Forum
|
265
|
+
@manager.has_role "moderator", Forum.first
|
266
|
+
@manager.has_role "moderator", Forum.last
|
267
|
+
@manager.has_role "warrior"
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should set a class scoped role" do
|
271
|
+
expect { @manager.has_role "player", Forum }.to change{ Rolify.role_cname.count }.by(1)
|
272
|
+
player = Rolify.role_cname.last
|
273
|
+
player.name.should eq("player")
|
274
|
+
player.resource_type.should eq(Forum.to_s)
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should not create another role if already existing" do
|
278
|
+
expect { @manager.has_role "manager", Forum }.not_to change{ Rolify.role_cname.count }
|
279
|
+
expect { @manager.has_role "manager", Forum }.not_to change{ @manager.roles.size }
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should get a class scoped role" do
|
283
|
+
@manager.has_role?("manager", Forum).should be(true)
|
284
|
+
@manager.has_role?("manager", Forum.first).should be(true)
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should get any of class scoped role" do
|
288
|
+
@manager.has_role?("manager", :any).should be(true)
|
289
|
+
end
|
290
|
+
|
291
|
+
it "should not get a scoped role when asking for a global" do
|
292
|
+
@manager.has_role?("manager").should be(false)
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should be able to use dynamic shortcut", :if => dynamic do
|
296
|
+
@manager.is_manager?.should be(false)
|
297
|
+
@manager.is_manager_of?(Forum.first).should be(true)
|
298
|
+
@manager.is_manager_of?(Forum.last).should be(true)
|
299
|
+
@manager.is_manager_of?(Forum).should be(true)
|
300
|
+
@manager.is_manager_of?(Group).should be(false)
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should not get a global role" do
|
304
|
+
@manager.has_role?("admin").should be(false)
|
305
|
+
end
|
306
|
+
|
307
|
+
it "should not get the same role on another resource" do
|
308
|
+
Rolify.role_cname.create(:name => "manager", :resource_type => "Group")
|
309
|
+
@manager.has_role?("manager", Group).should be(false)
|
310
|
+
end
|
311
|
+
|
312
|
+
it "should not get the another role on the same resource" do
|
313
|
+
Rolify.role_cname.create(:name => "member", :resource_type => "Forum")
|
314
|
+
@manager.has_role?("member", Forum).should be(false)
|
315
|
+
@manager.has_role?("member", :any).should be(false)
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should not get inexisting role" do
|
319
|
+
@manager.has_role?("dummy", Forum).should be(false)
|
320
|
+
@manager.has_role?("dumber").should be(false)
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should check if user has all of a scoped roles set" do
|
324
|
+
@manager.has_all_roles?({ :name => "player", :resource => Forum }).should be(true)
|
325
|
+
@manager.has_all_roles?({ :name => "manager", :resource => Forum },
|
326
|
+
{ :name => "player", :resource => Forum }).should be(true)
|
327
|
+
@manager.has_all_roles?({ :name => "manager", :resource => Forum },
|
328
|
+
{ :name => "dummy", :resource => Forum }).should be(false)
|
329
|
+
@manager.has_all_roles?({ :name => "dummy", :resource => Forum },
|
330
|
+
{ :name => "dumber", :resource => Group }).should be(false)
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should check if user has any of a scoped roles set" do
|
334
|
+
@manager.has_any_role?( { :name => "player", :resource => Forum }).should be(true)
|
335
|
+
@manager.has_any_role?( { :name => "manager", :resource => Forum },
|
336
|
+
{ :name => "player", :resource => Forum }).should be(true)
|
337
|
+
@manager.has_any_role?( { :name => "manager", :resource => Forum },
|
338
|
+
{ :name => "dummy", :resource => Forum }).should be(true)
|
339
|
+
@manager.has_any_role?( { :name => "dummy", :resource => Forum },
|
340
|
+
{ :name => "dumber", :resource => Group }).should be(false)
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should not remove a global role of a user" do
|
344
|
+
expect { @manager.has_no_role("warrior", Forum) }.not_to change{ @manager.roles.size }
|
345
|
+
end
|
346
|
+
|
347
|
+
it "should remove a class scoped role of a user" do
|
348
|
+
expect { @manager.has_no_role("manager", Forum) }.to change{ @manager.roles.size }.by(-1)
|
349
|
+
@manager.has_role?("manager", Forum).should be(false)
|
350
|
+
@manager.has_role?("moderator", Forum.first).should be(true)
|
351
|
+
@manager.has_role?("moderator", Forum.last).should be(true)
|
352
|
+
@manager.has_role?("warrior").should be(true)
|
353
|
+
end
|
244
354
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
355
|
+
it "should remove two instance scoped roles of a user" do
|
356
|
+
expect { @manager.has_no_role("moderator", Forum) }.to change{ @manager.roles.size }.by(-2)
|
357
|
+
@manager.has_role?("manager", Forum).should be(true)
|
358
|
+
@manager.has_role?("moderator", Forum.first).should be(false)
|
359
|
+
@manager.has_role?("moderator", Forum.last).should be(false)
|
360
|
+
@manager.has_role?("warrior").should be(true)
|
361
|
+
end
|
362
|
+
|
363
|
+
it "should not remove another scoped role" do
|
364
|
+
expect { @manager.has_no_role("visitor", Forum.first) }.not_to change{ @manager.roles.size }
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
context "with different roles" do
|
369
|
+
before do
|
370
|
+
@user = Rolify.user_cname.find(4)
|
371
|
+
@user.has_role "admin"
|
372
|
+
@user.has_role "anonymous"
|
373
|
+
@user.has_role "moderator", Forum.first
|
374
|
+
@user.has_role "visitor", Forum.last
|
375
|
+
@user.has_role "manager", Forum
|
376
|
+
@user.has_role "leader", Group
|
377
|
+
end
|
378
|
+
|
379
|
+
it "should get a global role" do
|
380
|
+
@user.has_role?("admin").should be(true)
|
381
|
+
@user.has_role?("anonymous").should be(true)
|
258
382
|
end
|
259
383
|
|
384
|
+
it "should get an instance scoped role" do
|
385
|
+
@user.has_role?("moderator", Forum.first).should be(true)
|
386
|
+
@user.has_role?("visitor", Forum.last).should be(true)
|
387
|
+
end
|
388
|
+
|
389
|
+
it "should get an class scoped role" do
|
390
|
+
@user.has_role?("manager", Forum).should be(true)
|
391
|
+
@user.has_role?("leader", Group).should be(true)
|
392
|
+
end
|
393
|
+
|
394
|
+
it "should check if user has all of a mix of global and scoped roles set" do
|
395
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Forum }).should be(true)
|
396
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Forum }).should be(false)
|
397
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Group }).should be(false)
|
398
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Group.first }).should be(false)
|
399
|
+
@user.has_all_roles?({ :name => "admin", :resource => Forum }, { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Forum }).should be(true)
|
400
|
+
@user.has_all_roles?({ :name => "admin", :resource => Forum.first }, { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Forum }).should be(true)
|
401
|
+
@user.has_all_roles?("admin", { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Forum.first }).should be(true)
|
402
|
+
@user.has_all_roles?("dummy", { :name => "dumber", :resource => Forum.last }, { :name => "dumberer", :resource => Forum }).should be(false)
|
403
|
+
end
|
404
|
+
|
405
|
+
it "should check if user has any of a mix of global and scoped roles set" do
|
406
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.first }, { :name => "manager", :resource => Forum }).should be(true)
|
407
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Forum }).should be(true)
|
408
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Group }).should be(true)
|
409
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Group.first }).should be(true)
|
410
|
+
@user.has_any_role?({ :name => "admin", :resource => Forum }, { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Forum }).should be(true)
|
411
|
+
@user.has_any_role?({ :name => "admin", :resource => Forum.first }, { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Forum }).should be(true)
|
412
|
+
@user.has_any_role?("admin", { :name => "moderator", :resource => Forum.last }, { :name => "manager", :resource => Forum.first }).should be(true)
|
413
|
+
@user.has_any_role?("dummy", { :name => "dumber", :resource => Forum.last }, { :name => "dumberer", :resource => Forum }).should be(false)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
260
417
|
end
|
261
418
|
|
262
419
|
describe Rolify do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,8 +2,9 @@ require 'rubygems'
|
|
2
2
|
require 'bundler/setup'
|
3
3
|
|
4
4
|
Bundler.require(:default, :test)
|
5
|
-
|
6
5
|
require 'rolify'
|
6
|
+
require 'ammeter/init'
|
7
|
+
|
7
8
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
9
|
|
9
10
|
load File.dirname(__FILE__) + '/support/schema.rb'
|
data/spec/support/data.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
+
# Users
|
1
2
|
User.create(:login => "admin")
|
2
3
|
User.create(:login => "moderator")
|
3
4
|
User.create(:login => "god")
|
5
|
+
User.create(:login => "zombie")
|
6
|
+
Customer.create(:login => "customer VIP")
|
7
|
+
Customer.create(:login => "customer Doe")
|
8
|
+
Customer.create(:login => "customer John")
|
9
|
+
Customer.create(:login => "customer Edgar")
|
10
|
+
|
11
|
+
# Resources
|
4
12
|
Forum.create(:name => "forum 1")
|
5
13
|
Forum.create(:name => "forum 2")
|
6
14
|
Forum.create(:name => "forum 3")
|
7
|
-
|
8
|
-
|
9
|
-
Customer.create(:login => "customer Doe")
|
15
|
+
Group.create(:name => "group 1")
|
16
|
+
Group.create(:name => "group 2")
|
data/spec/support/models.rb
CHANGED
@@ -12,6 +12,9 @@ end
|
|
12
12
|
class Forum < ActiveRecord::Base
|
13
13
|
end
|
14
14
|
|
15
|
+
class Group < ActiveRecord::Base
|
16
|
+
end
|
17
|
+
|
15
18
|
class Customer < ActiveRecord::Base
|
16
19
|
has_and_belongs_to_many :roles, :join_table => :customers_privileges, :class_name => "Privilege"
|
17
20
|
include Rolify::Roles
|
data/spec/support/schema.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rolify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,23 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-10-14 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
13
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
requirement: &2152413060 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.1.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2152413060
|
14
26
|
- !ruby/object:Gem::Dependency
|
15
27
|
name: sqlite3
|
16
|
-
requirement: &
|
28
|
+
requirement: &2152483900 !ruby/object:Gem::Requirement
|
17
29
|
none: false
|
18
30
|
requirements:
|
19
31
|
- - ! '>='
|
@@ -21,18 +33,51 @@ dependencies:
|
|
21
33
|
version: '0'
|
22
34
|
type: :development
|
23
35
|
prerelease: false
|
24
|
-
version_requirements: *
|
36
|
+
version_requirements: *2152483900
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
38
|
+
name: ammeter
|
39
|
+
requirement: &2152578220 !ruby/object:Gem::Requirement
|
28
40
|
none: false
|
29
41
|
requirements:
|
30
|
-
- -
|
42
|
+
- - ! '>='
|
31
43
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
-
type: :
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2152578220
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: &2160177360 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
34
57
|
prerelease: false
|
35
|
-
version_requirements: *
|
58
|
+
version_requirements: *2160177360
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: &2160394640 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *2160394640
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: bundler
|
72
|
+
requirement: &2160528120 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *2160528120
|
36
81
|
description: ! 'Very simple Roles library without any authorization enforcement (built
|
37
82
|
to use with cancan) supporting scope on resource: user.is_moderator?(Forum.first)
|
38
83
|
=> # return false if user is moderator of another Forum '
|
@@ -57,12 +102,13 @@ files:
|
|
57
102
|
- lib/rolify/role.rb
|
58
103
|
- lib/rolify/version.rb
|
59
104
|
- rolify.gemspec
|
105
|
+
- spec/generators/rolify/role/role_generator_spec.rb
|
60
106
|
- spec/rolify/role_spec.rb
|
61
|
-
- spec/spec.opts
|
62
107
|
- spec/spec_helper.rb
|
63
108
|
- spec/support/data.rb
|
64
109
|
- spec/support/models.rb
|
65
110
|
- spec/support/schema.rb
|
111
|
+
has_rdoc: true
|
66
112
|
homepage: https://github.com/EppO/rolify
|
67
113
|
licenses: []
|
68
114
|
post_install_message:
|
@@ -83,8 +129,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
129
|
version: '0'
|
84
130
|
requirements: []
|
85
131
|
rubyforge_project: rolify
|
86
|
-
rubygems_version: 1.
|
132
|
+
rubygems_version: 1.6.2
|
87
133
|
signing_key:
|
88
134
|
specification_version: 3
|
89
135
|
summary: Roles library with resource scoping
|
90
|
-
test_files:
|
136
|
+
test_files:
|
137
|
+
- spec/generators/rolify/role/role_generator_spec.rb
|
138
|
+
- spec/rolify/role_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/support/data.rb
|
141
|
+
- spec/support/models.rb
|
142
|
+
- spec/support/schema.rb
|
data/spec/spec.opts
DELETED