-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathUsersMailer.php
More file actions
130 lines (118 loc) · 4.4 KB
/
Copy pathUsersMailer.php
File metadata and controls
130 lines (118 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Mailer;
use Cake\Datasource\EntityInterface;
use Cake\Mailer\Mailer;
use Cake\Mailer\Message;
use CakeDC\Users\Utility\UsersUrl;
/**
* User Mailer
*/
class UsersMailer extends Mailer
{
/**
* Send the templated email to the user
*
* @param \Cake\Datasource\EntityInterface $user User entity
* @return void
*/
protected function validation(EntityInterface $user, array $options = [])
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
// un-hide the token to be able to send it in the email content
$user->setHidden(['password', 'token_expires', 'api_token']);
$subject = __d('cake_d_c/users', 'Your account validation link');
if (isset($options['linkGenerator']) && is_callable($options['linkGenerator'])) {
$generator = $options['linkGenerator'];
$link = $generator($user['token']);
} else {
$link = UsersUrl::actionUrl('validateEmail', [
'_full' => true,
$user['token'],
]);
}
$viewVars = [
'activationUrl' => $link,
] + $user->toArray();
$this
->setTo($user['email'])
->setSubject($firstName . $subject)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars($viewVars);
$this->viewBuilder()
->setTemplate('CakeDC/Users.validation');
}
/**
* Send the reset password email to the user
*
* @param \Cake\Datasource\EntityInterface $user User entity
* @return void
*/
protected function resetPassword(EntityInterface $user, array $options = [])
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
$subject = __d('cake_d_c/users', '{0}Your reset password link', $firstName);
// un-hide the token to be able to send it in the email content
$user->setHidden(['password', 'token_expires', 'api_token']);
if (isset($options['linkGenerator']) && is_callable($options['linkGenerator'])) {
$generator = $options['linkGenerator'];
$link = $generator($user['token']);
} else {
$link = UsersUrl::actionUrl('resetPassword', [
'_full' => true,
$user['token'],
]);
}
$viewVars = [
'activationUrl' => $link,
] + $user->toArray();
$this
->setTo($user['email'])
->setSubject($subject)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars($viewVars);
$this
->viewBuilder()
->setTemplate('CakeDC/Users.resetPassword');
}
/**
* Send account validation email to the user
*
* @param \Cake\Datasource\EntityInterface $user User entity
* @param \Cake\Datasource\EntityInterface $socialAccount SocialAccount entity
* @return void
*/
protected function socialAccountValidation(EntityInterface $user, EntityInterface $socialAccount)
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
// note: we control the space after the username in the previous line
$subject = __d('cake_d_c/users', '{0}Your social account validation link', $firstName);
$activationUrl = [
'_full' => true,
'prefix' => false,
'plugin' => 'CakeDC/Users',
'controller' => 'SocialAccounts',
'action' => 'validateAccount',
$socialAccount['provider'] ?? null,
$socialAccount['reference'] ?? null,
$socialAccount['token'] ?? null,
];
$this
->setTo($user['email'])
->setSubject($subject)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars(['user' => $user, 'socialAccount' => $socialAccount, 'activationUrl' => $activationUrl]);
$this
->viewBuilder()
->setTemplate('CakeDC/Users.socialAccountValidation');
}
}