Skip to content

Commit

Permalink
NavigationConfigForm: Apply share restrictions for users and groups
Browse files Browse the repository at this point in the history
refs #5600
  • Loading branch information
Johannes Meyer committed Sep 16, 2015
1 parent 0cff2ca commit 4385d74
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions application/forms/Navigation/NavigationConfigForm.php
Expand Up @@ -5,6 +5,7 @@

use InvalidArgumentException;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotFoundError;
use Icinga\Forms\ConfigForm;
Expand Down Expand Up @@ -427,6 +428,63 @@ public function onRequest()
}
}

/**
* {@inheritdoc}
*/
public function isValid($formData)
{
if (! parent::isValid($formData)) {
return false;
}

$valid = true;
if (isset($formData['users']) && $formData['users']) {
$parsedUserRestrictions = array();
foreach (Auth::getInstance()->getRestrictions('application/share/users') as $userRestriction) {
$parsedUserRestrictions[] = array_map('trim', explode(',', $userRestriction));
}

if (! empty($parsedUserRestrictions)) {
$desiredUsers = array_map('trim', explode(',', $formData['users']));
array_unshift($parsedUserRestrictions, $desiredUsers);
$forbiddenUsers = call_user_func_array('array_diff', $parsedUserRestrictions);
if (! empty($forbiddenUsers)) {
$valid = false;
$this->getElement('users')->addError(
$this->translate(sprintf(
'You are not permitted to share this navigation item with the following users: %s',
implode(', ', $forbiddenUsers)
))
);
}
}
}

if (isset($formData['groups']) && $formData['groups']) {
$parsedGroupRestrictions = array();
foreach (Auth::getInstance()->getRestrictions('application/share/groups') as $groupRestriction) {
$parsedGroupRestrictions[] = array_map('trim', explode(',', $groupRestriction));
}

if (! empty($parsedGroupRestrictions)) {
$desiredGroups = array_map('trim', explode(',', $formData['groups']));
array_unshift($parsedGroupRestrictions, $desiredGroups);
$forbiddenGroups = call_user_func_array('array_diff', $parsedGroupRestrictions);
if (! empty($forbiddenGroups)) {
$valid = false;
$this->getElement('groups')->addError(
$this->translate(sprintf(
'You are not permitted to share this navigation item with the following groups: %s',
implode(', ', $forbiddenGroups)
))
);
}
}
}

return $valid;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 4385d74

Please sign in to comment.