ldc-supportweb/app/Console/Commands/CreateUser.php
2021-04-18 20:31:07 +02:00

78 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Project;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Hash;
class CreateUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create:user';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a user';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$admin = false;
$name = $this->ask('What is the name?');
$email = $this->ask('What is the email?');
$pwd = $this->secret('What is the password?');
if ($this->confirm('Is the user an admin?')) {
$admin = true;
} else {
$project = null;
try {
$project = Project::where(
'id',
$this->ask("What's the id of the project (s)he's working on?")
)->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->error('The project does not exists');
return -1;
}
}
$user = new User();
$user->name = $name;
$user->email = $email;
if ($admin) {
$user->role = 1;
} else {
$user->project_id = $project->id;
}
$user->password = Hash::make($pwd);
$user->save();
$this->info('User created.');
return 0;
}
}