1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Community |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Community |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Community". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Community\Controller\Admin; |
17
|
|
|
|
18
|
|
|
use Community\Token; |
19
|
|
|
use Core\Event\EventManager; |
20
|
|
|
use Community\Model\Entity\User; |
21
|
|
|
use Community\Model\Table\UsersTable; |
22
|
|
|
use Cake\ORM\Exception\RolledbackTransactionException; |
23
|
|
|
use Cake\Datasource\Exception\RecordNotFoundException; |
24
|
|
|
use Cake\Datasource\Exception\InvalidPrimaryKeyException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class UsersController |
28
|
|
|
* |
29
|
|
|
* @package Community\Controller\Admin |
30
|
|
|
* @property UsersTable $Users |
31
|
|
|
*/ |
32
|
|
|
class UsersController extends AppController |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add action. |
37
|
|
|
* |
38
|
|
|
* @return \Cake\Http\Response|null |
39
|
|
|
* |
40
|
|
|
* @throws \Cake\ORM\Exception\RolledbackTransactionException |
41
|
|
|
*/ |
42
|
|
|
public function add() |
43
|
|
|
{ |
44
|
|
|
$user = $this->Users->newEntity(); |
45
|
|
|
if ($this->request->is('post')) { |
46
|
|
|
$user = $this->Users->patchEntity($user, $this->request->getData()); |
47
|
|
|
|
48
|
|
|
if ($user->get('notify') === '1') { |
49
|
|
|
$user->set('token', Token::generate()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
EventManager::trigger('Admin.Controller.User.Add.BeforeSave', $this, [ |
53
|
|
|
'user' => $user |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
if ($result = $this->Users->save($user)) { |
57
|
|
|
EventManager::trigger('Admin.Controller.User.Add.AfterSave', $this, [ |
58
|
|
|
'user' => $result |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf( |
62
|
|
|
'<strong>«%s»</strong>', |
63
|
|
|
$result->get('login') |
64
|
|
|
))); |
65
|
|
|
|
66
|
|
|
return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]); |
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
$this->Flash->error(__d('community', 'User could not be updated. Please, try again.')); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$groups = $this->Users->Groups->getTreeList(); |
73
|
|
|
|
74
|
|
|
$this |
75
|
|
|
->set(compact('user', 'groups')) |
76
|
|
|
->set('page_title', __d('community', 'Profiles: Add new user')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Edit action. |
81
|
|
|
* |
82
|
|
|
* @param null|int $id |
83
|
|
|
* @return \Cake\Http\Response|null |
84
|
|
|
* |
85
|
|
|
* @throws RecordNotFoundException |
86
|
|
|
* @throws InvalidPrimaryKeyException |
87
|
|
|
* @throws RolledbackTransactionException |
88
|
|
|
*/ |
89
|
|
|
public function edit($id = null) |
90
|
|
|
{ |
91
|
|
|
/** @var User $user */ |
92
|
|
|
$user = $this->Users->get($id); |
93
|
|
|
$groups = $this->Users->Groups->getTreeList(); |
94
|
|
|
|
95
|
|
View Code Duplication |
if ($this->request->is(['patch', 'post', 'put'])) { |
|
|
|
|
96
|
|
|
$group = $this->Users->patchEntity($user, $this->request->getData()); |
97
|
|
|
if ($result = $this->Users->save($group)) { |
98
|
|
|
$this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf( |
99
|
|
|
'<strong>«%s»</strong>', |
100
|
|
|
$result->get('login') |
101
|
|
|
))); |
102
|
|
|
return $this->App->redirect([ |
103
|
|
|
'apply' => ['action' => 'edit', $result->id] |
|
|
|
|
104
|
|
|
]); |
105
|
|
|
} else { |
106
|
|
|
$this->Flash->error(__d('community', 'User could not be updated. Please, try again.')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this |
111
|
|
|
->set(compact('user', 'groups')) |
112
|
|
|
->set('page_title', __d('community', 'Profiles: Edit user - {0}', $user->name)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Index action. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* |
120
|
|
|
* @throws \RuntimeException |
121
|
|
|
*/ |
122
|
|
|
public function index() |
123
|
|
|
{ |
124
|
|
|
$query = $this->Users |
125
|
|
|
->find('search', $this->Users->filterParams($this->request->getQueryParams())) |
126
|
|
|
->contain('Groups'); |
127
|
|
|
|
128
|
|
|
$this |
129
|
|
|
->set('users', $this->paginate($query)) |
|
|
|
|
130
|
|
|
->set('page_title', __d('community', 'Profiles: User list')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Initialization hook method. |
135
|
|
|
* |
136
|
|
|
* Implement this method to avoid having to overwrite |
137
|
|
|
* the constructor and call parent. |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
* |
141
|
|
|
* @throws \Cake\Core\Exception\Exception |
142
|
|
|
*/ |
143
|
|
|
public function initialize() |
144
|
|
|
{ |
145
|
|
|
parent::initialize(); |
146
|
|
|
$this->Security->setConfig('unlockedFields', ['password', 'password_confirm']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Process action. |
151
|
|
|
* |
152
|
|
|
* @return \Cake\Http\Response|null |
153
|
|
|
*/ |
154
|
|
|
public function process() |
155
|
|
|
{ |
156
|
|
|
list($action, $ids) = $this->Process->getRequestVars($this->name); |
157
|
|
|
return $this->Process->make($this->Users, $action, $ids); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: