bro_hub 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e8da50149016fb9bbf5775f4257a85855b058372b1077ee0e1f8e24d6ac4811
4
- data.tar.gz: c609ff0fca64a15b7a3682cdcc74c5a61ff8b944e18c075d0583b6515e0fa1e2
3
+ metadata.gz: b1fbd1cddc86bdf0bc203daff3dbe7eab88f49d7046c2f7444b215b8a24ddee1
4
+ data.tar.gz: 80a631c65cf8f57fca41e6c67b059a978aef763aa9fcdc1d386634b0bae43de7
5
5
  SHA512:
6
- metadata.gz: 2ee002d30eff44fa14bbff1261b76c0ac101ea6adf6c756e328abb6d059e9e7971f75912e03db7f1a05e490205250a0396109d9db6742a22ba87a70c9d6fae9a
7
- data.tar.gz: 8f963753dd73fa6a0ac48e4c59e130ddd5f5d5bbb47e5e73228580c1bef3f48e2067b21ce004498f8dcd494913ae2e82937e6336ad08c276f3d4b7cdf92d059c
6
+ metadata.gz: 5fc326ddbfcb13bbb77adae1928de8a1cbbdff6ab454c1b8896ea81a3d8c0d08ff3ecea53ed75af1e47f5eecb0c9cbf8a2691066d6ffe66a76560234af9ac022
7
+ data.tar.gz: 3cc2551c4d6cdf13fce248f5b18991bdfd9c1c54cc39bfa4983e815898ddeb322da844880e469a760e094d47974357b11a9968ff8a99c2835f0db6c8945f18f2
data/README.md CHANGED
@@ -17,6 +17,9 @@ Rails 7.1+, Ruby 3.2+. No database, no JavaScript, no asset pipeline.
17
17
  gem "bro_hub"
18
18
  ```
19
19
 
20
+ Minimum versions: **env_bro 0.2.0** and **safe_bro 0.1.0**. Older EnvBro (0.1.x) has no BroHub support: the installer skips
21
+ it with a warning and leaves its old mount alone, so `bundle update env_bro` first.
22
+
20
23
  ```sh
21
24
  bin/rails generate bro_hub:install
22
25
  ```
@@ -32,9 +35,16 @@ This sets up the whole dashboard in one go:
32
35
  (`--remove-direct-mounts` says yes without asking; without a terminal the lines are left alone with a warning),
33
36
  4. runs the installer of every Bro gem in your Gemfile (SafeBro's initializer and migrations, EnvBro's, ...).
34
37
 
38
+ Existing config is never overwritten: if `config/initializers/bro_hub.rb` (or `env_bro.rb` / `safe_bro.rb`) is there already,
39
+ it is skipped, and `--skip` / `--force` / `--pretend` are passed on to every gem's installer. In scripts and agents, pass
40
+ `--skip` and `--mount-path=...` so nothing waits on a prompt.
41
+
35
42
  You can also run a gem's own installer on its own: `bin/rails generate safe_bro:install` sets up BroHub first, then SafeBro.
36
43
  Running both, in any order, mounts BroHub once.
37
44
 
45
+ Who is signed in: the default calls `current_user` on your controller, and falls back to `Current.user` (the Rails 8
46
+ authentication generator has only that). The top-right label tries `email`, `email_address` (Rails 8) then `name`.
47
+
38
48
  ```ruby
39
49
  # config/initializers/bro_hub.rb
40
50
  BroHub.configure do |config|
@@ -75,12 +85,12 @@ BroHub.register :safe_bro,
75
85
 
76
86
  ```ruby
77
87
  class SafeBro::ApplicationController < ::ApplicationController
78
- include BroHub::Layout
88
+ include BroHub::ControllerBase
79
89
  bro_hub_section :safe_bro
80
90
  end
81
91
  ```
82
92
 
83
- `BroHub::Layout` sets the layout and adds `bro_hub_user`. Override it if your gem has its own idea of who is signed in.
93
+ `BroHub::ControllerBase` includes `BroHub::Layout` (the layout, and `bro_hub_user`), turns on CSRF protection and opts out of the host app's Pundit `verify_authorized` checks, so every Bro controller behaves the same under any host `ApplicationController`. Override it if your gem has its own idea of who is signed in.
84
94
 
85
95
  **3. Give your gem an installer** that sets up BroHub first, then itself. Name it `<section key>:install` so
86
96
  `bro_hub:install` finds it:
@@ -1,8 +1,6 @@
1
1
  module BroHub
