/** * @param array $data * @throws Exception * @throws ValidatorException * @return integer */ public function createOne($data) { // password $password = $data['password'] ?? null; $password2 = $data['password2'] ?? null; if (empty($password)) { throw ValidatorException::exception('password', __('Password can\'t be empty')); } if ($password !== $password2) { throw ValidatorException::exception('password2', __('Password is not equal')); } if ($data['id'] == '') { unset($data['id']); } /** @var $row Row */ $row = $this->getTable()->create(); $row->setFromArray($data); $row->status = Table::STATUS_PENDING; $row->save(); $userId = $row->id; // create auth Auth\Table::getInstance()->generateEquals($row, $password); // create activation token // valid for 5 days $actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5); // send activation email // generate activation URL $activationUrl = Router::getFullUrl('users', 'activation', ['code' => $actionRow->code, 'id' => $userId]); $subject = "Activation"; $body = Application::getInstance()->dispatch('users', 'mail/template', ['template' => 'registration', 'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password]])->render(); try { $mail = Mailer::create(); $mail->Subject = $subject; $mail->msgHTML(nl2br($body)); $mail->addAddress($data['email']); Mailer::send($mail); } catch (\Exception $e) { Logger::log('error', $e->getMessage(), ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId]); throw new Exception('Unable to send email. Please contact administrator.'); } // show notification and redirect Messages::addSuccess("Your account has been created and an activation link has" . "been sent to the e-mail address you entered.<br/>" . "Note that you must activate the account by clicking on the activation link" . "when you get the e-mail before you can login."); // wtf? // redirectTo('index', 'index'); return $userId; }
$mail->msgHTML(nl2br($body)); $mail->addAddress($email); Mailer::send($mail); Messages::addNotice('Check your email and follow instructions in letter.'); } catch (\Exception $e) { Logger::log('error', $e->getMessage(), ['module' => 'users', 'controller' => 'change-email', 'userId' => $userId]); throw new Exception('Unable to send email. Please contact administrator.'); } // try back to index Response::redirectTo('users', 'profile'); } catch (Exception $e) { Messages::addError($e->getMessage()); $this->assign('email', $email); } catch (AuthException $e) { Messages::addError($e->getMessage()); $this->assign('email', $email); } } elseif ($token) { // process activation $actionRow = UsersActions\Table::findRowWhere(['code' => $token, 'userId' => $userId]); if (!$actionRow) { throw new Exception('Invalid token'); } $params = $actionRow->getParams(); $user->email = $params['email']; $user->save(); $actionRow->delete(); Messages::addSuccess('Email was updated'); Response::redirectTo('users', 'profile'); } };
use Bluz\Proxy\Messages; use Bluz\Proxy\Request; use Bluz\Proxy\Response; /** * @param int $id User UID * @param string $code * @param string $password * @param string $password2 */ return function ($id, $code, $password = null, $password2 = null) { /** * @var Controller $this */ // change layout $this->useLayout('small.phtml'); $actionRow = UsersActions\Table::findRow(['userId' => $id, 'code' => $code]); $datetime1 = new \DateTime(); // now $datetime2 = new \DateTime($actionRow->expired); $interval = $datetime1->diff($datetime2); if (!$actionRow or $actionRow->action !== UsersActions\Table::ACTION_RECOVERY) { Messages::addError('Invalid code'); Response::redirectTo('index', 'index'); } elseif ($interval->invert) { Messages::addError('The activation code has expired'); $actionRow->delete(); Response::redirectTo('index', 'index'); } else { $user = Users\Table::findRow($id); $this->assign('user', $user); $this->assign('code', $code);
} } else { throw new Exception('Email is invalid'); } // check exists $user = Users\Table::findRowWhere(['email' => $email]); if (!$user) { throw new Exception('Email not found'); } // check status, only for active users if ($user->status != Users\Table::STATUS_ACTIVE) { throw new Exception('User is inactive'); } // create activation token // valid for 5 days $actionRow = UsersActions\Table::getInstance()->generate($user->id, UsersActions\Table::ACTION_RECOVERY, 5); // send activation email // generate restore URL $resetUrl = Router::getFullUrl('users', 'recovery-reset', ['code' => $actionRow->code, 'id' => $user->id]); $subject = "Password Recovery"; $body = $this->dispatch('users', 'mail-template', ['template' => 'recovery', 'vars' => ['user' => $user, 'resetUrl' => $resetUrl]])->render(); try { $mail = Mailer::create(); // subject $mail->Subject = $subject; $mail->MsgHTML(nl2br($body)); $mail->AddAddress($user->email); Mailer::send($mail); } catch (\Exception $e) { // log it Logger::log('error', $e->getMessage(), ['module' => 'users', 'controller' => 'recovery', 'email' => $email]);