json($users); } public function getUser(int $userID): ResponseInterface { $user = \User::find($userID); return $this->json($user); } public function getCurrentUser(): ResponseInterface { return $this->json($this->user); } public function getUserWithprojects(): ResponseInterface { $usersWithProjects = \User::getUserWithprojects(); return $this->json($usersWithProjects); } public function getUserActiveProjects(int $userID): ResponseInterface { $userActiveProjects = \User::getUserActiveProjects($userID); return $this->json($userActiveProjects); } public function getUniqueprojectEmployees(): ResponseInterface { $projectEmployees = \User::getUniqueProjectUsers(); return $this->json($projectEmployees); } public function getUniqueEmployeesWithActivity(string $archive): ResponseInterface { $projectEmployees = \Projectemployee::getUniqueEmployeesWithActivity($archive); return $this->json($projectEmployees); } public function saveUser(): ResponseInterface { $userValidation = new \UserValidation(); if (!$userValidation->getValidationResult()) { return $this->json([ 'success' => false, 'messages' => $userValidation->getErrorMessages() ]); } $user = $this->request->getPost(); // Ensure we always have an ID, default to 0 for new user $userID = (int)($user['id'] ?? 0); unset($user['id']); if ($userID == 0) { $user = $this->newUser($user); } $user['role'] = empty($user['role']) ? null : $user['role']; $userID = \User::saveUser($userID, $user); return $this->json(['success' => true, 'userID' => $userID]); } public function savePassword(): ResponseInterface { $password = $this->cleanInput('password'); $user = \User::find(USERID); $saltedPassword = \User::getSaltedPassword($password, $user->salt); \User::whereId(USERID)->update(['password' => $saltedPassword]); return $this->json(['success' => true]); } public function archiveUser(int $userID, string $status): ResponseInterface { \User::whereId($userID)->update(['archive' => $status]); return $this->json(['success' => true]); } public function removeUser(int $userID): ResponseInterface { $removeUser = new \RemoveUser($userID); if ($removeUser->getAllowDelete() === false) { return $this->json(['result' => false]); } \User::removeUser($userID); return $this->json(['result' => true]); } private function newUser(array $user): array { $userCreation = new \UserCreation($user); $user = $userCreation->getUser(); $password = $userCreation->getPassword(); $this->sendWelcomeEmail($user, $password); return $user; } private function sendWelcomeEmail(array $user, string $password): bool { $email = \Config\Services::email(); $html = view('emails/welcome_email', [ 'password' => $password, 'user' => $user ]); $email->setFrom('noreply@debeleidsonderzoekers.nl', 'DBOIS'); $email->setTo($user['email']); $email->setSubject('Wachtwoord DBOIS'); $email->setMessage($html); return $email->send(); } }