rhino-rails 4.2.0 → 4.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1168eadabff8797a003d07665dfb30f4c1581e78e5df400ac9d72116d323d413
4
- data.tar.gz: 0ec4d0b8ce91cadecdc94af585261ecd148157eacf7e720494adeb566187010c
3
+ metadata.gz: 43ae824cbc25eeacd6ce09fc6a9d5b218206d8877fc672a49b728dd6fbf10b1c
4
+ data.tar.gz: 6590704092005ad3b6c0b959b0166332387b2503d8db19e57fc17be7734c2d0b
5
5
  SHA512:
6
- metadata.gz: e79bfcd2a29520962874939ec5621c5dbb6242c19681515a36aac49f0ba158a95bbf631638055a342de181248c43e2ce7f9480d77ef79900ec216682f238e3bf
7
- data.tar.gz: 264a1e3201782888918077db9e46ba4bea10bb7262d14b5d4a2eaa67ff843fd5b859579dfdfca57041d5a11babfc26f57bc23806359fcf7ca931f07c70d74d41
6
+ metadata.gz: a8007c1c7f90ceae1cb0aed88ca032009e417f0e8052897f5ed73e514d9f3c782c6304f92f9b16002179d6492f5510d7ef3a3e00f5902094a925c2dd74c3b781
7
+ data.tar.gz: a58f92293f66ad18700a9ec39bf48e0696f64c72b7ac82c64fde715775821a5f00c3b90ce8e61dbf76b4edf4973eb520bc04b075e04ffb16c84fe7253c350e74
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rhino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ module Blueprint
5
+ # Orders blueprints so that a referenced model's table is created before any
6
+ # model whose migration adds a foreign key to it (parents before children).
7
+ #
8
+ # Foreign keys are taken from +foreignId+ columns that carry a +foreign_model+
9
+ # mapping to another model in the same generation set. References that impose
10
+ # no ordering are ignored:
11
+ # - self-references (a model's FK to its own table — one migration),
12
+ # - references to models NOT in this set (e.g. Organization/User, whose
13
+ # tables are created by +rhino:install+, not the blueprint run).
14
+ #
15
+ # Uses Kahn's algorithm with a stable tie-break: among models with no
16
+ # remaining unmet dependency, the one earliest in the input order wins. The
17
+ # input is already alphabetical (by file name), so the output stays
18
+ # alphabetical wherever relationships don't force a reorder.
19
+ #
20
+ # A circular FK dependency (A -> B -> A) has no linear migration order. Such
21
+ # models are emitted in a deterministic best-effort order and reported via
22
+ # {#cycles} so the caller can warn (one side should be a nullable/deferred FK).
23
+ class Sorter
24
+ # Model names involved in a circular foreign-key dependency during the last
25
+ # {#sort} (empty when the dependency graph is acyclic).
26
+ attr_reader :cycles
27
+
28
+ def initialize
29
+ @cycles = []
30
+ end
31
+
32
+ # Re-order blueprints into a valid migration sequence (parents first).
33
+ #
34
+ # @param blueprints [Array<Hash>] normalized blueprints (each with a
35
+ # +:model+ name and +:columns+).
36
+ # @return [Array<Hash>] the blueprints, re-ordered.
37
+ def sort(blueprints)
38
+ @cycles = []
39
+ return blueprints.dup if blueprints.length < 2
40
+
41
+ by_model = {}
42
+ blueprints.each do |bp|
43
+ model = bp[:model]
44
+ by_model[model] ||= bp if model
45
+ end
46
+
47
+ dependents = {}
48
+ indegree = {}
49
+ by_model.each_key do |m|
50
+ dependents[m] = []
51
+ indegree[m] = 0
52
+ end
53
+
54
+ by_model.each do |model, bp|
55
+ seen = {}
56
+ dependency_models(bp).each do |ref|
57
+ next if ref == model || !by_model.key?(ref) || seen[ref]
58
+
59
+ seen[ref] = true
60
+ dependents[ref] << model
61
+ indegree[model] += 1
62
+ end
63
+ end
64
+
65
+ input_order = by_model.keys
66
+
67
+ # Record the models that actually participate in a cycle (reachable from
68
+ # themselves), in input order, so the caller can warn about the full cycle.
69
+ input_order.each do |model|
70
+ @cycles << model if reachable_from_self?(model, dependents)
71
+ end
72
+
73
+ ordered = []
74
+ resolved = {}
75
+ while ordered.length < by_model.length
76
+ # Earliest-input model with all dependencies already emitted...
77
+ pick = input_order.find { |m| !resolved[m] && indegree[m].zero? }
78
+ # ...or, when a cycle blocks the graph, the earliest unresolved model
79
+ # (deterministic cycle-break; the cycle itself is reported via #cycles).
80
+ pick ||= input_order.find { |m| !resolved[m] }
81
+
82
+ ordered << by_model[pick]
83
+ resolved[pick] = true
84
+ dependents[pick].each { |child| indegree[child] -= 1 }
85
+ end
86
+
87
+ ordered
88
+ end
89
+
90
+ private
91
+
92
+ # Whether +start+ can reach itself by following dependency edges — i.e. it
93
+ # participates in a circular foreign-key dependency. +adj+ maps a model to
94
+ # the models that reference it (its dependents).
95
+ def reachable_from_self?(start, adj)
96
+ stack = (adj[start] || []).dup
97
+ visited = {}
98
+ until stack.empty?
99
+ node = stack.pop
100
+ return true if node == start
101
+ next if visited[node]
102
+
103
+ visited[node] = true
104
+ (adj[node] || []).each { |n| stack << n }
105
+ end
106
+ false
107
+ end
108
+
109
+ # The model names this blueprint's migration adds foreign keys to, taken
110
+ # from its +foreignId+ columns that carry a +foreign_model+.
111
+ def dependency_models(blueprint)
112
+ (blueprint[:columns] || [])
113
+ .select { |c| c[:type] == "foreignId" && c[:foreign_model] }
114
+ .map { |c| c[:foreign_model] }
115
+ end
116
+ end
117
+ end
118
+ end
@@ -4,6 +4,7 @@ require "rhino/commands/base_command"
4
4
  require "fileutils"
