246 lines
6.1 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Controllers;
use Lang;
use Flash;
2014-05-14 23:24:20 +10:00
use Backend;
use Redirect;
2014-05-14 23:24:20 +10:00
use BackendMenu;
use BackendAuth;
use Backend\Models\UserGroup;
2014-05-14 23:24:20 +10:00
use Backend\Classes\Controller;
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()
{
parent::__construct();
2014-10-10 23:26:57 +02:00
if ($this->action == 'myaccount') {
$this->requiredPermissions = null;
2014-10-10 23:26:57 +02: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
}
/**
* 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();
}
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) {
return Backend::redirect('backend/users/myaccount');
2014-10-10 23:26:57 +02:00
}
2014-05-14 23:24:20 +10:00
return $this->asExtension('FormController')->update($recordId, $context);
2014-05-14 23:24:20 +10:00
}
/**
* 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');
}
/**
* 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();
}
2014-05-14 23:24:20 +10:00
/**
* My Settings controller
*/
public function myaccount()
2014-05-14 23:24:20 +10:00
{
SettingsManager::setContext('October.Backend', 'myaccount');
$this->pageTitle = 'backend::lang.myaccount.menu_label';
return $this->update($this->user->id, 'myaccount');
2014-05-14 23:24:20 +10:00
}
/**
* Proxy update onSave event
*/
public function myaccount_onSave()
2014-05-14 23:24:20 +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.
* Mark default groups as checked for new Users.
2014-05-14 23:24:20 +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
if (!$this->user->isSuperUser()) {
$form->removeField('is_superuser');
}
2015-05-21 22:54:44 +10:00
/*
* Add permissions tab
*/
2016-02-19 22:12:41 -08:00
$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');
2019-02-25 13:21:33 -06:00
if ($groupField) {
$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
}