Files
core/app/Console/Commands/ChangePassword.php
T
Michal Kváček 8ba8a45938 FEATURE: add commands for creating new user and changing password of existing one.
This commit adds two new artisan commants user:new and user:change-password.
2016-02-15 12:24:23 +01:00

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.');
}
}