5
5
  require "rhino/blueprint/blueprint_parser"
6
6
  require "rhino/blueprint/blueprint_validator"
7
+ require "rhino/blueprint/sorter"
7
8
  require "rhino/blueprint/manifest_manager"
8
9
  require "rhino/blueprint/generators/policy_generator"
9
10
  require "rhino/blueprint/generators/test_generator"
@@ -85,6 +86,27 @@ module Rhino
85
86
 
86
87
  say " Found #{yaml_files.length} blueprint(s)", :cyan
87
88
 
89
+ # Order so a referenced model's table is migrated before any model that
90
+ # foreign-keys to it (parents before children). Migration timestamps are
91
+ # assigned in iteration order, so this is what makes the set runnable.
92
+ parsed_for_order = []
93
+ unparseable = []
94
+ yaml_files.each do |f|
95
+ parsed_for_order << { file: f, blueprint: parser.parse_model(f) }
96
+ rescue StandardError
97
+ unparseable << f
98
+ end
99
+ sorter = Rhino::Blueprint::Sorter.new
100
+ ordered = sorter.sort(parsed_for_order.map { |p| p[:blueprint] })
101
+ file_by_model = parsed_for_order.each_with_object({}) do |p, h|
102
+ h[p[:blueprint][:model]] ||= p[:file]
103
+ end
104
+ yaml_files = ordered.map { |bp| file_by_model[bp[:model]] }.compact + unparseable
105
+ if sorter.cycles.any?
106
+ say " ⚠ Circular foreign-key dependency among: #{sorter.cycles.join(', ')}. " \
107
+ "Migration order is best-effort — make one side nullable or add the FK in a later migration.", :yellow
108
+ end
109
+
88
110
  # 3. Process each blueprint
89
111
  is_multi_tenant = multi_tenant_enabled?
90
112
  org_identifier = detect_org_identifier
@@ -109,7 +109,8 @@ module Rhino
109
109
  "create_users" => "#{timestamp}00",
110
110
  "create_organizations" => "#{timestamp}01",
111
111
  "create_roles" => "#{timestamp}02",
112
- "create_user_roles" => "#{timestamp}03"
112
+ "create_user_roles" => "#{timestamp}03",
113
+ "create_org_role_permissions" => "#{timestamp}04"
113
114
  }.each do |name, ts|
114
115
  template = File.join(templates_dir, "#{name}.rb.erb")
115
116
  next unless File.exist?(template)
@@ -125,7 +126,7 @@ module Rhino
125
126
 
126
127
  templates_dir = File.expand_path("../../templates/multi_tenant/models", __FILE__)
127
128
 
128
- %w[user organization role user_role].each do |model|
129
+ %w[user organization role user_role org_role_permission].each do |model|
129
130
  template = File.join(templates_dir, "#{model}.rb.erb")
130
131
  next unless File.exist?(template)
131
132
 
@@ -12,21 +12,36 @@ module Rhino
12
12
  # end
13
13
  #
14
14
  # Permission format: '{slug}.{action}' (e.g., 'posts.index', 'blogs.store')
15
- # Wildcard support:
16
- # - '*' grants access to everything
17
- # - 'posts.*' grants access to all actions on posts
15
+ # Wildcard support on every layer:
16
+ # - '*' grants/denies everything
17
+ # - 'posts.*' grants/denies all actions on posts
18
18
  #
19
- # Two permission sources:
20
- # - users.permissions: used for non-tenant route groups (no organization context).
21
- # Stored as a JSON array directly on the user model.
22
- # - role.permissions (via user_roles): used for tenant route groups (organization context present).
23
- # Resolved per-organization via the user_roles → role association.
19
+ # ── Layered resolution (organization context) ────────────────────────────
20
+ # The effective decision for an org-scoped check is:
24
21
  #
25
- # Resolution:
26
- # 1. When an organization is provided (tenant route group) → checks role.permissions
27
- # for that specific organization via user_roles.
28
- # 2. When no organization is provided (non-tenant route group) → checks users.permissions
29
- # directly on the user model.
22
+ # effective = (role ∪ granted) − denied (deny always wins)
23
+ #
24
+ # - role → org_role_permissions[(organization, role)].permissions
25
+ # The shared "role layer" an org manages once per role.
26
+ # - granted user_roles.granted_permissions (per-user additive delta)
27
+ # - denied → user_roles.denied_permissions (per-user subtractive delta)
28
+ # - legacy → user_roles.permissions (kept in the allow set)
29
+ #
30
+ # Deny is checked first and overrides everything — even a role '*'. This is
31
+ # intentionally deny-overrides (not most-specific-wins).
32
+ #
33
+ # Backward compatibility:
34
+ # - The global roles.permissions column is preserved as a FALLBACK: it is
35
+ # consulted only when the primary union (legacy ∪ granted ∪ org role layer)
36
+ # is empty — exactly the pre-layer "fall back to role.permissions when
37
+ # user_role.permissions is empty" behavior.
38
+ # - When org_role_permissions has no row and the granted/denied columns are
39
+ # absent, resolution reduces to the previous behavior byte-for-byte.
40
+ #
41
+ # Sources:
42
+ # 1. organization provided (tenant route group) → user_roles layers above.
43
+ # 2. no organization (non-tenant route group) → users.permissions (with
44
+ # optional user-level granted/denied if those columns exist; deny wins).
30
45
  module HasPermissions
31
46
  extend ActiveSupport::Concern
32
47
 
@@ -34,54 +49,58 @@ module Rhino
34
49
  #
35
50
  # @param permission [String] Permission string like 'posts.index'
