2019-07-31 11:36:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'bigbluebutton_api'
|
|
|
|
|
|
|
|
namespace :user do
|
|
|
|
desc "Creates a user account"
|
2019-08-06 14:03:12 -04:00
|
|
|
task :create, [:name, :email, :password, :role, :provider] => :environment do |_task, args|
|
2019-07-31 11:36:38 -04:00
|
|
|
u = {
|
|
|
|
name: args[:name],
|
|
|
|
password: args[:password],
|
|
|
|
email: args[:email],
|
2019-08-06 14:03:12 -04:00
|
|
|
role: args[:role] || "user",
|
|
|
|
provider: args[:provider] || "greenlight"
|
2019-07-31 11:36:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if u[:role] == "admin"
|
|
|
|
# Set default variables
|
|
|
|
u[:name] = "Administrator" if u[:name].blank?
|
|
|
|
u[:password] = Rails.configuration.admin_password_default if u[:password].blank?
|
|
|
|
u[:email] = "admin@example.com" if u[:email].blank?
|
|
|
|
elsif u[:name].blank? || u[:password].blank? || u[:email].blank?
|
|
|
|
# Check that all fields exist
|
|
|
|
puts "Missing Arguments"
|
|
|
|
exit
|
|
|
|
end
|
2020-06-03 14:44:45 -04:00
|
|
|
|
2020-06-04 14:08:25 -04:00
|
|
|
# Create the default roles if not already created
|
|
|
|
Role.create_default_roles(u[:provider]) if Role.where(provider: u[:provider]).count.zero?
|
|
|
|
|
2020-06-03 14:44:45 -04:00
|
|
|
unless Role.exists?(name: u[:role], provider: u[:provider])
|
|
|
|
puts "Invalid Role - Role does not exist"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2019-07-31 11:36:38 -04:00
|
|
|
u[:email].prepend "superadmin-" if args[:role] == "super_admin"
|
|
|
|
|
|
|
|
# Create account if it doesn't exist
|
2021-02-26 17:34:07 -05:00
|
|
|
if User.exists?(email: u[:email], provider: u[:provider])
|
|
|
|
puts "Account with that email already exists"
|
|
|
|
puts "Email: #{u[:email]}"
|
|
|
|
else
|
2019-07-31 11:36:38 -04:00
|
|
|
user = User.create(name: u[:name], email: u[:email], password: u[:password],
|
2020-05-26 17:37:23 -04:00
|
|
|
provider: u[:provider], email_verified: true, accepted_terms: true)
|
2019-07-31 11:36:38 -04:00
|
|
|
|
|
|
|
unless user.valid?
|
|
|
|
puts "Invalid Arguments"
|
|
|
|
puts user.errors.messages
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2020-05-28 14:27:46 -04:00
|
|
|
user.set_role(u[:role])
|
|
|
|
|
2020-07-29 11:03:22 -04:00
|
|
|
puts "Account successfully created."
|
2019-07-31 11:36:38 -04:00
|
|
|
puts "Email: #{u[:email]}"
|
|
|
|
puts "Password: #{u[:password]}"
|
|
|
|
puts "Role: #{u[:role]}"
|
|
|
|
puts "PLEASE CHANGE YOUR PASSWORD IMMEDIATELY" if u[:password] == Rails.configuration.admin_password_default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|