| 1 | # replace line 17 with this: |
| 2 | devise :pwned_password if Rails.env.production? |
create_admin.rb
· 2.1 KiB · Ruby
Raw
# Run this script and follow the onscreen instructions:
# bundle exec rails r script/create_admin.rb
require "csv"
include Rails.application.routes.url_helpers
# Base URL
default_url_options[:host] = ArchiveConfig.APP_URL
def multi_gets(all_text = +"")
until (text = gets) == "\n"
all_text << text
end
all_text.chomp
end
puts "======== This is the #{Rails.env} environment. ========"
puts <<~PROMPT
Paste or enter admins, one per line, in the format
USERNAME, EMAIL, PASSWORD, ROLE
or
USERNAME, EMAIL, PASSWORD, ROLE, ROLE, ROLE
where
- 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"
- EMAIL is whatever their email is
- PASSWORD is a password of at least ten characters
- EMAIL/PASSWORD/ROLE can be left blank to skip updating email/roles of existing admins
- for dev you probably just want to make a superadmin
- ROLE is one of:
#{Admin::VALID_ROLES.sort.map { |r| " #{r}" }.join("\n")}
then two line breaks to end:
PROMPT
list = CSV.parse(multi_gets)
admins = []
list.each do |user|
login = user[0].gsub(/\s+/, "")
email = user[1]&.strip
password = user[2]&.strip
roles = user.drop(3).compact.map(&:strip).map(&:downcase)
a = Admin.find_or_initialize_by(login: "admin-#{login}")
success_message = a.new_record? ? "Created and notified" : "Updated"
a.email = email if email.present?
a.roles = roles if roles.present?
# Set their password if that's set
if password.present?
a.password = password
a.password_confirmation = password
end
puts "==== #{a.login}"
if a.save
puts success_message
admins << a
else
puts a.errors.full_messages
end
puts
end
admins.sort_by!(&:created_at)
admins.each do |admin|
role_description = admin.roles.map { |r| I18n.t("activerecord.attributes.admin/role.#{r}") }.sort.join(", ")
role_description = "UPDATE WITH USER COMMITTEE" if role_description.blank?
puts "|-\n| #{admin.created_at.strftime('%Y-%m-%d')} || #{admin.login} || #{role_description}"
end
| 1 | # Run this script and follow the onscreen instructions: |
| 2 | # bundle exec rails r script/create_admin.rb |
| 3 | |
| 4 | require "csv" |
| 5 | |
| 6 | include Rails.application.routes.url_helpers |
| 7 | |
| 8 | # Base URL |
| 9 | default_url_options[:host] = ArchiveConfig.APP_URL |
| 10 | |
| 11 | def multi_gets(all_text = +"") |
| 12 | until (text = gets) == "\n" |
| 13 | all_text << text |
| 14 | end |
| 15 | all_text.chomp |
| 16 | end |
| 17 | puts "======== This is the #{Rails.env} environment. ========" |
| 18 | puts <<~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 | |
| 40 | PROMPT |
| 41 | |
| 42 | list = CSV.parse(multi_gets) |
| 43 | |
| 44 | admins = [] |
| 45 | list.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 |
| 70 | end |
| 71 | |
| 72 | admins.sort_by!(&:created_at) |
| 73 | admins.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}" |
| 77 | end |
| 78 |