/** * Manually set some permissions on an entity. * @param \BookStack\Entity $entity * @param $actions */ protected function setEntityRestrictions(\BookStack\Entity $entity, $actions) { $entity->restricted = true; $entity->permissions()->delete(); $role = $this->user->roles->first(); $viewerRole = $this->viewer->roles->first(); foreach ($actions as $action) { $entity->permissions()->create(['role_id' => $role->id, 'action' => strtolower($action)]); $entity->permissions()->create(['role_id' => $viewerRole->id, 'action' => strtolower($action)]); } $entity->save(); $entity->load('permissions'); $this->restrictionService->buildJointPermissionsForEntity($entity); $entity->load('jointPermissions'); }
/** * Updates entity restrictions from a request * @param $request * @param Entity $entity */ public function updateEntityPermissionsFromRequest($request, Entity $entity) { $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true'; $entity->permissions()->delete(); if ($request->has('restrictions')) { foreach ($request->get('restrictions') as $roleId => $restrictions) { foreach ($restrictions as $action => $value) { $entity->permissions()->create(['role_id' => $roleId, 'action' => strtolower($action)]); } } } $entity->save(); $this->permissionService->buildJointPermissionsForEntity($entity); }