lundi 20 juin 2016

Check particular role's permission in Vue JS and Laravel 5.1 + Entrust

I am using Zizaco/entrust in Laravel 5.1. I want explicitly give permissions to particular role by using checkboxes. This is my code: `

Fetching Roles:

fetchRoles: function(){
        this.$http.get('api/role',function(data){
            this.$set('roles',data);
        });
    }

Fetching Permissions:

fetchPermissions: function(){
            this.$http.get('api/permission',function(data){
                this.$set('permissions',data);
            });
        }

Here is the table for assigning roles and permissions:

<table class="table table-bordered table-responsive">
    <thead>                                                        
       <th>Permissions/Roles</th>
       <th v-for="role in roles">
           @
       </th>
    </thead>
    <tbody>
       <tr v-for="permission in permissions">
           <td>@</td>
           <td v-for="role in roles">
               <input type="checkbox" value="@" name="roles[@][permissions][]">
           </td>
       </tr>
    </tbody>
    <tfoot>
       <tr>
          <td>
              <button type="submit" class="btn btn-primary">Alter Permission</button>
          </td>
       </tr>
    </tfoot>
</table>

The output is like: Screenshot of Output

Permissions are also assigned successfully by following method:

public function changePermission(Request $request){
        $input = $request->all();
        $roles = Role::all();

        foreach($roles as $role)
        {
            $permissions_sync = isset($input['roles'][$role->id])? $input['roles'][$role->id]['permissions'] : [];
            $role->perms()->sync($permissions_sync);
        }
        Flash::success('Successfully modified permissions!');
        return redirect()->back();
    }

Only thing I am stucked in is making the checked boxed checked if the the role has particular permission.

I could do the following if it was in php object:

   @if(count($permissions)>0)
     @foreach($permissions as $permission)
     <tr>
     <td></td>
         @if(count($roles)>0)                                                          
            @foreach($roles as $role)
            <td><input type="checkbox" value="" name="roles[][permissions][]" @if($role->hasPermission($permission->name)) checked="checked" @endif></td>@endforeach @endif </tr> @endforeach @endif

How can I can hasPermission() method in Vue JS?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire