2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Controllers;
|
|
|
|
|
|
|
|
use Backend;
|
|
|
|
use BackendMenu;
|
|
|
|
use BackendAuth;
|
2014-12-16 12:39:38 +11:00
|
|
|
use Backend\Models\UserGroup;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Backend\Classes\Controller;
|
2014-07-24 15:19:00 +11:00
|
|
|
use System\Classes\SettingsManager;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend user controller
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Users extends Controller
|
|
|
|
{
|
2017-07-27 17:35:14 +10:00
|
|
|
/**
|
|
|
|
* @var array Extensions implemented by this controller.
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public $implement = [
|
2017-07-27 17:35:14 +10:00
|
|
|
\Backend\Behaviors\FormController::class,
|
|
|
|
\Backend\Behaviors\ListController::class
|
2014-05-14 23:24:20 +10:00
|
|
|
];
|
|
|
|
|
2017-07-27 17:35:14 +10:00
|
|
|
/**
|
|
|
|
* @var array `FormController` configuration.
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public $formConfig = 'config_form.yaml';
|
|
|
|
|
2017-07-27 17:35:14 +10:00
|
|
|
/**
|
|
|
|
* @var array `ListController` configuration.
|
|
|
|
*/
|
|
|
|
public $listConfig = 'config_list.yaml';
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2017-07-27 17:35:14 +10:00
|
|
|
/**
|
|
|
|
* @var array Permissions required to view this page.
|
|
|
|
*/
|
2017-07-28 00:05:35 +10:00
|
|
|
public $requiredPermissions = ['backend.manage_users'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string HTML body tag class
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public $bodyClass = 'compact-container';
|
|
|
|
|
2017-07-27 17:35:14 +10:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public function __construct()
|
|
|
|
{
|
2017-10-14 21:55:56 -06:00
|
|
|
$this->user = BackendAuth::getUser();
|
|
|
|
if (!$this->user->isSuperUser()) {
|
|
|
|
// Prevent non-superusers from even seeing the is_superuser filter
|
|
|
|
$this->listConfig = $this->makeConfig($this->listConfig);
|
|
|
|
$this->listConfig->filter = $this->makeConfig($this->listConfig->filter);
|
|
|
|
unset($this->listConfig->filter->scopes['is_superuser']);
|
|
|
|
}
|
2017-10-14 00:25:52 -06:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
parent::__construct();
|
|
|
|
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($this->action == 'myaccount') {
|
2014-06-16 21:12:50 +10:00
|
|
|
$this->requiredPermissions = null;
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-06-16 21:12:50 +10:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
BackendMenu::setContext('October.System', 'system', 'users');
|
2014-07-27 15:07:22 +11:00
|
|
|
SettingsManager::setContext('October.System', 'administrators');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
2017-10-14 00:25:52 -06:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* Update controller
|
|
|
|
*/
|
|
|
|
public function update($recordId, $context = null)
|
|
|
|
{
|
|
|
|
// Users cannot edit themselves, only use My Settings
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($context != 'myaccount' && $recordId == $this->user->id) {
|
2015-02-11 18:35:39 +11:00
|
|
|
return Backend::redirect('backend/users/myaccount');
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-08-23 09:41:48 +10:00
|
|
|
return $this->asExtension('FormController')->update($recordId, $context);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* My Settings controller
|
|
|
|
*/
|
2014-07-01 17:17:53 +10:00
|
|
|
public function myaccount()
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-07-24 15:19:00 +11:00
|
|
|
SettingsManager::setContext('October.Backend', 'myaccount');
|
|
|
|
|
2014-10-15 19:53:44 +11:00
|
|
|
$this->pageTitle = 'backend::lang.myaccount.menu_label';
|
2014-07-01 17:17:53 +10:00
|
|
|
return $this->update($this->user->id, 'myaccount');
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy update onSave event
|
|
|
|
*/
|
2014-07-01 17:17:53 +10:00
|
|
|
public function myaccount_onSave()
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-08-23 09:41:48 +10:00
|
|
|
$result = $this->asExtension('FormController')->update_onSave($this->user->id, 'myaccount');
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the password or login name has been updated, reauthenticate the user
|
|
|
|
*/
|
|
|
|
$loginChanged = $this->user->login != post('User[login]');
|
|
|
|
$passwordChanged = strlen(post('User[password]'));
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($loginChanged || $passwordChanged) {
|
2014-05-14 23:24:20 +10:00
|
|
|
BackendAuth::login($this->user->reload(), true);
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add available permission fields to the User form.
|
2014-12-16 12:39:38 +11:00
|
|
|
* Mark default groups as checked for new Users.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2015-09-10 20:42:24 +10:00
|
|
|
public function formExtendFields($form)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-10-10 23:26:57 +02:00
|
|
|
if ($form->getContext() == 'myaccount') {
|
2014-05-14 23:24:20 +10:00
|
|
|
return;
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2015-09-24 12:04:26 +01:00
|
|
|
if (!$this->user->isSuperUser()) {
|
2015-11-28 10:21:41 +11:00
|
|
|
$form->removeField('is_superuser');
|
2015-09-24 12:04:26 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:54:44 +10:00
|
|
|
/*
|
|
|
|
* Add permissions tab
|
|
|
|
*/
|
2016-02-19 22:12:41 -08:00
|
|
|
$form->addTabFields($this->generatePermissionsField());
|
2014-12-16 12:39:38 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark default groups
|
|
|
|
*/
|
|
|
|
if (!$form->model->exists) {
|
|
|
|
$defaultGroupIds = UserGroup::where('is_new_user_default', true)->lists('id');
|
|
|
|
|
|
|
|
$groupField = $form->getField('groups');
|
|
|
|
$groupField->value = $defaultGroupIds;
|
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2015-05-21 22:54:44 +10:00
|
|
|
|
|
|
|
/**
|
2016-02-19 22:12:41 -08:00
|
|
|
* Adds the permissions editor widget to the form.
|
2015-05-21 22:54:44 +10:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-19 22:12:41 -08:00
|
|
|
protected function generatePermissionsField()
|
2015-05-21 22:54:44 +10:00
|
|
|
{
|
2016-02-19 22:12:41 -08:00
|
|
|
return [
|
|
|
|
'permissions' => [
|
2015-05-21 22:54:44 +10:00
|
|
|
'tab' => 'backend::lang.user.permissions',
|
2016-02-19 22:12:41 -08:00
|
|
|
'type' => 'Backend\FormWidgets\PermissionEditor',
|
|
|
|
'trigger' => [
|
|
|
|
'action' => 'disable',
|
|
|
|
'field' => 'is_superuser',
|
|
|
|
'condition' => 'checked'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2015-05-21 22:54:44 +10:00
|
|
|
}
|
2014-10-10 23:26:57 +02:00
|
|
|
}
|