Last active 1 month ago

admin.rb Raw
1# replace line 17 with this:
2 devise :pwned_password if Rails.env.production?
create_admin.rb Raw
1# Run this script and follow the onscreen instructions:
2# bundle exec rails r script/create_admin.rb
3
4require "csv"
5
6include Rails.application.routes.url_helpers
7
8# Base URL
9default_url_options[:host] = ArchiveConfig.APP_URL
10
11def multi_gets(all_text = +"")
12 until (text = gets) == "\n"
13 all_text << text
14 end
15 all_text.chomp
16end
17puts "======== This is the #{Rails.env} environment. ========"
18puts <<~PROMPT
19 Paste or enter admins, one per line, in the format
20
21 USERNAME, EMAIL, PASSWORD, ROLE
22
23 or
24
25 USERNAME, EMAIL, PASSWORD, ROLE, ROLE, ROLE
26
27 where
28
29 - USERNAME is their username without the admin- prefix (spaces will be removed). e.g. if you put in "carly" the username will end up as "admin-carly"
30 - EMAIL is whatever their email is
31 - PASSWORD is a password of at least ten characters
32 - EMAIL/PASSWORD/ROLE can be left blank to skip updating email/roles of existing admins
33 - for dev you probably just want to make a superadmin
34 - ROLE is one of:
35
36 #{Admin::VALID_ROLES.sort.map { |r| " #{r}" }.join("\n")}
37
38 then two line breaks to end:
39
40PROMPT
41
42list = CSV.parse(multi_gets)
43
44admins = []
45list.each do |user|
46 login = user[0].gsub(/\s+/, "")
47 email = user[1]&.strip
48 password = user[2]&.strip
49 roles = user.drop(3).compact.map(&:strip).map(&:downcase)
50
51 a = Admin.find_or_initialize_by(login: "admin-#{login}")
52 success_message = a.new_record? ? "Created and notified" : "Updated"
53 a.email = email if email.present?
54 a.roles = roles if roles.present?
55
56 # Set their password if that's set
57 if password.present?
58 a.password = password
59 a.password_confirmation = password
60 end
61
62 puts "==== #{a.login}"
63 if a.save
64 puts success_message
65 admins << a
66 else
67 puts a.errors.full_messages
68 end
69 puts
70end
71
72admins.sort_by!(&:created_at)
73admins.each do |admin|
74 role_description = admin.roles.map { |r| I18n.t("activerecord.attributes.admin/role.#{r}") }.sort.join(", ")
75 role_description = "UPDATE WITH USER COMMITTEE" if role_description.blank?
76 puts "|-\n| #{admin.created_at.strftime('%Y-%m-%d')} || #{admin.login} || #{role_description}"
77end
78