ldc-supportweb/app/Console/Commands/CreateUser.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2020-11-18 10:23:51 +01:00
<?php
namespace App\Console\Commands;
use App\Enums\Role;
2021-04-18 20:31:07 +02:00
use App\Models\Project;
2020-11-18 10:23:51 +01:00
use App\Models\User;
use Illuminate\Console\Command;
2021-04-18 20:31:07 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2020-11-18 10:23:51 +01:00
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()
{
$name = $this->ask('What is the name?');
$email = $this->ask('What is the email?');
$pwd = $this->secret('What is the password?');
$user = new User();
$user->name = $name;
$user->email = $email;
2020-11-18 10:23:51 +01:00
if ($this->confirm('Is the user an admin?')) {
$user->role = Role::admin();
2020-11-18 10:23:51 +01:00
} else {
$user->role = Role::client();
2021-04-18 20:31:07 +02:00
try {
$project = Project::where(
'id',
$this->ask("What's the id of the project (s)he's working on?")
)->firstOrFail();
$user->projects()->sync([$project->id]);
2021-04-18 20:31:07 +02:00
} catch (ModelNotFoundException $e) {
$this->error('The project does not exists');
return -1;
}
2020-11-18 10:23:51 +01:00
}
$user->password = Hash::make($pwd);
$user->save();
$this->info('User created.');
2021-04-18 20:31:07 +02:00
return 0;
2020-11-18 10:23:51 +01:00
}
}