1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: zenn1 |
5
|
|
|
* Date: 11.01.2018 |
6
|
|
|
* Time: 21:00 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Apps\Controller\Front\User; |
10
|
|
|
|
11
|
|
|
use Apps\ActiveRecord\UserRecovery; |
12
|
|
|
use Apps\Model\Front\User\FormLogin; |
13
|
|
|
use Apps\Model\Front\User\FormPasswordChange; |
14
|
|
|
use Apps\Model\Front\User\FormRecovery; |
15
|
|
|
use Ffcms\Core\App; |
16
|
|
|
use Ffcms\Core\Arch\View; |
17
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
18
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
19
|
|
|
use Ffcms\Core\Helper\Type\Any; |
20
|
|
|
use Ffcms\Core\Helper\Type\Str; |
21
|
|
|
use Ffcms\Core\Network\Request; |
22
|
|
|
use Ffcms\Core\Network\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Trait ActionRecovery |
26
|
|
|
* @package Apps\Controller\Front\User |
27
|
|
|
* @property View $view |
28
|
|
|
* @property Request $request |
29
|
|
|
* @property Response $response |
30
|
|
|
*/ |
31
|
|
|
trait ActionRecovery |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Recovery form and recovery submit action |
35
|
|
|
* @param int|null $id |
36
|
|
|
* @param string|null $token |
37
|
|
|
* @return string |
38
|
|
|
* @throws ForbiddenException |
39
|
|
|
* @throws NotFoundException |
40
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
41
|
|
|
*/ |
42
|
|
|
public function recovery($id = null, $token = null) |
43
|
|
|
{ |
44
|
|
|
if (App::$User->isAuth()) { |
45
|
|
|
throw new ForbiddenException(__('You are always authorized on website, recovery is rejected')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// check if recovery token and user_id is passed and validate it |
49
|
|
|
if (Any::isInt($id) && Str::length($token) >= 64) { |
50
|
|
|
$rObject = UserRecovery::where('id', $id) |
51
|
|
|
->where('token', $token) |
52
|
|
|
->where('archive', false); |
53
|
|
|
|
54
|
|
|
// check if recovery row exist |
55
|
|
|
if ($rObject->count() !== 1) { |
56
|
|
|
throw new NotFoundException(__('This recovery data is not found')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @var UserRecovery $rData */ |
60
|
|
|
$rData = $rObject->first(); |
61
|
|
|
// check if user with this "user_id" in recovery row exist |
62
|
|
|
$rUser = App::$User->identity($rData->user_id); |
63
|
|
|
if ($rUser === null) { |
64
|
|
|
throw new NotFoundException(__('User is not found')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// email link valid, show new password set form |
68
|
|
|
$modelPwd = new FormPasswordChange($rUser); |
69
|
|
|
// process new password submit |
70
|
|
|
if ($modelPwd->send() && $modelPwd->validate()) { |
71
|
|
|
// new password is valid, update user data |
72
|
|
|
$modelPwd->make(); |
73
|
|
|
// set password change token as archived row |
74
|
|
|
$rData->archive = true; |
75
|
|
|
$rData->save(); |
76
|
|
|
// add event notification |
77
|
|
|
// add success event |
78
|
|
|
App::$Event->run(static::EVENT_USER_RECOVERY_SUCCESS, [ |
|
|
|
|
79
|
|
|
'model' => $modelPwd |
80
|
|
|
]); |
81
|
|
|
// add notification |
82
|
|
|
App::$Session->getFlashBag()->add('success', __('Your account password is successful changed!')); |
83
|
|
|
|
84
|
|
|
// lets open user session with recovered data |
85
|
|
|
$loginModel = new FormLogin(); |
86
|
|
|
$loginModel->openSession($rUser); |
87
|
|
|
$this->response->redirect('/'); // session is opened, refresh page |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->view->render('password_recovery', [ |
91
|
|
|
'model' => $modelPwd |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// initialize and process recovery form data |
96
|
|
|
$model = new FormRecovery(true); |
97
|
|
|
if ($model->send()) { |
98
|
|
|
if ($model->validate()) { |
99
|
|
|
$model->make(); |
100
|
|
|
App::$Session->getFlashBag()->add('success', __('We send to you email with instruction to recovery your account')); |
101
|
|
|
} else { |
102
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// render visual form content |
107
|
|
|
return $this->view->render('user/recovery', [ |
108
|
|
|
'model' => $model |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|