2
2
  class ApplicationController < BroHub.config.parent_controller.constantize
3
- include BroHub::Layout
4
-
5
- protect_from_forgery with: :exception
3
+ include BroHub::ControllerBase
6
4
 
7
5
  before_action :bro_hub_require_ssl
8
6
  before_action :bro_hub_no_store
@@ -3,7 +3,8 @@ module BroHub
3
3
  # Controller the shell's own pages inherit from, so the host's authentication runs.
4
4
  attr_accessor :parent_controller
5
5
 
6
- # ->(controller) { the signed-in user, or nil }. The default calls `current_user` when the controller has it.
6
+ # ->(controller) { the signed-in user, or nil }. The default calls `current_user` when the controller has it,
7
+ # and otherwise falls back to `Current.user` (what the Rails 8 authentication generator sets up).
7
8
  attr_accessor :current_user
8
9
 
9
10
  # ->(user) { text shown at the top right }
@@ -16,8 +17,14 @@ module BroHub
16
17
 
17
18
  def initialize
18
19
  @parent_controller = "::ApplicationController"
19
- @current_user = ->(controller) { controller.send(:current_user) if controller.respond_to?(:current_user, true) }
20
- @user_label = ->(user) { user.try(:email) || user.try(:name) || user.to_s }
20
+ @current_user = ->(controller) do
21
+ if controller.respond_to?(:current_user, true)
22
+ controller.send(:current_user)
23
+ elsif defined?(::Current) && ::Current.respond_to?(:user)
24
+ ::Current.user
25
+ end
26
+ end
27
+ @user_label = ->(user) { user.try(:email) || user.try(:email_address) || user.try(:name) || user.to_s }
21
28
  @require_ssl = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env.production? : false
22
29
  @brand_name = "DeadBro"
23
30
  @brand_url = "https://www.deadbro.com"
@@ -0,0 +1,22 @@
1
+ module BroHub
2
+ # Include in every Bro controller base (BroHub's own, and each gem's ApplicationController). It holds
3
+ # what must be right for *any* host app, so a clash with the host's ApplicationController is fixed once, here:
4
+ #
5
+ # class SafeBro::ApplicationController < SafeBro.config.parent_controller.constantize
6
+ # include BroHub::ControllerBase
7
+ # bro_hub_section :safe_bro
8
+ module ControllerBase
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include BroHub::Layout
13
+
14
+ protect_from_forgery with: :exception
15
+
16
+ # The host's Pundit checks (`after_action :verify_authorized`) don't apply: Bro controllers authorize
17
+ # people themselves and have no policy to ask. No-ops when Pundit isn't in use.
18
+ skip_after_action :verify_authorized, raise: false
19
+ skip_after_action :verify_policy_scoped, raise: false
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module BroHub
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/bro_hub.rb CHANGED
@@ -3,6 +3,7 @@ require "bro_hub/configuration"
3
3
  require "bro_hub/section"
4
4
  require "bro_hub/registry"
5
5
  require "bro_hub/layout"
6
+ require "bro_hub/controller_base"
6
7
  require "bro_hub/root_middleware"
7
8
  require "bro_hub/engine"
8
9
 
@@ -19,8 +19,25 @@ module BroHub
19
19
  class_option :remove_direct_mounts, type: :boolean, default: nil,
20
20
  desc: "Comment out old `mount SafeBro::Engine`-style lines (asked interactively when omitted)"
21
21
 
22
+ # Bro gems that only work with BroHub from these versions on.
23
+ MINIMUM_VERSIONS = { "env_bro" => "0.2.0", "safe_bro" => "0.1.0" }.freeze
24
+
25
+ def check_gem_versions
26
+ MINIMUM_VERSIONS.each do |name, minimum|
27
+ installed = Gem.loaded_specs[name]&.version
28
+ next if installed.nil? || installed >= Gem::Version.new(minimum)
29
+
30
+ say_status :warning, "#{name} #{installed} is too old for BroHub (needs #{minimum}+), so it is not installed " \
31
+ "and its old mount is left alone. Run `bundle update #{name}` and install again.", :red
32
+ end
33
+ end
34
+
22
35
  def copy_initializer
23
- copy_file "bro_hub.rb", "config/initializers/bro_hub.rb"
36
+ if File.exist?(File.join(destination_root, "config/initializers/bro_hub.rb"))
37
+ say_status :skip, "config/initializers/bro_hub.rb already exists, leaving it alone", :yellow
38
+ else
39
+ copy_file "bro_hub.rb", "config/initializers/bro_hub.rb"
40
+ end
24
41
  end
