Skip to content

Commit 02caaea

Browse files
authored
Merge pull request #1 from secultce/develop
feat[BREAKING]: Atualizando renderização dos templates de e-mail para implementação do Mapas
2 parents a3ff47d + 6fee602 commit 02caaea

File tree

8 files changed

+194
-1736
lines changed

8 files changed

+194
-1736
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
Todas as mudanças notáveis no projeto serão documentadas neste arquivo.
3+
4+
O formato é baseado no [Keep a Changelog](https://keepachangelog.com/pt-BR/1.0.0/)
5+
e este projeto adere ao [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
> [!NOTE]
8+
> Apenas a partir da versão 2.3.0 deste fork
9+
10+
## [2.3.0] - 2024-09-17
11+
### Modificado
12+
- Implementação de renderização de e-mail igual ao da plataforma.
13+
14+
> [!WARNING]
15+
> Os templates de e-mail foram movidos da pasta views/auth para templates

Plugin.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ class Plugin extends \MapasCulturais\Plugin {
1414

1515
public function _init() {
1616
$app = App::i();
17-
17+
$app->_config['mailer.templates'] = array_merge($app->_config['mailer.templates'], [
18+
'email-to-validate-account' => [
19+
'title' => 'Email de validação de conta',
20+
'template' => 'email-to-validate-account.html',
21+
],
22+
'email-to-reset-password' => [
23+
'title' => 'Email de recuperação de senha',
24+
'template' => 'email-to-reset-password.html',
25+
],
26+
]);
27+
1828
// register translation text domain
1929
i::load_textdomain( 'multipleLocal', __DIR__ . "/translations" );
2030

Provider.php

Lines changed: 69 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,16 @@ protected function _init() {
151151
$this->json(array("passwordRules"=>$passwordRules));
152152
});
153153

154-
$app->hook('GET(auth.confirma-email)', function () use($app){
154+
$app->hook('GET(auth.confirma-email)', function () use($app) {
155155

156156
$app = App::i();
157157
$token = filter_var($app->request->get('token'), FILTER_SANITIZE_STRING);
158158

159159
$usermeta = $app->repo("UserMeta")->findOneBy(array('key' => Provider::$tokenVerifyAccountMetadata, 'value' => $token));
160160

161161
if (!$usermeta) {
162-
$errorMsg = i::__('Token inválidos', 'multipleLocal');
163-
// $app->auth->render('confirm-email',['msg'=>'TEM MSG NAO']);
164-
$this->render('confirm-email',['msg'=>$errorMsg]);
162+
$errorMsg = i::__('Token inválidos', 'multipleLocal');
163+
$this->render('confirm-email',['msg'=>$errorMsg]);
165164
}
166165

167166
$user = $usermeta->owner;
@@ -377,7 +376,7 @@ protected function _init() {
377376

378377
$app->hook('POST(auth.recover-resetform)', function () use($app){
379378

380-
if ($app->auth->dorecover()) {
379+
if ($app->auth->doRecover()) {
381380
$this->error_msg = i::__('Senha alterada com sucesso. Agora você pode fazer login', 'multipleLocal');
382381
$app->auth->renderForm($this);
383382
} else {
@@ -665,23 +664,24 @@ function renderRecoverForm($theme) {
665664
]);
666665
}
667666

668-
function dorecover() {
667+
public function doRecover(): bool
668+
{
669669
$app = App::i();
670670
$email = filter_var($app->request->post('email'), FILTER_SANITIZE_STRING);
671671
$pass = filter_var($app->request->post('password'), FILTER_SANITIZE_STRING);
672672
$pass_v = filter_var($app->request->post('confirm_password'), FILTER_SANITIZE_STRING);
673673
$user = $app->repo("User")->findOneBy(array('email' => $email));
674674
$token = filter_var($app->request->get('t'), FILTER_SANITIZE_STRING);
675-
675+
676676
if (!$user) {
677677
$this->feedback_success = false;
678678
$this->triedEmail = $email;
679679
$this->feedback_msg = i::__('Email ou token inválidos', 'multipleLocal');
680680
return false;
681681
}
682-
682+
683683
$savedToken = $user->getMetadata('recover_token');
684-
684+
685685
if (!$savedToken || $savedToken != $token) {
686686
$this->feedback_success = false;
687687
$this->triedEmail = $email;
@@ -690,106 +690,98 @@ function dorecover() {
690690
}
691691

692692
$recover_token_time = $user->getMetadata('recover_token_time');
693-
693+
694694
// check if token is still valid
695695
$now = time();
696696
$diff = $now - intval($recover_token_time);
697-
697+
698698
if ($diff > 60 * 60 * 24 * 30) {
699699
$this->feedback_success = false;
700700
$this->triedEmail = $email;
701701
$this->feedback_msg = i::__('Este token expirou', 'multipleLocal');
702702
return false;
703703
}
704-
705-
if (!$this->verifyPassowrds($pass, $pass_v))
704+
705+
if (!$this->verifyPassowrds($pass, $pass_v)) {
706706
return false;
707-
707+
}
708+
708709
$user->setMetadata(self::$passMetaName, $this->hashPassword($pass));
709710
$user->setMetadata(Provider::$accountIsActiveMetadata, '1');
710-
711+
711712
$app->disableAccessControl();
712-
$user->save(true);
713+
$user->save(true);
713714
$app->enableAccessControl();
714-
715+
715716
$this->middlewareLoginAttempts(true); //tira o BAN de login do usuario
716717

717718
$this->feedback_success = true;
718719
$this->triedEmail = $email;
719720
$this->feedback_msg = i::__('Senha alterada com sucesso! Você pode fazer login agora', 'multipleLocal');
720-
721+
721722
return true;
722723
}
723-
724-
function recover() {
724+
725+
private function recover(): void
726+
{
725727
$app = App::i();
726728
$config = $app->_config;
727729
$email = filter_var($app->request->post('email'), FILTER_SANITIZE_STRING);
728730
$user = $app->repo("User")->findOneBy(array('email' => $email));
729-
731+
730732
if (!$user) {
731733
$this->feedback_success = false;
732734
$this->triedEmail = $email;
733735
$this->feedback_msg = i::__('Email não encontrado', 'multipleLocal');
734-
return false;
736+
return;
737+
}
738+
739+
if (!$this->verifyRecaptcha2()) {
740+
$this->setFeedback(i::__('Captcha incorreto, tente novamente !', 'multipleLocal'));
741+
return;
735742
}
736743

737-
if (!$this->verifyRecaptcha2())
738-
return $this->setFeedback(i::__('Captcha incorreto, tente novamente !', 'multipleLocal'));
739-
740744
// generate the hash
741745
$source = rand(3333, 8888);
742746
$cut = rand(10, 30);
743747
$string = $this->hashPassword($source);
744748
$token = substr($string, $cut, 20);
745-
749+
746750
// save hash and created time
747751
$app->disableAccessControl();
748752
$user->setMetadata('recover_token', $token);
749753
$user->setMetadata('recover_token_time', time());
750754
$user->saveMetadata();
751755
$app->em->flush();
752756
$app->enableAccessControl();
753-
754-
757+
755758
// build recover URL
756759
$url = $app->createUrl('auth', 'recover-resetform') . '?t=' . $token;
757-
758760
$site_name = $app->view->dict('site: name', false);
759-
760761

761762
// send email
762763
$email_subject = sprintf(i::__('Pedido de recuperação de senha para %s', 'multipleLocal'), $site_name);
763-
$mustache = new \Mustache_Engine();
764-
765-
$content = $mustache->render(
766-
file_get_contents(
767-
// @todo: usar a $app->view->getTemplatePathname()
768-
__DIR__.
769-
DIRECTORY_SEPARATOR.'views'.
770-
DIRECTORY_SEPARATOR.'auth'.
771-
DIRECTORY_SEPARATOR.'email-resert-password.html'
772-
), array(
773-
"url" => $url,
774-
"user" => $user->email,
775-
"siteName" => $site_name,
776-
"urlSupportChat" => $this->_config['urlSupportChat'],
777-
"urlSupportEmail" => $this->_config['urlSupportEmail'],
778-
"urlSupportSite" => $this->_config['urlSupportSite'],
779-
"textSupportSite" => $this->_config['textSupportSite'],
780-
"urlImageToUseInEmails" => $this->_config['urlImageToUseInEmails'],
781-
));
782-
764+
765+
$content = $app->renderMailerTemplate('email-to-reset-password', [
766+
"url" => $url,
767+
"user" => $user->email,
768+
"siteName" => $site_name,
769+
"urlSupportChat" => $this->_config['urlSupportChat'],
770+
"urlSupportEmail" => $this->_config['urlSupportEmail'],
771+
"urlSupportSite" => $this->_config['urlSupportSite'],
772+
"textSupportSite" => $this->_config['textSupportSite'],
773+
"urlImageToUseInEmails" => $this->_config['urlImageToUseInEmails'],
774+
]);
775+
783776
$app->applyHook('multipleLocalAuth.recoverEmailSubject', $email_subject);
784777
$app->applyHook('multipleLocalAuth.recoverEmailBody', $content);
785-
778+
786779
if ($app->createAndSendMailMessage([
787-
'from' => $app->config['mailer.from'],
788-
'to' => $user->email,
789-
'subject' => $email_subject,
790-
'body' => $content
791-
])) {
792-
780+
'from' => $app->config['mailer.from'],
781+
'to' => $user->email,
782+
'subject' => $email_subject,
783+
'body' => $content['body']
784+
])) {
793785
// set feedback
794786
$this->feedback_success = true;
795787
$this->feedback_msg = i::__('Sucesso: Um e-mail foi enviado com instruções para recuperação da senha.', 'multipleLocal');
@@ -1071,12 +1063,10 @@ function verifyLogin() {
10711063
function doRegister() {
10721064
$app = App::i();
10731065
$config = $app->_config;
1074-
10751066

10761067
if ($this->validateRegisterFields()) {
1077-
10781068
$pass = filter_var($app->request->post('password'), FILTER_SANITIZE_STRING);
1079-
1069+
10801070
//retira ". e -" do $request->post('cpf')
10811071
$cpf = filter_var($app->request->post('cpf'), FILTER_SANITIZE_STRING);
10821072
$cpf = str_replace("-","",$cpf);
@@ -1097,56 +1087,46 @@ function doRegister() {
10971087
'email' => filter_var($app->request->post('email'), FILTER_SANITIZE_EMAIL),
10981088
'name' => filter_var($app->request->post('name'), FILTER_SANITIZE_STRING),
10991089
'cpf' => $cpf,
1100-
'token' => $token
1101-
]
1102-
]
1090+
'token' => $token,
1091+
],
1092+
],
11031093
];
11041094

11051095
//Removendo email em maiusculo
11061096
$response['auth']['uid'] = strtolower($response['auth']['uid']);
11071097
$response['auth']['info']['email'] = strtolower($response['auth']['info']['email']);
1108-
1098+
11091099
$app->applyHookBoundTo($this, 'auth.createUser:before', [$response]);
11101100
$user = $this->_createUser($response);
11111101
$app->applyHookBoundTo($this, 'auth.createUser:after', [$user, $response]);
1112-
11131102
$baseUrl = $app->getBaseUrl();
11141103

1115-
//ATENÇÃO !! Se for necessario "padronizar" os emails com header/footers, é necessario adapatar o 'mustache', e criar uma mini estrutura de pasta de emails em 'MultipleLocalAuth\views'
1116-
$mustache = new \Mustache_Engine();
1117-
11181104
$site_name = $app->view->dict('site: name', false);
11191105

1120-
$content = $mustache->render(
1121-
file_get_contents(
1122-
__DIR__.
1123-
DIRECTORY_SEPARATOR.'views'.
1124-
DIRECTORY_SEPARATOR.'auth'.
1125-
DIRECTORY_SEPARATOR.'email-to-validate-account.html'
1126-
), array(
1127-
"siteName" => $site_name,
1128-
// @todo não é melhor pegar o $user->profile->name ???
1129-
"user" => $response['auth']['info']['name'],
1130-
"urlToValidateAccount" => $baseUrl.'auth/confirma-email?token='.$token,
1131-
"baseUrl" => $baseUrl,
1132-
"urlSupportChat" => $this->_config['urlSupportChat'],
1133-
"urlSupportEmail" => $this->_config['urlSupportEmail'],
1134-
"urlSupportSite" => $this->_config['urlSupportSite'],
1135-
"textSupportSite" => $this->_config['textSupportSite'],
1136-
"urlImageToUseInEmails" => $this->_config['urlImageToUseInEmails'],
1137-
));
1106+
$content = $app->renderMailerTemplate('email-to-validate-account', [
1107+
"siteName" => $site_name,
1108+
// @todo não é melhor pegar o $user->profile->name ???
1109+
"user" => $response['auth']['info']['name'],
1110+
"urlToValidateAccount" => $baseUrl.'auth/confirma-email?token='.$token,
1111+
"baseUrl" => $baseUrl,
1112+
"urlSupportChat" => $this->_config['urlSupportChat'],
1113+
"urlSupportEmail" => $this->_config['urlSupportEmail'],
1114+
"urlSupportSite" => $this->_config['urlSupportSite'],
1115+
"textSupportSite" => $this->_config['textSupportSite'],
1116+
"urlImageToUseInEmails" => $this->_config['urlImageToUseInEmails'],
1117+
]);
11381118

11391119
$app->createAndSendMailMessage([
11401120
'from' => $app->config['mailer.from'],
11411121
'to' => $user->email,
11421122
'subject' => "Bem-vindo ao ".$site_name,
1143-
'body' => $content
1123+
'body' => $content['body']
11441124
]);
1145-
1125+
11461126
$user->setMetadata(self::$passMetaName, $app->auth->hashPassword( $pass ));
11471127
$user->setMetadata(self::$tokenVerifyAccountMetadata, $token);
11481128
$user->setMetadata(self::$accountIsActiveMetadata, '0');
1149-
1129+
11501130
// save
11511131
$app->disableAccessControl();
11521132
$user->saveMetadata(true);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{{{_header}}}
2+
<!-- START CENTERED WHITE CONTAINER -->
3+
<table role="presentation" class="main">
4+
<!-- START MAIN CONTENT AREA -->
5+
<tr>
6+
<td class="wrapper">
7+
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
8+
<tr>
9+
<td>
10+
<p>Olá, {{user}}</p>
11+
<p>
12+
Alguém solicitou a recuperação da senha utilizada no {{siteName}} por este e-mail.
13+
Para recuperá-la, acesse o link:
14+
</p>
15+
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
16+
<tbody>
17+
<tr>
18+
<td align="center">
19+
<a class="btn btn-primary" href="{{url}}" target="_blank">Redefinir senha</a>
20+
</td>
21+
</tr>
22+
</tbody>
23+
</table>
24+
<br>
25+
<p>Se você não pediu a recuperação da sua senha, apenas ignore esta mensagem.</p>
26+
<br>
27+
<!-- <p>Caso tenha qualquer dúvida, entre em contato via email ou chat pelos links de suporte abaixo:</p>-->
28+
29+
{{#urlSupportChat}}
30+
<p>Suporte por chat: <a href={{urlSupportChat}} target="_blank">{{urlSupportChat}}</a></p>
31+
{{/urlSupportChat}}
32+
33+
{{#urlSupportEmail}}
34+
<p>Suporte por email: <a href=mailto:{{urlSupportEmail}} target="_blank">{{urlSupportEmail}}</a></p>
35+
{{/urlSupportEmail}}
36+
37+
{{#urlSupportSite}}
38+
<p>{{textSupportSite}} <a href={{urlSupportSite}} target="_blank">{{urlSupportSite}}</a></p>
39+
{{/urlSupportSite}}
40+
41+
<p>Até mais!</p>
42+
</td>
43+
</tr>
44+
</table>
45+
</td>
46+
</tr>
47+
<!-- END MAIN CONTENT AREA -->
48+
</table>
49+
<!-- END CENTERED WHITE CONTAINER -->
50+
{{{_footer}}}

0 commit comments

Comments
 (0)