Created
June 20, 2017 09:53
-
-
Save harini-ua/7a2575ac13b43bf99ec1b8c83be1868a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Orchid\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Database\QueryException; | |
use Orchid\Core\Models\User; | |
use Orchid\Kernel\Dashboard; | |
class CreateAdminCommand extends Command | |
{ | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'make:admin'; | |
/** | |
* @var string | |
*/ | |
protected $signature = 'make:admin {name} {email} {password}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Create user administrator'; | |
/** | |
* Permissions. | |
* | |
* @var | |
*/ | |
protected $permissions; | |
/** | |
* CreateAdminCommand constructor. | |
* | |
* @param Dashboard $dashboard | |
*/ | |
public function __construct(Dashboard $dashboard) | |
{ | |
parent::__construct(); | |
$this->permissions = $dashboard->permission->get(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function fire() | |
{ | |
$permissions = []; | |
foreach ($this->permissions->flatten() as $permission) { | |
$permissions[$permission] = 1; | |
} | |
try { | |
User::create([ | |
'name' => $this->argument('name'), | |
'email' => $this->argument('email'), | |
'password' => bcrypt($this->argument('password')), | |
'permissions' => $permissions, | |
]); | |
$this->info('User created successfully.'); | |
} catch (QueryException $e) { | |
$this->error('User already exists or an error occurred!'); | |
$this->error($e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment