mirror of
https://github.com/manyfold3d/manyfold.git
synced 2026-10-22 11:42:19 -05:00
62 lines
2.0 KiB
Ruby
62 lines
2.0 KiB
Ruby
require "thor"
|
|
|
|
module Cli
|
|
class UsersCommand < Thor
|
|
namespace :users
|
|
DESCRIPTION = "manage users"
|
|
|
|
desc "approve", "approves a user"
|
|
option :email, required: true, type: :string, aliases: :mail
|
|
def approve
|
|
u = User.find_by(email: options[:email])
|
|
raise "User not found" if u.nil?
|
|
u.update(approved: true)
|
|
puts "\nUser #{u.email} approved"
|
|
rescue RuntimeError
|
|
puts "\nUser #{options[:email]} not found"
|
|
end
|
|
|
|
desc "password", "resets password for user"
|
|
option :email, required: true, type: :string, aliases: :mail
|
|
option :interactive, required: false, type: :boolean, default: false
|
|
def password
|
|
u = User.find_by(email: options[:email])
|
|
raise "User not found" if u.nil?
|
|
if options[:interactive] || !SiteSettings.email_configured?
|
|
u.password = ask("Enter password: ", echo: false)
|
|
puts "\n"
|
|
u.password_confirmation = ask("Confirm password: ", echo: false)
|
|
puts "\nPassword changed!" if u.save!
|
|
else
|
|
u.send_reset_password_instructions
|
|
puts "\nPassword reset email sent!"
|
|
end
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
puts "\n#{e}"
|
|
rescue RuntimeError => e
|
|
puts "\n#{e}"
|
|
end
|
|
|
|
desc "create", "create a new user"
|
|
option :email, required: true, type: :string, aliases: :mail
|
|
option :username, required: true, type: :string, aliases: :login
|
|
option :password, required: true, type: :string
|
|
option :role, required: false, type: :string, enum: %w[administrator moderator contributor member]
|
|
def create
|
|
u = User.create(
|
|
username: options[:username],
|
|
email: options[:email],
|
|
password: options[:password],
|
|
password_confirmation: options[:password_confirmation]
|
|
)
|
|
if u.valid?
|
|
u.add_role(options[:role].to_sym) if options[:role]
|
|
puts "\nNew user created OK"
|
|
else
|
|
puts "\nError when creating user:\n"
|
|
puts u.errors.full_messages.inspect
|
|
end
|
|
end
|
|
end
|
|
end
|