/** * Create a new monitoring backend */ public function createbackendAction() { $form = new BackendConfigForm(); $form->setRedirectUrl('monitoring/config'); $form->setTitle($this->translate('Create New Monitoring Backend')); $form->setIniConfig($this->Config('backends')); try { $form->setResourceConfig(ResourceFactory::getResourceConfigs()); } catch (ConfigurationError $e) { if ($this->hasPermission('config/application/resources')) { Notification::error($e->getMessage()); $this->redirectNow('config/createresource'); } throw $e; // No permission for resource configuration, show the error } $form->setOnSuccess(function (BackendConfigForm $form) { try { $form->add(array_filter($form->getValues())); } catch (Exception $e) { $form->error($e->getMessage()); return false; } if ($form->save()) { Notification::success(t('Monitoring backend successfully created')); return true; } return false; }); $form->handleRequest(); $this->view->form = $form; $this->render('form'); }
/** * Update the user backend order and save the configuration */ public function onSuccess() { $newPosData = $this->getValue('backend_newpos'); if ($newPosData) { $configForm = $this->getConfigForm(); list($backendName, $position) = explode('|', $newPosData, 2); try { if ($configForm->move($backendName, $position)->save()) { Notification::success($this->translate('Authentication order updated')); } else { return false; } } catch (NotFoundError $_) { Notification::error(sprintf($this->translate('User backend "%s" not found'), $backendName)); } } }
/** * Update the user backend order and save the configuration * * @see Form::onSuccess() */ public function onSuccess() { $newPosData = $this->getValue('backend_newpos'); if ($newPosData) { $configForm = $this->getConfigForm(); list($backendName, $position) = explode('|', $newPosData, 2); try { if ($configForm->move($backendName, $position)->save()) { Notification::success($this->translate('Authentication order updated')); } else { return false; } } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); } } }
/** * Remove a group member */ public function removememberAction() { $this->assertPermission('config/authentication/groups/edit'); $this->assertHttpMethod('POST'); $groupName = $this->params->getRequired('group'); $backend = $this->getUserGroupBackend($this->params->getRequired('backend'), 'Icinga\\Data\\Reducible'); $form = new Form(array('onSuccess' => function ($form) use($groupName, $backend) { foreach ($form->getValue('user_name') as $userName) { try { $backend->delete('group_membership', Filter::matchAll(Filter::where('group_name', $groupName), Filter::where('user_name', $userName))); Notification::success(sprintf(t('User "%s" has been removed from group "%s"'), $userName, $groupName)); } catch (NotFoundError $e) { throw $e; } catch (Exception $e) { Notification::error($e->getMessage()); } } $redirect = $form->getValue('redirect'); if (!empty($redirect)) { $form->setRedirectUrl(htmlspecialchars_decode($redirect)); } return true; })); $form->setUidDisabled(); $form->setSubmitLabel('btn_submit'); // Required to ensure that isSubmitted() is called $form->addElement('hidden', 'user_name', array('required' => true, 'isArray' => true)); $form->addElement('hidden', 'redirect'); try { $form->handleRequest(); } catch (NotFoundError $_) { $this->httpNotFound(sprintf($this->translate('Group "%s" not found'), $groupName)); } }
/** * Adjust preferences and persist them * * @see Form::onSuccess() */ public function onSuccess() { $this->preferences = new Preferences($this->store ? $this->store->load() : array()); $webPreferences = $this->preferences->get('icingaweb', array()); foreach ($this->getValues() as $key => $value) { if ($value === null || $value === 'autodetect') { if (isset($webPreferences[$key])) { unset($webPreferences[$key]); } } else { $webPreferences[$key] = $value; } } $this->preferences->icingaweb = $webPreferences; Session::getSession()->user->setPreferences($this->preferences); try { if ($this->store && $this->getElement('btn_submit_preferences')->isChecked()) { $this->save(); Notification::success($this->translate('Preferences successfully saved')); } else { Notification::success($this->translate('Preferences successfully saved for the current session')); } } catch (Exception $e) { Logger::error($e); Notification::error($e->getMessage()); } }
/** * Add or edit an user backend and save the configuration * * Performs a connectivity validation using the submitted values. A checkbox is * added to the form to skip the check if it fails and redirection is aborted. * * @see Form::onSuccess() */ public function onSuccess() { if (($el = $this->getElement('force_creation')) === null || false === $el->isChecked()) { $backendForm = $this->getBackendForm($this->getElement('type')->getValue()); if (false === $backendForm::isValidUserBackend($this)) { $this->addElement($this->getForceCreationCheckbox()); return false; } } $authBackend = $this->request->getQuery('backend'); try { if ($authBackend === null) { // create new backend $this->add($this->getValues()); $message = $this->translate('User backend "%s" has been successfully created'); } else { // edit existing backend $this->edit($authBackend, $this->getValues()); $message = $this->translate('User backend "%s" has been successfully changed'); } } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return; } if ($this->save()) { Notification::success(sprintf($message, $this->getElement('name')->getValue())); } else { return false; } }
/** * Add or edit a resource and save the configuration * * Performs a connectivity validation using the submitted values. A checkbox is * added to the form to skip the check if it fails and redirection is aborted. * * @see Form::onSuccess() */ public function onSuccess() { $resourceForm = $this->getResourceForm($this->getElement('type')->getValue()); if (($el = $this->getElement('force_creation')) === null || false === $el->isChecked()) { $inspection = static::inspectResource($this); if ($inspection !== null && $inspection->hasError()) { $this->error($inspection->getError()); $this->addElement($this->getForceCreationCheckbox()); return false; } } $resource = $this->request->getQuery('resource'); try { if ($resource === null) { // create new resource if (method_exists($resourceForm, 'beforeAdd')) { if (!$resourceForm::beforeAdd($this)) { return false; } } $this->add(array_filter($this->getValues())); $message = $this->translate('Resource "%s" has been successfully created'); } else { // edit existing resource $this->edit($resource, array_filter($this->getValues())); $message = $this->translate('Resource "%s" has been successfully changed'); } } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return false; } if ($this->save()) { Notification::success(sprintf($message, $this->getElement('name')->getValue())); } else { return false; } }
/** * Add or edit a monitoring backend and save the configuration */ public function onSuccess() { $monitoringBackend = $this->request->getQuery('backend'); try { if ($monitoringBackend === null) { // Create new backend $this->add($this->getValues()); $message = $this->translate('Monitoring backend "%s" has been successfully created'); } else { // Edit existing backend $this->edit($monitoringBackend, $this->getValues()); $message = $this->translate('Monitoring backend "%s" has been successfully changed'); } } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return null; } if ($this->save()) { Notification::success(sprintf($message, $this->getElement('name')->getValue())); } else { return false; } }
/** * Remove a role */ public function removeAction() { $this->assertPermission('config/authentication/roles/remove'); $name = $this->params->getRequired('role'); $role = new RoleForm(); try { $role->setIniConfig(Config::app('roles', true))->load($name); } catch (NotFoundError $e) { $this->httpNotFound($e->getMessage()); } $confirmation = new ConfirmRemovalForm(array('onSuccess' => function (ConfirmRemovalForm $confirmation) use($name, $role) { try { $role->remove($name); } catch (NotFoundError $e) { Notification::error($e->getMessage()); return false; } if ($role->save()) { Notification::success(t('Role removed')); return true; } return false; })); $confirmation->setSubmitLabel($this->translate('Remove Role'))->setRedirectUrl('role/list')->handleRequest(); $this->renderForm($confirmation, $this->translate('Remove Role')); }
/** * Return the names of all configured LDAP resources * * @return array */ protected function getLdapResourceNames() { $names = array(); foreach (ResourceFactory::getResourceConfigs() as $name => $config) { if (in_array(strtolower($config->type), array('ldap', 'msldap'))) { $names[] = $name; } } if (empty($names)) { Notification::error($this->translate('No LDAP resources available. Please configure an LDAP resource first.')); $this->getResponse()->redirectAndExit('config/createresource'); } return $names; }
public function removePaneAction() { $form = new ConfirmRemovalForm(); $this->createTabs(); $dashboard = $this->dashboard; if (!$this->_request->getParam('pane')) { throw new Zend_Controller_Action_Exception('Missing parameter "pane"', 400); } $pane = $this->_request->getParam('pane'); $action = $this; $form->setOnSuccess(function (Form $form) use($dashboard, $pane, $action) { $pane = $dashboard->getPane($pane); $dashboard->removePane($pane->getTitle()); $dashboardConfig = $dashboard->getConfig(); try { $dashboardConfig->saveIni(); Notification::success(t('Dashboard has been removed') . ': ' . $pane->getTitle()); return true; } catch (Exception $e) { $action->view->error = $e; $action->view->config = $dashboardConfig; $action->render('error'); return false; } catch (ProgrammingError $e) { Notification::error($e->getMessage()); return false; } return false; }); $form->setTitle($this->translate('Remove Dashboard')); $form->setRedirectUrl('dashboard/settings'); $form->handleRequest(); $this->view->pane = $pane; $this->view->form = $form; }
/** * Apply mode delete on the repository * * @return bool */ protected function onDeleteSuccess() { try { $this->repository->delete($this->repository->getBaseTable(), $this->createFilter()); } catch (Exception $e) { Notification::error($this->getDeleteMessage(false)); $this->error($e->getMessage()); return false; } Notification::success($this->getDeleteMessage(true)); return true; }
/** * Adjust preferences and persist them * * @see Form::onSuccess() */ public function onSuccess() { $this->preferences = new Preferences($this->store ? $this->store->load() : array()); $oldTheme = $this->preferences->getValue('icingaweb', 'theme'); $webPreferences = $this->preferences->get('icingaweb', array()); foreach ($this->getValues() as $key => $value) { if ($value === '' || $value === 'autodetect' || $key === 'theme' && $value === Config::app()->get('themes', 'default', StyleSheet::DEFAULT_THEME)) { if (isset($webPreferences[$key])) { unset($webPreferences[$key]); } } else { $webPreferences[$key] = $value; } } $this->preferences->icingaweb = $webPreferences; Session::getSession()->user->setPreferences($this->preferences); if (($theme = $this->getElement('theme')) !== null && ($theme = $theme->getValue()) !== $oldTheme) { $this->getResponse()->setReloadCss(true); } try { if ($this->store && $this->getElement('btn_submit_preferences')->isChecked()) { $this->save(); Notification::success($this->translate('Preferences successfully saved')); } else { Notification::success($this->translate('Preferences successfully saved for the current session')); } } catch (Exception $e) { Logger::error($e); Notification::error($e->getMessage()); } }
/** * Remove a role * * @throws Zend_Controller_Action_Exception If the required parameter 'role' is missing or the role does not exist */ public function removeAction() { $this->assertPermission('config/authentication/roles/remove'); $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->setTitle(sprintf($this->translate('Remove Role %s'), $name))->setSubmitLabel($this->translate('Remove Role'))->setRedirectUrl('role/list')->handleRequest(); $this->view->form = $confirmation; $this->render('form'); }
public function notifyError($message) { Notification::error($message); return $this; }
/** * Unshare a navigation item */ public function unshareAction() { $this->assertPermission('config/application/navigation'); $this->assertHttpMethod('POST'); // TODO: I'd like these being form fields $itemType = $this->params->getRequired('type'); $itemOwner = $this->params->getRequired('owner'); $navigationConfigForm = new NavigationConfigForm(); $navigationConfigForm->setUser($this->Auth()->getUser()); $navigationConfigForm->setShareConfig(Config::navigation($itemType)); $navigationConfigForm->setUserConfig(Config::navigation($itemType, $itemOwner)); $form = new Form(array('onSuccess' => function ($form) use($navigationConfigForm) { $itemName = $form->getValue('name'); try { $newConfig = $navigationConfigForm->unshare($itemName); if ($navigationConfigForm->save()) { if ($newConfig->getSection($itemName)->type === 'menu-item') { $form->getResponse()->setRerenderLayout(); } Notification::success(sprintf(t('Navigation item "%s" has been unshared'), $form->getValue('name'))); } else { // TODO: It failed obviously to write one of the configs, so we're leaving the user in // a inconsistent state. Luckily, it's nothing lost but possibly duplicated... Notification::error(sprintf(t('Failed to unshare navigation item "%s"'), $form->getValue('name'))); } } catch (NotFoundError $e) { throw $e; } catch (Exception $e) { Notification::error($e->getMessage()); } $redirect = $form->getValue('redirect'); if (!empty($redirect)) { $form->setRedirectUrl(htmlspecialchars_decode($redirect)); } return true; })); $form->setUidDisabled(); $form->setSubmitLabel('btn_submit'); // Required to ensure that isSubmitted() is called $form->addElement('hidden', 'name', array('required' => true)); $form->addElement('hidden', 'redirect'); try { $form->handleRequest(); } catch (NotFoundError $_) { $this->httpNotFound(sprintf($this->translate('Navigation item "%s" not found'), $form->getValue('name'))); } }
public function handleRequest($request) { $this->setUrl($request->getUrl()->without($this->ignoreParams)); $params = $this->url()->getParams(); $preserve = array(); foreach ($this->preserveParams as $key) { if (null !== ($value = $params->shift($key))) { $preserve[$key] = $value; } } $this->preservedParams = $preserve; $add = $params->shift('addFilter'); $remove = $params->shift('removeFilter'); $strip = $params->shift('stripFilter'); $modify = $params->shift('modifyFilter'); $search = null; if ($request->isPost()) { $search = $request->getPost('q'); } if ($search === null) { $search = $params->shift('q'); } $filter = $this->getFilter(); if ($search !== null) { if (strpos($search, '=') !== false) { list($k, $v) = preg_split('/=/', $search); $filter = $this->mergeRootExpression($filter, trim($k), '=', ltrim($v)); } else { if ($this->searchColumns === null && $this->query instanceof FilterColumns) { $this->searchColumns = $this->query->getSearchColumns($search); } if (!empty($this->searchColumns)) { if (!$this->resetSearchColumns($filter)) { $filter = Filter::matchAll(); } $filters = array(); $search = ltrim($search); foreach ($this->searchColumns as $searchColumn) { $filters[] = Filter::expression($searchColumn, '=', "*{$search}*"); } $filter = $filter->andFilter(new FilterOr($filters)); } else { Notification::error(mt('monitoring', 'Cannot search here')); return $this; } } $url = $this->url()->setQueryString($filter->toQueryString())->addParams($preserve); if ($modify) { $url->getParams()->add('modifyFilter'); } $this->redirectNow($url); } if ($remove) { $redirect = $this->url(); if ($filter->getById($remove)->isRootNode()) { $redirect->setQueryString(''); } else { $filter->removeId($remove); $redirect->setQueryString($filter->toQueryString())->getParams()->add('modifyFilter'); } $this->redirectNow($redirect->addParams($preserve)); } if ($strip) { $redirect = $this->url(); $subId = $strip . '-1'; if ($filter->getId() === $strip) { $filter = $filter->getById($strip . '-1'); } else { $filter->replaceById($strip, $filter->getById($strip . '-1')); } $redirect->setQueryString($filter->toQueryString())->getParams()->add('modifyFilter'); $this->redirectNow($redirect->addParams($preserve)); } if ($modify) { if ($request->isPost()) { if ($request->get('cancel') === 'Cancel') { $this->redirectNow($this->preservedUrl()->without('modifyFilter')); } $filter = $this->applyChanges($request->getPost()); $url = $this->url()->setQueryString($filter->toQueryString())->addParams($preserve); $url->getParams()->add('modifyFilter'); $this->redirectNow($url); } $this->url()->getParams()->add('modifyFilter'); } if ($add) { $this->addFilterToId($add); } if ($this->query !== null && $request->isGet()) { $this->query->applyFilter($this->getFilter()); } return $this; }
/** * Display a confirmation form to remove the instance identified by the 'instance' parameter */ public function removeinstanceAction() { $config = $this->Config('instances'); $form = new ConfirmRemovalForm(array('onSuccess' => function ($form) use($config) { $instanceName = $form->getRequest()->getQuery('instance'); $configForm = new InstanceConfigForm(); $configForm->setIniConfig($config); try { $configForm->remove($instanceName); } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return; } if ($configForm->save()) { Notification::success(sprintf($this->translate('Instance "%s" successfully removed.'), $instanceName)); } else { return false; } })); $form->setTitle($this->translate('Remove Existing Instance')); $form->addDescription($this->translate('If you have still any environments or views referring to this instance, ' . 'you won\'t be able to send commands anymore after deletion.')); $form->addElement('note', 'question', array('value' => $this->translate('Are you sure you want to remove this instance?'), 'decorators' => array('ViewHelper', array('HtmlTag', array('tag' => 'p'))))); $form->setRedirectUrl('monitoring/config'); $form->handleRequest(); $this->view->form = $form; }
/** * Insert the members for the group * * @return bool */ public function onSuccess() { $userNames = $this->getValue('user_name') ?: array(); if ($users = $this->getValue('users')) { $userNames = array_merge($userNames, array_map('trim', explode(',', $users))); } if (empty($userNames)) { $this->info($this->translate('Please provide at least one username, either by choosing one ' . 'in the list or by manually typing one in the text box below')); return false; } $single = null; foreach ($userNames as $userName) { try { $this->backend->insert('group_membership', array('group_name' => $this->groupName, 'user_name' => $userName)); } catch (NotFoundError $e) { throw $e; // Trigger 404, the group name is initially accessed as GET parameter } catch (Exception $e) { Notification::error(sprintf($this->translate('Failed to add "%s" as group member for "%s"'), $userName, $this->groupName)); $this->error($e->getMessage()); return false; } $single = $single === null; } if ($single) { Notification::success(sprintf($this->translate('Group member "%s" added successfully'), $userName)); } else { Notification::success($this->translate('Group members added successfully')); } return true; }
/** * Create the memberships for the user * * @return bool */ public function onSuccess() { $backendMap = array(); foreach ($this->backends as $backend) { $backendMap[$backend->getName()] = $backend; } $single = null; foreach ($this->getValue('groups') as $backendAndGroup) { list($backendName, $groupName) = explode(';', $backendAndGroup, 2); try { $backendMap[$backendName]->insert('group_membership', array('group_name' => $groupName, 'user_name' => $this->userName)); } catch (Exception $e) { Notification::error(sprintf($this->translate('Failed to add "%s" as group member for "%s"'), $this->userName, $groupName)); $this->error($e->getMessage()); return false; } $single = $single === null; } if ($single) { Notification::success(sprintf($this->translate('Membership for group %s created successfully'), $groupName)); } else { Notification::success($this->translate('Memberships created successfully')); } return true; }
/** * Display a confirmation form to remove a resource */ public function removeresourceAction() { $this->assertPermission('config/application/resources'); $this->getTabs()->add('resources/remove', array('label' => $this->translate('Remove Resource'), 'url' => Url::fromRequest()))->activate('resources/remove'); $form = new ConfirmRemovalForm(array('onSuccess' => function ($form) { $configForm = new ResourceConfigForm(); $configForm->setIniConfig(Config::app('resources')); $resource = $form->getRequest()->getQuery('resource'); try { $configForm->remove($resource); } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return false; } if ($configForm->save()) { Notification::success(sprintf(t('Resource "%s" has been successfully removed'), $resource)); } else { return false; } })); $form->setRedirectUrl('config/resource'); $form->handleRequest(); // Check if selected resource is currently used for authentication $resource = $this->getRequest()->getQuery('resource'); $authConfig = Config::app('authentication'); foreach ($authConfig as $backendName => $config) { if ($config->get('resource') === $resource) { $form->addDescription(sprintf($this->translate('The resource "%s" is currently utilized for authentication by user backend "%s". ' . 'Removing the resource can result in noone being able to log in any longer.'), $resource, $backendName)); } } $this->view->form = $form; $this->render('resource/remove'); }
/** * (non-PHPDoc) * @see Form::onSuccess() For the method documentation. */ public function onSuccess() { $instanceName = $this->request->getQuery('instance'); try { if ($instanceName === null) { // create new instance $this->add($this->getValues()); $message = $this->translate('Instance "%s" created successfully.'); } else { // edit existing instance $this->edit($instanceName, $this->getValues()); $message = $this->translate('Instance "%s" edited successfully.'); } } catch (InvalidArgumentException $e) { Notification::error($e->getMessage()); return; } if ($this->save()) { Notification::success(sprintf($message, $this->getElement('name')->getValue())); } else { return false; } }