25
42
 
26
43
  def mount_engine
@@ -35,7 +52,8 @@ module BroHub
35
52
  def retire_direct_mounts
36
53
  direct_mounts.each do |line|
37
54
  if remove_direct_mount?(line)
38
- gsub_file "config/routes.rb", line, "# BroHub mounts this now: #{line.strip}"
55
+ indent = line[/\A\s*/]
56
+ gsub_file "config/routes.rb", line, "#{indent}# BroHub mounts this now: #{line.strip}"
39
57
  else
40
58
  say_status :warning, "#{line.strip} is still mounted directly; BroHub also serves it under #{mount_path}. " \
41
59
  "Re-run with --remove-direct-mounts or delete the line.", :yellow
@@ -48,7 +66,7 @@ module BroHub
48
66
 
49
67
  bro_gem_installers.each do |section, namespace|
50
68
  say_status :install, "#{section.name} (#{namespace})", :green
51
- invoke namespace, [], skip_bro_hub: true, mount_path: mount_path
69
+ invoke namespace, [], { skip_bro_hub: true, mount_path: mount_path }.merge(conflict_options)
52
70
  end
53
71
  end
54
72
 
@@ -62,6 +80,11 @@ module BroHub
62
80
  end
63
81
 
64
82
  private
83
+ # Pass --skip/--force/--pretend/--quiet on to the installers of the other gems.
84
+ def conflict_options
85
+ options.slice(:force, :skip, :pretend, :quiet).to_h.symbolize_keys.select { |_, value| value }
86
+ end
87
+
65
88
  def routes_source
66
89
  path = File.join(destination_root, "config/routes.rb")
67
90
  File.exist?(path) ? File.read(path) : ""
@@ -1,9 +1,10 @@
1
1
  BroHub.configure do |config|
2
- # How to find the signed-in user. The default calls `current_user` on the controller when it has one.
2
+ # How to find the signed-in user. The default calls `current_user` on the controller when it has one,
3
+ # and falls back to `Current.user` (Rails 8 authentication generator).
3
4
  # config.current_user = ->(controller) { controller.current_user }
4
5
 
5
6
  # What to show at the top right.
6
- # config.user_label = ->(user) { user.email }
7
+ # config.user_label = ->(user) { user.email } # Rails 8 auth generator: user.email_address (also tried by default)
7
8
 
8
9
  # Which controller the dashboard's own pages inherit from (so your authentication runs).
9
10
  # config.parent_controller = "::ApplicationController"
@@ -29,7 +29,7 @@ module BroHub
29
29
  def install_bro_hub
30
30
  return if options[:skip_bro_hub]
31
31
 
32
- forward = { install_gems: false }
32
+ forward = { install_gems: false }.merge(bro_hub_conflict_options)
33
33
  forward[:mount_path] = options[:mount_path] if options[:mount_path].present?
34
34
  forward[:remove_direct_mounts] = options[:remove_direct_mounts] unless options[:remove_direct_mounts].nil?
35
35
  invoke "bro_hub:install", [], forward
@@ -37,6 +37,21 @@ module BroHub
37
37
  end
38
38
 
39
39
  private
40
+ # --skip / --force / --pretend / --quiet must reach the generators we invoke, or a nested
41
+ # installer would fall back to asking about conflicts (and with no terminal, overwrite).
42
+ def bro_hub_conflict_options
43
+ options.slice(:force, :skip, :pretend, :quiet).to_h.symbolize_keys.select { |_, value| value }
44
+ end
45
+
46
+ # Copies a config file once. An existing file is yours: it is never replaced, even with --force.
47
+ def copy_config_once(source, destination)
48
+ if File.exist?(File.join(destination_root, destination))
49
+ say_status :skip, "#{destination} already exists, leaving it alone", :yellow
50
+ else
51
+ copy_file source, destination
52
+ end
53
+ end
54
+
40
55
  # Where BroHub ended up, read back from the routes (it may have been mounted before).
41
56
  def bro_hub_path
42
57
  routes = File.join(destination_root, "config/routes.rb")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bro_hub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby Dev SRL
@@ -44,6 +44,7 @@ files:
44
44
  - config/routes.rb
45
45
  - lib/bro_hub.rb
46
46
  - lib/bro_hub/configuration.rb
47
+ - lib/bro_hub/controller_base.rb
47
48
  - lib/bro_hub/engine.rb
48
49
  - lib/bro_hub/layout.rb
49
50
  - lib/bro_hub/registry.rb