コード例 #1
0
ファイル: CreateRoleCommand.php プロジェクト: osvaldino/crud
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $role = new Role();
     $name = camel_case($this->argument('name'));
     $role->name = $name;
     if (!is_null($this->option('label'))) {
         $role->label = $this->option('label');
     }
     $role->root = $this->option('root');
     try {
         $role->save();
     } catch (QueryException $e) {
         $this->error('The ' . $name . ' role exists');
     }
 }
コード例 #2
0
ファイル: CreateAdminCommand.php プロジェクト: osvaldino/crud
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $user = new User();
     $user->email = $this->argument('email');
     $raw = '';
     if (is_null($this->option('password'))) {
         $password = substr(sha1(time() . $this->argument('email')), 0, 9);
         $raw = $password;
     } else {
         $password = $this->option('password');
     }
     $user->password = bcrypt($password);
     if (!is_null($this->option('name'))) {
         $user->name = $this->option('name');
     }
     try {
         $user->save();
         /* Assign as admin */
         $this->info('The generated password is ' . $raw);
     } catch (QueryException $e) {
         $this->error("Failed to create user, possible reasons: table doesn't exists yet, or there is another user with the supplied email address");
     }
     $user = User::where('email', $this->argument('email'))->firstOrFail();
     try {
         $role = Role::where('root', true)->firstOrFail();
         $user->assignRole($role);
         $this->info('User assigned to: ' . $role->name . ' role');
     } catch (ModelNotFoundException $e) {
         $this->error('Administrator role not found');
         $role = new Role();
         $role->name = 'Admin';
         $role->label = 'Administrators';
         $role->root = true;
         $role->save();
         $this->info('Role ' . $role->name . ' created with root access level');
         $user->assignRole($role);
         $this->info('User assigned to: ' . $role->name . ' role');
     }
 }
コード例 #3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $permissions = ['C' => 'Create', 'R' => 'Read', 'U' => 'Update', 'D' => 'Delete'];
     $requests = str_split(strtoupper($this->option('only')));
     foreach ($requests as $permissionTo) {
         $permission = new Permission();
         $permission->name = $this->argument('name') . '.' . strtolower($permissions[$permissionTo]);
         $permission->label = $permissions[strtoupper($permissionTo)] . ' ' . $this->argument('name');
         $permission->save();
         if (!is_null($this->option('assign'))) {
             if (is_numeric($this->option('assign'))) {
                 try {
                     /** @var Role $role */
                     $role = Role::findOrFail($this->option('assign'));
                     $role->givePermission($permission);
                 } catch (QueryException $e) {
                     $this->error('Role not found!');
                 }
             } else {
                 $this->error('The supplied role id is not a number!');
             }
         }
     }
 }