36
51
  # @param organization [Object, nil] Organization to check permissions for
52
+ # @param route_group [String, nil] Resolved route group (group enforcement only)
37
53
  # @return [Boolean]
38
54
  def has_permission?(permission, organization = nil, route_group: nil)
39
55
  return false if permission.blank?
40
56
 
41
57
  # Group-aware permission resolution (GROUP_AUTH_DESIGN.md §6). Only active
42
58
  # when enforce_group_membership is on. Permissions then resolve from the
43
- # membership row matching (route_group, organization), not the heuristic.
59
+ # membership row matching (route_group, organization).
44
60
  if group_membership_enforced?
45
61
  membership = Rhino::GroupMembership.matching_membership(self, route_group, organization)
46
- return false unless membership
62
+ return decide_for_record(permission, membership, organization)
63
+ end
47
64
 
48
- ur_permissions = parse_permissions(membership.respond_to?(:permissions) ? membership.permissions : nil)
49
- return matches_permission?(permission, ur_permissions) if ur_permissions.present?
65
+ if organization
66
+ # Tenant route group: layered resolution from the user_role for this org.
67
+ return decide_for_record(permission, find_user_role(organization), organization)
68
+ end
50
69
 
51
- role = membership.respond_to?(:role) ? membership.role : nil
52
- if role
53
- role_permissions = parse_permissions(role.respond_to?(:permissions) ? role.permissions : nil)
54
- return matches_permission?(permission, role_permissions) if role_permissions.present?
55
- end
70
+ # Non-tenant route group: users.permissions (+ optional user-level deltas).
71
+ deny = parse_permissions(safe_attr(self, :denied_permissions))
72
+ return false if matches_permission?(permission, deny)
73
+
74
+ allow = parse_permissions(safe_attr(self, :permissions)) +
75
+ parse_permissions(safe_attr(self, :granted_permissions))
76
+ matches_permission?(permission, allow)
77
+ end
78
+
79
+ # Explain a permission decision — returns the deciding layer.
80
+ #
81
+ # @return [Hash] { granted: Boolean, reason: String }
82
+ # reason ∈ { 'denied', 'role', 'granted', 'legacy', 'user', 'default-deny' }
83
+ def explain_permission(permission, organization = nil, route_group: nil)
84
+ return { granted: false, reason: "default-deny" } if permission.blank?
56
85
 
57
- return false
86
+ if group_membership_enforced?
87
+ membership = Rhino::GroupMembership.matching_membership(self, route_group, organization)
88
+ return decide_for_record(permission, membership, organization, explain: true)
58
89
  end
59
90
 
60
91
  if organization
61
- # Tenant route group: check permissions for this organization
62
- user_role = find_user_role(organization)
63
-
64
- if user_role
65
- # Check user_role.permissions first (per-user-org permissions on the pivot)
66
- ur_permissions = parse_permissions(user_role.respond_to?(:permissions) ? user_role.permissions : nil)
67
- if ur_permissions.present?
68
- return matches_permission?(permission, ur_permissions)
69
- end
70
-
71
- # Fallback to role.permissions (shared role-level permissions)
72
- role = user_role.respond_to?(:role) ? user_role.role : nil
73
- if role
74
- role_permissions = parse_permissions(role.respond_to?(:permissions) ? role.permissions : nil)
75
- return matches_permission?(permission, role_permissions) if role_permissions.present?
76
- end
77
- end
78
-
79
- return false
92
+ return decide_for_record(permission, find_user_role(organization), organization, explain: true)
80
93
  end
81
94
 
82
- # Non-tenant route group: check users.permissions directly
83
- user_perms = parse_permissions(respond_to?(:permissions) ? self.permissions : nil)
84
- matches_permission?(permission, user_perms)
95
+ deny = parse_permissions(safe_attr(self, :denied_permissions))
96
+ return { granted: false, reason: "denied" } if matches_permission?(permission, deny)
97
+
98
+ user = parse_permissions(safe_attr(self, :permissions))
99
+ granted = parse_permissions(safe_attr(self, :granted_permissions))
100
+ return { granted: true, reason: "granted" } if matches_permission?(permission, granted)
101
+ return { granted: true, reason: "user" } if matches_permission?(permission, user)
102
+
103
+ { granted: false, reason: "default-deny" }
85
104
  end
86
105
 
87
106
  # Get the role slug for validation purposes.
@@ -100,7 +119,77 @@ module Rhino
100
119
 
101
120
  private
102
121
 
122
+ # Resolve a decision from a single membership/user_role record, applying
123
+ # deny-overrides over the layered allow set.
124
+ def decide_for_record(permission, record, organization, explain: false)
125
+ return explain ? { granted: false, reason: "default-deny" } : false unless record
126
+
127
+ # Deny always wins.
128
+ deny = parse_permissions(safe_attr(record, :denied_permissions))
129
+ if matches_permission?(permission, deny)
130
+ return explain ? { granted: false, reason: "denied" } : false
131
+ end
132
+
133
+ role_id = record.respond_to?(:role_id) ? record.role_id : nil
134
+ role_layer = org_role_permissions(organization, role_id)
135
+ granted = parse_permissions(safe_attr(record, :granted_permissions))
136
+ legacy = parse_permissions(safe_attr(record, :permissions))
137
+
138
+ primary = legacy + granted + role_layer
139
+
140
+ if primary.present?
141
+ unless explain
142
+ return matches_permission?(permission, primary)
143
+ end
144
+
145
+ return { granted: true, reason: "role" } if matches_permission?(permission, role_layer)
146
+ return { granted: true, reason: "granted" } if matches_permission?(permission, granted)
147
+ return { granted: true, reason: "legacy" } if matches_permission?(permission, legacy)
148
+ return { granted: false, reason: "default-deny" }
149
+ end
150
+
151
+ # Legacy fallback: the global roles.permissions column, consulted only when
152
+ # the primary union is empty (preserving pre-layer behavior).
153
+ role = record.respond_to?(:role) ? record.role : nil
154
+ global = role ? parse_permissions(safe_attr(role, :permissions)) : []
155
+ allowed = matches_permission?(permission, global)
156
+
157
+ return { granted: allowed, reason: allowed ? "role" : "default-deny" } if explain
158
+
159
+ allowed
160
+ end
161
+
162
+ # Resolve the shared role-layer permissions for (organization, role) from the
163
+ # org_role_permissions table. Memoized per instance; tolerant of the table not
164
+ # existing (un-migrated apps) so it degrades to "no role layer".
165
+ def org_role_permissions(organization, role_id)
166
+ return [] unless organization && role_id
167
+
168
+ org_id = organization.respond_to?(:id) ? organization.id : organization
169
+ return [] if org_id.nil?
170
+
171
+ @_org_role_permissions_cache ||= {}
172
+ key = "#{org_id}:#{role_id}"
173
+ return @_org_role_permissions_cache[key] if @_org_role_permissions_cache.key?(key)
174
+
175
+ perms =
176
+ begin
177
+ sql = ActiveRecord::Base.sanitize_sql_array(
178
+ ["SELECT permissions FROM org_role_permissions WHERE organization_id = ? AND role_id = ? LIMIT 1",
179
+ org_id, role_id]
180
+ )
181
+ row = ActiveRecord::Base.connection.select_one(sql)
182
+ row ? parse_permissions(row["permissions"]) : []
183
+ rescue ActiveRecord::ActiveRecordError
184
+ # Table absent (app has not run the new migration) → no role layer.
185
+ []
186
+ end
187
+
188
+ @_org_role_permissions_cache[key] = perms
189
+ end
190
+
103
191
  def matches_permission?(permission, granted_permissions)
192
+ return false if permission.blank?
104
193
  return true if granted_permissions.include?(permission)
105
194
  return true if granted_permissions.include?("*")
