*/ class DefaultController extends \Piko\Controller { protected PikoUser $user; protected \PDO $db; public function init(): void { $app = $this->module->getApplication(); $user = $app->getComponent('Piko\User'); assert($user instanceof PikoUser); $this->user = $user; $db = $app->getComponent('PDO'); assert($db instanceof \PDO); $this->db = $db; } /** * Render and process user registration * * @return string */ public function registerAction() { if (!$this->user->isGuest()) { return $this->redirect('/'); } $message = false; $post = $this->request->getParsedBody(); if (!empty($post)) { $module = $this->module; assert ($module instanceof \app\modules\user\Module); $user = new User($this->db); $user->scenario = User::SCENARIO_REGISTER; $user->passwordMinLength = $module->passwordMinLength; $user->bind($post); if ($user->isValid() && $user->save()) { // $user->sendRegistrationConfirmation(); $message['type'] = 'success'; $message['content'] = __( 'user', 'Your account was created. Please activate it through the confirmation email that was sent to you.' ); } else { $message['type'] = 'danger'; $message['content'] = implode(', ', $user->errors); } } return $this->render('register', [ 'message' => $message, ]); } /** * Validate registration (AJAX) * * @return string */ public function checkRegistrationAction() { $errors = []; $this->layout = false; $post = $this->request->getParsedBody(); if (!empty($post)) { $module = $this->module; assert ($module instanceof \app\modules\user\Module); $user = new User($this->db); $user->scenario = 'register'; $user->passwordMinLength = $module->passwordMinLength; $user->bind($post); $user->isValid(); $errors = $user->getErrors(); } return $this->jsonResponse($errors); } /** * Render user activation confirmation * * @throws HttpException * @return string */ public function confirmationAction($token) { $user = User::findByAuthKey($token); if (!$user) { throw new HttpException('Not found.', 404); } $message = false; if (!$user->isActivated()) { if ($user->activate()) { $message['type'] = 'success'; $message['content'] = __('user', 'Your account has been activated. You can now log in.'); } else { $message['type'] = 'danger'; $message['content'] = __( 'user', 'Unable to activate your account. Please contact the site manager.' ); } } else { $message['type'] = 'warning'; $message['content'] = __('user', 'Your account has already been activated.'); } return $this->render('login', ['message' => $message]); } /** * Render reminder password form and send email to change password * * @return string */ public function reminderAction() { $message = false; $post = $this->request->getParsedBody(); $reminder = $post['reminder']?? ''; if (!empty($reminder)) { $user = User::findByUsername($reminder); if (!$user) { $user = User::findByEmail($reminder); } if ($user) { $app = $this->module->getApplication(); $router = $app->getComponent('Piko\Router'); $mailer = $app->getComponent('Nette\Mail\SmtpMailer'); $user->sendResetPassword($router, $mailer); $message['type'] = 'success'; $message['content'] = __( 'user', 'A link has been sent to you by email ({email}). It will allow you to recreate your password.', ['email' => $user->email] ); $reminder = ''; } else { $message['type'] = 'danger'; $message['content'] = __('user', 'Account not found.'); } } return $this->render('reminder', [ 'message' => $message, 'reminder' => $reminder, ]); } /** * Render and process reset password * * @throws HttpException * @return string */ public function resetPasswordAction($token) { $user = User::findByAuthKey($token); if (!$user) { throw new HttpException('User not found', 404); } $message = false; $post = $this->request->getParsedBody(); if (!empty($post)) { $user->scenario = 'reset'; $user->bind($post); if ($user->isValid() && $user->save()) { $message['type'] = 'success'; $message['content'] = __('user', 'Your password has been successfully updated.'); } else { $message['type'] = 'danger'; $message['content'] = implode(', ', $user->errors); } } return $this->render('reset', [ 'message' => $message, 'user' => $user, ]); } /** * Render user form and update changes * * @throws HttpException * @return string */ public function editAction() { if ($this->user->isGuest()) { throw new HttpException(__('user', 'You must be logged to access this page.'), 401); } $identity = $this->user->getIdentity(); assert($identity instanceof User); $message = false; $post = $this->request->getParsedBody(); if (!empty($post)) { $identity->bind($post); if ($identity->isValid() && $identity->save()) { $message['type'] = 'success'; $message['content'] = __('user', 'Changes saved!'); } else { $message['type'] = 'danger'; $message['content'] = implode(', ', $identity->getErrors()); } } return $this->render('edit', [ 'user' => $identity, 'message' => $message, ]); } /** * Render login form and process login * * @return string */ public function loginAction() { $message = false; $post = $this->request->getParsedBody(); if (!empty($post)) { $identity = User::findByUsername($post['username']); if ($identity instanceof User && $identity->validatePassword($post['password'])) { $this->user->login($identity); $identity->last_login_at = time(); $identity->save(); return $this->redirect('/'); } else { $message['type'] = 'danger'; $message['content'] = __('user', 'Authentication failure'); } } assert($this->module instanceof \app\modules\user\Module); return $this->render('login', [ 'message' => $message, 'canRegister' => $this->module->allowUserRegistration ]); } /** * User logout */ public function logoutAction() { $this->user->logout(); $this->redirect('/'); } }