124 lines
3.6 KiB
PHP
124 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\UserStatus;
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use function Laravel\Prompts\password;
|
|
use function Laravel\Prompts\select;
|
|
use function Laravel\Prompts\text;
|
|
|
|
class CreateUserCommand extends Command
|
|
{
|
|
protected $signature = 'create:user';
|
|
|
|
protected $description = 'Command description';
|
|
|
|
public function handle(): void
|
|
{
|
|
$firstName = text(
|
|
label: 'What is your first name?',
|
|
placeholder: 'E.g. Michael',
|
|
required: 'Your first name is required.',
|
|
);
|
|
|
|
$lastName = text(
|
|
label: 'What is your last name?',
|
|
placeholder: 'E.g. Price',
|
|
required: 'Your last name is required.',
|
|
);
|
|
|
|
$emailAddress = text(
|
|
label: 'What is your email address?',
|
|
placeholder: 'E.g. mprice@nexigen.digital',
|
|
required: 'Your email is required.',
|
|
validate: function (string $value) {
|
|
$validator = Validator::make([
|
|
'email' => $value,
|
|
], [
|
|
'email' => ['required', 'email:rfc', Rule::unique(User::class, 'email')]
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $validator->messages()->first();
|
|
}
|
|
}
|
|
);
|
|
|
|
$uuid = text(
|
|
label: 'What is your uuid?',
|
|
placeholder: 'E.g. U05ES1730UE',
|
|
required: 'Your uuid is required.',
|
|
validate: function (string $value) {
|
|
$validator = Validator::make([
|
|
'uuid' => $value,
|
|
], [
|
|
'uuid' => ['required', Rule::unique(User::class, 'uuid')]
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $validator->messages()->first();
|
|
}
|
|
},
|
|
hint: 'This is your slack User ID.'
|
|
);
|
|
|
|
$profilePic = text(
|
|
label: 'What is your profile pic?',
|
|
placeholder: 'E.g. https://secure.gravatar.com/avatar/f2ca68078ae3b3228b0307c20ae84dd2.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0015-512.png',
|
|
required: 'Your url is required.',
|
|
validate: function (string $value) {
|
|
$validator = Validator::make([
|
|
'url' => $value,
|
|
], [
|
|
'url' => ['required', 'url']
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $validator->messages()->first();
|
|
}
|
|
},
|
|
hint: 'This is your slack profile url.'
|
|
);
|
|
|
|
$isAdmin = select(
|
|
label: 'Is your account an admin?',
|
|
options: [
|
|
true => 'Yes',
|
|
false =>'No'
|
|
],
|
|
);
|
|
|
|
$password = password(
|
|
label: 'What is your password?',
|
|
placeholder: '**********',
|
|
hint: 'One will be randomly generated if non is provided',
|
|
);
|
|
|
|
if (empty($password)) {
|
|
$password = Str::password(12);
|
|
$this->info("Randomly Generated Password: $password");
|
|
}
|
|
|
|
User::create([
|
|
'firstname' => $firstName,
|
|
'lastname' => $lastName,
|
|
'email' => $emailAddress,
|
|
'uuid' => $uuid,
|
|
'profile' => $profilePic,
|
|
'status' => UserStatus::ACTIVE,
|
|
'is_admin' => $isAdmin,
|
|
'password' => $password,
|
|
]);
|
|
|
|
$this->info('Complete!');
|
|
}
|
|
}
|