third-prestige-rolify 3.3.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +17 -0
- data/CHANGELOG.rdoc +157 -0
- data/Gemfile +21 -0
- data/LICENSE +20 -0
- data/README.md +220 -0
- data/Rakefile +34 -0
- data/UPGRADE.rdoc +44 -0
- data/gemfiles/Gemfile.rails-3.2 +21 -0
- data/gemfiles/Gemfile.rails-4.0 +27 -0
- data/lib/generators/active_record/rolify_generator.rb +50 -0
- data/lib/generators/active_record/templates/README +8 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/mongoid/rolify_generator.rb +51 -0
- data/lib/generators/mongoid/templates/README-mongoid +4 -0
- data/lib/generators/rolify/rolify_generator.rb +35 -0
- data/lib/generators/rolify/templates/README +13 -0
- data/lib/generators/rolify/templates/initializer.rb +8 -0
- data/lib/generators/rolify/templates/role-active_record.rb +11 -0
- data/lib/generators/rolify/templates/role-mongoid.rb +17 -0
- data/lib/generators/rolify/user_generator.rb +39 -0
- data/lib/rolify.rb +57 -0
- data/lib/rolify/adapters/active_record/resource_adapter.rb +26 -0
- data/lib/rolify/adapters/active_record/role_adapter.rb +86 -0
- data/lib/rolify/adapters/active_record/scopes.rb +27 -0
- data/lib/rolify/adapters/base.rb +60 -0
- data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -0
- data/lib/rolify/adapters/mongoid/role_adapter.rb +89 -0
- data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
- data/lib/rolify/configure.rb +56 -0
- data/lib/rolify/dynamic.rb +21 -0
- data/lib/rolify/finders.rb +40 -0
- data/lib/rolify/matchers.rb +13 -0
- data/lib/rolify/railtie.rb +20 -0
- data/lib/rolify/resource.rb +31 -0
- data/lib/rolify/role.rb +85 -0
- data/lib/rolify/utils.rb +10 -0
- data/lib/rolify/version.rb +3 -0
- data/rolify.gemspec +30 -0
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +163 -0
- data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
- data/spec/generators_helper.rb +21 -0
- data/spec/rolify/config_spec.rb +191 -0
- data/spec/rolify/custom_spec.rb +20 -0
- data/spec/rolify/matchers_spec.rb +24 -0
- data/spec/rolify/namespace_spec.rb +24 -0
- data/spec/rolify/resource_spec.rb +389 -0
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
- data/spec/rolify/role_spec.rb +20 -0
- data/spec/rolify/shared_contexts.rb +92 -0
- data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +92 -0
- data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +151 -0
- data/spec/rolify/shared_examples/shared_examples_for_finders.rb +77 -0
- data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
- data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
- data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +135 -0
- data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
- data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +121 -0
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +102 -0
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/support/adapters/active_record.rb +76 -0
- data/spec/support/adapters/mongoid.rb +143 -0
- data/spec/support/adapters/mongoid.yml +6 -0
- data/spec/support/data.rb +25 -0
- data/spec/support/schema.rb +52 -0
- metadata +254 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2db18461c5d4b36fba759899728a36106c6a6591
|
4
|
+
data.tar.gz: bded9de6408ca7582da1d41125633f92d72d9441
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6fae547b69f1c7f89a3fca11735dd153826ed6f477773bb95ac76bc46829493e791f7cf85a2eeefa5bd7847c10a08d4c7a627f76cc120e8655af138fe6f5f889
|
7
|
+
data.tar.gz: 55ad679b5c49555ee6124a6e36f5e39c93bc2a02d224765e8db6a4732d736b680c06d5bdf843ee277e3671ce70034001e8dffb5c1ad0adb9c5f59d455c092d3e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
rvm:
|
2
|
+
- 1.9.3
|
3
|
+
- 2.0.0
|
4
|
+
- rbx-19mode
|
5
|
+
- jruby-19mode
|
6
|
+
|
7
|
+
gemfile:
|
8
|
+
- gemfiles/Gemfile.rails-3.2
|
9
|
+
- gemfiles/Gemfile.rails-4.0
|
10
|
+
|
11
|
+
env:
|
12
|
+
- ADAPTER=active_record
|
13
|
+
- ADAPTER=mongoid
|
14
|
+
|
15
|
+
services: mongodb
|
16
|
+
|
17
|
+
before_script: rails --version
|
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
= 3.3 (not released yet)
|
2
|
+
* <b>DEPRECATION NOTICE:</b>Rails 3.1 support dropped: if you use Rails 3.1, please stick to rolify 3.2
|
3
|
+
* code cleanup in finders methods
|
4
|
+
* generators rewritten entirely. now using ActiveRecord/Mongoid model generator to create Role model
|
5
|
+
* added rspec matchers for detailed spec error messages (thanks to @delwyn)
|
6
|
+
* clean up specs (thanks to @delwyn), removed subject and let declarations in before(:all) block
|
7
|
+
* roles query needs 1 DB hit instead of 1 up to 3 (thanks to @terraplane)
|
8
|
+
* remove nil entries from ResourceAdapter#resources_find when using Mongoid adapter (thanks to @daviscabral)
|
9
|
+
* fixed a bug regarding redundant index for Mongoid Role model (thanks to @rschultheis)
|
10
|
+
* added support for rolify and resourcify methods on the same model class (specs by @amer)
|
11
|
+
* added support for namespaced models (thanks to @intrica)
|
12
|
+
* fixed compatibility issue with squeel when using symbols as role parameters (hint by @f3ndot)
|
13
|
+
* now raises a warning in the initializer if migration has not been run
|
14
|
+
* add support for primary key different than 'id' for resource Model (thanks to @rafaeldl)
|
15
|
+
* Rails 4 (thanks to @adammathys) and ruby 2.0 compliant
|
16
|
+
* configured travis-ci to run the specs on Rails 3.2/4.0 and Rubies 1.9.3/2.0/rbx/jruby
|
17
|
+
* added code climate to check for code smell
|
18
|
+
|
19
|
+
= 3.2 (Aug 7, 2012)
|
20
|
+
* <b>DEPRECATION NOTICE:</b> Ruby 1.8 support dropped ! Mongoid 3.0 only supports MRI 1.9.3, and HEAD, and JRuby 1.6.0+ in 1.9 mode
|
21
|
+
* removed <tt>dynamic_shortcuts</tt> arguments from the generator
|
22
|
+
* to use dynamic shortcuts feature when you're using ActiveRecord, you have to enable it _after_ running rake db:migrate as it relies on the roles table
|
23
|
+
* support for Mongoid 3.x (thanks to @Leonas)
|
24
|
+
* new class methods on the User class to find users depending on roles they have
|
25
|
+
* added scopes to Role class to be able to fetch global, class scoped and instance scoped roles for a specific user
|
26
|
+
* deletions of n-n relation are unreliable with Mongoid. Removing ids instead (thanks to @nfo)
|
27
|
+
* <tt>has_role?</tt> method now supports new instance (i.e. record not saved in the database yet) (thanks to @demental)
|
28
|
+
* added association callbacks <tt>(before|after)_add</tt>, <tt>(before|after)_remove</tt> on <tt>rolify</tt> method (thanks to @shekibobo)
|
29
|
+
* added ability to pass an array of roles to <tt>Resource.with_role()</tt>, aliased by <tt>Resource.with_roles()</tt> (thanks to @lukes)
|
30
|
+
* added option to have roles be destroyed by default if parent resource is destroyed (thanks to @treydock)
|
31
|
+
* added new method <tt>only_has_role?</tt> to check if user has only a specific role (thanks to @jalcine)
|
32
|
+
* better edge cases covering in the specs
|
33
|
+
* fixed a bug regarding the loading order of the railtie when using Mongoid ORM and other gems using initializer files (thanks to @stigi)
|
34
|
+
* fixed double quote syntax when using MySQL
|
35
|
+
* fixed a nasty bug regarding class level queries (thanks to @kamrulhassan)
|
36
|
+
* fixed uninitialized constant error in scopify method
|
37
|
+
* documentation improvement
|
38
|
+
|
39
|
+
= 3.1 (Apr 6, 2012)
|
40
|
+
* Mongoid adapter optimization
|
41
|
+
* adapter code refactoring
|
42
|
+
* generator now adds the role class name to the rolify method injected in the user class
|
43
|
+
* fixed a bug on the generator when using a 2 words Camel case for the Role class name
|
44
|
+
* <b>DEPRECATION NOTICE:</b> <tt>has_role</tt> and <tt>has_no_role</tt> have been depecrated. They are replaced by <tt>add_role</tt> and <tt>remove_role</tt>
|
45
|
+
* some internals cleanup (backward compatible)
|
46
|
+
* stop requiring <tt>active_record</tt> in <tt>rolify.rb</tt> to prevent other gems ORM detection issue
|
47
|
+
* fixed a bug when removing a role to the user using Mongoid adapter
|
48
|
+
* added indexes to generator for mongoid (thanks to @stigi)
|
49
|
+
* fixed a bug regarding **with_role** method on resource classes (thanks to @nfo)
|
50
|
+
|
51
|
+
= 3.0 (Apr 2, 2012)
|
52
|
+
* support for Mongoid
|
53
|
+
* roles search on resources on instance level (e.g. <tt>Forum.first.roles</tt>) and class level (e.g. <tt>Forum.with_role("admin", user)</tt>)
|
54
|
+
* heavy lifting and redesign of the library, code and specs refactoring
|
55
|
+
* enhanced drastically specs coverage: 1001 examples !
|
56
|
+
|
57
|
+
= 2.2.2 (Feb 17, 2012)
|
58
|
+
* fixed another bug occurring when dynamic shortcuts is enabled
|
59
|
+
* display now a README file after running the generator to show the next setup steps
|
60
|
+
|
61
|
+
= 2.2.1 (Jan 24, 2012)
|
62
|
+
* fixed a backward incompatible change introduced in Rails 3.2 release (<tt>find_or_create_by_* generated methods</tt>)
|
63
|
+
|
64
|
+
= 2.2 (Jan 18, 2012)
|
65
|
+
* fixed a bug in the initializer file regarding dynamic shortcuts
|
66
|
+
|
67
|
+
= 2.1 (Nov 30, 2011)
|
68
|
+
* added syntactic sugar: <tt>grant</tt> and <tt>revoke</tt> are aliases for <tt>has_role</tt> and <tt>has_no_role</tt>
|
69
|
+
* check if RUBY_ENGINE is defined in the gemspec to be able to use jdbc with JRuby for SQLite
|
70
|
+
|
71
|
+
= 2.0 (Nov 10, 2011)
|
72
|
+
* improved performance of <tt>has_all_roles?</tt> method using one single DB query instead of doing one DB lookup per argument
|
73
|
+
* significant speed-up when requesting with many arguments
|
74
|
+
* database choice can mitigate the results
|
75
|
+
* clean up the initializer code
|
76
|
+
* using a DSL to configure the library
|
77
|
+
* setting defaults for User and Role classes
|
78
|
+
* dynamic shortcuts feature is now <b>disabled</b> by default. To turn it on:
|
79
|
+
* set it to true in the initializer file
|
80
|
+
* uncomment the <tt>extend Rolify::Dynamic</tt> line in the User class
|
81
|
+
* detecting if it's loaded by Rails::Server or Rails::Console
|
82
|
+
* now also running on Rubinius, JRuby, REE and Ruby 1.8. all specs pass successfully, yeah !
|
83
|
+
|
84
|
+
= 1.2 (Nov 4, 2011)
|
85
|
+
* fixed a strange bug, probably rails related (thanks to @scottkf)
|
86
|
+
* when using rails in development mode, the <tt>config.cache_classes = false</tt> makes the role class to be loaded at every call and can lead to a <tt>AssociationTypeMismatch</tt>
|
87
|
+
* use of <tt>role_ids</tt> array instead of the <tt>roles</tt> association
|
88
|
+
* now running on JRuby (<b>specs are still failing for JRuby though</b>)
|
89
|
+
|
90
|
+
= 1.1 (Oct 14, 2011)
|
91
|
+
* added a spec to test the rails generator using ammeter gem
|
92
|
+
* Gemfile cleanup, moved all dependencies in gemspec instead
|
93
|
+
* edited the dependency to Rails 3.1 and newer, now that Rails 3.1 has been released
|
94
|
+
* new role scoping capabilities
|
95
|
+
* 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
|
96
|
+
* class level : <tt>user.has_role "moderator", Forum</tt>. User has the moderator role on all instances of Forum
|
97
|
+
* 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)
|
98
|
+
* new scoped query capabilities
|
99
|
+
* <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
|
100
|
+
* <tt>user.has_role? "moderator", Forum</tt>. asks if the user has the moderator role on all Forum instances
|
101
|
+
* <tt>user.has_role? "moderator"</tt> (<em>already supported in previous release</em>). asks if the user has the global moderator role
|
102
|
+
* <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).
|
103
|
+
|
104
|
+
= 1.0 (Aug 25, 2011)
|
105
|
+
* added a new parameter to disable dynamic shortcut methods due to potential incompatibility with other gems using method_missing with the same pattern
|
106
|
+
* add <tt>Rolify.dynamic_shortcuts = false</tt> in the initializer file or
|
107
|
+
* use the generator command with a third parameter:
|
108
|
+
* <tt>rails g rolify:role Role User false</tt>
|
109
|
+
* removed the railtie as it created more problems than it solved
|
110
|
+
* code refactoring to do some speed improvements and code clean up
|
111
|
+
* added a lot of specs to improve tests coverage
|
112
|
+
* wrote a tutorial showing how to use rolify with CanCan and Devise
|
113
|
+
* rolify is now on travis-ci to monitor build status
|
114
|
+
|
115
|
+
= 0.7 (June 20, 2011)
|
116
|
+
* 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)
|
117
|
+
* dynamic shortcut is created on the fly in the method_missing to avoid extra method_missing for the same dynamic shortcut
|
118
|
+
* check if the role actually exists in the database before defining the new method
|
119
|
+
* first call is slower due to method_missing but next calls are fast
|
120
|
+
* 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
|
121
|
+
|
122
|
+
= 0.6 (June 19, 2011)
|
123
|
+
* custom User and Role class names support
|
124
|
+
* can now use other class names for Role and User classes
|
125
|
+
* fixed generators and templates
|
126
|
+
* join table is explicitly set to avoid alphabetical order issue
|
127
|
+
* created a new railtie to load the dynamic shortcuts at startup
|
128
|
+
|
129
|
+
= 0.5.1 (June 07, 2011)
|
130
|
+
* fixed a nasty typo on a variable name and added a spec to make it never happen again
|
131
|
+
|
132
|
+
= 0.5 (June 07, 2011)
|
133
|
+
* dynamic shortcuts support
|
134
|
+
* creates automatically new methods upon new role creation (or at startup for a Rails app)
|
135
|
+
* <tt>has_role "admin"</tt> will create a method called <tt>is_admin?</tt>
|
136
|
+
* <tt>has_role "moderator", Forum.first</tt> will create 2 methods:
|
137
|
+
* <tt>is_moderator_of?(resource)</tt>
|
138
|
+
* <tt>is_moderator?</tt>
|
139
|
+
|
140
|
+
= v0.4 (June 07, 2011)
|
141
|
+
* removing role support
|
142
|
+
* <tt>has_no_role</tt> removes a global role or a role scoped to a resource
|
143
|
+
* 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
|
144
|
+
* Trying to remove a role scoped to a resource whereas the user has a global role won't remove it
|
145
|
+
|
146
|
+
= v0.3 (June 06, 2011)
|
147
|
+
* multiple roles check:
|
148
|
+
* <tt>has_all_roles?</tt> returns true if the user has ALL the roles in arguments
|
149
|
+
* <tt>has_any_role?</tt> returns true if the user has ANY the roles in arguments
|
150
|
+
|
151
|
+
= v0.2 (June 04, 2011)
|
152
|
+
* fixed the generator to include the lib
|
153
|
+
* fixed the migration file with missing polymorphic field
|
154
|
+
* added some examples in documentation
|
155
|
+
|
156
|
+
= v0.1 (June 04, 2011)
|
157
|
+
* first release
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
case ENV["ADAPTER"]
|
4
|
+
when nil, "active_record"
|
5
|
+
group :test do
|
6
|
+
gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.rc", :platform => "jruby"
|
7
|
+
gem "sqlite3", :platform => "ruby"
|
8
|
+
end
|
9
|
+
gem "activerecord", ">= 3.2.0", :require => "active_record"
|
10
|
+
when "mongoid"
|
11
|
+
gem "mongoid", ">= 3.1"
|
12
|
+
gem "bson_ext", :platform => "ruby"
|
13
|
+
else
|
14
|
+
raise "Unknown model adapter: #{ENV["ADAPTER"]}"
|
15
|
+
end
|
16
|
+
|
17
|
+
group :test do
|
18
|
+
gem 'coveralls', :require => false
|
19
|
+
end
|
20
|
+
|
21
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Florent Monbillard
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,220 @@
|
|
1
|
+
# rolify [![Gem Version](https://badge.fury.io/rb/rolify.png)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/EppO/rolify.png)](http://travis-ci.org/EppO/rolify) [![dependency status](https://gemnasium.com/EppO/rolify.png)](https://gemnasium.com/EppO/rolify) [![Code Climate](https://codeclimate.com/github/EppO/rolify.png)](https://codeclimate.com/github/EppO/rolify) [![Coverage Status](https://coveralls.io/repos/EppO/rolify/badge.png?branch=master)](https://coveralls.io/r/EppO/rolify)
|
2
|
+
|
3
|
+
|
4
|
+
Very simple Roles library without any authorization enforcement supporting scope on resource object.
|
5
|
+
|
6
|
+
Let's see an example:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
user.has_role?(:moderator, Forum.first)
|
10
|
+
=> false # if user is moderator of another Forum
|
11
|
+
```
|
12
|
+
|
13
|
+
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> ([CanCan](https://github.com/ryanb/cancan), [authority](https://github.com/nathanl/authority))
|
14
|
+
|
15
|
+
<span style="color: red"><strong>*</strong></span>: authorization gem that doesn't provide a role class
|
16
|
+
|
17
|
+
## Requirements
|
18
|
+
|
19
|
+
* Rails >= 3.2
|
20
|
+
* ActiveRecord >= 3.2 <b>or</b> Mongoid >= 3.1
|
21
|
+
* supports ruby 2.0/1.9.3, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)
|
22
|
+
* support of ruby 1.8 has been dropped due to Mongoid >=3.0 that only supports 1.9 new hash syntax
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem "rolify"
|
30
|
+
```
|
31
|
+
|
32
|
+
## Getting Started
|
33
|
+
|
34
|
+
### 1. Generate Role Model
|
35
|
+
|
36
|
+
First, create your Role model and migration file using this generator:
|
37
|
+
|
38
|
+
```
|
39
|
+
rails g rolify Role User
|
40
|
+
```
|
41
|
+
|
42
|
+
**NB** for versions of Rolify prior to 3.3, use:
|
43
|
+
|
44
|
+
```
|
45
|
+
rails g rolify:role Role User
|
46
|
+
```
|
47
|
+
|
48
|
+
Role and User classes are the default. You can specify any Role class name you want. This is completly a new file so any name can do the job.
|
49
|
+
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.
|
50
|
+
|
51
|
+
If you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3
|
52
|
+
|
53
|
+
### 2. Run the migration (only required when using ActiveRecord)
|
54
|
+
|
55
|
+
Let's migrate !
|
56
|
+
|
57
|
+
```
|
58
|
+
rake db:migrate
|
59
|
+
```
|
60
|
+
|
61
|
+
### 3.1 Configure your user model
|
62
|
+
|
63
|
+
This gem adds the `rolify` method to your User class. You can also specify optional callbacks on the User class for when roles are added or removed:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User < ActiveRecord::Base
|
67
|
+
rolify :before_add => :before_add_method
|
68
|
+
|
69
|
+
def before_add_method(role)
|
70
|
+
# do something before it gets added
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
The `rolify` method accepts the following callback options:
|
76
|
+
|
77
|
+
- `before_add`
|
78
|
+
- `after_add`
|
79
|
+
- `before_remove`
|
80
|
+
- `after_remove`
|
81
|
+
|
82
|
+
Mongoid callbacks are also supported and works the same way.
|
83
|
+
|
84
|
+
### 3.2 Configure your resource models
|
85
|
+
|
86
|
+
In the resource models you want to apply roles on, just add ``resourcify`` method.
|
87
|
+
For example, on this ActiveRecord class:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Forum < ActiveRecord::Base
|
91
|
+
resourcify
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
### 4. Add a role to a user
|
96
|
+
|
97
|
+
To define a global role:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
user = User.find(1)
|
101
|
+
user.add_role :admin
|
102
|
+
```
|
103
|
+
|
104
|
+
To define a role scoped to a resource instance
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
user = User.find(2)
|
108
|
+
user.add_role :moderator, Forum.first
|
109
|
+
```
|
110
|
+
|
111
|
+
To define a role scoped to a resource class
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
user = User.find(3)
|
115
|
+
user.add_role :moderator, Forum
|
116
|
+
```
|
117
|
+
|
118
|
+
That's it !
|
119
|
+
|
120
|
+
### 5. Role queries
|
121
|
+
|
122
|
+
To check if a user has a global role:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
user = User.find(1)
|
126
|
+
user.add_role :admin # sets a global role
|
127
|
+
user.has_role? :admin
|
128
|
+
=> true
|
129
|
+
```
|
130
|
+
|
131
|
+
To check if a user has a role scoped to a resource instance:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
user = User.find(2)
|
135
|
+
user.add_role :moderator, Forum.first # sets a role scoped to a resource instance
|
136
|
+
user.has_role? :moderator, Forum.first
|
137
|
+
=> true
|
138
|
+
user.has_role? :moderator, Forum.last
|
139
|
+
=> false
|
140
|
+
```
|
141
|
+
|
142
|
+
To check if a user has a role scoped to a resource class:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
user = User.find(3)
|
146
|
+
user.add_role :moderator, Forum # sets a role scoped to a resource class
|
147
|
+
user.has_role? :moderator, Forum
|
148
|
+
=> true
|
149
|
+
user.has_role? :moderator, Forum.first
|
150
|
+
=> true
|
151
|
+
user.has_role? :moderator, Forum.last
|
152
|
+
=> true
|
153
|
+
```
|
154
|
+
|
155
|
+
A global role overrides resource role request:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
user = User.find(4)
|
159
|
+
user.add_role :moderator # sets a global role
|
160
|
+
user.has_role? :moderator, Forum.first
|
161
|
+
=> true
|
162
|
+
user.has_role? :moderator, Forum.last
|
163
|
+
=> true
|
164
|
+
```
|
165
|
+
|
166
|
+
### 6. Resource roles querying
|
167
|
+
|
168
|
+
Starting from rolify 3.0, you can search roles on instance level or class level resources.
|
169
|
+
|
170
|
+
#### Instance level
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
forum = Forum.first
|
174
|
+
forum.roles
|
175
|
+
# => [ list of roles that are only binded to forum instance ]
|
176
|
+
forum.applied_roles
|
177
|
+
# => [ list of roles binded to forum instance and to the Forum class ]
|
178
|
+
```
|
179
|
+
|
180
|
+
#### Class level
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
Forum.with_role(:admin)
|
184
|
+
# => [ list of Forum instances that has role "admin" binded to it ]
|
185
|
+
Forum.with_role(:admin, current_user)
|
186
|
+
# => [ list of Forum instances that has role "admin" binded to it and belongs to current_user roles ]
|
187
|
+
|
188
|
+
User.with_any_role(:user, :admin)
|
189
|
+
# => [ list of User instances that has role "admin" or "user" binded to it ]
|
190
|
+
|
191
|
+
Forum.find_roles
|
192
|
+
# => [ list of roles that binded to any Forum instance or to the Forum class ]
|
193
|
+
Forum.find_roles(:admin)
|
194
|
+
# => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name ]
|
195
|
+
Forum.find_roles(:admin, current_user)
|
196
|
+
# => [ list of roles that binded to any Forum instance or to the Forum class with "admin" as a role name and belongs to current_user roles ]
|
197
|
+
```
|
198
|
+
|
199
|
+
## Resources
|
200
|
+
|
201
|
+
* [Wiki](https://github.com/EppO/rolify/wiki)
|
202
|
+
* [Usage](https://github.com/EppO/rolify/wiki/Usage): all the available commands
|
203
|
+
* [Tutorials](https://github.com/EppO/rolify/wiki#wiki-tutorials):
|
204
|
+
* [How-To use rolify with Devise and CanCan](https://github.com/EppO/rolify/wiki/Tutorial)
|
205
|
+
* [Using rolify with Devise and Authority](https://github.com/EppO/rolify/wiki/Using-rolify-with-Devise-and-Authority)
|
206
|
+
* [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)
|
207
|
+
|
208
|
+
## Upgrade from previous versions
|
209
|
+
|
210
|
+
Please read the [upgrade instructions](UPGRADE.rdoc).
|
211
|
+
|
212
|
+
## Known issues
|
213
|
+
|
214
|
+
If you are using Mongoid and/or less-rails gem, please read [this](https://github.com/EppO/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error)
|
215
|
+
|
216
|
+
## Questions or Problems?
|
217
|
+
|
218
|
+
If you have any issue or feature request with/for rolify, please create an new [issue on GitHub](https://github.com/EppO/rolify/issues) **specifying the ruby runtime, rails and rolify versions you're using and the gems listed in your Gemfile**, or fork the project and send a pull request.
|
219
|
+
|
220
|
+
To get the specs running you should call `bundle` and then `rake`. See the spec/README for more information.
|