diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php deleted file mode 100644 index 8e436017..00000000 --- a/src/Controllers/UserController.php +++ /dev/null @@ -1,176 +0,0 @@ -config = $config; - $this->privilegeService = $privilegeService; - $this->userService = $userService; - $this->tokenService = $tokenService; - $this->userSearchParser = $userSearchParser; - $this->inputReader = $inputReader; - $this->userViewProxy = $userViewProxy; - } - - public function registerRoutes(Router $router) - { - $router->post('/api/users', [$this, 'createUser']); - $router->get('/api/users', [$this, 'getFiltered']); - $router->get('/api/users/:userNameOrEmail', [$this, 'getByNameOrEmail']); - $router->put('/api/users/:userNameOrEmail', [$this, 'updateUser']); - $router->delete('/api/users/:userNameOrEmail', [$this, 'deleteUser']); - $router->post('/api/password-reset/:userNameOrEmail', [$this, 'passwordReset']); - $router->post('/api/finish-password-reset/:tokenName', [$this, 'finishPasswordReset']); - $router->post('/api/activation/:userNameOrEmail', [$this, 'activation']); - $router->post('/api/finish-activation/:tokenName', [$this, 'finishActivation']); - } - - public function getByNameOrEmail($userNameOrEmail) - { - if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) - $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userViewProxy->fromEntity($user); - } - - public function getFiltered() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_USERS); - - $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $filter->setPageSize($this->config->users->usersPerPage); - $result = $this->userService->getFiltered($filter); - $entities = $this->userViewProxy->fromArray($result->getEntities()); - return [ - 'data' => $entities, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function createUser() - { - $this->privilegeService->assertPrivilege(Privilege::REGISTER); - $formData = new RegistrationFormData($this->inputReader); - $user = $this->userService->createUser($formData); - return $this->userViewProxy->fromEntity($user); - } - - public function updateUser($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - $formData = new UserEditFormData($this->inputReader); - - if ($formData->avatarStyle !== null || $formData->avatarContent !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_AVATAR_STYLE - : Privilege::CHANGE_ALL_AVATAR_STYLES); - } - - if ($formData->userName !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_NAME - : Privilege::CHANGE_ALL_NAMES); - } - - if ($formData->password !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_PASSWORD - : Privilege::CHANGE_ALL_PASSWORDS); - } - - if ($formData->email !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_EMAIL_ADDRESS - : Privilege::CHANGE_ALL_EMAIL_ADDRESSES); - } - - if ($formData->accessRank) - { - $this->privilegeService->assertPrivilege(Privilege::CHANGE_ACCESS_RANK); - } - - if ($formData->browsingSettings) - { - $this->privilegeService->assertLoggedIn($userNameOrEmail); - } - - if ($formData->banned !== null) - { - $this->privilegeService->assertPrivilege(Privilege::BAN_USERS); - } - - $user = $this->userService->updateUser($user, $formData); - return $this->userViewProxy->fromEntity($user); - } - - public function deleteUser($userNameOrEmail) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::DELETE_OWN_ACCOUNT - : Privilege::DELETE_ACCOUNTS); - - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userService->deleteUser($user); - } - - public function passwordReset($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userService->sendPasswordResetEmail($user); - } - - public function activation($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail, true); - return $this->userService->sendActivationEmail($user); - } - - public function finishPasswordReset($tokenName) - { - $token = $this->tokenService->getByName($tokenName); - return ['newPassword' => $this->userService->finishPasswordReset($token)]; - } - - public function finishActivation($tokenName) - { - $token = $this->tokenService->getByName($tokenName); - $this->userService->finishActivation($token); - } -} diff --git a/src/Routes/Users/AbstractUserRoute.php b/src/Routes/Users/AbstractUserRoute.php new file mode 100644 index 00000000..9a2f3e35 --- /dev/null +++ b/src/Routes/Users/AbstractUserRoute.php @@ -0,0 +1,7 @@ +userService = $userService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/activation/:userNameOrEmail'; + } + + public function work() + { + $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail'), true); + return $this->userService->sendActivationEmail($user); + } +} diff --git a/src/Routes/Users/CreateUser.php b/src/Routes/Users/CreateUser.php new file mode 100644 index 00000000..62b2f5b9 --- /dev/null +++ b/src/Routes/Users/CreateUser.php @@ -0,0 +1,46 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/users'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::REGISTER); + $formData = new RegistrationFormData($this->inputReader); + $user = $this->userService->createUser($formData); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/Routes/Users/DeleteUser.php b/src/Routes/Users/DeleteUser.php new file mode 100644 index 00000000..5b5438f1 --- /dev/null +++ b/src/Routes/Users/DeleteUser.php @@ -0,0 +1,43 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::DELETE_OWN_ACCOUNT + : Privilege::DELETE_ALL_ACCOUNTS); + + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + return $this->userService->deleteUser($user); + } +} diff --git a/src/Routes/Users/FinishActivation.php b/src/Routes/Users/FinishActivation.php new file mode 100644 index 00000000..8946cf10 --- /dev/null +++ b/src/Routes/Users/FinishActivation.php @@ -0,0 +1,34 @@ +userService = $userService; + $this->tokenService = $tokenService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/finish-activation/:tokenName'; + } + + public function work() + { + $token = $this->tokenService->getByName($this->getArgument('tokenName')); + $this->userService->finishActivation($token); + } +} diff --git a/src/Routes/Users/FinishPasswordReset.php b/src/Routes/Users/FinishPasswordReset.php new file mode 100644 index 00000000..c4ee8cb5 --- /dev/null +++ b/src/Routes/Users/FinishPasswordReset.php @@ -0,0 +1,34 @@ +userService = $userService; + $this->tokenService = $tokenService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/finish-password-reset/:tokenName'; + } + + public function work() + { + $token = $this->tokenService->getByName($this->getArgument('tokenName')); + return ['newPassword' => $this->userService->finishPasswordReset($token)]; + } +} diff --git a/src/Routes/Users/GetUser.php b/src/Routes/Users/GetUser.php new file mode 100644 index 00000000..b31f9977 --- /dev/null +++ b/src/Routes/Users/GetUser.php @@ -0,0 +1,46 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->userSearchParser = $userSearchParser; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) + $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/Routes/Users/GetUsers.php b/src/Routes/Users/GetUsers.php new file mode 100644 index 00000000..00f3dad8 --- /dev/null +++ b/src/Routes/Users/GetUsers.php @@ -0,0 +1,59 @@ +config = $config; + $this->privilegeService = $privilegeService; + $this->userService = $userService; + $this->userSearchParser = $userSearchParser; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/users'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_USERS); + + $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); + $filter->setPageSize($this->config->users->usersPerPage); + $result = $this->userService->getFiltered($filter); + $entities = $this->userViewProxy->fromArray($result->getEntities()); + return [ + 'data' => $entities, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Users/PasswordReset.php b/src/Routes/Users/PasswordReset.php new file mode 100644 index 00000000..70c4df27 --- /dev/null +++ b/src/Routes/Users/PasswordReset.php @@ -0,0 +1,27 @@ +userService = $userService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/password-reset/:userNameOrEmail'; + } + + public function work() + { + $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail')); + return $this->userService->sendPasswordResetEmail($user); + } +} diff --git a/src/Routes/Users/UpdateUser.php b/src/Routes/Users/UpdateUser.php new file mode 100644 index 00000000..ec340e9f --- /dev/null +++ b/src/Routes/Users/UpdateUser.php @@ -0,0 +1,96 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + $formData = new UserEditFormData($this->inputReader); + + if ($formData->avatarStyle !== null || $formData->avatarContent !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_AVATAR_STYLE + : Privilege::CHANGE_ALL_AVATAR_STYLES); + } + + if ($formData->userName !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_NAME + : Privilege::CHANGE_ALL_NAMES); + } + + if ($formData->password !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_PASSWORD + : Privilege::CHANGE_ALL_PASSWORDS); + } + + if ($formData->email !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_EMAIL_ADDRESS + : Privilege::CHANGE_ALL_EMAIL_ADDRESSES); + } + + if ($formData->accessRank) + { + $this->privilegeService->assertPrivilege(Privilege::CHANGE_ACCESS_RANK); + } + + if ($formData->browsingSettings) + { + $this->privilegeService->assertLoggedIn($userNameOrEmail); + } + + if ($formData->banned !== null) + { + $this->privilegeService->assertPrivilege(Privilege::BAN_USERS); + } + + $user = $this->userService->updateUser($user, $formData); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/di.php b/src/di.php index e2952279..f4047fcf 100644 --- a/src/di.php +++ b/src/di.php @@ -57,7 +57,6 @@ return [ 'controllers' => DI\factory(function (DI\container $container) { return [ - $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), ]; @@ -95,6 +94,15 @@ return [ $container->get(\Szurubooru\Routes\Tags\GetTags::class), $container->get(\Szurubooru\Routes\Tags\MergeTags::class), $container->get(\Szurubooru\Routes\Tags\UpdateTag::class), + $container->get(\Szurubooru\Routes\Users\ActivateAccount::class), + $container->get(\Szurubooru\Routes\Users\CreateUser::class), + $container->get(\Szurubooru\Routes\Users\DeleteUser::class), + $container->get(\Szurubooru\Routes\Users\FinishActivation::class), + $container->get(\Szurubooru\Routes\Users\FinishPasswordReset::class), + $container->get(\Szurubooru\Routes\Users\GetUser::class), + $container->get(\Szurubooru\Routes\Users\GetUsers::class), + $container->get(\Szurubooru\Routes\Users\PasswordReset::class), + $container->get(\Szurubooru\Routes\Users\UpdateUser::class), ]; }), ];