Class: Sessions::Generators::MadminGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/sessions/madmin_generator.rb

Overview

rails generate sessions:madmin — drop-in admin surfaces for https://github.com/excid3/madmin: the live session registry (with a per-row Revoke action) and the login trail with its triage scopes.

The generated files use only STOCK Madmin APIs, so they work on any Madmin install and are yours to restyle (swap in your custom fields, add member actions). Two Madmin footguns are pre-solved in the generated code: the trail's namespaced model needs an explicit resource_class_name on its flat controller, and the events routes must be drawn BEFORE resources :sessions (or /sessions/events gets captured as a session id).

Instance Method Summary collapse

Instance Method Details

#check_for_madmin!Object

Raises:

  • (Thor::Error)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/sessions/madmin_generator.rb', line 26

def check_for_madmin!
  return if madmin_available?

  raise Thor::Error, <<~MSG
    āŒ Madmin isn't loaded in this app (gem "madmin").

    This generator produces Madmin resources for the session registry and
    the login trail. For other admin frameworks, build on the same
    primitives it uses: Session.live / session.revoke! /
    Sessions::Event scopes (failed_logins, last_24_hours, …).
  MSG
end

#create_controllersObject



44
45
46
47
# File 'lib/generators/sessions/madmin_generator.rb', line 44

def create_controllers
  template "sessions_controller.rb", "app/controllers/madmin/sessions_controller.rb"
  template "session_events_controller.rb", "app/controllers/madmin/session_events_controller.rb"
end

#create_resourcesObject



39
40
41
42
# File 'lib/generators/sessions/madmin_generator.rb', line 39

def create_resources
  template "session_resource.rb", "app/madmin/resources/session_resource.rb"
  template "event_resource.rb", "app/madmin/resources/sessions/event_resource.rb"
end

#create_user_panelObject



49
50
51
52
53
54
# File 'lib/generators/sessions/madmin_generator.rb', line 49

def create_user_panel
  return unless options[:user_panel]

  copy_file "user_panel/madmin_user_panel.rb", "app/controllers/concerns/sessions/madmin_user_panel.rb"
  copy_file "user_panel/sessions.html.erb", "app/views/madmin/users/sessions.html.erb"
end

#display_post_install_messageObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/sessions/madmin_generator.rb', line 56

def display_post_install_message
  say "\nšŸ” Madmin resources for sessions installed.", :green
  say "\nTo complete the setup:"

  say "  1. Add the routes where you draw your Madmin routes"
  say "     (config/routes/madmin.rb in most apps):"
  say ""
  say "       # Login trail — drawn BEFORE `resources :sessions`, or"
  say "       # /sessions/events would match as a session id."
  say "       namespace :sessions do"
  say "         resources :events, only: [ :index, :show ], controller: \"/madmin/session_events\""
  say "       end"
  say ""
  say "       resources :sessions, only: [ :index, :show ] do"
  say "         member do"
  say "           post :revoke"
  say "         end"
  say "       end"

  say "\n  2. (Optional) Group them in the sidebar — both resources declare"
  say "     `parent: \"Security\"`; pre-seed its position in an initializer:"
  say ""
  say "       Madmin.menu.before_render do"
  say "         add label: \"Security\", position: 91"
  say "       end"

  say "\n  3. (Optional) For a per-user panel (devices + trail on the user's"
  say "     show page), re-run with --user-panel, include"
  say "     `Sessions::MadminUserPanel` in Madmin::UsersController, and add:"
  say ""
  say "       resources :users do"
  say "         member do"
  say "           get :sessions"
  say "           post :revoke_all_sessions"
  say "         end"
  say "       end"

  say "\nRevoking from the index ends the row in place: that device is signed out"
  say "on its very next matching request, and the revocation lands in the trail"
  say "with admin attribution. šŸ”\n", :green
end