app/Console/Commands/Setup/RolesAndPermissions.php
/**
     * Execute the console command.
     *
     * @return mixed
     */
Execute the console command.
@return mixed
Execute the console command . @return mixed
public function handle()
    {
        $this->info('Setting up roles and permissions...');
        try {
            $permissions = $this->setupPermissions($this->permissions);
            $this->line('Processing roles... Available: '.count($this->roles));
            foreach ($this->roles as $name => $configuration) {
                $this->info('Permission: '.$name);
                if (!is_array($configuration)) {
                    $this->warn('Configuration problems; configuration should be an array...');
                    continue;
                }
                $guards = $configuration['guards'] ?? (array) 'api';
                $this->table(['Guard'], [$guards]);
                $requiredPermissions = [];
                if (is_string($configuration['permissions'])) {
                    $requiredPermissions = (array) $configuration['permissions'];
                } elseif (is_array($configuration['permissions'])) {
                    $requiredPermissions = $configuration['permissions'];
                }
                if (empty($requiredPermissions)) {
                    $this->warn('Improperly configured permissions. It should either be a string or an array...');
                }
                $requiredPermissions = collect($requiredPermissions);
                $this->table(['Requested Permission'], $requiredPermissions->map(function ($p) { return [$p]; }));
                $this->line('Filtering the permissions, based on those that are available...');
                $allowedPermissions = $requiredPermissions->filter(function ($permission) use ($permissions) {
                    return in_array($permission, $permissions);
                });
                $this->table(['Allowed Permission'], $allowedPermissions->map(function ($p) { return [$p]; }));
                foreach ($guards as $guard) {
                    $this->line('Setting up for guard: '.$guard);
                    $role = Role::firstOrCreate([
                        'name' => $name,
                        'guard_name' => $guard
                    ]);
                    if ($role !== null) {
                        $this->info('created... setting up permissions');
                        $role->syncPermissions($allowedPermissions->all());
                    }
                }
                $this->info('Completed!');
            }
        } catch (\UnderflowException $e) {
            $this->error($e->getMessage());
        }
        return;
    }
public function handle ( ) { $ this -> info ( 'Setting up roles and permissions...' ) ; try { $ permissions = $ this -> setupPermissions ( $ this -> permissions ) ; $ this -> line ( 'Processing roles... Available: ' . count ( $ this -> roles ) ) ; foreach ( $ this -> roles as $ name => $ configuration ) { $ this -> info ( 'Permission: ' . $ name ) ; if ( ! is_array ( $ configuration ) ) { $ this -> warn ( 'Configuration problems; configuration should be an array...' ) ; continue ; } $ guards = $ configuration [ 'guards' ] ?? ( array ) 'api' ; $ this -> table ( [ 'Guard' ] , [ $ guards ] ) ; $ requiredPermissions = [ ] ; if ( is_string ( $ configuration [ 'permissions' ] ) ) { $ requiredPermissions = ( array ) $ configuration [ 'permissions' ] ; } elseif ( is_array ( $ configuration [ 'permissions' ] ) ) { $ requiredPermissions = $ configuration [ 'permissions' ] ; } if ( empty ( $ requiredPermissions ) ) { $ this -> warn ( 'Improperly configured permissions. It should either be a string or an array...' ) ; } $ requiredPermissions = collect ( $ requiredPermissions ) ; $ this -> table ( [ 'Requested Permission' ] , $ requiredPermissions -> map ( function ( $ p ) { return [ $ p ] ; } ) ) ; $ this -> line ( 'Filtering the permissions, based on those that are available...' ) ; $ allowedPermissions = $ requiredPermissions -> filter ( function ( $ permission ) use ( $ permissions ) { return in_array ( $ permission , $ permissions ) ; } ) ; $ this -> table ( [ 'Allowed Permission' ] , $ allowedPermissions -> map ( function ( $ p ) { return [ $ p ] ; } ) ) ; foreach ( $ guards as $ guard ) { $ this -> line ( 'Setting up for guard: ' . $ guard ) ; $ role = Role :: firstOrCreate ( [ 'name' => $ name , 'guard_name' => $ guard ] ) ; if ( $ role !== null ) { $ this -> info ( 'created... setting up permissions' ) ; $ role -> syncPermissions ( $ allowedPermissions -> all ( ) ) ; } } $ this -> info ( 'Completed!' ) ; } } catch ( \ UnderflowException $ e ) { $ this -> error ( $ e -> getMessage ( ) ) ; } return ; }
Execute the console command.
Execute the console command .
# configure the permissions # get the guards for this role # the guards # the guards # the guards