add verify mail and email address saving
This commit is contained in:
parent
0b9873ef7b
commit
dd579b3513
7 changed files with 178 additions and 4 deletions
|
@ -7,6 +7,7 @@ core:
|
||||||
author: "EaterOfCode"
|
author: "EaterOfCode"
|
||||||
sessionName: '0sess'
|
sessionName: '0sess'
|
||||||
github-repo: 'zerooo-vpn/zer.ooo'
|
github-repo: 'zerooo-vpn/zer.ooo'
|
||||||
|
base-url: 'https://zer.ooo'
|
||||||
services:
|
services:
|
||||||
slim: Eater\Glim\Service\Slim
|
slim: Eater\Glim\Service\Slim
|
||||||
user: Eater\Glim\Service\User
|
user: Eater\Glim\Service\User
|
||||||
|
@ -17,3 +18,5 @@ core:
|
||||||
server: Eater\Glim\Service\Server
|
server: Eater\Glim\Service\Server
|
||||||
stash: Eater\Glim\Service\Stash
|
stash: Eater\Glim\Service\Stash
|
||||||
github: Eater\Glim\Service\GitHub
|
github: Eater\Glim\Service\GitHub
|
||||||
|
mail-sender: Eater\Glim\Service\MailSender
|
||||||
|
mail: Eater\Glim\Service\Mail
|
||||||
|
|
21
src/Core.php
21
src/Core.php
|
@ -9,6 +9,7 @@ use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Noodlehaus\Config;
|
use Noodlehaus\Config;
|
||||||
use Propel\Runtime\Propel;
|
use Propel\Runtime\Propel;
|
||||||
|
use Psr\Http\Message\UriInterface;
|
||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Container;
|
use Slim\Container;
|
||||||
use Slim\Http\Request;
|
use Slim\Http\Request;
|
||||||
|
@ -140,7 +141,7 @@ class Core implements ContainerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return Config
|
||||||
*/
|
*/
|
||||||
public function getConfig()
|
public function getConfig()
|
||||||
{
|
{
|
||||||
|
@ -148,7 +149,7 @@ class Core implements ContainerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $config
|
* @param Config $config
|
||||||
*/
|
*/
|
||||||
public function setConfig($config)
|
public function setConfig($config)
|
||||||
{
|
{
|
||||||
|
@ -314,6 +315,17 @@ class Core implements ContainerInterface
|
||||||
return $this->get('slim');
|
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 string $class
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
|
@ -324,6 +336,11 @@ class Core implements ContainerInterface
|
||||||
public function handle($class, Request $request, Response $response, ContainerInterface $containerInterface)
|
public function handle($class, Request $request, Response $response, ContainerInterface $containerInterface)
|
||||||
{
|
{
|
||||||
$this->startTimer(['response']);
|
$this->startTimer(['response']);
|
||||||
|
|
||||||
|
/** set host */
|
||||||
|
|
||||||
|
$this->set('base-url', $this->getBaseUrlFromUri($request->getUri()));
|
||||||
|
|
||||||
/** @var Handler\Main $handler */
|
/** @var Handler\Main $handler */
|
||||||
$handler = new $class($this, $request, $response, $containerInterface);
|
$handler = new $class($this, $request, $response, $containerInterface);
|
||||||
|
|
||||||
|
|
64
src/Service/Mail.php
Normal file
64
src/Service/Mail.php
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Eater\Glim\Service;
|
||||||
|
|
||||||
|
use Eater\Glim\Model\EmailMessage;
|
||||||
|
use Eater\Glim\Model\User as UserModel;
|
||||||
|
use Nette\Mail\IMailer;
|
||||||
|
use Nette\Mail\Message;
|
||||||
|
|
||||||
|
class Mail extends Main
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param UserModel $user
|
||||||
|
* @param string $template
|
||||||
|
* @param array $context
|
||||||
|
*/
|
||||||
|
public function sendFromSystem(UserModel $user, $template, $context)
|
||||||
|
{
|
||||||
|
$message = $this->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);
|
||||||
|
}
|
||||||
|
}
|
41
src/Service/MailSender.php
Normal file
41
src/Service/MailSender.php
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: eater
|
||||||
|
* Date: 6/29/16
|
||||||
|
* Time: 1:15 AM
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Eater\Glim\Service;
|
||||||
|
|
||||||
|
|
||||||
|
use Eater\Glim\Core;
|
||||||
|
use Nette\Mail\IMailer;
|
||||||
|
use Nette\Mail\SendmailMailer;
|
||||||
|
use Nette\Mail\SmtpMailer;
|
||||||
|
use Slim\Container;
|
||||||
|
|
||||||
|
class MailSender
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param Container $c
|
||||||
|
* @param string $name
|
||||||
|
* @return IMailer
|
||||||
|
*/
|
||||||
|
static public function init($c, $name) {
|
||||||
|
/** @var Core $core */
|
||||||
|
$core = $c->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
namespace Eater\Glim\Service;
|
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\InviteQuery;
|
||||||
use Eater\Glim\Model\Invite;
|
use Eater\Glim\Model\Invite;
|
||||||
use Eater\Glim\Model\User as UserModel;
|
use Eater\Glim\Model\User as UserModel;
|
||||||
|
@ -31,7 +33,12 @@ class User extends Main
|
||||||
|
|
||||||
$user = new UserModel();
|
$user = new UserModel();
|
||||||
$user->setUsername($username);
|
$user->setUsername($username);
|
||||||
$user->setEmail($email);
|
|
||||||
|
$emailAddress = new EmailAddress();
|
||||||
|
$emailAddress->setAddress($email);
|
||||||
|
$emailAddress->fillVerification();
|
||||||
|
|
||||||
|
$user->setEmailAddressRelatedByEmail($emailAddress);
|
||||||
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
||||||
|
|
||||||
if ($inviteUser === null || $inviteUser->getMaxInvites() === -1) {
|
if ($inviteUser === null || $inviteUser->getMaxInvites() === -1) {
|
||||||
|
@ -44,6 +51,11 @@ class User extends Main
|
||||||
|
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
|
$emailAddress->setUserRelatedByOwner($user);
|
||||||
|
$emailAddress->save();
|
||||||
|
|
||||||
|
$this->sendVerifyMail($user);
|
||||||
|
|
||||||
$invite->delete();
|
$invite->delete();
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
|
@ -84,8 +96,18 @@ class User extends Main
|
||||||
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
||||||
$user->setSuperuser(true);
|
$user->setSuperuser(true);
|
||||||
$user->setMaxInvites(-1);
|
$user->setMaxInvites(-1);
|
||||||
|
|
||||||
|
$emailAddress = new EmailAddress();
|
||||||
|
$emailAddress->setAddress($email);
|
||||||
|
$emailAddress->fillVerification();
|
||||||
|
$user->setEmailAddressRelatedByEmail($emailAddress);
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
|
$emailAddress->setUserRelatedByOwner($user);
|
||||||
|
$emailAddress->save();
|
||||||
|
|
||||||
|
$this->sendVerifyMail($user);
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,4 +149,20 @@ class User extends Main
|
||||||
|
|
||||||
return $invite->getInvite();
|
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()
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
11
views/mails/verify_email.txt.twig
Normal file
11
views/mails/verify_email.txt.twig
Normal file
|
@ -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
|
Loading…
Reference in a new issue