|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Module yii2-activeuser |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/inblank/yii2-activeuser |
|
6
|
|
|
* @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]> |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace inblank\activeuser; |
|
10
|
|
|
|
|
11
|
|
|
use yii; |
|
12
|
|
|
use yii\base\Module as BaseModule; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is the main module class |
|
16
|
|
|
* |
|
17
|
|
|
* @property array $modelMap |
|
18
|
|
|
* |
|
19
|
|
|
* @author Pavel Aleksandrov <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Module extends BaseModule |
|
22
|
|
|
{ |
|
23
|
|
|
/** Module version */ |
|
24
|
|
|
const VERSION = '0.1.0'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array view templates for emails composing */ |
|
27
|
|
|
public $mailViews = [ |
|
28
|
|
|
'confirm' => 'confirm', |
|
29
|
|
|
'register' => 'register', |
|
30
|
|
|
'restore' => 'restore', |
|
31
|
|
|
'passchanged' => 'passchanged', |
|
32
|
|
|
'block' => 'block', |
|
33
|
|
|
'unblock' => 'unblock', |
|
34
|
|
|
]; |
|
35
|
|
|
/** @var bool whether to enable user registration */ |
|
36
|
|
|
public $enableRegistration = true; |
|
37
|
|
|
/** |
|
38
|
|
|
* List of fields used for registration. |
|
39
|
|
|
* Email is always used and can be omitted. |
|
40
|
|
|
* If you not specify password, they will be generated automatically |
|
41
|
|
|
* You can specify: password, name, gender, birth |
|
42
|
|
|
* if you specify `password` or `name` they required for fill. |
|
43
|
|
|
* `gender` and `birth` is always optional. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public $registrationFields = []; |
|
48
|
|
|
/** @var bool whether to enable send the email to the user for confirm the email address */ |
|
49
|
|
|
public $enableConfirmation = true; |
|
50
|
|
|
/** @var bool whether to enable send notification email about register to the user */ |
|
51
|
|
|
public $enableRegistrationEmail = true; |
|
52
|
|
|
/** @var bool whether to enable send notification email about user blocking */ |
|
53
|
|
|
public $enableBlockingEmail = true; |
|
54
|
|
|
/** @var bool whether to enable send notification email about user unblocking */ |
|
55
|
|
|
public $enableUnblockingEmail = true; |
|
56
|
|
|
/** @var bool whether to enable password restore by email */ |
|
57
|
|
|
public $enablePasswordRestore = true; |
|
58
|
|
|
/** @var bool whether to enable send new password email */ |
|
59
|
|
|
public $enableNewPasswordEmail = true; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool whether to automatically generate password on restore |
|
62
|
|
|
* Password will be generated only if user password is empty |
|
63
|
|
|
*/ |
|
64
|
|
|
public $generatePassOnRestore = true; |
|
65
|
|
|
/** |
|
66
|
|
|
* Email sender address |
|
67
|
|
|
* If not set use Yii::$app->params['adminEmail'], and if they empty use 'no-reply@'.$_SERVER['HTTP_HOST'] |
|
68
|
|
|
* Can be set as array ['email'=>'name'] |
|
69
|
|
|
* @var string|array |
|
70
|
|
|
*/ |
|
71
|
|
|
public $sender; |
|
72
|
|
|
/** |
|
73
|
|
|
* Use only email for login (medium.com style) |
|
74
|
|
|
* If true, user's email used for send unique URL link to enter on site |
|
75
|
|
|
* @var bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public $loginByEmail = false; |
|
78
|
|
|
/** @var int the time you want the user will be remembered without asking for credentials */ |
|
79
|
|
|
public $rememberTime = 2592000; |
|
80
|
|
|
/** @var int the time before a confirmation token becomes invalid */ |
|
81
|
|
|
public $confirmationTime = 86400; // one month |
|
82
|
|
|
/** @var int the time before a recovery token becomes invalid */ |
|
83
|
|
|
public $restoreTime = 10800; // one day |
|
84
|
|
|
/** @var array Model map */ |
|
85
|
|
|
public $modelMap = []; |
|
86
|
|
|
/** |
|
87
|
|
|
* @var string The prefix for user module URL. |
|
88
|
|
|
* @See [[GroupUrlRule::prefix]] |
|
89
|
|
|
*/ |
|
90
|
|
|
public $urlPrefix = 'activeuser'; |
|
91
|
|
|
/** @var array The rules for frontend to be used in URL management. */ |
|
92
|
|
|
public $urlRulesFrontend = [ |
|
93
|
|
|
]; |
|
94
|
|
|
public $frontendUrlManager; |
|
95
|
|
|
/** @var array The rules for backend to be used in URL management. */ |
|
96
|
|
|
public $urlRulesBackend = [ |
|
97
|
|
|
]; |
|
98
|
|
|
/** |
|
99
|
|
|
* The URL for login in email only mode |
|
100
|
|
|
* @var string|array |
|
101
|
|
|
*/ |
|
102
|
|
|
public $loginUrl = ['/activeuser/account/login']; |
|
103
|
|
|
/** |
|
104
|
|
|
* @var yii\mail\BaseMailer |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $mailer; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Send email |
|
110
|
|
|
* @param int $type email type |
|
111
|
|
|
* @param array $params email views params |
|
112
|
|
|
*/ |
|
113
|
|
|
public function sendMessage($type, $params) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->mailer === null) { |
|
116
|
|
|
/** @var yii\swiftmailer\Mailer mailer */ |
|
117
|
|
|
$this->mailer = Yii::$app->mailer; |
|
118
|
|
|
$this->mailer->viewPath = $this->getViewPath() . '/mails'; |
|
119
|
|
|
$this->mailer->getView()->theme = Yii::$app->view->theme; |
|
120
|
|
|
} |
|
121
|
|
|
switch ($type) { |
|
122
|
|
View Code Duplication |
case 'register': |
|
|
|
|
|
|
123
|
|
|
if ($this->enableRegistrationEmail) { |
|
124
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
125
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'Thank you for register on site')); |
|
126
|
|
|
} |
|
127
|
|
|
break; |
|
128
|
|
View Code Duplication |
case 'confirm': |
|
|
|
|
|
|
129
|
|
|
if ($this->enableConfirmation) { |
|
130
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
131
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'Email address confirmation needed')); |
|
132
|
|
|
} |
|
133
|
|
|
break; |
|
134
|
|
View Code Duplication |
case 'restore': |
|
|
|
|
|
|
135
|
|
|
if ($this->enableConfirmation) { |
|
136
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
137
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'Password restore request')); |
|
138
|
|
|
} |
|
139
|
|
|
break; |
|
140
|
|
View Code Duplication |
case 'passchanged': |
|
|
|
|
|
|
141
|
|
|
if ($this->enableConfirmation) { |
|
142
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
143
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'Password was changed')); |
|
144
|
|
|
} |
|
145
|
|
|
break; |
|
146
|
|
View Code Duplication |
case 'block': |
|
|
|
|
|
|
147
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
148
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'You are blocked')); |
|
149
|
|
|
break; |
|
150
|
|
View Code Duplication |
case 'unblock': |
|
|
|
|
|
|
151
|
|
|
$message = $this->mailer->compose($this->mailViews[$type], $params); |
|
152
|
|
|
$message->setSubject(Yii::t('activeuser_general', 'You are unblocked')); |
|
153
|
|
|
break; |
|
154
|
|
|
} |
|
155
|
|
|
if (!empty($message)) { |
|
156
|
|
|
$user = $params['user']; |
|
157
|
|
|
if ($this->sender === null) { |
|
158
|
|
|
$this->sender = isset(Yii::$app->params['adminEmail']) ? Yii::$app->params['adminEmail'] : 'no-reply@' . (empty($_SERVER['HTTP_HOST']) ? 'example.com' : $_SERVER['HTTP_HOST']); |
|
159
|
|
|
} |
|
160
|
|
|
$message->setTo(empty($user->name) ? $user->email : [$user->email => $user->name]); |
|
161
|
|
|
$message->setFrom($this->sender); |
|
162
|
|
|
$this->mailer->send($message); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Check that registration enabled |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
|
|
public function isRegistrationEnabled() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->enableRegistration; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @inheritdoc |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getViewPath() |
|
179
|
|
|
{ |
|
180
|
|
|
return defined('IS_BACKEND') ? $this->getBasePath() . DIRECTORY_SEPARATOR . 'views/_backend' : parent::getViewPath(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Check that field need for register |
|
185
|
|
|
* @param string $name field name |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function isFieldForRegister($name) |
|
189
|
|
|
{ |
|
190
|
|
|
return in_array($name, $this->registrationFields); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: