rolify 4.1.1 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/activerecord.yml +27 -0
- data/.github/workflows/mongoid.yml +32 -0
- data/.hakiri.yml +1 -0
- data/Appraisals +42 -0
- data/CHANGELOG.rdoc +52 -8
- data/CONTRIBUTORS +11 -0
- data/Gemfile +6 -14
- data/LICENSE +1 -1
- data/README.md +72 -26
- data/Rakefile +6 -1
- data/gemfiles/activerecord_4.gemfile +20 -0
- data/gemfiles/activerecord_5.gemfile +22 -0
- data/gemfiles/activerecord_6.gemfile +22 -0
- data/gemfiles/mongoid_5.gemfile +20 -0
- data/gemfiles/mongoid_6.gemfile +19 -0
- data/gemfiles/mongoid_7.gemfile +20 -0
- data/lib/generators/active_record/rolify_generator.rb +45 -11
- data/lib/generators/active_record/templates/migration.rb +2 -3
- data/lib/generators/active_record/templates/model.rb +15 -0
- data/lib/generators/rolify/templates/initializer.rb +4 -1
- data/lib/rolify.rb +3 -4
- data/lib/rolify/adapters/active_record/resource_adapter.rb +14 -8
- data/lib/rolify/adapters/active_record/role_adapter.rb +40 -17
- data/lib/rolify/adapters/mongoid/role_adapter.rb +38 -8
- data/lib/rolify/configure.rb +4 -2
- data/lib/rolify/dynamic.rb +2 -16
- data/lib/rolify/finders.rb +6 -1
- data/lib/rolify/matchers.rb +2 -2
- data/lib/rolify/resource.rb +1 -1
- data/lib/rolify/role.rb +11 -2
- data/lib/rolify/version.rb +1 -1
- data/rolify.gemspec +15 -7
- data/spec/common_helper.rb +16 -0
- data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +112 -6
- data/spec/generators_helper.rb +9 -2
- data/spec/rolify/config_spec.rb +2 -0
- data/spec/rolify/custom_spec.rb +1 -1
- data/spec/rolify/resource_spec.rb +6 -0
- data/spec/rolify/shared_examples/shared_examples_for_finders.rb +50 -32
- data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +65 -0
- data/spec/rolify/utils_spec.rb +19 -0
- data/spec/spec_helper.rb +19 -6
- data/spec/support/adapters/active_record.rb +4 -3
- data/spec/support/adapters/mongoid.rb +19 -2
- data/spec/support/adapters/{mongoid.yml → mongoid_5.yml} +2 -2
- data/spec/support/adapters/mongoid_6.yml +6 -0
- data/spec/support/adapters/mongoid_7.yml +6 -0
- data/spec/support/adapters/utils/active_record.rb +12 -0
- data/spec/support/adapters/utils/mongoid.rb +13 -0
- metadata +60 -26
- data/.travis.yml +0 -25
- data/gemfiles/Gemfile.rails-3.2 +0 -27
- data/gemfiles/Gemfile.rails-4.0 +0 -33
- data/gemfiles/Gemfile.rails-4.1 +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39183b0e66a9dfdbe636059ec28145106f14347129676c334598ff4f330edfc0
|
4
|
+
data.tar.gz: 686c5b98efcc79e84b104cca586053b920032cbc0bf7978baa0b1caa04e021bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a24f0ef337c8c5d1b9381e30020ba18c790193f4b00085cbd2581f25d6322d5ffeee099a22a43b4b4c86de66a2774364f799426ae71ed331027d5e565a06be72
|
7
|
+
data.tar.gz: b1d55761a7a5df75a9c1f462ca04232c44e81adcc180ece7232386f0c6f4dff0441cc463e674075c5906fc3c3ddcc71922dd20cf2129eecff099fa604db56b94
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: activerecord
|
2
|
+
on: [push]
|
3
|
+
jobs:
|
4
|
+
ubuntu:
|
5
|
+
runs-on: ubuntu-latest
|
6
|
+
continue-on-error: true
|
7
|
+
strategy:
|
8
|
+
matrix:
|
9
|
+
gemfile: [activerecord_4, activerecord_5, activerecord_6]
|
10
|
+
ruby: [2.5.7, 2.6.5, 2.7.2]
|
11
|
+
env:
|
12
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
13
|
+
ADAPTER: active_record
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Setup ruby
|
17
|
+
uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby }}
|
20
|
+
- run: sudo apt-get install -y libsqlite3-dev
|
21
|
+
- run: gem update --system
|
22
|
+
- run: gem install bundler
|
23
|
+
- run: gem --version
|
24
|
+
- run: bundle install
|
25
|
+
- name: Run Tests
|
26
|
+
run: |
|
27
|
+
bundle exec rake
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: mongoid
|
2
|
+
on: [push]
|
3
|
+
jobs:
|
4
|
+
ubuntu:
|
5
|
+
runs-on: ubuntu-latest
|
6
|
+
continue-on-error: true
|
7
|
+
services:
|
8
|
+
mongodb:
|
9
|
+
image: mongo:3.4.23
|
10
|
+
ports:
|
11
|
+
- 27017:27017
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
gemfile: [mongoid_5]
|
15
|
+
ruby: [2.5.7, 2.6.5, 2.7.2]
|
16
|
+
env:
|
17
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
18
|
+
ADAPTER: mongoid
|
19
|
+
steps:
|
20
|
+
- uses: actions/checkout@v2
|
21
|
+
- name: Setup ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
25
|
+
- run: sudo apt-get install -y libsqlite3-dev
|
26
|
+
- run: gem update --system
|
27
|
+
- run: gem install bundler
|
28
|
+
- run: gem --version
|
29
|
+
- run: bundle install
|
30
|
+
- name: Run Tests
|
31
|
+
run: |
|
32
|
+
bundle exec rake
|
data/.hakiri.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
dependency_source: gemspec_file
|
data/Appraisals
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
appraise 'mongoid-5' do
|
2
|
+
gem "mongoid", "~> 5"
|
3
|
+
gem "bson_ext", "1.5.1"
|
4
|
+
gem 'bigdecimal', '1.4.2'
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise 'mongoid-6' do
|
8
|
+
gem "mongoid", "~> 6"
|
9
|
+
gem "bson_ext", "1.5.1"
|
10
|
+
end
|
11
|
+
|
12
|
+
appraise 'mongoid-7' do
|
13
|
+
gem "mongoid", "~> 7"
|
14
|
+
gem "bson_ext", "1.5.1"
|
15
|
+
gem "railties", "5.2.4.1"
|
16
|
+
end
|
17
|
+
|
18
|
+
appraise 'activerecord-4' do
|
19
|
+
gem "sqlite3", "~> 1.3.6"
|
20
|
+
gem "activerecord", "~> 4.2.11", :require => "active_record"
|
21
|
+
gem 'bigdecimal', '1.4.2'
|
22
|
+
end
|
23
|
+
|
24
|
+
appraise 'activerecord-5' do
|
25
|
+
gem "sqlite3", "~> 1.3.6"
|
26
|
+
gem "activerecord", "~> 5.2.4", :require => "active_record"
|
27
|
+
|
28
|
+
# Ammeter dependencies:
|
29
|
+
gem "actionpack", "~> 5.2.4"
|
30
|
+
gem "activemodel", "~> 5.2.4"
|
31
|
+
gem "railties", "~> 5.2.4"
|
32
|
+
end
|
33
|
+
|
34
|
+
appraise 'activerecord-6' do
|
35
|
+
gem "sqlite3", "~> 1.4", :platform => "ruby"
|
36
|
+
gem "activerecord", ">= 6.0.0", :require => "active_record"
|
37
|
+
|
38
|
+
# Ammeter dependencies:
|
39
|
+
gem "actionpack", ">= 6.0.0"
|
40
|
+
gem "activemodel", ">= 6.0.0"
|
41
|
+
gem "railties", ">= 6.0.0"
|
42
|
+
end
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,47 @@
|
|
1
|
+
= 6.0.0 (April 2, 2021)
|
2
|
+
|
3
|
+
Breaking changes:
|
4
|
+
|
5
|
+
* Support strict <tt>with_role</tt> queries. Note this changes behaviour of with_role when running in strict mode to behave as previously documented. #543 describes the behaviour change.
|
6
|
+
* Dropped support for Ruby 2.3 and 2.4
|
7
|
+
|
8
|
+
Improvements:
|
9
|
+
|
10
|
+
* Updated ActiveRecord adapters implementation of <tt>in</tt> to utilise a subquery instead of two queries
|
11
|
+
|
12
|
+
= 5.3.0 (June 1, 2020)
|
13
|
+
|
14
|
+
5.3.0 will be the last version to support Ruby < 2.5 and Rails < 5.2. Support for these will be dropped in the next major release.
|
15
|
+
|
16
|
+
* Fix deprecation warning in Ruby 2.7
|
17
|
+
* Add Rails 6 support to migration generator
|
18
|
+
* Significant ActiveRecord performance improvements to <tt>add_role</tt> and <tt>without_role</tt>
|
19
|
+
* Mongoid fix and performance improvement and to <tt>roles_name</tt>
|
20
|
+
* Make it safe to call <tt>Thing.with_role(:admin, user)</tt> with new record
|
21
|
+
|
22
|
+
= 5.2.0 (Dec 14, 2017)
|
23
|
+
* Fix regression in generator around belongs_to options compatibility
|
24
|
+
* Update version of database_cleaner
|
25
|
+
* Update initializer.rb to include the remove_role_if_empty option and description
|
26
|
+
* Allow inverse_of option on rolify method
|
27
|
+
* Fix the code-climate-reporter issue that's causing travis to fail
|
28
|
+
* Remove a separate index for the name column
|
29
|
+
* Fix migration generator for AR 5.x
|
30
|
+
* Fixed ambiguous column error
|
31
|
+
|
32
|
+
= 5.1.0 (Mar 19, 2016)
|
33
|
+
* Rails 5 Support (thanks @lorefnon)
|
34
|
+
* Fix user handling in generator (thanks @lorefnon)
|
35
|
+
* Fix quoting issues (thanks @lorefnon)
|
36
|
+
* Improvements to dynamic module loading (thanks to @DmitryKK)
|
37
|
+
|
38
|
+
= 5.0.0 (Nov 17, 2015)
|
39
|
+
* Fix migration warning showing when it shouldn't
|
40
|
+
* Add role lazy-loading
|
41
|
+
* Add has_cached_role and has_cached_strict_role to project.
|
42
|
+
See: https://github.com/RolifyCommunity/rolify#cached-roles-to-avoid-n1-issue
|
43
|
+
* Update specs to pass with all Mongoid version.
|
44
|
+
|
1
45
|
= 4.1.1 (Aug 06, 2015)
|
2
46
|
* Fix a regression in the dynamic method loader.
|
3
47
|
|
@@ -103,7 +147,7 @@
|
|
103
147
|
* fixed a backward incompatible change introduced in Rails 3.2 release (<tt>find_or_create_by_* generated methods</tt>)
|
104
148
|
|
105
149
|
= 2.2 (Jan 18, 2012)
|
106
|
-
* fixed a bug in the initializer file regarding dynamic shortcuts
|
150
|
+
* fixed a bug in the initializer file regarding dynamic shortcuts
|
107
151
|
|
108
152
|
= 2.1 (Nov 30, 2011)
|
109
153
|
* added syntactic sugar: <tt>grant</tt> and <tt>revoke</tt> are aliases for <tt>has_role</tt> and <tt>has_no_role</tt>
|
@@ -144,8 +188,8 @@
|
|
144
188
|
|
145
189
|
= 1.0 (Aug 25, 2011)
|
146
190
|
* added a new parameter to disable dynamic shortcut methods due to potential incompatibility with other gems using method_missing with the same pattern
|
147
|
-
* add <tt>Rolify.dynamic_shortcuts = false</tt> in the initializer file or
|
148
|
-
* use the generator command with a third parameter:
|
191
|
+
* add <tt>Rolify.dynamic_shortcuts = false</tt> in the initializer file or
|
192
|
+
* use the generator command with a third parameter:
|
149
193
|
* <tt>rails g rolify:role Role User false</tt>
|
150
194
|
* removed the railtie as it created more problems than it solved
|
151
195
|
* code refactoring to do some speed improvements and code clean up
|
@@ -154,16 +198,16 @@
|
|
154
198
|
* rolify is now on travis-ci to monitor build status
|
155
199
|
|
156
200
|
= 0.7 (June 20, 2011)
|
157
|
-
* 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)
|
158
|
-
* dynamic shortcut is created on the fly in the method_missing to avoid extra method_missing for the same dynamic shortcut
|
159
|
-
* check if the role actually exists in the database before defining the new method
|
201
|
+
* 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)
|
202
|
+
* dynamic shortcut is created on the fly in the method_missing to avoid extra method_missing for the same dynamic shortcut
|
203
|
+
* check if the role actually exists in the database before defining the new method
|
160
204
|
* first call is slower due to method_missing but next calls are fast
|
161
205
|
* 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
|
162
206
|
|
163
207
|
= 0.6 (June 19, 2011)
|
164
208
|
* custom User and Role class names support
|
165
209
|
* can now use other class names for Role and User classes
|
166
|
-
* fixed generators and templates
|
210
|
+
* fixed generators and templates
|
167
211
|
* join table is explicitly set to avoid alphabetical order issue
|
168
212
|
* created a new railtie to load the dynamic shortcuts at startup
|
169
213
|
|
@@ -185,7 +229,7 @@
|
|
185
229
|
* Trying to remove a role scoped to a resource whereas the user has a global role won't remove it
|
186
230
|
|
187
231
|
= v0.3 (June 06, 2011)
|
188
|
-
* multiple roles check:
|
232
|
+
* multiple roles check:
|
189
233
|
* <tt>has_all_roles?</tt> returns true if the user has ALL the roles in arguments
|
190
234
|
* <tt>has_any_role?</tt> returns true if the user has ANY the roles in arguments
|
191
235
|
|
data/CONTRIBUTORS
CHANGED
@@ -4,3 +4,14 @@ Joel Azemar (@joel)
|
|
4
4
|
Alex Klim (@AlexKlim)
|
5
5
|
Mauro George (@MauroGeorge)
|
6
6
|
Cheri Allen(@cherimarie)
|
7
|
+
Gaurab Paul (@lorefnon)
|
8
|
+
Dmitry Krakosevich (@DmitryKK)
|
9
|
+
Michael Watts (@mikwat)
|
10
|
+
Derek Ethier (@ethier)
|
11
|
+
Michael (@mibamur)
|
12
|
+
Undo1 (@Undo1)
|
13
|
+
Hitabis Engineering (@Hitabis)
|
14
|
+
Sergey Alekseev (@sergey-alekseev)
|
15
|
+
Sankalp Kulshreshtha (@sankalpk)
|
16
|
+
Kirill Sevastyanenko (@kirillseva)
|
17
|
+
Denis Shevchenko (@SimplySorc)
|
data/Gemfile
CHANGED
@@ -1,23 +1,15 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
|
3
3
|
group :test do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
gem "sqlite3", :platform => "ruby"
|
8
|
-
gem "activerecord", ">= 3.2.0", :require => "active_record"
|
9
|
-
when "mongoid"
|
10
|
-
gem "mongoid", ">= 3.1"
|
11
|
-
gem "bson_ext", :platform => "ruby"
|
12
|
-
else
|
13
|
-
raise "Unknown model adapter: #{ENV["ADAPTER"]}"
|
14
|
-
end
|
15
|
-
|
16
|
-
gem 'coveralls', :require => false
|
4
|
+
gem 'codeclimate-test-reporter', require: nil
|
5
|
+
gem 'coveralls', require: false
|
6
|
+
gem 'database_cleaner', '~> 1.6.2'
|
17
7
|
gem 'its'
|
8
|
+
gem 'test-unit' # Implicitly loaded by ammeter
|
9
|
+
|
18
10
|
gem 'byebug'
|
11
|
+
gem 'pry'
|
19
12
|
gem 'pry-byebug'
|
20
|
-
gem 'codeclimate-test-reporter', :require => nil
|
21
13
|
end
|
22
14
|
|
23
15
|
gemspec
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
# rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://
|
1
|
+
# rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://travis-ci.org/RolifyCommunity/rolify.svg)](http://travis-ci.org/RolifyCommunity/rolify) [![Code Climate](https://codeclimate.com/github/RolifyCommunity/rolify.svg)](https://codeclimate.com/github/RolifyCommunity/rolify) [![Coverage Status](https://coveralls.io/repos/RolifyCommunity/rolify/badge.svg?branch=master&service=github)](https://coveralls.io/github/RolifyCommunity/rolify?branch=master)
|
2
2
|
|
3
3
|
Very simple Roles library without any authorization enforcement supporting scope on resource object.
|
4
4
|
|
5
5
|
Let's see an example:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
user.has_role?(:moderator,
|
8
|
+
user.has_role?(:moderator, @forum)
|
9
9
|
=> false # if user is moderator of another Forum
|
10
10
|
```
|
11
11
|
|
12
|
-
This library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem<span style="color: red"><strong>*</strong></span> ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority))
|
12
|
+
This library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem<span style="color: red"><strong>*</strong></span> ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority), [Pundit](https://github.com/elabs/pundit))
|
13
13
|
|
14
14
|
<span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class
|
15
15
|
|
16
16
|
## Requirements
|
17
17
|
|
18
|
-
* Rails >=
|
19
|
-
* ActiveRecord >=
|
20
|
-
* supports ruby 2.
|
18
|
+
* Rails >= 4.2
|
19
|
+
* ActiveRecord >= 4.2 <b>or</b> Mongoid >= 4.0
|
20
|
+
* supports ruby 2.2+, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)
|
21
21
|
* support of ruby 1.8 has been dropped due to Mongoid >=3.0 that only supports 1.9 new hash syntax
|
22
22
|
|
23
23
|
## Installation
|
24
24
|
|
25
|
-
Add this to your Gemfile and run the
|
25
|
+
Add this to your Gemfile and run the `bundle` command.
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
gem "rolify"
|
@@ -32,7 +32,9 @@ gem "rolify"
|
|
32
32
|
|
33
33
|
### 1. Generate Role Model
|
34
34
|
|
35
|
-
First,
|
35
|
+
First, use the generator to setup Rolify. Role and User class are the default names. However, you can specify any class name you want. For the User class name, you would probably use the one provided by your authentication solution.
|
36
|
+
|
37
|
+
If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3.
|
36
38
|
|
37
39
|
```
|
38
40
|
rails g rolify Role User
|
@@ -44,14 +46,11 @@ rails g rolify Role User
|
|
44
46
|
rails g rolify:role Role User
|
45
47
|
```
|
46
48
|
|
47
|
-
|
48
|
-
For the User class name, you would probably use the one provided by your authentication solution. rolify just adds some class methods in an existing User class.
|
49
|
-
|
50
|
-
If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
|
49
|
+
The generator will create your Role model, add a migration file, and update your User class with new class methods.
|
51
50
|
|
52
51
|
### 2. Run the migration (only required when using ActiveRecord)
|
53
52
|
|
54
|
-
Let's migrate
|
53
|
+
Let's migrate!
|
55
54
|
|
56
55
|
```
|
57
56
|
rake db:migrate
|
@@ -80,6 +79,8 @@ The `rolify` method accepts the following callback options:
|
|
80
79
|
|
81
80
|
Mongoid callbacks are also supported and works the same way.
|
82
81
|
|
82
|
+
The `rolify` method also accepts the `inverse_of` option if you need to disambiguate the relationship.
|
83
|
+
|
83
84
|
### 3.2 Configure your resource models
|
84
85
|
|
85
86
|
In the resource models you want to apply roles on, just add ``resourcify`` method.
|
@@ -91,6 +92,18 @@ class Forum < ActiveRecord::Base
|
|
91
92
|
end
|
92
93
|
```
|
93
94
|
|
95
|
+
### 3.3 Assign default role
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class User < ActiveRecord::Base
|
99
|
+
after_create :assign_default_role
|
100
|
+
|
101
|
+
def assign_default_role
|
102
|
+
self.add_role(:newuser) if self.roles.blank?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
94
107
|
### 4. Add a role to a user
|
95
108
|
|
96
109
|
To define a global role:
|
@@ -100,21 +113,27 @@ user = User.find(1)
|
|
100
113
|
user.add_role :admin
|
101
114
|
```
|
102
115
|
|
103
|
-
To define a role scoped to a resource instance
|
116
|
+
To define a role scoped to a resource instance:
|
104
117
|
|
105
118
|
```ruby
|
106
119
|
user = User.find(2)
|
107
120
|
user.add_role :moderator, Forum.first
|
108
121
|
```
|
109
122
|
|
110
|
-
To define a role scoped to a resource class
|
123
|
+
To define a role scoped to a resource class:
|
111
124
|
|
112
125
|
```ruby
|
113
126
|
user = User.find(3)
|
114
127
|
user.add_role :moderator, Forum
|
115
128
|
```
|
116
129
|
|
117
|
-
|
130
|
+
Remove role:
|
131
|
+
```ruby
|
132
|
+
user = User.find(3)
|
133
|
+
user.remove_role :moderator
|
134
|
+
```
|
135
|
+
|
136
|
+
That's it!
|
118
137
|
|
119
138
|
### 5. Role queries
|
120
139
|
|
@@ -162,6 +181,17 @@ user.has_role? :moderator, Forum.last
|
|
162
181
|
=> true
|
163
182
|
```
|
164
183
|
|
184
|
+
To check if a user has the exact role scoped to a resource class:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
user = User.find(5)
|
188
|
+
user.add_role :moderator # sets a global role
|
189
|
+
user.has_role? :moderator, Forum.first
|
190
|
+
=> true
|
191
|
+
user.has_strict_role? :moderator, Forum.last
|
192
|
+
=> false
|
193
|
+
```
|
194
|
+
|
165
195
|
### 6. Resource roles querying
|
166
196
|
|
167
197
|
Starting from rolify 3.0, you can search roles on instance level or class level resources.
|
@@ -171,36 +201,38 @@ Starting from rolify 3.0, you can search roles on instance level or class level
|
|
171
201
|
```ruby
|
172
202
|
forum = Forum.first
|
173
203
|
forum.roles
|
174
|
-
# => [ list of roles that are only
|
204
|
+
# => [ list of roles that are only bound to forum instance ]
|
175
205
|
forum.applied_roles
|
176
|
-
# => [ list of roles
|
206
|
+
# => [ list of roles bound to forum instance and to the Forum class ]
|
177
207
|
```
|
178
208
|
|
179
209
|
#### Class level
|
180
210
|
|
181
211
|
```ruby
|
182
212
|
Forum.with_role(:admin)
|
183
|
-
# => [ list of Forum instances that
|
213
|
+
# => [ list of Forum instances that have role "admin" bound to them ]
|
214
|
+
Forum.without_role(:admin)
|
215
|
+
# => [ list of Forum instances that do NOT have role "admin" bound to them ]
|
184
216
|
Forum.with_role(:admin, current_user)
|
185
|
-
# => [ list of Forum instances that
|
217
|
+
# => [ list of Forum instances that have role "admin" bound to them and belong to current_user roles ]
|
186
218
|
Forum.with_roles([:admin, :user], current_user)
|
187
|
-
# => [ list of Forum instances that
|
219
|
+
# => [ list of Forum instances that have role "admin" or "user" bound to them and belong to current_user roles ]
|
188
220
|
|
189
221
|
User.with_any_role(:user, :admin)
|
190
|
-
# => [ list of User instances that
|
222
|
+
# => [ list of User instances that have role "admin" or "user" bound to them ]
|
191
223
|
User.with_role(:site_admin, current_site)
|
192
224
|
# => [ list of User instances that have a scoped role of "site_admin" to a site instance ]
|
193
225
|
User.with_role(:site_admin, :any)
|
194
226
|
# => [ list of User instances that have a scoped role of "site_admin" for any site instances ]
|
195
227
|
User.with_all_roles(:site_admin, :admin)
|
196
|
-
# => [ list of User instances that have a role of "site_admin" and a role of "admin"
|
228
|
+
# => [ list of User instances that have a role of "site_admin" and a role of "admin" bound to it ]
|
197
229
|
|
198
230
|
Forum.find_roles
|
199
|
-
# => [ list of roles that
|
231
|
+
# => [ list of roles that are bound to any Forum instance or to the Forum class ]
|
200
232
|
Forum.find_roles(:admin)
|
201
|
-
# => [ list of roles that
|
233
|
+
# => [ list of roles that are bound to any Forum instance or to the Forum class, with "admin" as a role name ]
|
202
234
|
Forum.find_roles(:admin, current_user)
|
203
|
-
# => [ list of roles that
|
235
|
+
# => [ list of roles that are bound to any Forum instance, or to the Forum class with "admin" as a role name, and belongs to current_user ]
|
204
236
|
```
|
205
237
|
|
206
238
|
### Strict Mode
|
@@ -221,6 +253,20 @@ end
|
|
221
253
|
```
|
222
254
|
I.e. you get true only on a role that you manually add.
|
223
255
|
|
256
|
+
### Cached Roles (to avoid N+1 issue)
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
@user.add_role :admin, Forum
|
260
|
+
@user.add_role :member, Forum
|
261
|
+
|
262
|
+
users = User.with_role(:admin, Forum).preload(:roles)
|
263
|
+
users.each do |user|
|
264
|
+
user.has_cached_role?(:member, Forum) # no extra queries
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
This method should be used with caution. If you don't preload the roles, the `has_cached_role?` might return `false`. In the above example, it would return `false` for `@user.has_cached_role?(:member, Forum)`, because `User.with_role(:admin, Forum)` will load only the `:admin` roles.
|
269
|
+
|
224
270
|
## Resources
|
225
271
|
|
226
272
|
* [Wiki](https://github.com/RolifyCommunity/rolify/wiki)
|