2015-06-23 14:37:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director;
|
|
|
|
|
2015-07-31 14:38:22 +02:00
|
|
|
use Icinga\Authentication\Auth;
|
2015-07-30 09:11:53 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
|
|
|
use Icinga\Module\Director\Web\Form\QuickForm;
|
2015-07-30 09:12:19 +02:00
|
|
|
use Icinga\Web\Url;
|
2015-06-23 14:37:23 +02:00
|
|
|
use Zend_Db_Expr;
|
|
|
|
|
|
|
|
class Util
|
|
|
|
{
|
2015-07-30 09:04:42 +02:00
|
|
|
protected static $auth;
|
|
|
|
|
2015-07-30 09:11:53 +02:00
|
|
|
protected static $allowedDbResources;
|
|
|
|
|
2015-06-23 14:37:23 +02:00
|
|
|
public static function pgBinEscape($binary)
|
|
|
|
{
|
2015-07-30 09:12:19 +02:00
|
|
|
return new Zend_Db_Expr("'\\x" . bin2hex($binary) . "'");
|
2015-06-23 14:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function hex2binary($bin)
|
|
|
|
{
|
|
|
|
return pack('H*', $bin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function binary2hex($hex)
|
|
|
|
{
|
2015-06-23 14:38:37 +02:00
|
|
|
return current(unpack('H*', $hex));
|
2015-06-23 14:37:23 +02:00
|
|
|
}
|
2015-07-30 09:04:42 +02:00
|
|
|
|
|
|
|
public static function auth()
|
|
|
|
{
|
|
|
|
if (self::$auth === null) {
|
2015-07-31 14:38:22 +02:00
|
|
|
self::$auth = Auth::getInstance();
|
2015-07-30 09:04:42 +02:00
|
|
|
}
|
|
|
|
return self::$auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function hasPermission($name)
|
|
|
|
{
|
|
|
|
return self::auth()->hasPermission($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRestrictions($name)
|
|
|
|
{
|
|
|
|
return self::auth()->getRestrictions($name);
|
|
|
|
}
|
2015-07-30 09:11:53 +02:00
|
|
|
|
|
|
|
public static function dbResourceIsAllowed($name)
|
|
|
|
{
|
|
|
|
if (self::$allowedDbResources === null) {
|
|
|
|
$restrictions = self::getRestrictions('director/dbresources/use');
|
|
|
|
$list = array();
|
|
|
|
foreach ($restrictions as $restriction) {
|
|
|
|
foreach (preg_split('/\s*,\s*/', $restriction, -1, PREG_SPLIT_NO_EMPTY) as $key) {
|
|
|
|
$list[$key] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$allowedDbResources = $list;
|
|
|
|
} else {
|
|
|
|
$list = self::$allowedDbResources;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($list) || array_key_exists($name, $list)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function enumDbResources()
|
|
|
|
{
|
|
|
|
$resources = array();
|
|
|
|
foreach (ResourceFactory::getResourceConfigs() as $name => $resource) {
|
|
|
|
if ($resource->type === 'db' && self::dbResourceIsAllowed($name)) {
|
|
|
|
$resources[$name] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function addDbResourceFormElement(QuickForm $form, $name)
|
|
|
|
{
|
|
|
|
$list = Util::enumDbResources();
|
|
|
|
|
|
|
|
$form->addElement('select', $name, array(
|
|
|
|
'label' => 'Resource name',
|
|
|
|
'multiOptions' => $form->optionalEnum($list),
|
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
if (true && empty($list)) {
|
|
|
|
if (self::hasPermission('config/application/resources')) {
|
|
|
|
$hint = $form->translate('Please click %s to create new DB resources');
|
|
|
|
$link = sprintf(
|
|
|
|
'<a href="' . Url::fromPath('config/resource') . '" data-base-target="_main">%s</a>',
|
|
|
|
$form->translate('here')
|
|
|
|
);
|
|
|
|
$form->addHtmlHint(sprintf($hint, $link));
|
|
|
|
$msg = $form->translate('No db resource available');
|
|
|
|
} else {
|
|
|
|
$msg = $form->translate('Please ask an administrator to grant you access to DB resources');
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->getElement($name)->addError($msg);
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:37:23 +02:00
|
|
|
}
|