-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForgotPassController.php
More file actions
277 lines (249 loc) · 8.31 KB
/
ForgotPassController.php
File metadata and controls
277 lines (249 loc) · 8.31 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* パスワード再発行Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AuthAppController', 'Auth.Controller');
App::uses('NetCommonsMail', 'Mails.Utility');
App::uses('SiteSettingUtil', 'SiteManager.Utility');
App::uses('UserAttributeChoice', 'UserAttributes.Model');
/**
* パスワード再発行Controller
*
* @property SecurityComponent $Security
* @property ForgotPass $ForgotPass
* @property User $User
* @property SiteSetting $SiteSetting
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Auth\Controller
*/
class ForgotPassController extends AuthAppController {
/**
* ウィザード定数(再発行の受付)
*
* @var string
*/
const WIZARD_REQUEST = 'request';
/**
* ウィザード定数(再発行受付確認画面)
*
* @var string
*/
const WIZARD_CONFIRM = 'confirm';
/**
* ウィザード定数(新しいパスワード登録)
*
* @var string
*/
const WIZARD_UPDATE = 'update';
/**
* 使用するComponents
*
* - [SecurityComponent](http://book.cakephp.org/2.0/ja/core-libraries/components/security-component.html)
*
* @var array
*/
public $components = array(
'Security',
);
/**
* 使用するModels
*
* - [Auth.ForgotPass](../../Auth/classes/ForgotPass.html)
* - [Users.User](../../Users/classes/User.html)
*
* @var array
*/
public $uses = array(
'Auth.ForgotPass',
'Users.User',
'SiteManager.SiteSetting',
);
/**
* 使用するHelpers
*
* - [NetCommons.Wizard](../../NetCommons/classes/WizardHelper.html)
*
* @var array
*/
public $helpers = array(
'NetCommons.Wizard' => array(
'navibar' => array(
self::WIZARD_REQUEST => array(
'url' => array(
'controller' => 'forgot_pass', 'action' => 'request',
),
'label' => array('auth', 'Forgot your Password?'),
),
self::WIZARD_CONFIRM => array(
'url' => array(
'controller' => 'forgot_pass', 'action' => 'confirm',
),
'label' => array('auth', 'Authorization key confirm?'),
),
self::WIZARD_UPDATE => array(
'url' => array(
'controller' => 'forgot_pass', 'action' => 'update',
),
'label' => array('auth', 'Entry new password'),
),
),
'cancelUrl' => array('controller' => 'auth', 'action' => 'login')
),
);
/**
* beforeFilter
*
* @return void
**/
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('request', 'confirm', 'update');
//ページタイトル
$this->set('pageTitle', __d('auth', 'Forgot your Password?'));
SiteSettingUtil::setup('ForgotPass');
if (! SiteSettingUtil::read('ForgotPass.use_password_reissue', '0')) {
return $this->setAction('throwBadRequest');
}
//メール通知の場合、NetCommonsMailUtilityをメンバー変数にセットする。Mockであれば、newをしない。
//テストでMockに差し替えが必要なための処理であるので、カバレッジレポートから除外する。
//@codeCoverageIgnoreStart
if (empty($this->mail) ||
substr(get_class($this->mail), 0, 4) !== 'Mock') {
$this->mail = new NetCommonsMail();
}
//@codeCoverageIgnoreEnd
}
/**
* パスワード再発行の受付
*
* @return void
**/
public function request() {
if ($this->request->is('post')) {
$forgotPass = $this->ForgotPass->validateRequest($this->request->data);
if ($forgotPass) {
$forgotPass = $forgotPass['ForgotPass'];
$this->Session->write('ForgotPass', $forgotPass);
//対象のユーザがいる場合は、メール送る。
//成否を出すと、悪意ある人がやった場合、メールアドレスがバレてしまうため、送ったことにする。
//⇒有名どころのWebサービスでは、エラーを表示している。
// かなり昔では、送らない方が良かったかもしれないが、新規登録などで既に登録されているなどの
// エラーを返すことから、この回避方法は意味がないため、エラーを返すようにする。
if (Hash::get($forgotPass, 'user_id')) {
$this->mail->mailAssignTag->setFixedPhraseSubject(
SiteSettingUtil::read('ForgotPass.issue_mail_subject')
);
$this->mail->mailAssignTag->setFixedPhraseBody(
SiteSettingUtil::read('ForgotPass.issue_mail_body')
);
$this->mail->mailAssignTag->assignTags(array(
'X-AUTHORIZATION_KEY' => Hash::get($forgotPass, 'authorization_key'),
));
$this->mail->mailAssignTag->initPlugin(Current::read('Language.id'));
$this->mail->initPlugin(Current::read('Language.id'));
$this->mail->to(Hash::get($forgotPass, 'email'));
$this->mail->setFrom(Current::read('Language.id'));
if (! $this->mail->sendMailDirect()) {
return $this->NetCommons->handleValidationError(array('SendMail Error'));
}
}
$this->NetCommons->setFlashNotification(
__d(
'auth',
'We have sent you the key to obtain a new password to your registered e-mail address.'
),
array('class' => 'success')
);
return $this->redirect('/auth/forgot_pass/confirm');
}
$this->NetCommons->handleValidationError($this->ForgotPass->validationErrors);
} else {
$this->request->data['ForgotPass']['email'] = Hash::get($this->request->query, 'email');
}
}
/**
* パスワード再発行
*
* @return void
**/
public function confirm() {
if ($this->request->is('post')) {
if ($this->ForgotPass->validateAuthorizationKey($this->request->data)) {
$this->Session->write('ForgotPass.isValidAuthorizationKey', true);
$forgotPass = $this->Session->read('ForgotPass');
$this->mail->mailAssignTag->setFixedPhraseSubject(
SiteSettingUtil::read('ForgotPass.request_mail_subject')
);
$this->mail->mailAssignTag->setFixedPhraseBody(
SiteSettingUtil::read('ForgotPass.request_mail_body')
);
$this->mail->mailAssignTag->assignTags(array(
'X-HANDLENAME' => $forgotPass['handlename'],
'X-USERNAME' => $forgotPass['username'],
));
$this->mail->mailAssignTag->initPlugin(Current::read('Language.id'));
$this->mail->initPlugin(Current::read('Language.id'));
$this->mail->to(Hash::get($forgotPass, 'email'));
$this->mail->setFrom(Current::read('Language.id'));
if (! $this->mail->sendMailDirect()) {
return $this->NetCommons->handleValidationError(array('SendMail Error'));
}
$this->NetCommons->setFlashNotification(
__d('auth', 'We have sent your login id to your registered e-mail address.'),
array('class' => 'success')
);
return $this->redirect('/auth/forgot_pass/update');
}
$this->NetCommons->handleValidationError($this->ForgotPass->validationErrors);
}
}
/**
* パスワード登録
*
* @return void
**/
public function update() {
if ($this->Session->read('ForgotPass.isValidAuthorizationKey') !== true) {
$this->Session->delete('ForgotPass');
return $this->throwBadRequest();
}
if ($this->request->is('put')) {
// 承認済ならば、利用可へ
$userId = $this->request->data[$this->User->alias]['id'] ?? null;
$user = $this->User->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $userId)
));
if ($user && $user[$this->User->alias]['status'] === UserAttributeChoice::STATUS_CODE_APPROVED) {
$this->request->data[$this->User->alias]['status']
= UserAttributeChoice::STATUS_CODE_ACTIVE;
}
if ($this->ForgotPass->savePassowrd($this->request->data)) {
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully saved.'), array('class' => 'success')
);
$this->Session->delete('ForgotPass');
$this->Auth->authenticate['all']['scope'] = array(
'User.status' => '1'
);
if ($this->Auth->login()) {
$this->User->updateLoginTime($this->Auth->user('id'));
Current::write('User', $this->Auth->user());
$this->Auth->loginRedirect = $this->SiteSetting->getDefaultStartPage();
return $this->redirect($this->Auth->redirect());
}
return $this->redirect('/auth/auth/login');
}
$this->NetCommons->handleValidationError($this->ForgotPass->validationErrors);
} else {
$this->request->data['User']['id'] = $this->Session->read('ForgotPass.user_id');
}
}
}