bullet_train-roles 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -2
- data/README.md +15 -5
- data/lib/bullet_train/roles/version.rb +1 -1
- data/lib/bullet_train/roles.rb +1 -0
- data/lib/generators/bullet_train/roles/install/install_generator.rb +32 -9
- data/lib/generators/bullet_train/roles/install/templates/roles.yml +0 -2
- data/lib/models/role.rb +0 -2
- data/lib/roles/user.rb +26 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b04c87b6d26e9f326fc15cd10833fe874d9a2351d5a733d8c5fcff2210a10b7d
|
4
|
+
data.tar.gz: 7993de795a580ecac546e2697c79af02e7803f1530d3d9da51801b849ed53d37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8cd90ecb2da1e985dfe304fbbf287a7c4878415844888f359659deeca337271b18a793f79888897f10ee629d6f3986ffbf047dc65c0cf2d6776598771da345b
|
7
|
+
data.tar.gz: 21ae06c5cd83a62edca13832ad4a59330273b97022aff8b0ffdc7236b8f0dad238d459fb9610882f1511ee5e3559b790a2fc9156c8f024f97b00767971ef3ad1
|
data/Gemfile.lock
CHANGED
@@ -9,7 +9,7 @@ GIT
|
|
9
9
|
PATH
|
10
10
|
remote: .
|
11
11
|
specs:
|
12
|
-
bullet_train-roles (0.1.
|
12
|
+
bullet_train-roles (0.1.1)
|
13
13
|
active_hash
|
14
14
|
activesupport
|
15
15
|
cancancan
|
@@ -173,6 +173,7 @@ GEM
|
|
173
173
|
|
174
174
|
PLATFORMS
|
175
175
|
arm64-darwin-20
|
176
|
+
arm64-darwin-21
|
176
177
|
|
177
178
|
DEPENDENCIES
|
178
179
|
active_hash!
|
@@ -187,4 +188,4 @@ DEPENDENCIES
|
|
187
188
|
standard (~> 1.5.0)
|
188
189
|
|
189
190
|
BUNDLED WITH
|
190
|
-
2.
|
191
|
+
2.3.4
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Bullet Train Roles
|
2
2
|
|
3
|
-
|
3
|
+
Bullet Train Roles provides a Yaml-based configuration layer on top of [CanCanCan](https://github.com/CanCanCommunity/cancancan). You can use this configuration file to simplify the definition of many common permissions, while still implementing more complicated permissions in CanCanCan's traditional `app/model/ability.rb`.
|
4
|
+
|
5
|
+
Additionally, Bullet Train Roles makes it trivial to assign the same roles and associated permissions at different levels in your application. For example, you can assign someone administrative privileges at a team level, or only at a project level.
|
6
|
+
|
7
|
+
Bullet Train Roles was created by [Andrew Culver](http://twitter.com/andrewculver) and [Adam Pallozzi](https://twitter.com/adampallozzi).
|
4
8
|
|
5
9
|
## Example Domain Model
|
6
10
|
|
@@ -17,12 +21,15 @@ You don't have to name your models the same thing in order to use this Ruby Gem,
|
|
17
21
|
|
18
22
|
## Installation
|
19
23
|
|
20
|
-
Add
|
24
|
+
Add these lines to your application's Gemfile:
|
21
25
|
|
22
26
|
```ruby
|
27
|
+
gem "active_hash", github: "bullet-train-co/active_hash"
|
23
28
|
gem "bullet_train-roles"
|
24
29
|
```
|
25
30
|
|
31
|
+
> We have to link to a specific downstream version of ActiveHash temporarily while working to merge certain fixes and updates upstream.
|
32
|
+
|
26
33
|
And then execute the following in your shell:
|
27
34
|
|
28
35
|
```
|
@@ -32,23 +39,26 @@ bundle install
|
|
32
39
|
Finally, run the installation generator:
|
33
40
|
|
34
41
|
```
|
35
|
-
rails generate bullet_train:roles:install
|
42
|
+
rails generate bullet_train:roles:install
|
36
43
|
```
|
37
44
|
|
38
|
-
|
45
|
+
The installer defaults to installing a configuration for `Membership` and `Team`, but it will prompt you so you can specify different models if they differ in your application.
|
46
|
+
|
47
|
+
The installer will:
|
39
48
|
|
40
49
|
- stub out a configuration file in `config/models/roles.yml`.
|
41
50
|
- create a database migration to add `role_ids:jsonb` to `Membership`.
|
42
51
|
- add `include Role::Support` to `app/models/membership.rb`.
|
43
52
|
- add a basic `permit` call in `app/models/ability.rb`.
|
44
53
|
|
54
|
+
|
45
55
|
### Limitations
|
46
56
|
|
47
57
|
The generators currently assume you're using PostgreSQL and `jsonb` will be available when generating a `role_ids` column. If you're using MySQL, you can edit these migrations and use `json` instead, although you won't be able to set a default value and you'll need to take care of this in the model.
|
48
58
|
|
49
59
|
## Usage
|
50
60
|
|
51
|
-
The provided `Role` model is backed
|
61
|
+
The provided `Role` model is backed by a Yaml configuration in `config/models/roles.yml`.
|
52
62
|
|
53
63
|
To help explain this configuration and it's options, we'll provide the following hypothetical example:
|
54
64
|
|
data/lib/bullet_train/roles.rb
CHANGED
@@ -31,6 +31,7 @@ module BulletTrain
|
|
31
31
|
# TODO: follow back Andrew on question about this in Slack
|
32
32
|
|
33
33
|
add_permit_to_ability_model(top_level_model, associated_model)
|
34
|
+
add_concern_to_user
|
34
35
|
end
|
35
36
|
|
36
37
|
private
|
@@ -160,22 +161,44 @@ module BulletTrain
|
|
160
161
|
end
|
161
162
|
|
162
163
|
def add_permit_to_ability_model(top_level_model, associated_model)
|
163
|
-
# TODO: need to make this more smart e.g.
|
164
|
-
# 1. should know what parameter is used for user e.g. def initialize(user)
|
165
|
-
# 2. what if code doesn't include "if user.present?", maybe we need to handle that, consult with Andrew
|
166
164
|
file_location = "app/models/ability.rb"
|
167
|
-
line_to_match = "
|
168
|
-
content_to_add = "\n
|
169
|
-
|
170
|
-
puts("Adding 'permit user, through: :#{top_level_model.downcase}, parent: :#{associated_model.downcase}' to #{associated_model}\n\n")
|
165
|
+
line_to_match = "include CanCan::Ability"
|
166
|
+
content_to_add = "\n include Roles::Permit\n"
|
167
|
+
puts("Adding 'include Roles::Permit' to #{associated_model}\n\n")
|
171
168
|
|
172
169
|
if line_exists_in_file?(file_location, content_to_add)
|
173
170
|
message = "#{remove_new_lines_and_spaces(content_to_add)} already exists in #{associated_model}!!\n\n"
|
171
|
+
line_already_exists(message)
|
172
|
+
else
|
173
|
+
add_in_file(file_location, line_to_match, content_to_add)
|
174
|
+
end
|
174
175
|
|
175
|
-
|
176
|
+
line_to_match = "def initialize(user)"
|
177
|
+
content_to_add = "\n permit user, through: :#{top_level_model.downcase.pluralize}, parent: :#{associated_model.downcase} if user.present?\n"
|
178
|
+
puts("Adding 'permit' to #{associated_model}\n\n")
|
179
|
+
|
180
|
+
if line_exists_in_file?(file_location, content_to_add)
|
181
|
+
message = "#{remove_new_lines_and_spaces(content_to_add)} already exists in #{associated_model}!!\n\n"
|
182
|
+
line_already_exists(message)
|
183
|
+
else
|
184
|
+
add_in_file(file_location, line_to_match, content_to_add)
|
176
185
|
end
|
177
186
|
|
178
|
-
|
187
|
+
puts("Success 🎉🎉\n\n")
|
188
|
+
end
|
189
|
+
|
190
|
+
def add_concern_to_user
|
191
|
+
file_location = "app/models/user.rb"
|
192
|
+
line_to_match = "class User < ApplicationRecord"
|
193
|
+
content_to_add = "\n include Roles::User\n"
|
194
|
+
puts("Adding 'include Roles::User' to User\n\n")
|
195
|
+
|
196
|
+
if line_exists_in_file?(file_location, content_to_add)
|
197
|
+
message = "#{remove_new_lines_and_spaces(content_to_add)} already exists in User!!\n\n"
|
198
|
+
line_already_exists(message)
|
199
|
+
else
|
200
|
+
add_in_file(file_location, line_to_match, content_to_add)
|
201
|
+
end
|
179
202
|
|
180
203
|
puts("Success 🎉🎉\n\n")
|
181
204
|
end
|
data/lib/models/role.rb
CHANGED
data/lib/roles/user.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support"
|
4
|
+
|
5
|
+
module Roles
|
6
|
+
module User
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def parent_ids_for(role, through, parent)
|
11
|
+
parent_id_column = "#{parent}_id"
|
12
|
+
key = "#{role.key}_#{through}_#{parent_id_column}s"
|
13
|
+
# TODO Maybe we should make ability caching a default feature of the gem?
|
14
|
+
# If we do that, we would just make it check whether `ability_cache` exists.
|
15
|
+
# return ability_cache[key] if ability_cache && ability_cache[key]
|
16
|
+
role = nil if role.default?
|
17
|
+
value = send(through).with_role(role).distinct.pluck(parent_id_column)
|
18
|
+
# TODO Maybe we should make ability caching a default feature of the gem?
|
19
|
+
# current_cache = ability_cache || {}
|
20
|
+
# current_cache[key] = value
|
21
|
+
# update_column :ability_cache, current_cache
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-roles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Prabin Poudel
|
@@ -192,6 +192,7 @@ files:
|
|
192
192
|
- lib/models/role.rb
|
193
193
|
- lib/roles/permit.rb
|
194
194
|
- lib/roles/support.rb
|
195
|
+
- lib/roles/user.rb
|
195
196
|
homepage: https://github.com/bullet-train-co/bullet_train-roles
|
196
197
|
licenses:
|
197
198
|
- MIT
|