rabarber 6.1.0 → 6.1.1
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +30 -14
- data/lib/generators/rabarber/roles_generator.rb +6 -2
- data/lib/generators/rabarber/templates/create_rabarber_roles.rb.erb +2 -2
- data/lib/rabarber/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4d7c9478a3e3bae59ba23360971a29fc00f0718c511a8529cdf4865d69a59685
|
|
4
|
+
data.tar.gz: 11c553d7b6cba6941feaccfb45b30099c383540b3cbc351f0ccf7a2bd2a16ff5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2210f4142798a26facddda4b28ddb428c4b5ea8f996899b9491457a6783a394daf27c10166743ace01160ba1c1768a5e3d930732750ef6e314cbee2848c630c8
|
|
7
|
+
data.tar.gz: 1f4587f2d045041ddf81e1a75935a152c15994b3d14e073eef89ee7875cbdebb99def7be7df22dad27cf943b1e7fef91a818c5631613149ab3d9d575e07f4fcf
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -34,10 +34,9 @@ end
|
|
|
34
34
|
- [User Role Methods](#user-role-methods)
|
|
35
35
|
- [Direct Role Management](#direct-role-management)
|
|
36
36
|
- [Authorization](#authorization)
|
|
37
|
-
- [Dynamic Authorization Rules](#dynamic-authorization-rules)
|
|
38
|
-
- [When Unauthorized](#when-unauthorized)
|
|
39
37
|
- [Context / Multi-tenancy](#context--multi-tenancy)
|
|
40
38
|
- [View Helpers](#view-helpers)
|
|
39
|
+
- [Caching](#caching)
|
|
41
40
|
|
|
42
41
|
**Community Resources:**
|
|
43
42
|
- [Getting Help and Contributing](#getting-help-and-contributing)
|
|
@@ -65,15 +64,28 @@ Generate the migration to store roles (replace `users` with your table name if d
|
|
|
65
64
|
rails generate rabarber:roles users
|
|
66
65
|
```
|
|
67
66
|
|
|
68
|
-
Rabarber's tables use the same primary key type as your users table, so the table must exist before you run the generator.
|
|
67
|
+
Rabarber's tables use the same primary key type as your users table, so the table must exist before you run the generator.
|
|
68
|
+
|
|
69
|
+
If your database can't handle your UUIDs on its own, apply your app's UUID handling to Rabarber's models in an initializer. The examples below show which models and attributes need it; the code itself depends on how your app handles UUIDs. If your app generates UUIDs:
|
|
70
|
+
|
|
71
|
+
```rb
|
|
72
|
+
Rails.application.config.to_prepare do
|
|
73
|
+
Rabarber::Role.before_create { self.id ||= SecureRandom.uuid }
|
|
74
|
+
end
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
If it also stores them as binary, add your binary type on top of that:
|
|
69
78
|
|
|
70
79
|
```rb
|
|
71
80
|
Rails.application.config.to_prepare do
|
|
72
|
-
Rabarber::Role.
|
|
73
|
-
Rabarber::
|
|
81
|
+
Rabarber::Role.before_create { self.id ||= SecureRandom.uuid }
|
|
82
|
+
[:id, :context_id].each { |name| Rabarber::Role.attribute name, YourBinaryUuidType.new }
|
|
83
|
+
[:role_id, :roleable_id].each { |name| Rabarber::RoleAssignment.attribute name, YourBinaryUuidType.new }
|
|
74
84
|
end
|
|
75
85
|
```
|
|
76
86
|
|
|
87
|
+
If your app sets any other kind of id itself, such as its own integer ids, do the same with your own id handling.
|
|
88
|
+
|
|
77
89
|
Run the migration:
|
|
78
90
|
|
|
79
91
|
```shell
|
|
@@ -95,12 +107,6 @@ Rabarber.configure do |config|
|
|
|
95
107
|
end
|
|
96
108
|
```
|
|
97
109
|
|
|
98
|
-
Roles are cached by default for better performance. Clear the cache manually when needed:
|
|
99
|
-
|
|
100
|
-
```rb
|
|
101
|
-
Rabarber::Cache.clear
|
|
102
|
-
```
|
|
103
|
-
|
|
104
110
|
## User Role Methods
|
|
105
111
|
|
|
106
112
|
Your user model is automatically augmented with role management methods:
|
|
@@ -275,7 +281,7 @@ class MixedController < ApplicationController
|
|
|
275
281
|
end
|
|
276
282
|
```
|
|
277
283
|
|
|
278
|
-
|
|
284
|
+
### Dynamic Authorization Rules
|
|
279
285
|
|
|
280
286
|
For more complex scenarios, Rabarber supports dynamic authorization rules:
|
|
281
287
|
|
|
@@ -324,7 +330,7 @@ class InvoicesController < ApplicationController
|
|
|
324
330
|
end
|
|
325
331
|
```
|
|
326
332
|
|
|
327
|
-
|
|
333
|
+
### When Unauthorized
|
|
328
334
|
|
|
329
335
|
By default, when unauthorized, Rabarber will redirect back (HTML format) or return 403 (other formats). You can override `when_unauthorized` method to customize unauthorized access behavior:
|
|
330
336
|
|
|
@@ -446,7 +452,7 @@ Rabarber.prune
|
|
|
446
452
|
|
|
447
453
|
### Context Migrations
|
|
448
454
|
|
|
449
|
-
When you rename or remove models used as contexts, you need to update Rabarber's stored context data accordingly.
|
|
455
|
+
When you rename or remove models used as contexts, you need to update Rabarber's stored context data accordingly. Rabarber adds these helpers to your migrations. The changes they make are irreversible:
|
|
450
456
|
|
|
451
457
|
```rb
|
|
452
458
|
# Rename a context class (e.g., when you rename your Ticket model to Task)
|
|
@@ -490,6 +496,16 @@ Use conditional rendering based on roles:
|
|
|
490
496
|
|
|
491
497
|
Like authorization, these helpers require an authenticated user. In views that can be rendered for both authenticated and unauthenticated visitors, wrap them in your authentication check.
|
|
492
498
|
|
|
499
|
+
## Caching
|
|
500
|
+
|
|
501
|
+
Roles are cached by default for better performance. They're kept in your app's cache store for up to an hour, so all app processes must share the same store. Otherwise, a role change made in one process may not be seen by the others until their cached copy expires. If you can't use a shared store, disable caching.
|
|
502
|
+
|
|
503
|
+
Rabarber clears cached roles automatically when you assign, revoke, rename, or delete roles. If you ever need to clear the cache manually, run:
|
|
504
|
+
|
|
505
|
+
```rb
|
|
506
|
+
Rabarber::Cache.clear
|
|
507
|
+
```
|
|
508
|
+
|
|
493
509
|
## Getting Help and Contributing
|
|
494
510
|
|
|
495
511
|
### Getting Help
|
|
@@ -11,12 +11,16 @@ module Rabarber
|
|
|
11
11
|
argument :table_name, type: :string, required: true
|
|
12
12
|
|
|
13
13
|
def create_migrations
|
|
14
|
-
|
|
14
|
+
primary_key = ActiveRecord::Base.with_connection do |connection|
|
|
15
15
|
raise Thor::Error, "Table `#{table_name}` does not exist, run its migration first" unless connection.table_exists?(table_name)
|
|
16
16
|
|
|
17
|
-
connection.columns(table_name).find { |column| column.name == connection.primary_key(table_name) }
|
|
17
|
+
connection.columns(table_name).find { |column| column.name == connection.primary_key(table_name) }
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
@id_type = primary_key.sql_type
|
|
21
|
+
@roles_id_type = @id_type unless primary_key.auto_incremented_by_db?
|
|
22
|
+
@roles_id_default = primary_key.default_function if @roles_id_type
|
|
23
|
+
|
|
20
24
|
migration_template "create_rabarber_roles.rb.erb", "db/migrate/create_rabarber_roles.rb"
|
|
21
25
|
end
|
|
22
26
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
class CreateRabarberRoles < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version.to_s %>]
|
|
4
4
|
def change
|
|
5
|
-
create_table :rabarber_roles do |t|
|
|
5
|
+
create_table :rabarber_roles<%= ", id: #{@roles_id_type.inspect}" if @roles_id_type %><%= ", default: -> { #{@roles_id_default.inspect} }" if @roles_id_default %> do |t|
|
|
6
6
|
t.string :name, null: false
|
|
7
7
|
t.belongs_to :context, polymorphic: true, index: true, type: <%= @id_type.inspect %>
|
|
8
8
|
t.timestamps
|
|
@@ -16,7 +16,7 @@ class CreateRabarberRoles < ActiveRecord::Migration[<%= ActiveRecord::Migration.
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
create_table :rabarber_roles_roleables, id: false do |t|
|
|
19
|
-
t.belongs_to :role, null: false, index: true, foreign_key: { to_table: :rabarber_roles }
|
|
19
|
+
t.belongs_to :role, null: false, index: true, foreign_key: { to_table: :rabarber_roles }<%= ", type: #{@roles_id_type.inspect}" if @roles_id_type %>
|
|
20
20
|
t.belongs_to :roleable, null: false, index: true, foreign_key: { to_table: <%= table_name.to_sym.inspect %>, primary_key: primary_key(<%= table_name.to_sym.inspect %>) }, type: <%= @id_type.inspect %>
|
|
21
21
|
end
|
|
22
22
|
|
data/lib/rabarber/version.rb
CHANGED