This commit adds two new artisan commants user:new and user:change-password.
49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class ChangePassword extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:change-password {userID} {newPassword}';
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Change password for user with given ID';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$id = $this->argument('userID');
|
|
$newPassword = $this->argument('newPassword');
|
|
|
|
try {
|
|
$user = User::findOrFail($id);
|
|
} catch (ModelNotFoundException $e) {
|
|
$this->error('Invalid user ID ' . $id . '. Please see table `users` for correct IDs.');
|
|
exit(1);
|
|
}
|
|
|
|
// set new password
|
|
$user->password = Hash::make($newPassword);
|
|
$user->save();
|
|
|
|
$this->info('Password successfully changed.');
|
|
}
|
|
}
|