Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Small Admin Control Panel to add roles in Laravel #921

Open IkramMeraghni opened 6 years ago

IkramMeraghni commented 6 years ago

I'm trying to develop a small control panel for admin to distribute roles to other users, the thing is my code doesn't show me any error but at the same time it doesn't affect any roles to other users. it stays just the same way, my control panel is based on an automatic submit method whenever i check the role for a specific user or uncheck it.

My view addmin.blade.php code that contains control panel is;

   <h4>Panel de Controle </h4><br>
   <h6> <b>Liste des utilisateurs :</b></h6>
    <br><br>
   <div>
    <table class="table table-hover">
        <tr>
            <th>#</th>
            <th>Nom</th>
            <th>Email</th>
            <th>Elève</th>
            <th>Enseignant</th>   
            <th>Parent</th>   
            <th>Administrateur</th>
            </tr>
            @foreach($users as $user)
             <form method="post" action="/add-role">

           {{ csrf_field() }}
            <input type="hidden" name="email" value="{{ $user->email }}">
            <tr>
                  <th>{{ $user->id }}</th>
                  <td>{{ $user->name }}</td>
                   <td>{{ $user->email }}</td>
                    <td>
                        <input type= "checkbox" name='role_elève' onChange="this.form.submit()" {{ $user->hasRole('Elève') ? 'checked' : ' ' }}>  </td>
                     <td> <input type= "checkbox" name='role_ens' onChange="this.form.submit()" {{ $user->hasRole('Enseignant') ? 'checked' : ' ' }}></td>
                      <td> <input type= "checkbox" name='role_parent'  onChange="this.form.submit()" {{ $user->hasRole('Parent') ? 'checked' : ' ' }}></td>
                       <td> <input type= "checkbox" name='role_admin'  onChange="this.form.submit()" {{ $user->hasRole('Admin') ? 'checked' : ' ' }}></td>
                   </tr>
               </form>
                   @endforeach
                   </table>
                   </div>

          </div></div>

web.php contains the route add-role below:

  Route::post('add-role', [ 
  'uses' => 'PostsController@addRole',
   'as' => 'addmin',
    'middleware' => 'roles',
      'roles' => ['Admin']
   ]);

addRole method in PostsController implements the code below:

   Public function addRole(Request $request) {

        $user = User::where('email', $request['email'])->first();
        $user->roles()->detach();

      if($request['role_elève'])
      {
         $user->roles()->attach(Role::where('name', 'Elève')->first());

      }

       if($request['role_ens'])
      {
         $user->roles()->attach(Role::where('name', 'Enseignant')->first());

      }

       if($request['role_parent'])
      {
         $user->roles()->attach(Role::where('name', 'Parent')->first());

      }

    if($request['role_admin'])
      {
         $user->roles()->attach(Role::where('name', 'Admin')->first());

      }

      return redirect()->back();
        }

I can't find the error when laravel doesn"t detect the details of it, any help would be appreciated alot, any ideas would be appreicated alot!