106
195
 
@@ -115,7 +204,8 @@ module Rhino
115
204
 
116
205
  if perms.is_a?(String)
117
206
  begin
118
- JSON.parse(perms)
207
+ parsed = JSON.parse(perms)
208
+ parsed.is_a?(Array) ? parsed : []
119
209
  rescue JSON::ParserError
120
210
  []
121
211
  end
@@ -126,6 +216,12 @@ module Rhino
126
216
  end
127
217
  end
128
218
 
219
+ def safe_attr(obj, name)
220
+ return nil unless obj.respond_to?(name)
221
+
222
+ obj.public_send(name)
223
+ end
224
+
129
225
  def find_user_role(organization)
130
226
  return nil unless respond_to?(:user_roles)
131
227
  return nil unless organization
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Rhino
6
+ # Lift per-user permissions into the shared org role layer.
7
+ #
8
+ # For each (organization, role) group, the literal intersection of every user's
9
+ # `user_roles.permissions` becomes the `org_role_permissions` row (the shared
10
+ # role layer). Each user's row is then reduced to only its delta
11
+ # (`granted_permissions = permissions − roleLayer`) and its legacy `permissions`
12
+ # is cleared. Effective permissions are preserved exactly (the intersection is a
13
+ # subset of every user's set, so nothing is gained or lost).
14
+ #
15
+ # Safe & idempotent:
16
+ # - Dry-run by default; pass apply: true to write.
17
+ # - Groups that already have an org_role_permissions row are skipped.
18
+ # - After a run the legacy permissions are empty, so a second run is a no-op.
19
+ # - Non-tenant (NULL organization) rows are left untouched.
20
+ class PermissionsMigrator
21
+ Result = Struct.new(:groups_migrated, :rows_reduced, :skipped_existing, :lines, keyword_init: true)
22
+
23
+ def self.call(apply: false)
24
+ new.call(apply: apply)
25
+ end
26
+
27
+ def call(apply: false)
28
+ conn = ActiveRecord::Base.connection
29
+ unless conn.data_source_exists?("user_roles") && conn.data_source_exists?("org_role_permissions")
30
+ raise "Required tables (user_roles, org_role_permissions) are missing. Run migrations first."
31
+ end
32
+
33
+ groups = conn.select_all(
34
+ "SELECT DISTINCT organization_id, role_id FROM user_roles " \
35
+ "WHERE organization_id IS NOT NULL AND role_id IS NOT NULL"
36
+ )
37
+
38
+ groups_migrated = 0
39
+ rows_reduced = 0
40
+ skipped_existing = 0
41
+ lines = []
42
+
43
+ groups.each do |g|
44
+ org_id = g["organization_id"]
45
+ role_id = g["role_id"]
46
+
47
+ rows = conn.select_all(
48
+ ActiveRecord::Base.sanitize_sql_array(
49
+ ["SELECT id, permissions, granted_permissions FROM user_roles " \
50
+ "WHERE organization_id = ? AND role_id = ?", org_id, role_id]
51
+ )
52
+ ).to_a
53
+
54
+ with_legacy = rows.select { |r| decode(r["permissions"]).any? }
55
+ next if with_legacy.empty?
56
+
57
+ existing = conn.select_value(
58
+ ActiveRecord::Base.sanitize_sql_array(
59
+ ["SELECT 1 FROM org_role_permissions WHERE organization_id = ? AND role_id = ? LIMIT 1",
60
+ org_id, role_id]
61
+ )
62
+ )
63
+ if existing
64
+ skipped_existing += 1
65
+ next
66
+ end
67
+
68
+ sets = with_legacy.map { |r| decode(r["permissions"]) }
69
+ role_layer = sets.reduce { |acc, s| acc & s } || []
70
+ lines << "org=#{org_id} role=#{role_id} → role layer [#{role_layer.join(', ')}] (#{with_legacy.size} user rows)"
71
+
72
+ if apply
73
+ now = Time.now.utc
74
+ conn.execute(
75
+ ActiveRecord::Base.sanitize_sql_array(
76
+ ["INSERT INTO org_role_permissions (organization_id, role_id, permissions, created_at, updated_at) " \
77
+ "VALUES (?, ?, ?, ?, ?)", org_id, role_id, JSON.generate(role_layer), now, now]
78
+ )
79
+ )
80
+
81
+ with_legacy.each do |r|
82
+ legacy = decode(r["permissions"])
83
+ grants = decode(r["granted_permissions"])
84
+ delta = ((legacy - role_layer) + grants).uniq
85
+
86
+ conn.execute(
87
+ ActiveRecord::Base.sanitize_sql_array(
88
+ ["UPDATE user_roles SET permissions = ?, granted_permissions = ?, updated_at = ? WHERE id = ?",
89
+ JSON.generate([]), JSON.generate(delta), now, r["id"]]
90
+ )
91
+ )
92
+ end
93
+ end
94
+
95
+ groups_migrated += 1
96
+ rows_reduced += with_legacy.size
97
+ end
98
+
99
+ Result.new(
100
+ groups_migrated: groups_migrated,
101
+ rows_reduced: rows_reduced,
102
+ skipped_existing: skipped_existing,
103
+ lines: lines
104
+ )
105
+ end
106
+
107
+ private
108
+
109
+ def decode(value)
110
+ return value.select { |v| v.is_a?(String) } if value.is_a?(Array)
111
+
112
+ if value.is_a?(String) && !value.empty?
113
+ parsed = begin
114
+ JSON.parse(value)
115
+ rescue JSON::ParserError
116
+ []
117
+ end
118
+ return parsed.is_a?(Array) ? parsed.select { |v| v.is_a?(String) } : []
119
+ end
120
+
121
+ []
122
+ end
123
+ end
124
+ end
@@ -26,6 +26,19 @@ namespace :rhino do
26
26
  cmd.perform
