Files
core/app/Console/Commands/NewUser.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

53 lines
1.3 KiB
PHP

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