Class: Kowl::UsersAndAuthGenerator

Inherits:
Generators::Base show all
Defined in:
lib/kowl/generators/users_and_auth_generator.rb

Instance Method Summary collapse

Methods inherited from Generators::Base

default_source_root, source_paths

Methods included from Docker

#alpine_docker_dependencies, #app_js_volumes, #app_volumes, #db_volumes, #debian_database_dependencies, #debian_docker_dependencies, #docker_app_command, #docker_compose_database_string, #docker_databases, #docker_depends_on, #docker_port_watcher, #docker_redis_service, #docker_sidekiq_service, #docker_variables, #docker_volumes, #docker_webpacker_service, #dockerfile_database_args, #dockerfile_migration_snip, #js_volumes, #mysql_volumes, #postgresql_volumes, #redis_volumes

Methods included from Actions

#add_extension_routes, #add_package, #append_to_file, #database_route, #dev_config, #dup_file, #file_exists?, #mailer_gems, #mailer_route, #mk_dir, #move_file, #pry_gems, #rails_cmd, #remove_dir, #remove_file, #remove_gem, #replace_string_in_file, #robocop_test_engine, #sidekiq_route, #template_linter_gems

Instance Method Details

#adjust_devise_migrationObject

Update the devise migration to enable trackable and lockable attributes



55
56
57
58
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 55

def adjust_devise_migration
  # Fetch the devise migration file
  Dir.glob('db/migrate/**_devise_create_users.rb').select { |e| update_devise_migration(e) }
end

#adjust_devise_notificationsObject

Update devise/User notificationss



74
75
76
77
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 74

def adjust_devise_notifications
  inject_into_file('config/initializers/devise.rb', optimize_indentation("config.send_email_changed_notification = true\n", 2), after: "  # config.send_email_changed_notification = false\n")
  inject_into_file('config/initializers/devise.rb', optimize_indentation("config.send_password_change_notification = true\n", 2), after: "  # config.send_password_change_notification = false\n")
end

#adjust_lock_stragetyObject

Change devise lock_strategy



67
68
69
70
71
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 67

def adjust_lock_stragety
  devise_initialzier = 'config/initializers/devise.rb'
  content = File.read(devise_initialzier).gsub(/\s?\#\s(config.lock_strategy = :failed_attempts)\n?/i, optimize_indentation('config.lock_strategy = :failed_attempts', 2))
  File.open(devise_initialzier, 'wb') { |file| file.write(content) }
end

#copy_login_activity_modelObject

Generate a model for monitoring user login activity



86
87
88
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 86

def 
  template 'app/models/login_activity.rb.tt', 'app/models/login_activity.rb', force: true
end

#generate_and_copy_policiesObject

Generate Pundit policies for basic authentication policies



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 37

def generate_and_copy_policies
  # Begin setting up pundit
  generate('pundit:install')
  # these are generated, so they'll also create tests policies
  generate('pundit:policy user')
  generate('pundit:policy login_activity')

  # Remove policies and replace with a base policy build
  remove_dir('app/policies')
  directory('app/policies', 'app/policies')
end

#generate_dashboard_views_and_assetsObject

Generate administrate viewss



97
98
99
100
101
102
103
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 97

def generate_dashboard_views_and_assets
  # The main reason these are generated, before copying over is some tests may rely on theses generators
  generate('administrate:views:index User --quiet')
  generate('administrate:views:edit User --quiet')
  generate('administrate:assets:stylesheets --quiet')
  generate('administrate:views:navigation --quiet')
end

#generate_deviseObject

Run devise generators unless you don’t want authentication allowed in the application



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 20

def generate_devise
  # generate devise config
  generate('devise:install')

  # Generate devise users models
  generate('devise User role:integer')

  # Generate additional Devise security practices
  generate('devise_security:install')
  # Copy initializer for ensure a more secure devise application setup
  copy_file('config/initializers/devise-security.rb', 'config/initializers/devise-security.rb', force: true) unless options[:noauth]

  # Generate devise authentication log
  generate('authtrail:install')
end

#generate_users_controllerObject

Generate and copy a Users controller



91
92
93
94
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 91

def generate_users_controller
  generate('controller Users') # copied mainly for testing purposes
  copy_file 'app/controllers/users_controller.rb', 'app/controllers/users_controller.rb', force: true
end

#overrider_devise_configObject

Override user model with devise specific settings



50
51
52
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 50

def overrider_devise_config
  template 'app/models/user.rb.tt', 'app/models/user.rb', force: true
end

#replace_logins_activity_migrationObject

This is because we encrypt certain values being logged into the LoginActivity table

No not all data is plain text readable in the database


62
63
64
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 62

def replace_logins_activity_migration
  Dir.glob('db/migrate/**_create_login_activities.rb').select { |e| (e) }
end

#set_devise_as_paranoidObject

Update devise initializer to be paranoid, to prevent credential stuffing with password resets



80
81
82
83
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 80

def set_devise_as_paranoid
  # This displays the visitor with a confirmation even if the user does or doesn't exist
  gsub_file('config/initializers/devise.rb', '# config.paranoid = true', 'config.paranoid = true')
end

#setup_dashboard_stylesheetsObject

Copy over administrate SCSS stylesheet files



113
114
115
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 113

def setup_dashboard_stylesheets
  copy_file('app/assets/stylesheets/administrate/application.scss', 'app/assets/stylesheets/administrate/application.scss', force: true)
end

#setup_dashbord_javascriptObject

Add flattpickr to administrate for datetime picker fields



118
119
120
121
122
123
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 118

def setup_dashbord_javascript
  return nil if options[:skip_javascript]

  # flatpicker is required for date/time selection components
  add_package('flatpickr')
end

#setup_user_dashboard_viewsObject

Copy over admin views and layouts



106
107
108
109
110
# File 'lib/kowl/generators/users_and_auth_generator.rb', line 106

def setup_user_dashboard_views
  remove_dir('app/views/admin')
  directory('app/views/admin/views', 'app/views/admin')
  template('app/views/admin/templates/navigation.erb.tt', 'app/views/admin/application/_navigation.html.erb')
end