我在 Laravel 5.1 中使用Zizaco/ entrust。我想通过使用复选框明确授予特定角色的权限。这是我的代码:
获取角色:
fetchRoles: function(){
this.$http.get('api/role',function(data){
this.$set('roles',data);
});
}
获取权限:
fetchPermissions: function(){
this.$http.get('api/permission',function(data){
this.$set('permissions',data);
});
}
下面是分配角色和权限的表格:
<table class="table table-bordered table-responsive">
<thead>
<th>Permissions/Roles</th>
<th v-for="role in roles">
@{{ role.display_name }}
</th>
</thead>
<tbody>
<tr v-for="permission in permissions">
<td>@{{ permission.display_name }}</td>
<td v-for="role in roles">
<input type="checkbox" value="@{{ permission.id }}" name="roles[@{{ role.id }}][permissions][]">
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>
<button type="submit" class="btn btn-primary">Alter Permission</button>
</td>
</tr>
</tfoot>
</table>
输出类似于: 输出截图
权限也通过以下方法成功分配:
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();
}
如果角色具有特定权限,我唯一坚持的是选中复选框。
如果它在 php 对象中,我可以执行以下操作:
@if(count($permissions)>0)
@foreach($permissions as $permission)
<tr>
<td>{{$permission->display_name}}</td>
@if(count($roles)>0)
@foreach($roles as $role)
<td><input type="checkbox" value="{{$permission->id}}" name="roles[{{$role->id}}][permissions][]" @if($role->hasPermission($permission->name)) checked="checked" @endif></td>@endforeach @endif </tr> @endforeach @endif
如何在 Vue JS 中使用 hasPermission() 方法?