# 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