staffmanager/app/Console/Commands/CreateUser.php

141 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2020-10-10 23:25:14 +00:00
<?php
2020-10-11 01:54:22 +00:00
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager.
*
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Raspberry Staff Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/
2020-10-10 23:25:14 +00:00
namespace App\Console\Commands;
use App\Facades\UUID;
use App\Profile;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
class CreateUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'users:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates an application user. Seeding the database is for testing environments, so use this command in production for your first admin user.';
2020-10-11 01:54:22 +00:00
2020-10-10 23:25:14 +00:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
2020-10-11 01:54:22 +00:00
do {
2020-10-10 23:25:14 +00:00
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
system('cls');
} else {
system('clear');
}
$this->info('Welcome to the user account creation wizard. If you just installed the application, we recommend you create your first admin user here. If you don\'t, you won\'t gain admin privileges after creating an account in the web interface.');
$this->info('We\'ll ask some questions to get you started.');
$username = $this->ask('Username');
2020-10-11 01:54:22 +00:00
do {
2020-10-10 23:25:14 +00:00
$password = $this->secret('Password');
$password_confirm = $this->secret('Confirm Password');
2020-10-11 01:54:22 +00:00
if ($password === $password_confirm) {
2020-10-10 23:25:14 +00:00
$password = Hash::make($password);
$matches = true;
2020-10-11 01:54:22 +00:00
} else {
2020-10-10 23:25:14 +00:00
$this->error('Password doesn\'t match. Please try again.');
$matches = false;
}
2020-10-11 01:54:22 +00:00
} while (! $matches);
2020-10-10 23:25:14 +00:00
$email = $this->ask('E-mail address');
$name = $this->ask('First/Last Name');
2020-10-11 01:54:22 +00:00
do {
try {
2020-10-10 23:25:14 +00:00
$uuid = UUID::toUUID($this->ask('Minecraft username (Must be a valid Premium account)'));
2020-10-11 01:54:22 +00:00
} catch (\InvalidArgumentException $e) {
2020-10-10 23:25:14 +00:00
$this->error($e->getMessage());
$hasError = true;
}
2020-10-11 01:54:22 +00:00
if (isset($hasError)) {
2020-10-10 23:25:14 +00:00
$continue = true;
2020-10-11 01:54:22 +00:00
} else {
2020-10-10 23:25:14 +00:00
$continue = false;
}
unset($hasError);
2020-10-11 01:54:22 +00:00
} while ($continue);
2020-10-10 23:25:14 +00:00
$this->info('Please check if these details are correct: ');
2020-10-11 01:54:22 +00:00
$this->info('Username: '.$username);
$this->info('Email: '.$email);
$this->info('Name: '.$name);
} while (! $this->confirm('Create user now? You can go back to correct any details.'));
2020-10-10 23:25:14 +00:00
$user = User::create([
'uuid' => $uuid,
'name' => $name,
'email' => $email,
'username' => $username,
'originalIP' => '127.0.0.1',
2020-10-11 01:54:22 +00:00
'password' => $password,
2020-10-10 23:25:14 +00:00
]);
2020-10-11 01:54:22 +00:00
if ($user) {
2020-10-10 23:25:14 +00:00
$user->assignRole('admin', 'reviewer', 'user', 'hiringManager');
Profile::create([
'profileShortBio' => 'Random data '.rand(0, 1000),
'profileAboutMe' => 'Random data '.rand(0, 1000),
'socialLinks' => '[]',
'avatarPreference' => 'gravatar',
'userID' => $user->id,
]);
2020-10-11 01:54:22 +00:00
$this->info('Account created! You may now login at '.route('login').'. Enjoy the app!');
2020-10-10 23:25:14 +00:00
return 0;
2020-10-11 01:54:22 +00:00
} else {
2020-10-10 23:25:14 +00:00
$this->error('There was an unknown problem creating the user. There might have been errors above. Please try again.');
2020-10-11 01:54:22 +00:00
2020-10-10 23:25:14 +00:00
return 1;
}
}
}