diff --git a/src/Cli/AddUserCommand.php b/src/Cli/AddUserCommand.php index 45fb57d..90f3ba3 100644 --- a/src/Cli/AddUserCommand.php +++ b/src/Cli/AddUserCommand.php @@ -1,7 +1,7 @@ $input->getArgument('email'), - ]); - + $user = new User(); + $user->setEmail($input->getArgument('email')); $user->setPlainPassword($input->getArgument('password')); if ($input->hasArgument('type')) { @@ -31,9 +29,8 @@ class AddUserCommand extends Command } try { - $modify = new Modify(\Container::$dbConnection, 'users'); - $modify->fill($user->toArray()); - $modify->save(); + $pdm = new PersistentDataManager(); + $pdm->saveToDb($user); } catch (\Exception $e) { $output->writeln('Adding user failed!'); $output->writeln(''); diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index a8227af..fe0cb2d 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -1,11 +1,9 @@ request = $request; + $this->userRepository = new UserRepository(); } public function getLoginForm() @@ -40,19 +41,13 @@ class LoginController return new JsonContent($data); } - $select = new Select(\Container::$dbConnection, 'users'); - $select->columns(User::getFields()); - $select->where('email', '=', $this->request->post('email')); + $user = $this->userRepository->getByEmail($this->request->post('email')); - $userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC); - - if ($userData === null) { + if ($user === null) { $data = ['error' => 'user_not_found']; return new JsonContent($data); } - $user = new User($userData); - if (!$user->getActive()) { $data = ['error' => 'user_not_active']; return new JsonContent($data); diff --git a/src/Controller/SignupController.php b/src/Controller/SignupController.php index b50bfe2..1e85b82 100644 --- a/src/Controller/SignupController.php +++ b/src/Controller/SignupController.php @@ -7,7 +7,9 @@ use MapGuesser\Interfaces\Request\IRequest; use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IRedirect; use MapGuesser\Mailing\Mail; -use MapGuesser\Model\User; +use MapGuesser\PersistentData\PersistentDataManager; +use MapGuesser\PersistentData\Model\User; +use MapGuesser\Repository\UserRepository; use MapGuesser\Response\HtmlContent; use MapGuesser\Response\JsonContent; use MapGuesser\Response\Redirect; @@ -16,9 +18,15 @@ class SignupController { private IRequest $request; + private PersistentDataManager $pdm; + + private UserRepository $userRepository; + public function __construct(IRequest $request) { $this->request = $request; + $this->pdm = new PersistentDataManager(); + $this->userRepository = new UserRepository(); } public function getSignupForm() @@ -48,15 +56,9 @@ class SignupController return new JsonContent($data); } - $select = new Select(\Container::$dbConnection, 'users'); - $select->columns(User::getFields()); - $select->where('email', '=', $this->request->post('email')); - - $userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC); - - if ($userData !== null) { - $user = new User($userData); + $user = $this->userRepository->getByEmail($this->request->post('email')); + if ($user !== null) { if ($user->getActive()) { $data = ['error' => 'user_found']; } else { @@ -75,23 +77,18 @@ class SignupController return new JsonContent($data); } - $user = new User([ - 'email' => $this->request->post('email'), - ]); - + $user = new User(); + $user->setEmail($this->request->post('email')); $user->setPlainPassword($this->request->post('password')); \Container::$dbConnection->startTransaction(); - $modify = new Modify(\Container::$dbConnection, 'users'); - $modify->fill($user->toArray()); - $modify->save(); - $userId = $modify->getId(); + $this->pdm->saveToDb($user); $token = hash('sha256', serialize($user) . random_bytes(10) . microtime()); $modify = new Modify(\Container::$dbConnection, 'user_confirmations'); - $modify->set('user_id', $userId); + $modify->set('user_id', $user->getId()); $modify->set('token', $token); $modify->save(); @@ -128,20 +125,13 @@ class SignupController $modify->setId($confirmation['id']); $modify->delete(); - $modify = new Modify(\Container::$dbConnection, 'users'); - $modify->setId($confirmation['user_id']); - $modify->set('active', true); - $modify->save(); + $user = $this->userRepository->getById($confirmation['user_id']); + $user->setActive(true); + + $this->pdm->saveToDb($user); \Container::$dbConnection->commit(); - $select = new Select(\Container::$dbConnection, 'users'); - $select->columns(User::getFields()); - $select->whereId($confirmation['user_id']); - - $userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC); - $user = new User($userData); - $session->set('user', $user); return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY); @@ -172,9 +162,9 @@ class SignupController $modify->setId($confirmation['id']); $modify->delete(); - $modify = new Modify(\Container::$dbConnection, 'users'); - $modify->setId($confirmation['user_id']); - $modify->delete(); + $user = $this->userRepository->getById($confirmation['user_id']); + + $this->pdm->deleteFromDb($user); \Container::$dbConnection->commit(); diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index f60f809..001ad23 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -1,9 +1,10 @@ request = $request; + $this->pdm = new PersistentDataManager(); } public function authorize(): bool @@ -25,6 +29,9 @@ class UserController implements ISecured public function getProfile(): IContent { + /** + * @var User $user + */ $user = $this->request->user(); $data = ['user' => $user->toArray()]; @@ -33,6 +40,9 @@ class UserController implements ISecured public function saveProfile(): IContent { + /** + * @var User $user + */ $user = $this->request->user(); if (!$user->checkPassword($this->request->post('password'))) { @@ -54,9 +64,7 @@ class UserController implements ISecured $user->setPlainPassword($this->request->post('password_new')); } - $modify = new Modify(\Container::$dbConnection, 'users'); - $modify->fill($user->toArray()); - $modify->save(); + $this->pdm->saveToDb($user); $this->request->session()->set('user', $user); diff --git a/src/PersistentData/Model/User.php b/src/PersistentData/Model/User.php index 52165f5..0390fc2 100644 --- a/src/PersistentData/Model/User.php +++ b/src/PersistentData/Model/User.php @@ -4,15 +4,15 @@ use MapGuesser\Interfaces\Authentication\IUser; class User extends Model implements IUser { - private static array $types = ['user', 'admin']; - protected static string $table = 'users'; protected static array $fields = ['email', 'password', 'type', 'active']; - private string $email; + private static array $types = ['user', 'admin']; - private string $password; + private string $email = ''; + + private string $password = ''; private string $type = 'user'; diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php new file mode 100644 index 0000000..b67771d --- /dev/null +++ b/src/Repository/UserRepository.php @@ -0,0 +1,28 @@ +pdm = new PersistentDataManager(); + } + + public function getById(int $userId): ?User + { + return $this->pdm->selectFromDbById($userId, User::class); + } + + public function getByEmail(string $email): ?User + { + $select = new Select(\Container::$dbConnection); + $select->where('email', '=', $email); + + return $this->pdm->selectFromDb($select, User::class); + } +}