slow_your_roles 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +73 -0
- data/README.md +223 -0
- data/Rakefile +15 -0
- data/init.rb +3 -0
- data/lib/generators/active_record/slow_your_roles_generator.rb +55 -0
- data/lib/generators/active_record/templates/migration_bitmask.rb.erb +12 -0
- data/lib/generators/active_record/templates/migration_serialize.rb.erb +12 -0
- data/lib/generators/active_record/templates/model.rb.erb +5 -0
- data/lib/generators/install_generator_helpers.rb +27 -0
- data/lib/generators/slow_your_roles/slow_your_roles_generator.rb +26 -0
- data/lib/generators/templates/README +9 -0
- data/lib/methods/bitmask.rb +78 -0
- data/lib/methods/serialize.rb +108 -0
- data/lib/slow_your_roles.rb +63 -0
- data/slow_your_roles.gemspec +37 -0
- data/spec/methods/bitmask_spec.rb +261 -0
- data/spec/methods/serialize_spec.rb +321 -0
- data/spec/spec_helper.rb +67 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 683edb9b195a92a1350e19fed6ec1422bd63c69bd7b49346f17ea938fc4b27f9
|
4
|
+
data.tar.gz: cce3a768afe37fe29f1b8f22c6392b1a569f6b99667f858ba7ce9c222b19fbc0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5770dbf6768fe7d95b22d9e810f7a6035841d6f9f2573e275c43a007ffed3f001e9f60fa95f2e2adfece6a4ab9e609d13f4fe483e17ac64a12f215003dd7c6d0
|
7
|
+
data.tar.gz: 56ee32fea12d12f113c635690538d8b59628ad40e67ad1ff93d540cb1c02fe251a101001973693637f6cac71ef436ab4d586fea18755366bb03aa09c75c17bb5
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
### 2 June 2020 - Version 2.0.2
|
2
|
+
Fixed incompatibility issue with PostgresSQL when using serialize role method.
|
3
|
+
|
4
|
+
### 21 April 2020 - Version 2.0.1
|
5
|
+
Ported over from unmaintained library, easy_roles. Because we cannot push new
|
6
|
+
versions of this library to rubygems.org, it's now called slow_your_roles.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
slow_your_roles (2.0.2)
|
5
|
+
activesupport (>= 6.0.3.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.3.1)
|
11
|
+
activesupport (= 6.0.3.1)
|
12
|
+
activerecord (6.0.3.1)
|
13
|
+
activemodel (= 6.0.3.1)
|
14
|
+
activesupport (= 6.0.3.1)
|
15
|
+
activesupport (6.0.3.1)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
21
|
+
ast (2.4.0)
|
22
|
+
concurrent-ruby (1.1.6)
|
23
|
+
diff-lcs (1.3)
|
24
|
+
i18n (1.8.2)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
jaro_winkler (1.5.4)
|
27
|
+
minitest (5.14.1)
|
28
|
+
parallel (1.19.1)
|
29
|
+
parser (2.7.1.1)
|
30
|
+
ast (~> 2.4.0)
|
31
|
+
rainbow (3.0.0)
|
32
|
+
rexml (3.2.4)
|
33
|
+
rspec (3.9.0)
|
34
|
+
rspec-core (~> 3.9.0)
|
35
|
+
rspec-expectations (~> 3.9.0)
|
36
|
+
rspec-mocks (~> 3.9.0)
|
37
|
+
rspec-core (3.9.1)
|
38
|
+
rspec-support (~> 3.9.1)
|
39
|
+
rspec-expectations (3.9.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.9.0)
|
42
|
+
rspec-mocks (3.9.1)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.9.0)
|
45
|
+
rspec-support (3.9.2)
|
46
|
+
rubocop (0.82.0)
|
47
|
+
jaro_winkler (~> 1.5.1)
|
48
|
+
parallel (~> 1.10)
|
49
|
+
parser (>= 2.7.0.1)
|
50
|
+
rainbow (>= 2.2.2, < 4.0)
|
51
|
+
rexml
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
54
|
+
ruby-progressbar (1.10.1)
|
55
|
+
sqlite3 (1.4.2)
|
56
|
+
thread_safe (0.3.6)
|
57
|
+
tzinfo (1.2.7)
|
58
|
+
thread_safe (~> 0.1)
|
59
|
+
unicode-display_width (1.7.0)
|
60
|
+
zeitwerk (2.3.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
activerecord (>= 6.0.3.1)
|
67
|
+
rspec
|
68
|
+
rubocop
|
69
|
+
slow_your_roles!
|
70
|
+
sqlite3
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
1.17.3
|
data/README.md
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
# Slow Your Roles
|
2
|
+
|
3
|
+
Simple rails gem for basic role authorization with ruby on rails.
|
4
|
+
|
5
|
+
>This gem has been cloned from [Easy Roles](https://github.com/platform45/easy_roles) due to owner no longer supporting or maintaining that library. Since we cannot publish this gem to rubygems under that name we renamed the library.
|
6
|
+
|
7
|
+
## Changelog
|
8
|
+
|
9
|
+
Please read the [CHANGELOG.md](https://github.com/aarona/slow_your_roles/blob/master/CHANGELOG.md) file.
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem install slow_your_roles
|
15
|
+
```
|
16
|
+
|
17
|
+
## Basic Setup
|
18
|
+
|
19
|
+
### Serialize Method
|
20
|
+
|
21
|
+
Add the following to your Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'slow_your_roles'
|
25
|
+
```
|
26
|
+
|
27
|
+
Then generate the migration:
|
28
|
+
|
29
|
+
```
|
30
|
+
rails g slow_your_roles user roles
|
31
|
+
```
|
32
|
+
|
33
|
+
Or add a `roles` column to your users model, and set the default value to `--- []`. Please note you can call this column anything you like, I like to use the name "roles".
|
34
|
+
|
35
|
+
```
|
36
|
+
t.string :roles, default: "--- []"
|
37
|
+
```
|
38
|
+
|
39
|
+
Then you need to add `slow_your_roles :column_name` to your model:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
slow_your_roles :roles
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Bitmask Method
|
48
|
+
|
49
|
+
Add the following to your Gemfile:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
gem 'slow_your_roles'
|
53
|
+
```
|
54
|
+
|
55
|
+
Then generate the migration:
|
56
|
+
|
57
|
+
```
|
58
|
+
rails g slow_your_roles user roles --use-bitmask-method
|
59
|
+
```
|
60
|
+
|
61
|
+
Or add a `roles_mask` column to your users model of type `integer`, and set the default value to `0`. Please note you can call this column anything you like, I like to use the name "`roles_mask`":
|
62
|
+
|
63
|
+
```
|
64
|
+
t.integer :roles_mask, default: 0
|
65
|
+
```
|
66
|
+
|
67
|
+
Add `slow_your_roles :column_name, method: :bitmask` to your model:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class User < ActiveRecord::Base
|
71
|
+
slow_your_roles :roles_mask, method: :bitmask
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
And lastly you need to add a constant variable which stores an array of the different roles for your system. The name of the constant must be the name of your column in full caps.
|
76
|
+
|
77
|
+
#### WARNING: Bitmask storage relies that you DO NOT change the order of your array of roles, if you need to add a new role, just append it to the end of the array.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
class User < ActiveRecord::Base
|
81
|
+
slow_your_roles :roles_mask, method: :bitmask
|
82
|
+
|
83
|
+
# Constant variable storing roles in the system
|
84
|
+
ROLES_MASK = %w[admin moderator user].freeze
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
## Usage
|
89
|
+
|
90
|
+
Slow your roles extends your model, and adds a few methods needed for basic role authorization.
|
91
|
+
|
92
|
+
adding a role to a user
|
93
|
+
|
94
|
+
```add_role 'role'```
|
95
|
+
|
96
|
+
adding multiple roles at the same time to a user
|
97
|
+
|
98
|
+
```add_roles 'admin', 'manager'```
|
99
|
+
|
100
|
+
removing a role from a user
|
101
|
+
|
102
|
+
```remove_role 'role'```
|
103
|
+
|
104
|
+
check to see if a user has a certain role
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
has_role? 'role'
|
108
|
+
# or
|
109
|
+
is_role? # role being anything you like, for example 'is_admin?' or 'is_awesome?'
|
110
|
+
```
|
111
|
+
|
112
|
+
For every method above there is a bang method too.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
add_role! 'role'
|
116
|
+
add_roles! 'admin', 'manager'
|
117
|
+
remove_role! 'role'
|
118
|
+
```
|
119
|
+
|
120
|
+
## Examples
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
@user = User.first
|
124
|
+
|
125
|
+
@user.add_role 'admin'
|
126
|
+
|
127
|
+
@user.is_admin?
|
128
|
+
=> true
|
129
|
+
|
130
|
+
@user.has_role? 'admin'
|
131
|
+
=> true
|
132
|
+
|
133
|
+
@user.is_awesome?
|
134
|
+
=> false
|
135
|
+
|
136
|
+
@user.add_role 'awesome'
|
137
|
+
|
138
|
+
@user.is_awesome?
|
139
|
+
=> true
|
140
|
+
|
141
|
+
@user.remove_role 'admin'
|
142
|
+
|
143
|
+
@user.is_admin?
|
144
|
+
=> false
|
145
|
+
|
146
|
+
etc etc
|
147
|
+
```
|
148
|
+
|
149
|
+
## Protecting controllers
|
150
|
+
|
151
|
+
There are many ways to implement views for specific roles, so I did not specifically supply one. Here's an example on what you could do:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
class ApplicationController < ActionController::Base
|
155
|
+
def admin_required
|
156
|
+
unless current_user && current_user.is_admin?
|
157
|
+
flash[:error] = "Sorry, you don't have access to that."
|
158
|
+
redirect_to root_url and return false
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
Then in your `AdminsController` or any controller that you only want admins to view:
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
class AdminsController < ApplicationController
|
168
|
+
before_filter :admin_required
|
169
|
+
end
|
170
|
+
|
171
|
+
class MarksController < ApplicationController
|
172
|
+
before_filter :admin_required, only: %w(create update)
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
## Scopes
|
177
|
+
|
178
|
+
By default, slow_your_roles adds the `with_role` scope to your models.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
@admins = User.with_role('admin')
|
182
|
+
```
|
183
|
+
|
184
|
+
If you're using the bitmask method, an `ArgumentError` will be thrown if an undeclared scope is queried. Since an `ActiveRecord::Relation` is returned, the query is chainable:
|
185
|
+
|
186
|
+
```log
|
187
|
+
BitmaskUser.with_role('admin').where(active: true).to_sql
|
188
|
+
# => SELECT "bitmask_users".* FROM "bitmask_users" WHERE "bitmask_users"."roles_mask" IN (1, 3, 5, 7) AND "bitmask_users"."active" = 't'
|
189
|
+
|
190
|
+
SerializeUser.with_role('admin').where(active: true).to_sql
|
191
|
+
# => SELECT "serialize_users".* FROM "serialize_users" WHERE "serialize_users"."active" = 't' AND (serialize_users.roles LIKE '%!admin!%')
|
192
|
+
```
|
193
|
+
|
194
|
+
slow_your_roles also supports a `without_role` scope.
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
@non_admins = User.without_role('admin')
|
198
|
+
```
|
199
|
+
|
200
|
+
Follow me on twitter: http://twitter.com/_aaron0
|
201
|
+
|
202
|
+
## License
|
203
|
+
|
204
|
+
Copyright (c) 2020 Platform45
|
205
|
+
|
206
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
207
|
+
a copy of this software and associated documentation files (the
|
208
|
+
"Software"), to deal in the Software without restriction, including
|
209
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
210
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
211
|
+
permit persons to whom the Software is furnished to do so, subject to
|
212
|
+
the following conditions:
|
213
|
+
|
214
|
+
The above copyright notice and this permission notice shall be
|
215
|
+
included in all copies or substantial portions of the Software.
|
216
|
+
|
217
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
218
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
219
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
220
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
221
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
222
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
223
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'echoe'
|
6
|
+
|
7
|
+
Echoe.new('slow_your_roles', '2.0.2') do |p|
|
8
|
+
p.description = 'Easy role authorization in rails'
|
9
|
+
p.url = 'http://github.com/aarona/slow_your_roles'
|
10
|
+
p.author = 'Aaron A'
|
11
|
+
p.email = '_aaron@tutanota.com'
|
12
|
+
p.ignore_pattern = ['tmp/*', 'script/*']
|
13
|
+
p.dependencies = ['activesupport']
|
14
|
+
p.development_dependencies = %w[rspec activerecord rubocop sqlite3]
|
15
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/active_record'
|
4
|
+
require_relative '../install_generator_helpers'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module Generators
|
8
|
+
# Generator class to add SlowYourRoles to an ActiveRecord model.
|
9
|
+
class SlowYourRolesGenerator < ActiveRecord::Generators::Base
|
10
|
+
include SlowYourRoles::InstallGeneratorHelpers
|
11
|
+
|
12
|
+
argument :role_col, type: :string, required: false, default: 'roles', banner: 'role column'
|
13
|
+
|
14
|
+
class_option :use_bitmask_method, type: :boolean, required: false, default: false,
|
15
|
+
desc: 'Setup migration for Bitmask method'
|
16
|
+
|
17
|
+
class_option :add_index, type: :boolean, required: false, default: false,
|
18
|
+
desc: 'Add an index to the relevant column'
|
19
|
+
|
20
|
+
desc 'Internal use by slow_your_roles generator - use that instead'
|
21
|
+
|
22
|
+
source_root File.expand_path('templates', __dir__)
|
23
|
+
|
24
|
+
def create_user_model
|
25
|
+
fname = "app/models/#{table_name.singularize.underscore}.rb"
|
26
|
+
|
27
|
+
if File.exist?(File.join(destination_root, fname))
|
28
|
+
inclusion = "slow_your_roles :#{role_col}"
|
29
|
+
if parse_file_for_line(fname, inclusion)
|
30
|
+
say_status('skipped', fname)
|
31
|
+
else
|
32
|
+
after = "class #{table_name.singularize.camelize} < ApplicationRecord\n"
|
33
|
+
inject_into_file fname, after: after do
|
34
|
+
<<-HEREDOC
|
35
|
+
#{inclusion}
|
36
|
+
HEREDOC
|
37
|
+
end
|
38
|
+
end
|
39
|
+
else
|
40
|
+
template('model.rb.erb', fname)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy_slow_your_roles_migration
|
45
|
+
if options.use_bitmask_method
|
46
|
+
migration_template 'migration_bitmask.rb.erb',
|
47
|
+
"db/migrate/add_roles_to_#{table_name}.rb"
|
48
|
+
else
|
49
|
+
migration_template 'migration_serialize.rb.erb',
|
50
|
+
"db/migrate/add_roles_to_#{table_name}.rb"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class AddBitmaskRolesTo<%= table_name.camelize %> < ActiveRecord::Migration
|
4
|
+
def change
|
5
|
+
change_table :<%= table_name %> do |t|
|
6
|
+
t.integer :<%= self.role_col %>, default: 0
|
7
|
+
<%- if options.add_index -%>
|
8
|
+
t.index :<%= self.role_col %>
|
9
|
+
<%- end -%>
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class AddRolesTo<%= table_name.camelize %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
|
4
|
+
def change
|
5
|
+
change_table :<%= table_name %> do |t|
|
6
|
+
t.string :<%= self.role_col %>, default: '--- []'
|
7
|
+
<%- if options.add_index -%>
|
8
|
+
t.index :<%= self.role_col %>
|
9
|
+
<%- end -%>
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SlowYourRoles
|
4
|
+
# Helper module for file generators
|
5
|
+
module InstallGeneratorHelpers
|
6
|
+
class << self
|
7
|
+
def included(mod)
|
8
|
+
mod.class_eval do
|
9
|
+
source_root File.expand_path('templates', __dir__)
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def parse_file_for_line(filename, str)
|
14
|
+
match = false
|
15
|
+
|
16
|
+
File.open(File.join(destination_root, filename)) do |f|
|
17
|
+
f.each_line do |line|
|
18
|
+
match = line if line =~ /(#{Regexp.escape(str)})/mi
|
19
|
+
end
|
20
|
+
end
|
21
|
+
match
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|