function can($permission) { // WARNING: skip permissions check, for use in development mode ONLY if (env('SKIP_PERMISSION_CHECK')) { return true; } if (auth()->check()) { if (!auth()->user()->hasPermission($permission)) { abort(403, 'Access denied'); } } else { // check if permission is given to anonymous users $role = \App\Models\User\RoleModel::whereName('anonymous')->first(); if (!$role) { // anonymous role doesn't exist yet ! abort(403, 'Access denied'); } else { $permission = \App\Models\User\PermissionModel::whereName($permission)->first(); // anonymous role doesn't have this permission sadly if (!$permission or !$role->permissions()->find([$permission->id])->count()) { abort(403, 'Access denied'); } } } return true; }
public function postUser($user_id) { can('user.manage'); $user = UserModel::find($user_id); $role = RoleModel::find(request()->role_id); if ($user->hasRole($role)) { $user->revokeRole($role); } else { $user->assignRole($role); } return redirect('role/user/' . $user_id); }