|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2020 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace App\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use App\Application; |
|
16
|
|
|
use BEdita\SDK\BEditaClientException; |
|
17
|
|
|
use Cake\Event\Event; |
|
18
|
|
|
use Cake\Http\Response; |
|
19
|
|
|
use Cake\Routing\Router; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Perform password reset and change. |
|
24
|
|
|
*/ |
|
25
|
|
|
class PasswordController extends AppController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritDoc} |
|
29
|
|
|
* {@codeCoverageIgnore} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function initialize(): void |
|
32
|
|
|
{ |
|
33
|
|
|
parent::initialize(); |
|
34
|
|
|
$this->Auth->allow(['reset', 'change']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
* {@codeCoverageIgnore} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function beforeFilter(Event $event): ?Response |
|
42
|
|
|
{ |
|
43
|
|
|
// if authenticated, redirect to dashboard |
|
44
|
|
|
$tokens = $this->Auth->user('tokens'); |
|
45
|
|
|
if (!empty($tokens)) { |
|
46
|
|
|
return $this->redirect('/dashboard'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Password reset. |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Cake\Http\Response|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function reset(): ?Response |
|
58
|
|
|
{ |
|
59
|
|
|
$this->request->allowMethod(['get', 'post']); |
|
60
|
|
|
|
|
61
|
|
|
if ($this->request->is('get')) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$data = [ |
|
66
|
|
|
'contact' => $this->request->getData('email'), |
|
67
|
|
|
'change_url' => Router::url(['_name' => 'password:change'], true), |
|
68
|
|
|
]; |
|
69
|
|
|
try { |
|
70
|
|
|
$result = $this->apiClient->post( |
|
71
|
|
|
'/auth/change', |
|
72
|
|
|
json_encode($data), |
|
73
|
|
|
['Content-Type' => 'application/json'] |
|
74
|
|
|
); |
|
75
|
|
|
$this->set(compact('result')); |
|
76
|
|
|
} catch (BEditaClientException $ex) { |
|
77
|
|
|
$this->Flash->error(__($ex->getMessage())); |
|
78
|
|
|
|
|
79
|
|
|
return $this->redirect(['_name' => 'password:reset']); |
|
80
|
|
|
} |
|
81
|
|
|
$this->viewBuilder()->setTemplate('request_sent'); |
|
82
|
|
|
|
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Password change action. |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Cake\Http\Response|null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function change(): ?Response |
|
92
|
|
|
{ |
|
93
|
|
|
$this->request->allowMethod(['get', 'post']); |
|
94
|
|
|
|
|
95
|
|
|
if ($this->request->is('get')) { |
|
96
|
|
|
$this->set('uuid', $this->request->getQuery('uuid')); |
|
97
|
|
|
|
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$data = [ |
|
102
|
|
|
'uuid' => $this->request->getData('uuid'), |
|
103
|
|
|
'password' => $this->request->getData('password'), |
|
104
|
|
|
'login' => true, |
|
105
|
|
|
]; |
|
106
|
|
|
try { |
|
107
|
|
|
$result = (array)$this->apiClient->patch( |
|
108
|
|
|
'/auth/change', |
|
109
|
|
|
json_encode($data), |
|
110
|
|
|
['Content-Type' => 'application/json'] |
|
111
|
|
|
); |
|
112
|
|
|
} catch (BEditaClientException $ex) { |
|
113
|
|
|
$this->Flash->error(__($ex->getMessage())); |
|
114
|
|
|
|
|
115
|
|
|
return $this->redirect(['_name' => 'password:reset']); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$tokens = $result['meta']; |
|
119
|
|
|
$this->apiClient->setupTokens($tokens); |
|
120
|
|
|
$this->set(compact('result')); |
|
121
|
|
|
|
|
122
|
|
|
return $this->redirect(['_name' => 'dashboard']); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|