From dd579b3513f6b393e7c6bb4375ee880d44e9940f Mon Sep 17 00:00:00 2001 From: Corne Oppelaar Date: Wed, 29 Jun 2016 02:07:03 +0200 Subject: [PATCH] add verify mail and email address saving --- config/app.yml | 3 ++ db.sqlite | 0 src/Core.php | 21 +++++++++- src/Service/Mail.php | 64 +++++++++++++++++++++++++++++++ src/Service/MailSender.php | 41 ++++++++++++++++++++ src/Service/User.php | 42 +++++++++++++++++++- views/mails/verify_email.txt.twig | 11 ++++++ 7 files changed, 178 insertions(+), 4 deletions(-) delete mode 100644 db.sqlite create mode 100644 src/Service/Mail.php create mode 100644 src/Service/MailSender.php create mode 100644 views/mails/verify_email.txt.twig diff --git a/config/app.yml b/config/app.yml index a652521..10f1397 100644 --- a/config/app.yml +++ b/config/app.yml @@ -7,6 +7,7 @@ core: author: "EaterOfCode" sessionName: '0sess' github-repo: 'zerooo-vpn/zer.ooo' + base-url: 'https://zer.ooo' services: slim: Eater\Glim\Service\Slim user: Eater\Glim\Service\User @@ -17,3 +18,5 @@ core: server: Eater\Glim\Service\Server stash: Eater\Glim\Service\Stash github: Eater\Glim\Service\GitHub + mail-sender: Eater\Glim\Service\MailSender + mail: Eater\Glim\Service\Mail diff --git a/db.sqlite b/db.sqlite deleted file mode 100644 index e69de29..0000000 diff --git a/src/Core.php b/src/Core.php index 4eb17ef..011bbc9 100644 --- a/src/Core.php +++ b/src/Core.php @@ -9,6 +9,7 @@ use Monolog\Handler\StreamHandler; use Monolog\Logger; use Noodlehaus\Config; use Propel\Runtime\Propel; +use Psr\Http\Message\UriInterface; use Slim\App; use Slim\Container; use Slim\Http\Request; @@ -140,7 +141,7 @@ class Core implements ContainerInterface } /** - * @return mixed + * @return Config */ public function getConfig() { @@ -148,7 +149,7 @@ class Core implements ContainerInterface } /** - * @param mixed $config + * @param Config $config */ public function setConfig($config) { @@ -314,6 +315,17 @@ class Core implements ContainerInterface return $this->get('slim'); } + private function getBaseUrlFromUri(UriInterface $uri) { + $baseUrl = $uri->getScheme() . '://' . $uri->getHost(); + + if (($uri->getScheme() === 'http' && $uri->getPort() != 80) || + ($uri->getScheme() === 'https' && $uri->getPort() != 443)) { + $baseUrl .= ':' . $uri->getPort(); + } + + return $baseUrl; + } + /** * @param string $class * @param Request $request @@ -324,6 +336,11 @@ class Core implements ContainerInterface public function handle($class, Request $request, Response $response, ContainerInterface $containerInterface) { $this->startTimer(['response']); + + /** set host */ + + $this->set('base-url', $this->getBaseUrlFromUri($request->getUri())); + /** @var Handler\Main $handler */ $handler = new $class($this, $request, $response, $containerInterface); diff --git a/src/Service/Mail.php b/src/Service/Mail.php new file mode 100644 index 0000000..54b8c51 --- /dev/null +++ b/src/Service/Mail.php @@ -0,0 +1,64 @@ +createEmailMessage($template, $context); + $message->setRecipientEmailAddress($user->getEmailAddressRelatedByEmail()); + $message->save(); + + $this->sendEmailMessage($message); + } + + /** + * @param string $template + * @param array $context + * @return EmailMessage + */ + public function createEmailMessage($template, $context) { + $message = new EmailMessage(); + + /** @var \Twig_Environment $twig */ + $twig = $this->get('twig'); + + $txt = $twig->render($template, $context); + + list($subject, $contents) = explode('===', $txt); + + $message->setSubject(trim($subject)); + $message->setContents(trim($contents)); + + return $message; + } + + /** + * @param EmailMessage $emailMessage + */ + public function sendEmailMessage(EmailMessage $emailMessage) + { + $mail = new Message(); + $mail->setSubject($emailMessage->getSubject()); + $mail->setBody($emailMessage->getContents()); + $mail->setFrom($emailMessage->getSenderEmail(), $emailMessage->getSenderName()); + $mail->setReturnPath($emailMessage->getSenderEmail()); + + $mail->addTo($emailMessage->getRecipientEmail(), $emailMessage->getRecipientName()); + /** @var IMailer $sender */ + $sender = $this->get('mail-sender'); + + $sender->send($mail); + } +} \ No newline at end of file diff --git a/src/Service/MailSender.php b/src/Service/MailSender.php new file mode 100644 index 0000000..d94c5c2 --- /dev/null +++ b/src/Service/MailSender.php @@ -0,0 +1,41 @@ +get('core'); + $config = $core->getConfig(); + + if ($config->get('mail.type', 'sendmail') === 'smtp') { + return new SmtpMailer([ + 'host' => $config->get('mail.host', 'localhost'), + 'secure' => $config->get('mail.secure'), + 'username' => $config->get('mail.username'), + 'password' => $config->get('mail.password'), + ]); + } else { + return new SendmailMailer(); + } + } +} \ No newline at end of file diff --git a/src/Service/User.php b/src/Service/User.php index 53eee29..e192c91 100644 --- a/src/Service/User.php +++ b/src/Service/User.php @@ -2,7 +2,9 @@ namespace Eater\Glim\Service; -use Eater\Glim\Model\Base\UserQuery; +use Eater\Glim\Core; +use Eater\Glim\Model\UserQuery; +use Eater\Glim\Model\EmailAddress; use Eater\Glim\Model\InviteQuery; use Eater\Glim\Model\Invite; use Eater\Glim\Model\User as UserModel; @@ -31,7 +33,12 @@ class User extends Main $user = new UserModel(); $user->setUsername($username); - $user->setEmail($email); + + $emailAddress = new EmailAddress(); + $emailAddress->setAddress($email); + $emailAddress->fillVerification(); + + $user->setEmailAddressRelatedByEmail($emailAddress); $user->setPassword(\password_hash($password, PASSWORD_DEFAULT)); if ($inviteUser === null || $inviteUser->getMaxInvites() === -1) { @@ -44,6 +51,11 @@ class User extends Main $user->save(); + $emailAddress->setUserRelatedByOwner($user); + $emailAddress->save(); + + $this->sendVerifyMail($user); + $invite->delete(); return $user; @@ -84,8 +96,18 @@ class User extends Main $user->setPassword(\password_hash($password, PASSWORD_DEFAULT)); $user->setSuperuser(true); $user->setMaxInvites(-1); + + $emailAddress = new EmailAddress(); + $emailAddress->setAddress($email); + $emailAddress->fillVerification(); + $user->setEmailAddressRelatedByEmail($emailAddress); $user->save(); + $emailAddress->setUserRelatedByOwner($user); + $emailAddress->save(); + + $this->sendVerifyMail($user); + return $user; } @@ -127,4 +149,20 @@ class User extends Main return $invite->getInvite(); } + + /** + * @param UserModel $user + */ + private function sendVerifyMail(UserModel $user) + { + /** @var Mail $mail */ + $mail = $this->get('mail'); + + $address = $user->getEmailAddressRelatedByEmail(); + + $mail->sendFromSystem($user, 'mails/verify_email.txt.twig', [ + 'username' => $user->getUsername(), + 'verificationLink' => $this->get('base-url') . '/verify/' . $address->getId() . '-' . $address->getVerification() + ]); + } } diff --git a/views/mails/verify_email.txt.twig b/views/mails/verify_email.txt.twig new file mode 100644 index 0000000..a9b77c0 --- /dev/null +++ b/views/mails/verify_email.txt.twig @@ -0,0 +1,11 @@ +Verify your Zer.ooo account: {{ username }} +=== +Hey {{ username }}, + +You successfully created an account on Zer.ooo, please click on the following link to verify your account. + +{{ verificationLink }} + +Kind regards, + +The Zer.ooo team \ No newline at end of file