2014-11-25 15:18:49 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-11-25 15:18:49 +01:00
|
|
|
|
|
|
|
use Icinga\Application\Config;
|
|
|
|
use Icinga\Forms\ConfirmRemovalForm;
|
|
|
|
use Icinga\Forms\Security\RoleForm;
|
2015-06-01 16:43:11 +02:00
|
|
|
use Icinga\Web\Controller\AuthBackendController;
|
2014-11-25 15:18:49 +01:00
|
|
|
use Icinga\Web\Notification;
|
|
|
|
|
2015-06-02 11:57:13 +02:00
|
|
|
class RoleController extends AuthBackendController
|
2014-11-25 15:18:49 +01:00
|
|
|
{
|
2015-02-02 13:30:52 +01:00
|
|
|
/**
|
|
|
|
* List roles
|
|
|
|
*/
|
2015-06-02 11:57:13 +02:00
|
|
|
public function listAction()
|
2014-11-25 15:18:49 +01:00
|
|
|
{
|
2015-06-02 09:07:06 +02:00
|
|
|
$this->assertPermission('config/authentication/roles/show');
|
2015-06-02 11:57:13 +02:00
|
|
|
$this->createListTabs()->activate('role/list');
|
2014-11-25 15:18:49 +01:00
|
|
|
$this->view->roles = Config::app('roles', true);
|
|
|
|
}
|
|
|
|
|
2015-02-02 13:30:52 +01:00
|
|
|
/**
|
|
|
|
* Create a new role
|
|
|
|
*/
|
2015-06-02 11:57:13 +02:00
|
|
|
public function addAction()
|
2014-11-25 15:18:49 +01:00
|
|
|
{
|
2015-06-02 09:07:06 +02:00
|
|
|
$this->assertPermission('config/authentication/roles/add');
|
2014-11-25 15:18:49 +01:00
|
|
|
$role = new RoleForm(array(
|
|
|
|
'onSuccess' => function (RoleForm $role) {
|
|
|
|
$name = $role->getElement('name')->getValue();
|
|
|
|
$values = $role->getValues();
|
|
|
|
try {
|
|
|
|
$role->add($name, $values);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
$role->addError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($role->save()) {
|
|
|
|
Notification::success(t('Role created'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$role
|
2015-03-02 18:37:54 +01:00
|
|
|
->setTitle($this->translate('New Role'))
|
2014-11-25 15:18:49 +01:00
|
|
|
->setSubmitLabel($this->translate('Create Role'))
|
|
|
|
->setIniConfig(Config::app('roles', true))
|
2015-06-02 11:57:13 +02:00
|
|
|
->setRedirectUrl('role/list')
|
2014-11-25 15:18:49 +01:00
|
|
|
->handleRequest();
|
|
|
|
$this->view->form = $role;
|
2015-06-02 11:57:13 +02:00
|
|
|
$this->render('form');
|
2014-11-25 15:18:49 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 13:30:52 +01:00
|
|
|
/**
|
|
|
|
* Update a role
|
|
|
|
*
|
|
|
|
* @throws Zend_Controller_Action_Exception If the required parameter 'role' is missing or the role does not exist
|
|
|
|
*/
|
2015-06-02 11:57:13 +02:00
|
|
|
public function editAction()
|
2014-11-25 15:18:49 +01:00
|
|
|
{
|
2015-06-02 09:07:06 +02:00
|
|
|
$this->assertPermission('config/authentication/roles/edit');
|
2014-11-25 15:18:49 +01:00
|
|
|
$name = $this->_request->getParam('role');
|
|
|
|
if (empty($name)) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
sprintf($this->translate('Required parameter \'%s\' missing'), 'role'),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$role = new RoleForm();
|
2015-03-02 18:37:54 +01:00
|
|
|
$role->setTitle(sprintf($this->translate('Update Role %s'), $name));
|
2014-11-25 15:18:49 +01:00
|
|
|
$role->setSubmitLabel($this->translate('Update Role'));
|
|
|
|
try {
|
|
|
|
$role
|
|
|
|
->setIniConfig(Config::app('roles', true))
|
|
|
|
->load($name);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
$e->getMessage(),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$role
|
|
|
|
->setOnSuccess(function (RoleForm $role) use ($name) {
|
|
|
|
$oldName = $name;
|
|
|
|
$name = $role->getElement('name')->getValue();
|
|
|
|
$values = $role->getValues();
|
|
|
|
try {
|
|
|
|
$role->update($name, $values, $oldName);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
$role->addError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($role->save()) {
|
|
|
|
Notification::success(t('Role updated'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
2015-06-02 11:57:13 +02:00
|
|
|
->setRedirectUrl('role/list')
|
2014-11-25 15:18:49 +01:00
|
|
|
->handleRequest();
|
|
|
|
$this->view->form = $role;
|
2015-06-02 11:57:13 +02:00
|
|
|
$this->render('form');
|
2014-11-25 15:18:49 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 13:30:52 +01:00
|
|
|
/**
|
|
|
|
* Remove a role
|
|
|
|
*
|
|
|
|
* @throws Zend_Controller_Action_Exception If the required parameter 'role' is missing or the role does not exist
|
|
|
|
*/
|
2014-11-25 15:18:49 +01:00
|
|
|
public function removeAction()
|
|
|
|
{
|
2015-06-02 09:07:06 +02:00
|
|
|
$this->assertPermission('config/authentication/roles/remove');
|
2014-11-25 15:18:49 +01:00
|
|
|
$name = $this->_request->getParam('role');
|
|
|
|
if (empty($name)) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
sprintf($this->translate('Required parameter \'%s\' missing'), 'role'),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$role = new RoleForm();
|
|
|
|
try {
|
|
|
|
$role
|
|
|
|
->setIniConfig(Config::app('roles', true))
|
|
|
|
->load($name);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
$e->getMessage(),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$confirmation = new ConfirmRemovalForm(array(
|
|
|
|
'onSuccess' => function (ConfirmRemovalForm $confirmation) use ($name, $role) {
|
|
|
|
try {
|
|
|
|
$role->remove($name);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
Notification::error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($role->save()) {
|
|
|
|
Notification::success(t('Role removed'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$confirmation
|
2015-03-02 18:37:54 +01:00
|
|
|
->setTitle(sprintf($this->translate('Remove Role %s'), $name))
|
2014-11-25 15:18:49 +01:00
|
|
|
->setSubmitLabel($this->translate('Remove Role'))
|
2015-06-02 11:57:13 +02:00
|
|
|
->setRedirectUrl('role/list')
|
2014-11-25 15:18:49 +01:00
|
|
|
->handleRequest();
|
|
|
|
$this->view->form = $confirmation;
|
2015-06-02 11:57:13 +02:00
|
|
|
$this->render('form');
|
2014-11-25 15:18:49 +01:00
|
|
|
}
|
2015-06-22 13:26:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the tabs to display when listing roles
|
|
|
|
*/
|
|
|
|
protected function createListTabs()
|
|
|
|
{
|
|
|
|
$tabs = $this->getTabs();
|
|
|
|
$tabs->add(
|
|
|
|
'role/list',
|
|
|
|
array(
|
|
|
|
'title' => $this->translate(
|
|
|
|
'Configure roles to permit or restrict users and groups accessing Icinga Web 2'
|
|
|
|
),
|
|
|
|
'label' => $this->translate('Roles'),
|
2015-07-29 17:18:52 +02:00
|
|
|
'url' => 'role/list',
|
|
|
|
'baseTarget' => '_main'
|
2015-06-22 13:26:24 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $tabs;
|
|
|
|
}
|
2014-11-25 15:18:49 +01:00
|
|
|
}
|