checkPermission('receive-notifications'); $this->preventGuestAccess(); $requestData = $this->validate($request, [ 'level' => ['required', 'string'], ]); $watchable = $this->getValidatedModelFromRequest($request); $watchOptions = new UserEntityWatchOptions(user(), $watchable); $watchOptions->updateWatchLevel($requestData['level']); $this->showSuccessNotification(trans('activities.watch_update_level_notification')); return redirect()->back(); } /** * @throws ValidationException * @throws Exception */ protected function getValidatedModelFromRequest(Request $request): Entity { $modelInfo = $this->validate($request, [ 'type' => ['required', 'string'], 'id' => ['required', 'integer'], ]); if (!class_exists($modelInfo['type'])) { throw new Exception('Model not found'); } /** @var Model $model */ $model = new $modelInfo['type'](); if (!$model instanceof Entity) { throw new Exception('Model not an entity'); } $modelInstance = $model->newQuery() ->where('id', '=', $modelInfo['id']) ->first(['id', 'name', 'owned_by']); $inaccessibleEntity = ($modelInstance instanceof Entity && !userCan('view', $modelInstance)); if (is_null($modelInstance) || $inaccessibleEntity) { throw new Exception('Model instance not found'); } return $modelInstance; } }