|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2018 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 BEdita\SDK\BEditaClientException; |
|
16
|
|
|
use Cake\Event\EventInterface; |
|
17
|
|
|
use Cake\Http\Response; |
|
18
|
|
|
use Cake\Utility\Hash; |
|
19
|
|
|
use Psr\Log\LogLevel; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* User Profile controller: view and edit logged user data |
|
23
|
|
|
* |
|
24
|
|
|
* @property \App\Controller\Component\PropertiesComponent $Properties |
|
25
|
|
|
*/ |
|
26
|
|
|
class UserProfileController extends AppController |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function initialize(): void |
|
32
|
|
|
{ |
|
33
|
|
|
parent::initialize(); |
|
34
|
|
|
|
|
35
|
|
|
$this->loadComponent('Properties'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritDoc} |
|
40
|
|
|
* |
|
41
|
|
|
* @codeCoverageIgnore |
|
42
|
|
|
*/ |
|
43
|
|
|
public function beforeRender(EventInterface $event): ?Response |
|
44
|
|
|
{ |
|
45
|
|
|
$this->set('moduleLink', ['_name' => 'user_profile:view']); |
|
46
|
|
|
|
|
47
|
|
|
return parent::beforeRender($event); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* View profile data |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function view(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->getRequest()->allowMethod(['get']); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
$response = $this->apiClient->get('/auth/user'); |
|
61
|
|
|
} catch (BEditaClientException $e) { |
|
62
|
|
|
$this->log($e->getMessage(), LogLevel::ERROR); |
|
63
|
|
|
$this->Flash->error($e->getMessage(), ['params' => $e]); |
|
64
|
|
|
$response = []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$revision = Hash::get($response, 'meta.schema.users.revision', null); |
|
68
|
|
|
$schema = $this->Schema->getSchema('users', $revision); |
|
69
|
|
|
$object = (array)Hash::get($response, 'data'); |
|
70
|
|
|
$this->set('schema', $schema); |
|
71
|
|
|
$this->set('object', $object); |
|
72
|
|
|
$this->set('properties', $this->Properties->viewGroups($object, 'user_profile')); |
|
73
|
|
|
$this->set('currentAttributes', json_encode((array)Hash::get($object, 'attributes'))); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Save user profile data |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function save(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$data = $this->getRequest()->getData(); |
|
84
|
|
|
unset($data['id']); |
|
85
|
|
|
$this->changedAttributes($data); |
|
86
|
|
|
try { |
|
87
|
|
|
$this->changePassword($data); |
|
88
|
|
|
$this->changeData($data); |
|
89
|
|
|
$this->Flash->success(__('User profile saved')); |
|
90
|
|
|
} catch (BEditaClientException $e) { |
|
91
|
|
|
$this->log($e->getMessage(), LogLevel::ERROR); |
|
92
|
|
|
$this->Flash->error($e->getMessage(), ['params' => $e]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->redirect(['_name' => 'user_profile:view']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Change password, if needed. |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $data The data |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function changePassword(array &$data): void |
|
105
|
|
|
{ |
|
106
|
|
|
$password = (string)Hash::get($data, 'password'); |
|
107
|
|
|
if (!empty($password)) { |
|
108
|
|
|
$this->apiClient->patch('/auth/user', json_encode([ |
|
109
|
|
|
'password' => $password, |
|
110
|
|
|
'old_password' => (string)Hash::get($data, 'old_password'), |
|
111
|
|
|
])); |
|
112
|
|
|
} |
|
113
|
|
|
unset($data['password']); |
|
114
|
|
|
unset($data['old_password']); |
|
115
|
|
|
unset($data['confirm-password']); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Change data, if changed |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $data The data |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function changeData(array $data): void |
|
125
|
|
|
{ |
|
126
|
|
|
if (empty($data)) { |
|
127
|
|
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
$this->apiClient->patch('/auth/user', json_encode($data)); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|