forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehavior.php
More file actions
101 lines (91 loc) · 3 KB
/
Copy pathBehavior.php
File metadata and controls
101 lines (91 loc) · 3 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
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Model\Behavior;
use Cake\Datasource\EntityInterface;
use Cake\I18n\Time;
use Cake\Mailer\Email;
use Cake\ORM\Behavior as BaseBehavior;
/**
* Covers the user registration
*/
class Behavior extends BaseBehavior
{
/**
* Send the templated email to the user
*
* @param EntityInterface $user User entity
* @param string $subject Subject, note the first_name of the user will be prepended if exists
* @param Email $email instance, if null the default email configuration with the
* Users.validation template will be used, so set a ->template() if you pass an Email
* instance
*
* @return array email send result
*/
protected function _sendEmail(EntityInterface $user, $subject, Email $email = null)
{
$firstName = isset($user['first_name'])? $user['first_name'] . ', ' : '';
$emailInstance = $this->_getEmailInstance($email)
->to($user['email'])
->subject($firstName . $subject)
->viewVars($user->toArray());
if (empty($email)) {
$emailInstance->template('CakeDC/Users.validation');
}
return $emailInstance->send();
}
/**
* Get or initialize the email instance. Used for mocking.
*
* @param Email $email if email provided, we'll use the instance instead of creating a new one
* @return Email
*/
protected function _getEmailInstance(Email $email = null)
{
if ($email === null) {
$email = new Email('default');
$email->emailFormat('both');
}
return $email;
}
/**
* DRY for update active and token based on validateEmail flag
*
* @param EntityInterface $user User to be updated.
* @param type $validateEmail email user to validate.
* @param type $tokenExpiration token to be updated.
* @return EntityInterface
*/
protected function _updateActive(EntityInterface $user, $validateEmail, $tokenExpiration)
{
$emailValidated = $user['validated'];
if (!$emailValidated && $validateEmail) {
$user['active'] = false;
$user->updateToken($tokenExpiration);
} else {
$user['active'] = true;
$user['activation_date'] = new Time();
}
return $user;
}
/**
* Remove user token for validation
*
* @param User $user user object.
* @return EntityInterface
*/
protected function _removeValidationToken(EntityInterface $user)
{
$user->token = null;
$user->token_expires = null;
$result = $this->_table->save($user);
return $result;
}
}