57 lines
1.4 KiB
PHP
57 lines
1.4 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 {email}';
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Change password for user with given e-mail';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$email = $this->argument('email');
|
|
try {
|
|
$user = User::where(['email' => $email])
|
|
->first();
|
|
} catch (ModelNotFoundException $e) {
|
|
$this->error('Invalid user ' . $email . '.');
|
|
return;
|
|
}
|
|
|
|
do {
|
|
$password = $this->secret('Password');
|
|
$passwordAgain = $this->secret('Password again');
|
|
|
|
if ($password !== $passwordAgain) {
|
|
$this->error('Passwords does not match');
|
|
}
|
|
} while ($passwordAgain !== $password || strlen($password) === 0);
|
|
|
|
// set new password
|
|
$user->password = Hash::make($password);
|
|
$user->save();
|
|
|
|
$this->info('Password successfully changed.');
|
|
}
|
|
}
|