Add all files
This commit is contained in:
60
app/Console/Commands/User/Create.php
Normal file
60
app/Console/Commands/User/Create.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\User;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
||||
|
||||
use function Laravel\Prompts\password;
|
||||
use function Laravel\Prompts\select;
|
||||
|
||||
class Create extends Command implements PromptsForMissingInput
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'app:user:create {first_name} {last_name} {email} {password} {role}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create a new user';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
User::create([
|
||||
'first_name' => $this->argument('first_name'),
|
||||
'last_name' => $this->argument('last_name'),
|
||||
'email' => $this->argument('email'),
|
||||
'password' => $this->argument('password'),
|
||||
'role_id' => $this->argument('role') ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function promptForMissingArgumentsUsing(): array
|
||||
{
|
||||
$roleOptions = Role::all()->pluck('name', 'id')->toArray();
|
||||
$roleOptions[0] = 'None';
|
||||
|
||||
return [
|
||||
'first_name' => 'What is the user\'s first name?',
|
||||
'last_name' => 'What is the user\'s last name?',
|
||||
'email' => 'What is the user\'s email address?',
|
||||
'password' => fn () => password('What is the user\'s password?', required: true),
|
||||
'role' => fn () => select(
|
||||
label: 'What is the user\'s role?',
|
||||
options: $roleOptions,
|
||||
default: 0,
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
68
app/Console/Commands/User/PasswordReset.php
Normal file
68
app/Console/Commands/User/PasswordReset.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands\User;
|
||||
|
||||
use App\Helpers\NotificationHelper;
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
|
||||
class PasswordReset extends Command implements PromptsForMissingInput
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'app:user:password-reset {email}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Send password reset email to a user';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$email = $this->argument('email');
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->error('Invalid email format');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$user = User::where('email', $email)->first();
|
||||
|
||||
if (!$user) {
|
||||
$this->error("User with email '{$email}' not found");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
NotificationHelper::passwordResetNotification($user, [
|
||||
'url' => url(route('password.reset', [
|
||||
'token' => Password::createToken($user),
|
||||
'email' => $user->email,
|
||||
], false)),
|
||||
]);
|
||||
|
||||
$this->info("Password reset email sent successfully to '{$email}'");
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Failed to send password reset email: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected function promptForMissingArgumentsUsing(): array
|
||||
{
|
||||
return [
|
||||
'email' => 'What is the user\'s email address?',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user