ORIENT/modules/backend/controllers/Users.php

136 lines
3.5 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace Backend\Controllers;
use Backend;
use BackendMenu;
use BackendAuth;
use Backend\Models\UserGroup;
2014-05-14 13:24:20 +00:00
use Backend\Classes\Controller;
use System\Classes\SettingsManager;
2014-05-14 13:24:20 +00:00
/**
* Backend user controller
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*
*/
class Users extends Controller
{
public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController'
];
public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $requiredPermissions = ['backend.manage_users'];
public $bodyClass = 'compact-container';
public function __construct()
{
parent::__construct();
2014-10-10 21:26:57 +00:00
if ($this->action == 'myaccount') {
$this->requiredPermissions = null;
2014-10-10 21:26:57 +00:00
}
2014-05-14 13:24:20 +00:00
BackendMenu::setContext('October.System', 'system', 'users');
2014-07-27 04:07:22 +00:00
SettingsManager::setContext('October.System', 'administrators');
2014-05-14 13:24:20 +00:00
}
/**
* Update controller
*/
public function update($recordId, $context = null)
{
// Users cannot edit themselves, only use My Settings
2014-10-10 21:26:57 +00:00
if ($context != 'myaccount' && $recordId == $this->user->id) {
return Backend::redirect('backend/users/myaccount');
2014-10-10 21:26:57 +00:00
}
2014-05-14 13:24:20 +00:00
return $this->asExtension('FormController')->update($recordId, $context);
2014-05-14 13:24:20 +00:00
}
/**
* My Settings controller
*/
public function myaccount()
2014-05-14 13:24:20 +00:00
{
SettingsManager::setContext('October.Backend', 'myaccount');
$this->pageTitle = 'backend::lang.myaccount.menu_label';
return $this->update($this->user->id, 'myaccount');
2014-05-14 13:24:20 +00:00
}
/**
* Proxy update onSave event
*/
public function myaccount_onSave()
2014-05-14 13:24:20 +00:00
{
$result = $this->asExtension('FormController')->update_onSave($this->user->id, 'myaccount');
2014-05-14 13:24:20 +00: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 21:26:57 +00:00
if ($loginChanged || $passwordChanged) {
2014-05-14 13:24:20 +00:00
BackendAuth::login($this->user->reload(), true);
2014-10-10 21:26:57 +00:00
}
2014-05-14 13:24:20 +00:00
return $result;
}
/**
* Add available permission fields to the User form.
* Mark default groups as checked for new Users.
2014-05-14 13:24:20 +00:00
*/
public function formExtendFields($form)
2014-05-14 13:24:20 +00:00
{
2014-10-10 21:26:57 +00:00
if ($form->getContext() == 'myaccount') {
2014-05-14 13:24:20 +00:00
return;
2014-10-10 21:26:57 +00:00
}
2014-05-14 13:24:20 +00:00
if (!$this->user->isSuperUser()) {
$form->removeField('is_superuser');
$form->removeField('role');
}
2015-05-21 12:54:44 +00:00
/*
* Add permissions tab
*/
2016-02-20 06:12:41 +00: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');
$groupField->value = $defaultGroupIds;
}
2014-05-14 13:24:20 +00:00
}
2015-05-21 12:54:44 +00:00
/**
2016-02-20 06:12:41 +00:00
* Adds the permissions editor widget to the form.
2015-05-21 12:54:44 +00:00
* @return array
*/
2016-02-20 06:12:41 +00:00
protected function generatePermissionsField()
2015-05-21 12:54:44 +00:00
{
2016-02-20 06:12:41 +00:00
return [
'permissions' => [
2015-05-21 12:54:44 +00:00
'tab' => 'backend::lang.user.permissions',
2016-02-20 06:12:41 +00:00
'type' => 'Backend\FormWidgets\PermissionEditor',
'trigger' => [
'action' => 'disable',
'field' => 'is_superuser',
'condition' => 'checked'
]
]
];
2015-05-21 12:54:44 +00:00
}
2014-10-10 21:26:57 +00:00
}