Archived
feat: redo user flow
This commit is contained in:
1 parent
a61f6cae81
commit
19f07b0068
10 files changed
+212
-82
No files matched your search
@@ -0,0 +1,123 @@
|
||||
<?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!');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use App\Traits\EnumOptions;
|
||||
use App\Traits\EnumValues;
|
||||
|
||||
enum UserStatus: int
|
||||
{
|
||||
use EnumValues;
|
||||
use EnumOptions;
|
||||
|
||||
case ACTIVE = 1;
|
||||
case INACTIVE = 0;
|
||||
}
|
||||
+6
-2
@@ -17,8 +17,13 @@ class User extends Authenticatable
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'firstname',
|
||||
'lastname',
|
||||
'email',
|
||||
'uuid',
|
||||
'profile',
|
||||
'status',
|
||||
'is_admin',
|
||||
'password',
|
||||
];
|
||||
|
||||
@@ -38,7 +43,6 @@ class User extends Authenticatable
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait EnumOptions
|
||||
{
|
||||
public static function options(): array
|
||||
{
|
||||
$cases = static::cases();
|
||||
$options = [];
|
||||
foreach ($cases as $case) {
|
||||
$label = $case->name;
|
||||
if (Str::contains($label, '_')) {
|
||||
$label = Str::replace('_', ' ', $label);
|
||||
}
|
||||
$options[] = [
|
||||
'value' => $case->value,
|
||||
'label' => Str::title($label),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
trait EnumValues
|
||||
{
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user