rolify 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,18 @@
1
- # 0.3
1
+ = v0.4
2
+ * removing role support
3
+ * <tt>has_no_role</tt> removes a global role or a role scoped to a resource
4
+ * 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
5
+ * Trying to remove a role scoped to a resource whereas the user has a global role won't remove it
6
+
7
+ = v0.3
2
8
  * 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
9
+ * <tt>has_all_roles?</tt> returns true if the user has ALL the roles in arguments
10
+ * <tt>has_any_role?</tt> returns true if the user has ANY the roles in arguments
5
11
 
6
- # 0.2
12
+ = v0.2
7
13
  * fixed the generator to include the lib
8
14
  * fixed the migration file with missing polymorphic field
9
15
  * added some examples in documentation
10
16
 
11
- # 0.1
12
- * first release
17
+ = v0.1
18
+ * first release
@@ -82,6 +82,8 @@ A global role overrides resource role request:
82
82
  user.has_role? "moderator", Forum.last
83
83
  => true
84
84
 
85
+ Please see on {the wiki}[https://github.com/EppO/rolify/wiki/Usage] for all the available commands
86
+
85
87
  == Questions or Problems?
86
88
 
87
89
  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.
@@ -1,13 +1,13 @@
1
1
  module Rolify
2
- def has_role(role, resource = nil)
3
- role = Role.find_or_create_by_name_and_resource_type_and_resource_id( :name => role,
2
+ def has_role(role_name, resource = nil)
3
+ role = Role.find_or_create_by_name_and_resource_type_and_resource_id( :name => role_name,
4
4
  :resource_type => (resource.class.name if resource),
5
5
  :resource_id => (resource.id if resource))
6
6
  self.roles << role if !roles.include?(role)
7
7
  end
8
8
 
9
- def has_role?(role, resource = nil)
10
- query, values = build_query(role, resource)
9
+ def has_role?(role_name, resource = nil)
10
+ query, values = build_query(role_name, resource)
11
11
  self.roles.where(*query, *values).size > 0
12
12
  end
13
13
 
@@ -43,6 +43,13 @@ module Rolify
43
43
  self.roles.where([ conditions.join(' OR '), *values ]).size > 0
44
44
  end
45
45
 
46
+ def has_no_role(role_name, resource = nil)
47
+ role = Role.where( :name => role_name)
48
+ role = role.where( :resource_type => resource.class.name,
49
+ :resource_id => resource.id) if resource
50
+ self.roles.delete(role) if role
51
+ end
52
+
46
53
  def roles_name
47
54
  self.roles.select(:name).map { |r| r.name }
48
55
  end
@@ -1,3 +1,3 @@
1
1
  module Rolify
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Florent Monbillard"]
10
10
  s.email = ["f.monbillard@gmail.com"]
11
11
  s.homepage = "https://github.com/EppO/rolify"
12
- s.summary = %q{Roles library with object scoping}
13
- s.description = %q{Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum }
12
+ s.summary = %q{Roles library with resource scoping}
13
+ s.description = %q{Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on resource: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum }
14
14
 
15
15
  s.rubyforge_project = s.name
16
16
 
@@ -21,4 +21,4 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_development_dependency "sqlite3"
23
23
  s.add_dependency "activerecord", "~> 3.1.0.rc1"
24
- end
24
+ end
@@ -25,6 +25,11 @@ describe Rolify do
25
25
  @admin.should respond_to(:has_any_role?)
26
26
  @admin.should respond_to(:has_any_role?)
27
27
  end
28
+
29
+ it "should respond to has_no_role method" do
30
+ @admin.should respond_to(:has_no_role).with(1).arguments
31
+ @admin.should respond_to(:has_no_role).with(2).arguments
32
+ end
28
33
  end
29
34
 
30
35
  context "with a global role" do
@@ -32,6 +37,7 @@ describe Rolify do
32
37
  @admin = User.first
33
38
  @admin.has_role "admin"
34
39
  @admin.has_role "staff"
40
+ @admin.has_role "moderator", Forum.first
35
41
  end
36
42
 
37
43
  it "should set a global role" do
@@ -80,12 +86,30 @@ describe Rolify do
80
86
  @admin.has_any_role?("admin", "moderator").should be(true)
81
87
  @admin.has_any_role?("dummy", "dumber").should be(false)
82
88
  end
89
+
90
+ it "should remove a global role of a user" do
91
+ expect { @admin.has_no_role("admin") }.to change{ @admin.roles.size }.by(-1)
92
+ @admin.has_role?("admin").should be(false)
93
+ @admin.has_role?("staff").should be(true)
94
+ @admin.has_role?("moderator", Forum.first).should be(true)
95
+ end
96
+
97
+ it "should remove a scoped role of a user" do
98
+ expect { @admin.has_no_role("moderator") }.to change{ @admin.roles.size }.by(-1)
99
+ @admin.has_role?("staff").should be(true)
100
+ @admin.has_role?("moderator", Forum.first).should be(false)
101
+ end
102
+
103
+ it "should not remove a another global role" do
104
+ expect { @admin.has_no_role("global") }.not_to change{ @admin.roles.size }
105
+ end
83
106
  end
84
107
 
85
108
  context "with a scoped role" do
86
109
  before(:all) do
87
110
  @moderator = User.find(2)
88
111
  @moderator.has_role "moderator", Forum.first
112
+ @moderator.has_role "soldier"
89
113
  end
90
114
 
91
115
  it "should set a scoped role" do
@@ -140,6 +164,20 @@ describe Rolify do
140
164
  @moderator.has_any_role?( { :name => "dummy", :resource => Forum.first },
141
165
  { :name => "dumber", :resource => Forum.last }).should be(false)
142
166
  end
167
+
168
+ it "should not remove a global role of a user" do
169
+ expect { @moderator.has_no_role("soldier", Forum.first) }.not_to change{ @moderator.roles.size }
170
+ end
171
+
172
+ it "should remove a scoped role of a user" do
173
+ expect { @moderator.has_no_role("moderator", Forum.first) }.to change{ @moderator.roles.size }.by(-1)
174
+ @moderator.has_role?("moderator", Forum.first).should be(false)
175
+ @moderator.has_role?("soldier").should be(true)
176
+ end
177
+
178
+ it "should not remove another scoped role" do
179
+ expect { @moderator.has_no_role("visitor", Forum.first) }.not_to change{ @moderator.roles.size }
180
+ end
143
181
  end
144
182
 
145
183
  context "with different roles" do
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rolify
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florent Monbillard
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-07 00:00:00 -04:00
14
- default_executable:
13
+ date: 2011-06-07 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: sqlite3
@@ -35,7 +34,7 @@ dependencies:
35
34
  version: 3.1.0.rc1
36
35
  type: :runtime
37
36
  version_requirements: *id002
38
- description: "Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum "
37
+ description: "Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on resource: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum "
39
38
  email:
40
39
  - f.monbillard@gmail.com
41
40
  executables: []
@@ -64,7 +63,6 @@ files:
64
63
  - spec/support/data.rb
65
64
  - spec/support/models.rb
66
65
  - spec/support/schema.rb
67
- has_rdoc: true
68
66
  homepage: https://github.com/EppO/rolify
69
67
  licenses: []
70
68
 
@@ -88,14 +86,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  requirements: []
89
87
 
90
88
  rubyforge_project: rolify
91
- rubygems_version: 1.6.2
89
+ rubygems_version: 1.7.2
92
90
  signing_key:
93
91
  specification_version: 3
94
- summary: Roles library with object scoping
95
- test_files:
96
- - spec/rolify/role_spec.rb
97
- - spec/spec.opts
98
- - spec/spec_helper.rb
99
- - spec/support/data.rb
100
- - spec/support/models.rb
101
- - spec/support/schema.rb
92
+ summary: Roles library with resource scoping
93
+ test_files: []
94
+