-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathUsers.php
More file actions
260 lines (218 loc) · 6.63 KB
/
Users.php
File metadata and controls
260 lines (218 loc) · 6.63 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
<?php namespace Backend\Controllers;
use Lang;
use Flash;
use Backend;
use Redirect;
use Response;
use BackendMenu;
use BackendAuth;
use Backend\Models\UserGroup;
use Backend\Classes\Controller;
use System\Classes\SettingsManager;
/**
* Backend user controller
*
* @package winter\wn-backend-module
* @author Alexey Bobkov, Samuel Georges
*
*/
class Users extends Controller
{
/**
* @var array Extensions implemented by this controller.
*/
public $implement = [
\Backend\Behaviors\FormController::class,
\Backend\Behaviors\ListController::class
];
/**
* @var array `FormController` configuration.
*/
public $formConfig = 'config_form.yaml';
/**
* @var array `ListController` configuration.
*/
public $listConfig = 'config_list.yaml';
/**
* @var array Permissions required to view this page.
*/
public $requiredPermissions = ['backend.manage_users'];
/**
* @var string HTML body tag class
*/
public $bodyClass = 'compact-container';
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
if ($this->action == 'myaccount') {
$this->requiredPermissions = null;
}
BackendMenu::setContext('Winter.System', 'system', 'users');
SettingsManager::setContext('Winter.System', 'administrators');
}
/**
* Extends the list query to hide superusers if the current user is not a superuser themselves
*/
public function listExtendQuery($query)
{
if (!$this->user->isSuperUser()) {
$query->where('is_superuser', false);
}
}
/**
* Prevents non-superusers from even seeing the is_superuser filter
*/
public function listFilterExtendScopes($filterWidget)
{
if (!$this->user->isSuperUser()) {
$filterWidget->removeScope('is_superuser');
}
}
/**
* Strike out deleted records
*/
public function listInjectRowClass($record, $definition = null)
{
if ($record->trashed()) {
return 'strike';
}
}
/**
* Extends the form query to prevent non-superusers from accessing superusers at all
*/
public function formExtendQuery($query)
{
if (!$this->user->isSuperUser()) {
$query->where('is_superuser', false);
}
// Ensure soft-deleted records can still be managed
$query->withTrashed();
}
/**
* Update controller
*/
public function update($recordId, $context = null)
{
// Users cannot edit themselves, only use My Settings
if ($context != 'myaccount' && $recordId == $this->user->id) {
return Backend::redirect('backend/users/myaccount');
}
return $this->asExtension('FormController')->update($recordId, $context);
}
/**
* Handle restoring users
*/
public function update_onRestore($recordId)
{
$this->formFindModelObject($recordId)->restore();
Flash::success(Lang::get('backend::lang.form.restore_success', ['name' => Lang::get('backend::lang.user.name')]));
return Redirect::refresh();
}
/**
* Impersonate this user
*/
public function update_onImpersonateUser($recordId)
{
if (!$this->user->hasAccess('backend.impersonate_users')) {
return Response::make(Lang::get('backend::lang.page.access_denied.label'), 403);
}
$model = $this->formFindModelObject($recordId);
BackendAuth::impersonate($model);
Flash::success(Lang::get('backend::lang.account.impersonate_success'));
return Backend::redirect('backend/users/myaccount');
}
/**
* Send a password restore email to this user
*/
public function update_onSendPasswordRestore($recordId)
{
if (!$this->user->hasAccess('backend.manage_users')) {
return Response::make(Lang::get('backend::lang.page.access_denied.label'), 403);
}
$this->formFindModelObject($recordId)->sendPasswordRestore();
Flash::success(Lang::get('backend::lang.account.send_password_restore_success'));
}
/**
* Unsuspend this user
*/
public function update_onUnsuspendUser($recordId)
{
$model = $this->formFindModelObject($recordId);
$model->unsuspend();
Flash::success(Lang::get('backend::lang.account.unsuspend_success'));
return Redirect::refresh();
}
/**
* My Settings controller
*/
public function myaccount()
{
SettingsManager::setContext('Winter.Backend', 'myaccount');
$this->pageTitle = 'backend::lang.myaccount.menu_label';
return $this->update($this->user->id, 'myaccount');
}
/**
* Proxy update onSave event
*/
public function myaccount_onSave()
{
$result = $this->asExtension('FormController')->update_onSave($this->user->id, 'myaccount');
/*
* If the password or login name has been updated, reauthenticate the user
*/
$loginChanged = $this->user->login != post('User[login]');
$passwordChanged = strlen(post('User[password]'));
if ($loginChanged || $passwordChanged) {
BackendAuth::login($this->user->reload(), true);
}
return $result;
}
/**
* Add available permission fields to the User form.
* Mark default groups as checked for new Users.
*/
public function formExtendFields($form)
{
if ($form->getContext() == 'myaccount') {
return;
}
if (!$this->user->isSuperUser()) {
$form->removeField('is_superuser');
}
/*
* Add permissions tab
*/
$form->addTabFields($this->generatePermissionsField());
/*
* Mark default groups
*/
if (!$form->model->exists) {
$defaultGroupIds = UserGroup::where('is_new_user_default', true)->lists('id');
$groupField = $form->getField('groups');
if ($groupField) {
$groupField->value = $defaultGroupIds;
}
}
}
/**
* Adds the permissions editor widget to the form.
* @return array
*/
protected function generatePermissionsField()
{
return [
'permissions' => [
'tab' => 'backend::lang.user.permissions',
'type' => 'Backend\FormWidgets\PermissionEditor',
'trigger' => [
'action' => 'disable',
'field' => 'is_superuser',
'condition' => 'checked'
]
]
];
}
}