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.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Foundation\Inspiring;
|
||||
|
||||
class Inspire extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'inspire';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Display an inspiring quote';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enumerations\UserRoles;
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
|
||||
class NewUser extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:new {email} {password} {role}';
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Creates new user';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$email = $this->argument('email');
|
||||
$password = $this->argument('password');
|
||||
$role = $this->argument('role');
|
||||
|
||||
// check if given role is valid
|
||||
$roles = UserRoles::getSimple();
|
||||
if (!in_array($role, $roles)) {
|
||||
throw new InvalidArgumentException('Invalid role specified. valid roles are: ' . implode(', ', $roles));
|
||||
}
|
||||
|
||||
// create new user
|
||||
$user = new User();
|
||||
$user->username = $user->email = $email;
|
||||
$user->password = Hash::make($password);
|
||||
$user->role = $role;
|
||||
$user->save();
|
||||
|
||||
$this->info('User ' . $email . ' successfully created.');
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,8 @@ class Kernel extends ConsoleKernel
|
||||
* @var array
|
||||
*/
|
||||
protected $commands = [
|
||||
// Commands\Inspire::class,
|
||||
Commands\NewUser::class,
|
||||
Commands\ChangePassword::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user