27
27
  end
28
28
 
29
+ desc "Lift per-user permissions into the org_role_permissions role layer (APPLY=1 to write)"
30
+ task :permissions_migrate, [:apply] => :environment do |_t, args|
31
+ require "rhino/permissions_migrator"
32
+ apply = args[:apply].to_s == "apply" || ENV["APPLY"] == "1"
33
+ result = Rhino::PermissionsMigrator.call(apply: apply)
34
+ result.lines.each { |line| puts line }
35
+ verb = apply ? "Migrated" : "Would migrate"
36
+ summary = "#{verb} #{result.groups_migrated} (org, role) group(s); #{result.rows_reduced} user row(s) reduced to deltas."
37
+ summary += " Skipped #{result.skipped_existing} group(s) with an existing role layer." if result.skipped_existing.positive?
38
+ puts summary
39
+ puts "Dry-run only. Re-run with APPLY=1 to write these changes." if !apply && result.groups_migrated.positive?
40
+ end
41
+
29
42
  desc "Generate TypeScript type definitions from registered Rhino models"
30
43
  task :export_types, [:output] => :environment do |_t, args|
31
44
  require "rhino/commands/export_types_command"
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateOrgRolePermissions < ActiveRecord::Migration[8.0]
4
+ # The shared "role layer": the permission set a role has within a given
5
+ # organization. Defining permissions here (once per org+role) means user_roles
6
+ # rows only need to carry per-user deltas (granted_permissions /
7
+ # denied_permissions) instead of the full permission set.
8
+ def change
9
+ create_table :org_role_permissions do |t|
10
+ t.references :organization, null: false, foreign_key: true
11
+ t.references :role, null: false, foreign_key: true
12
+ t.json :permissions, default: []
13
+
14
+ t.timestamps
15
+ end
16
+
17
+ # One row per (organization, role).
18
+ add_index :org_role_permissions, %i[organization_id role_id], unique: true,
19
+ name: "index_org_role_permissions_on_org_role"
20
+ end
21
+ end
@@ -11,7 +11,14 @@ class CreateUserRoles < ActiveRecord::Migration[8.0]
11
11
  # Group membership scope. NULL = wildcard (member of every group), which
12
12
  # keeps existing rows valid when enforce_group_membership is enabled.
13
13
  t.string :route_group
14
+ # Legacy per-user permission set (full list). Still honored as an allow
15
+ # layer for backward compatibility.
14
16
  t.json :permissions, default: []
17
+ # Layered-permission deltas applied on top of the org role layer
18
+ # (org_role_permissions). granted = additive, denied = subtractive.
19
+ # Deny always wins. Leave empty to inherit the role layer as-is.
20
+ t.json :granted_permissions, default: []
21
+ t.json :denied_permissions, default: []
15
22
 
16
23
  t.timestamps
17
24
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The shared "role layer" for permissions: what a role can do within a given
4
+ # organization. One row per (organization, role).
5
+ #
6
+ # Resolution (see Rhino::HasPermissions):
7
+ # effective = (org_role_permissions ∪ user_roles.granted_permissions)
8
+ # − user_roles.denied_permissions (deny always wins)
9
+ class OrgRolePermission < ApplicationRecord
10
+ belongs_to :organization
11
+ belongs_to :role
12
+
13
+ validates :role_id, uniqueness: { scope: :organization_id }
14
+ end
data/lib/rhino/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rhino
4
- VERSION = "4.2.0"
4
+ VERSION = "4.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhino-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Cipolla
@@ -186,6 +186,7 @@ executables: []
186
186
  extensions: []
187
187
  extra_rdoc_files: []
188
188
  files:
189
+ - MIT-LICENSE
189
190
  - README.md
190
191
  - lib/rhino-rails.rb
191
192
  - lib/rhino.rb
@@ -198,6 +199,7 @@ files:
198
199
  - lib/rhino/blueprint/generators/seeder_generator.rb
199
200
  - lib/rhino/blueprint/generators/test_generator.rb
200
201
  - lib/rhino/blueprint/manifest_manager.rb
202
+ - lib/rhino/blueprint/sorter.rb
201
203
  - lib/rhino/commands/base_command.rb
202
204
  - lib/rhino/commands/blueprint_command.rb
203
205
  - lib/rhino/commands/export_postman_command.rb
@@ -224,6 +226,7 @@ files:
224
226
  - lib/rhino/models/audit_log.rb
225
227
  - lib/rhino/models/organization_invitation.rb
226
228
  - lib/rhino/models/rhino_model.rb
229
+ - lib/rhino/permissions_migrator.rb
227
230
  - lib/rhino/policies/invitation_policy.rb
228
231
  - lib/rhino/policies/resource_policy.rb
229
232
  - lib/rhino/query_builder.rb
@@ -244,10 +247,12 @@ files:
244
247
  - lib/rhino/templates/multi_tenant/factories/user_roles.rb.erb
245
248
  - lib/rhino/templates/multi_tenant/factories/users.rb.erb
246
249
  - lib/rhino/templates/multi_tenant/migrations/add_group_membership.rb.erb
250
+ - lib/rhino/templates/multi_tenant/migrations/create_org_role_permissions.rb.erb
247
251
  - lib/rhino/templates/multi_tenant/migrations/create_organizations.rb.erb
248
252
  - lib/rhino/templates/multi_tenant/migrations/create_roles.rb.erb
249
253
  - lib/rhino/templates/multi_tenant/migrations/create_user_roles.rb.erb
250
254
  - lib/rhino/templates/multi_tenant/migrations/create_users.rb.erb
255
+ - lib/rhino/templates/multi_tenant/models/org_role_permission.rb.erb
251
256
  - lib/rhino/templates/multi_tenant/models/organization.rb.erb
252
257
  - lib/rhino/templates/multi_tenant/models/role.rb.erb
253
258
  - lib/rhino/templates/multi_tenant/models/user.rb.erb