userService->getIdentity(); $projects = Project::find(["user_id = '$user_id'"]); if (count($projects) == 0) { $projects = User::findFirst([$user_id])->projects; if (count($projects) == 0) { throw new Exception(ErrorCodes::GENERAL_NOT_FOUND, 'Projects not found'); } else { var_dump($projects); return $this->createArrayResponse($projects, 'projects'); } } else { return $this->createArrayResponse($projects, 'projects'); } } /** * @param $data * @param $isUpdate * @return bool * @throws Exception */ public function postDataValid($data, $isUpdate) { if ($isUpdate) { if (isset($data['user_id'])) { throw new Exception(ErrorCodes::ACCESS_DENIED, 'Find user_id value in post-data. Operation is not allowed.'); } return true; } else { if (isset($data['user_id'])) { throw new Exception(ErrorCodes::ACCESS_DENIED, 'Find user_id value in post-data. Operation is not allowed.'); } return true; } } /** * * @param Project $item */ protected function beforeCreate(Project $item) { $user_id = $this->userService->getIdentity(); $item->user_id = $user_id; } /** * @param Project $item * @return bool|void */ protected function afterCreate(Project $item) { $data = ['project_id' => $item->id, 'user_id' => $item->user_id]; $junction = new UserProject(); $junction->user_id = $item->user_id; $junction->project_id = $item->id; if (!$junction->save()) { return $this->onCreateFailed($junction, $data); } return true; } /** * @param $id * @throws Exception */ protected function beforeHandleUpdate($id) { $user_id = $this->userService->getIdentity(); if(!UserProject::findFirst(["user_id = '$user_id'"]) && $this->userService->getRole() !== AclRoles::ADMINISTRATOR) { throw new Exception(ErrorCodes::ACCESS_DENIED, 'Operation is not allowed'); } } /** * @param $id * @throws Exception */ protected function beforeHandleRemove($id) { $project = Project::findFirst($id)->id; if (empty($project)) { throw new Exception(ErrorCodes::GENERAL_NOT_FOUND, 'Project with `id`='.$id.' not found'); } $user_id = $this->userService->getIdentity(); $project = Project::findFirst(["user_id = '$user_id' AND id = '$id'"]); if (empty($project) && $this->userService->getRole() !== AclRoles::ADMINISTRATOR) { throw new Exception(ErrorCodes::ACCESS_DENIED, 'Operation is not allowed'); } else { $project_links = UserProject::find(["project_id = '$id'"]); foreach ($project_links as $project_link) { $this->removeItem($project_link); } } } }