rhino-rails 4.7.0 → 4.7.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 +4 -4
- data/lib/rhino/concerns/route_group_context.rb +59 -0
- data/lib/rhino/configuration.rb +26 -2
- data/lib/rhino/context.rb +10 -0
- data/lib/rhino/engine.rb +1 -0
- data/lib/rhino/missing_tenant_context.rb +10 -1
- data/lib/rhino/query.rb +9 -4
- data/lib/rhino/templates/rhino.rb +16 -0
- data/lib/rhino/version.rb +1 -1
- 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: 632861555ee3e2508a90ec546b6cb0412368bc141a7d387b2f92698531605b86
|
|
4
|
+
data.tar.gz: 91b3ad79b1c0b0b492f1f3003e906ce11e9f3fa62e81b067ecfb9c0b7740a025
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2c5fee208f22ae88b0531f4075e6183d6d0a17137c43423e7f491dbf341848764ce16418feca131b13c45c4295f6b4a3507eebb52e1e30b2f887dcbcfdd624c1
|
|
7
|
+
data.tar.gz: 83468b5afe671a2b0313778e57c2f053747129156d643bb85d5543513ea2d02aa6c2b63c4b70bb5f04613cb79bbb21544ba142bb4f127ebf56247581aab4c079
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rhino
|
|
4
|
+
# Places a custom (non-Rhino) controller into a Rhino route group, so the
|
|
5
|
+
# resource-scope resolver knows whether the request has a tenant boundary.
|
|
6
|
+
#
|
|
7
|
+
# Rhino's own generated controllers already publish their group; a controller
|
|
8
|
+
# you write yourself is outside that chain, so include this concern in it:
|
|
9
|
+
#
|
|
10
|
+
# class Admin::DashboardController < ApplicationController
|
|
11
|
+
# include Rhino::RouteGroupContext
|
|
12
|
+
# rhino_route_group :admin
|
|
13
|
+
#
|
|
14
|
+
# def summary
|
|
15
|
+
# # :admin is declared `tenant: false`, so this spans every organization
|
|
16
|
+
# # instead of raising Rhino::MissingTenantContext.
|
|
17
|
+
# render json: { tasks: Rhino.query(Task).count }
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# Without an explicit `rhino_route_group`, the group is read from the route's
|
|
22
|
+
# own `route_group` default:
|
|
23
|
+
#
|
|
24
|
+
# get "/api/admin/dashboard", to: "admin/dashboard#summary",
|
|
25
|
+
# defaults: { route_group: "admin" }
|
|
26
|
+
module RouteGroupContext
|
|
27
|
+
extend ActiveSupport::Concern
|
|
28
|
+
|
|
29
|
+
included do
|
|
30
|
+
# Inheritable, so a base controller can declare the group for a whole
|
|
31
|
+
# namespace and subclasses may override it.
|
|
32
|
+
class_attribute :rhino_route_group_name, instance_writer: false, default: nil
|
|
33
|
+
|
|
34
|
+
before_action :rhino_set_route_group
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class_methods do
|
|
38
|
+
# Declare the route group this controller's actions belong to.
|
|
39
|
+
def rhino_route_group(name)
|
|
40
|
+
self.rhino_route_group_name = name.to_s
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# Publish the group for Rhino::Context (and therefore for Rhino.query, the
|
|
47
|
+
# policies and the permission resolver) for the rest of this request.
|
|
48
|
+
def rhino_set_route_group
|
|
49
|
+
return unless defined?(RequestStore)
|
|
50
|
+
|
|
51
|
+
# An explicit declaration wins; otherwise fall back to the route's own
|
|
52
|
+
# `route_group` default, which only exists once a request is bound.
|
|
53
|
+
resolved = rhino_route_group_name.presence
|
|
54
|
+
resolved ||= params[:route_group].presence if request
|
|
55
|
+
|
|
56
|
+
RequestStore.store[:rhino_route_group] = resolved
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/lib/rhino/configuration.rb
CHANGED
|
@@ -62,7 +62,15 @@ module Rhino
|
|
|
62
62
|
# captures the subdomain and feeds organization resolution exactly like the
|
|
63
63
|
# path-prefix ":organization" does. Groups without a domain (nil/blank)
|
|
64
64
|
# match any host (default, fully backward compatible).
|
|
65
|
-
|
|
65
|
+
#
|
|
66
|
+
# The optional `tenant:` keyword declares whether the group has a tenant
|
|
67
|
+
# boundary. It defaults to true: Rhino.query inside the group fails closed,
|
|
68
|
+
# raising Rhino::MissingTenantContext when an organization-scopable model is
|
|
69
|
+
# queried with no organization resolved. Pass `tenant: false` for a group
|
|
70
|
+
# that legitimately spans every organization — a back office or admin group
|
|
71
|
+
# whose operators see all tenants' rows.
|
|
72
|
+
def route_group(name, prefix: "", domain: nil, middleware: [], models: :all, auth: false, hooks: nil,
|
|
73
|
+
tenant: true)
|
|
66
74
|
normalized_domain = domain.to_s.strip
|
|
67
75
|
normalized_domain = nil if normalized_domain.empty?
|
|
68
76
|
|
|
@@ -72,10 +80,26 @@ module Rhino
|
|
|
72
80
|
middleware: Array(middleware),
|
|
73
81
|
models: models,
|
|
74
82
|
auth: !!auth,
|
|
75
|
-
hooks: hooks
|
|
83
|
+
hooks: hooks,
|
|
84
|
+
tenant: tenant != false
|
|
76
85
|
}
|
|
77
86
|
end
|
|
78
87
|
|
|
88
|
+
# Whether the named route group has a tenant boundary, i.e. whether
|
|
89
|
+
# Rhino.query must fail closed inside it. Only a group that explicitly
|
|
90
|
+
# declares `tenant: false` does not; every other answer — an unknown group,
|
|
91
|
+
# an untagged route, or no group at all (jobs, rake tasks, console) — is
|
|
92
|
+
# true, so the resolver keeps failing closed wherever the group is not
|
|
93
|
+
# provably non-tenant.
|
|
94
|
+
def group_tenant?(name)
|
|
95
|
+
return true if name.nil? || name.to_s.empty?
|
|
96
|
+
|
|
97
|
+
group = @route_groups[name.to_sym]
|
|
98
|
+
return true unless group
|
|
99
|
+
|
|
100
|
+
group.fetch(:tenant, true) != false
|
|
101
|
+
end
|
|
102
|
+
|
|
79
103
|
# Resolve a model class from its slug
|
|
80
104
|
def resolve_model(slug)
|
|
81
105
|
klass_name = @models[slug.to_sym]
|
data/lib/rhino/context.rb
CHANGED
|
@@ -30,6 +30,16 @@ module Rhino
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# The route group serving the current request, as stashed into RequestStore
|
|
34
|
+
# by Rhino's own controllers and by Rhino::RouteGroupContext in custom ones.
|
|
35
|
+
# Nil outside a request or when the route carries no group.
|
|
36
|
+
def route_group
|
|
37
|
+
return nil unless defined?(RequestStore)
|
|
38
|
+
|
|
39
|
+
value = RequestStore.store[:rhino_route_group]
|
|
40
|
+
value.nil? || value.to_s.empty? ? nil : value
|
|
41
|
+
end
|
|
42
|
+
|
|
33
43
|
# Run +block+ with the given user/organization installed into RequestStore.
|
|
34
44
|
# Snapshots the prior RequestStore user+org, sets the new ones, yields, and
|
|
35
45
|
# restores the snapshot in an ensure. Returns the block's value.
|
data/lib/rhino/engine.rb
CHANGED
|
@@ -4,5 +4,14 @@ module Rhino
|
|
|
4
4
|
# Raised by Rhino.query (and the explicit builder) when an organization-scopable
|
|
5
5
|
# model is queried with no organization context available. Fail-closed: the
|
|
6
6
|
# resolver never returns an unscoped relation for a tenant-scopable model.
|
|
7
|
-
class MissingTenantContext < StandardError
|
|
7
|
+
class MissingTenantContext < StandardError
|
|
8
|
+
def initialize(model_class = nil)
|
|
9
|
+
super(
|
|
10
|
+
"Rhino.query(#{model_class}) requires an organization context but none is set. " \
|
|
11
|
+
"Use Rhino.for_user(...).in_organization(...) outside a tenant request, or declare " \
|
|
12
|
+
"the route group serving this request non-tenant with `tenant: false` if it " \
|
|
13
|
+
"legitimately spans every organization."
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
8
17
|
end
|
data/lib/rhino/query.rb
CHANGED
|
@@ -20,7 +20,10 @@ module Rhino
|
|
|
20
20
|
# (BelongsToOrganization / HasAutoScope read RequestStore at BUILD time).
|
|
21
21
|
#
|
|
22
22
|
# Fail closed: an org-scopable model with no org context RAISES
|
|
23
|
-
# Rhino::MissingTenantContext rather than returning an unscoped relation
|
|
23
|
+
# Rhino::MissingTenantContext rather than returning an unscoped relation —
|
|
24
|
+
# unless the request is served by a route group declared non-tenant
|
|
25
|
+
# (`tenant: false`), where a query legitimately spans every organization. An
|
|
26
|
+
# explicit organization is always honored, in every group.
|
|
24
27
|
def query(model_class)
|
|
25
28
|
org = Rhino::Context.organization
|
|
26
29
|
|
|
@@ -28,9 +31,11 @@ module Rhino
|
|
|
28
31
|
relation = model_class.all
|
|
29
32
|
|
|
30
33
|
if Rhino::ScopesToOrganization.organization_scoped?(model_class)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
if org
|
|
35
|
+
relation = Rhino::ScopesToOrganization.scope_to_organization(relation, model_class, org, strict: true)
|
|
36
|
+
elsif Rhino.config.group_tenant?(Rhino::Context.route_group)
|
|
37
|
+
raise Rhino::MissingTenantContext, model_class.name
|
|
38
|
+
end
|
|
34
39
|
end
|
|
35
40
|
|
|
36
41
|
relation
|
|
@@ -44,6 +44,22 @@ Rhino.configure do |config|
|
|
|
44
44
|
# the path prefix ':organization' does. Groups without a domain match any host.
|
|
45
45
|
# config.route_group :admin, prefix: '', domain: 'admin.example.com', models: :all
|
|
46
46
|
# config.route_group :tenant, prefix: '', domain: '{organization}.example.com', models: :all
|
|
47
|
+
#
|
|
48
|
+
# The optional `tenant:` keyword declares whether a group has a tenant
|
|
49
|
+
# boundary. It defaults to true: Rhino.query inside the group fails closed,
|
|
50
|
+
# raising Rhino::MissingTenantContext when an organization-scopable model is
|
|
51
|
+
# queried with no organization resolved. Pass `tenant: false` for a group that
|
|
52
|
+
# legitimately spans every organization — a back office whose operators see all
|
|
53
|
+
# tenants' rows. There, Rhino.query applies no organization filter and does not
|
|
54
|
+
# raise; the models' own scopes and the policies still apply, and an explicit
|
|
55
|
+
# Rhino.for_user(u).in_organization(org) still scopes.
|
|
56
|
+
# config.route_group :admin, prefix: 'admin', tenant: false, models: :all
|
|
57
|
+
#
|
|
58
|
+
# Custom (non-Rhino) controllers publish their group by including
|
|
59
|
+
# Rhino::RouteGroupContext and declaring `rhino_route_group :admin`, or by
|
|
60
|
+
# tagging the route with `defaults: { route_group: 'admin' }`. Outside a
|
|
61
|
+
# request (jobs, rake tasks) no group resolves and the resolver keeps failing
|
|
62
|
+
# closed.
|
|
47
63
|
|
|
48
64
|
# config.route_group :default, prefix: '', middleware: [], models: :all
|
|
49
65
|
|
data/lib/rhino/version.rb
CHANGED
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.7.
|
|
4
|
+
version: 4.7.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bruno Cipolla
|
|
@@ -215,6 +215,7 @@ files:
|
|
|
215
215
|
- lib/rhino/concerns/has_uuid.rb
|
|
216
216
|
- lib/rhino/concerns/has_validation.rb
|
|
217
217
|
- lib/rhino/concerns/hidable_columns.rb
|
|
218
|
+
- lib/rhino/concerns/route_group_context.rb
|
|
218
219
|
- lib/rhino/configuration.rb
|
|
219
220
|
- lib/rhino/context.rb
|
|
220
221
|
- lib/rhino/controllers/auth_controller.rb
|