Welcome Guest, Not a member yet? Register   Sign In
shield problem with sync_permission
#1

(This post was last modified: 12-08-2024, 03:55 AM by pippuccio76.)

hi, to sync permission i created a table permission_list on db with the same value of  AuthGroups.php :
Code:
/**
    * --------------------------------------------------------------------
    * Permissions
    * --------------------------------------------------------------------
    * The available permissions in the system.
    *
    * If a permission is not listed here it cannot be used.
    */
    public array $permissions = [
        'superadmin.manage-admin'      => 'Can manage other admin and user',
        'admin.access'                  => 'Can access the sites admin area',
        'admin.settings'                => 'Can access the main site settings',
        'admin.remove_event_admin'      => 'Can remove all admin event',
        'admin.remove_event_user'      => 'Can remove all user event',
        'admin.manage-admins'          => 'Can manage other admins',
        'admin.manage-users'            => 'Can manage Users',
        'admin.payment-manage'          => 'Can manage payment',
        'user.create-panel'            => 'Can create panel',
        'user.access'                  => 'Can access the sites user area',
        'user.premium_front_end'        => 'Accesso completo lifetime', // 1 [old]
        'user.mountly_front_end'        => 'Accesso completo mensile', // 2 [old]
        'user.pro'                      => 'Funzioni avanzate valido per lifetime/mensile', // 3 [old]
        'user.agency'                  => 'accesso lifetime+PRO con possibilità di rivendita', // 4 [old]
        'user.white_label_100'          => '100 licence di sub-account admin completo', // 5 [old]
        'user.white_label_500'          => '500 licence di sub-account admin completo', // 6 [old]
        'user.new_plan'                => 'Accesso test illimitato', // 7 [old]
       
    ];
then i create a model to get all table's value and in user edit :
Code:
    <div class="row mt-2 mb-2">
     


      <?php foreach($tutti_permessi as $v):?>

        
       
        <div class="col-md-3">
          <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="id_<?=$v->nome  ?>" name="permessi['<?=$v->nome  ?>']" value="<?=$v->nome  ?>" >
            <label class="form-check-label" for="mySwitch"><?=ucfirst(str_replace('_',' ',$v->nome))  ?></label>
          </div>
        </div>
       



      <?php endforeach; ?>

    </div>

in the controller method :
Code:
                ################ GESTIONE PERMESSI #########################

                $string_permessi ='' ;

                foreach($post['permessi'] as $permesso){
               
                    $string_permessi  .=  "'user.".$permesso."'";


                    if (next($post['permessi'])) {
                        //Doesn't Process last element
                        $string_permessi  .= ",";
                    }
                }


                //dd($string_permessi);

                $user->syncPermissions($string_permessi);

                ################ FINE GESTIONE GRUPPO #########################

if i remove a comment of //dd line i have :

Code:
$string_permessi string (40) "'user.premium_front_end','user.new_plan'"
but if i comment dd i have :
Code:
'user.premium_front_end','user.new_plan' is not a valid permission

Why ?
Reply
#2

See souce code:
PHP Code:
    public function syncPermissions(string ...$permissions): self 

Perms add as syncPermissions('user.get', 'user.set', ...) Explode your string
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

i solve in this way :
Code:
                ################ GESTIONE PERMESSI #########################


                $array_permessi_tabella_db = [];
                //recupero tutti i permessi della tabella
                foreach ($data['tutti_permessi']  as $permesso_tabella) {


                    $array_permessi_tabella_db[] = $permesso_tabella->nome_shield;
                }


                $array_permessi_assegnati = [];

                if (isset($post['permessi'])) {

                    //recupero tutti i permessi assegnati dal form
                    foreach ($post['permessi'] as $permesso) {

                        $array_permessi_assegnati[] =  "user." . $permesso;
                    }

                    //faccio la difefrenza tra tutti i permessi e quelli passati dal form
                    $array_mancanti = array_diff($array_permessi_tabella_db, $array_permessi_assegnati);

                    //ciclo e rimuovo i permessi mancanti
                    foreach ($array_mancanti as $permesso_da_togliere) {

                        $user->removePermission($permesso_da_togliere);
                    }

                    //poi assegno tutti i permessi
                    foreach ($array_permessi_assegnati as $permesso_da_inserire) {

                        $user->addPermission($permesso_da_inserire);
                    }
                }



                //dd(json_encode($string_permessi))

                ################ FINE GESTIONE PERMESSI #########################

It works but i don't think is the better way...
Reply
#4

That's what the developers decided - you need to accept it or extend it with a function
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB