1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
<?php
/**
* The Perms_UI:: class provides UI methods for the Horde permissions
* system.
*
* $Horde: framework/Perms/Perms/UI.php,v 1.27.2.8 2006/01/01 21:28:31 jan Exp $
*
* Copyright 2001-2006 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @since Horde 2.1
* @package Horde_Perms
*/
class Perms_UI {
/**
* The Perms object we're displaying UI stuff for.
*
* @var Perms
*/
var $_perms;
/**
* The Horde_Form object that will be used for displaying the edit form.
*
* @var Horde_Form
*/
var $_form = null;
/**
* The Variables object used in Horde_Form.
*
* @var Variables
*/
var $_vars = null;
/**
* The permission type.
*
* @var string
*/
var $_type = 'matrix';
/**
* Constructor.
*
* @param Perms $perms The Perms object to display UI stuff for.
*/
function Perms_UI(&$perms)
{
$this->_perms = &$perms;
}
/**
* Return a Horde_Tree representation of the permissions tree.
*
* @return string The html showing the permissions as a Horde_Tree.
*/
function renderTree($current = DATATREE_ROOT)
{
global $registry;
require_once 'Horde/Tree.php';
/* Get the perms tree. */
$nodes = &$this->_perms->getTree();
$icondir = array('icondir' => $GLOBALS['registry']->getImageDir());
$perms_node = $icondir + array('icon' => 'perms.png');
$add = Horde::applicationUrl('admin/perms/addchild.php');
$edit = Horde::applicationUrl('admin/perms/edit.php');
$delete = Horde::applicationUrl('admin/perms/delete.php');
$edit_img = Horde::img('edit.png', _("Edit Permission"));
$delete_img = Horde::img('delete.png', _("Delete Permission"));
/* Set up the tree. */
$tree = &Horde_Tree::singleton('perms_ui', 'javascript');
$tree->setOption(array('alternate' => true, 'hideHeaders' => true));
$tree->setHeader(array(array('width' => '50%')));
foreach ($nodes as $perm_id => $node) {
$node_class = ($current == $perm_id) ? array('class' => 'selected') : array();
if ($perm_id == DATATREE_ROOT) {
$add_img = Horde::img('perms.png', _("Add New Permission"));
$add_link = Horde::link(Util::addParameter($add, 'perm_id', $perm_id), _("Add New Permission")) . $add_img . '</a>';
$base_node_params = $icondir + array('icon' => 'administration.png');
$tree->addNode($perm_id, null, _("All Permissions"), 0, true, $base_node_params + $node_class, array($add_link));
} else {
$parent_id = $this->_perms->_datatree->getParent($node);
if (is_a($parent_id, 'PEAR_Error')) {
Horde::fatal($parent_id, __FILE__, __LINE__);
}
$add_img = Horde::img('perms.png', _("Add Child Permission"));
$add_link = Horde::link(Util::addParameter($add, 'perm_id', $perm_id), _("Add Child Permission")) . $add_img . '</a>';
$edit_link = Horde::link(Util::addParameter($edit, 'perm_id', $perm_id), _("Edit Permission")) . $edit_img . '</a>';
$delete_link = Horde::link(Util::addParameter($delete, 'perm_id', $perm_id), _("Delete Permission")) . $delete_img . '</a>';
$perms_extra = array($add_link, $edit_link, $delete_link);
$name = $this->_perms->getTitle($node);
$tree->addNode($perm_id, $parent_id, $name, substr_count($node, ':') + 1, false, $perms_node + $node_class, $perms_extra);
}
}
$tree->sort('label');
return $tree->renderTree();
}
/**
* Set an existing form object to use for the edit form.
*
* @param Horde_Form $form An existing Horde_Form object to use.
*/
function setForm(&$form)
{
$this->_form = &$form;
}
/**
* Set an existing vars object to use for the edit form.
*
* @param Variables $vars An existing Variables object to use.
*/
function setVars(&$vars)
{
$this->_vars = &$vars;
}
/**
* Create a form to add a permission.
*
* @param Perms $permission
* @param string $force_choice If the permission to be added can be one of
* many, setting this will force the choice to
* one particular.
*/
function setupAddForm($permission, $force_choice = null)
{
/* Initialise form if required. */
$this->_formInit();
$this->_form->setTitle(sprintf(_("Add a child permission to \"%s\""), $this->_perms->getTitle($permission->getName())));
$this->_form->setButtons(_("Add"), true);
$this->_vars->set('perm_id', $this->_perms->getPermissionId($permission));
$this->_form->addHidden('', 'perm_id', 'text', false);
/* Set up the actual child adding field. */
$child_perms = $this->_perms->getAvailable($permission->getName());
if ($child_perms === false) {
/* False, so no childs are to be added below this level. */
$this->_form->addVariable(_("Permission"), 'child', 'invalid', true, false, null, array(_("No children can be added to this permission.")));
} elseif (is_array($child_perms)) {
if (!empty($force_choice)) {
/* Choice array available, but choice being forced. */
$this->_vars->set('child', $force_choice);
$this->_form->addVariable(_("Permissions"), 'child', 'enum', true, true, null, array($child_perms));
} else {
/* Choice array available, so set up enum field. */
$this->_form->addVariable(_("Permissions"), 'child', 'enum', true, false, null, array($child_perms));
}
}
}
/**
* Function to validate any add form input.
*
* @return mixed Either false if the form does not validate correctly or
* an array with all the form values.
*/
function validateAddForm(&$info)
{
if (!$this->_form->validate($this->_vars)) {
return false;
}
$this->_form->getInfo($this->_vars, $info);
return true;
}
/**
* Create a permission editing form.
*
* @param DataTreeObject_Permission $permission
*/
function setupEditForm($permission)
{
global $registry;
/* Initialise form if required. */
$this->_formInit();
$this->_form->setButtons(_("Update"), true);
$perm_id = $this->_perms->getPermissionId($permission);
$this->_form->addHidden('', 'perm_id', 'text', false);
/* Get permission configuration. */
$this->_type = $permission->get('type');
$params = $permission->get('params');
/* Default permissions. */
$perm_val = $permission->getDefaultPermissions();
$this->_form->setSection('default', _("All Authenticated Users"), Horde::img('perms.png', '', '', $registry->getImageDir('horde')), false);
/* We MUST use 'deflt' for the variable name because 'default' is a
* reserved word in JavaScript. */
if ($this->_type == 'matrix') {
/* Set up the columns for the permissions matrix. */
$cols = Perms::getPermsArray();
/* Define a single matrix row for default perms. */
$matrix = array();
$matrix[0] = Perms::integerToArray($perm_val);
$this->_form->addVariable('', 'deflt', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
} else {
$var = &$this->_form->addVariable('', 'deflt', $this->_type, false, false, null, $params);
$var->setDefault($perm_val);
}
/* Guest permissions. */
$perm_val = $permission->getGuestPermissions();
$this->_form->setSection('guest', _("Guest Permissions"), '', false);
if ($this->_type == 'matrix') {
/* Define a single matrix row for guest perms. */
$matrix = array();
$matrix[0] = Perms::integerToArray($perm_val);
$this->_form->addVariable('', 'guest', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
} else {
$var = &$this->_form->addVariable('', 'guest', $this->_type, false, false, null, $params);
$var->setDefault($perm_val);
}
/* Object creator permissions. */
$perm_val = $permission->getCreatorPermissions();
$this->_form->setSection('creator', _("Creator Permissions"), Horde::img('user.png', '', '', $registry->getImageDir('horde')), false);
if ($this->_type == 'matrix') {
/* Define a single matrix row for creator perms. */
$matrix = array();
$matrix[0] = Perms::integerToArray($perm_val);
$this->_form->addVariable('', 'creator', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
} else {
$var = &$this->_form->addVariable('', 'creator', $this->_type, false, false, null, $params);
$var->setDefault($perm_val);
}
/* Users permissions. */
$perm_val = $permission->getUserPermissions();
$this->_form->setSection('users', _("Individual Users"), Horde::img('user.png', '', '', $registry->getImageDir('horde')), false);
$auth = &Auth::singleton($GLOBALS['conf']['auth']['driver']);
if ($auth->hasCapability('list')) {
/* The auth driver has list capabilities so set up an array which
* the matrix field type will recognise to set up an enum box for
* adding new users to the permissions matrix. */
$new_users = array();
$user_list = $auth->listUsers();
if (is_a($user_list, 'PEAR_Error')) {
$new_users = true;
} else {
sort($user_list);
foreach ($user_list as $user) {
if (!isset($perm_val[$user])) {
$new_users[$user] = $user;
}
}
}
} else {
/* No list capabilities, setting to true so that the matrix field
* type will offer a text input box for adding new users. */
$new_users = true;
}
if ($this->_type == 'matrix') {
/* Set up the matrix array, breaking up each permission integer
* into an array. The keys of this array will be the row
* headers. */
$rows = array();
$matrix = array();
foreach ($perm_val as $u_id => $u_perms) {
$rows[$u_id] = $u_id;
$matrix[$u_id] = Perms::integerToArray($u_perms);
}
$this->_form->addVariable('', 'u', 'matrix', false, false, null, array($cols, $rows, $matrix, $new_users));
} else {
if ($new_users) {
if (is_array($new_users)) {
$u_n = Util::getFormData('u_n');
$u_n = empty($u_n['u']) ? null : $u_n['u'];
$user_html = '<select name="u_n[u]"><option>' . _("-- select --") . '</option>';
foreach ($new_users as $new_user) {
$user_html .= '<option value="' . $new_user . '"';
$user_html .= $u_n == $new_user ? ' selected="selected"' : '';
$user_html .= '>' . htmlspecialchars($new_user) . '</option>';
}
$user_html .= '</select>';
} else {
$user_html = '<input type="text" name="u_n[u]" />';
}
$this->_form->addVariable($user_html, 'u_n[v]', $this->_type, false, false, null, $params);
}
foreach ($perm_val as $u_id => $u_perms) {
$var = &$this->_form->addVariable($u_id, 'u_v[' . $u_id . ']', $this->_type, false, false, null, $params);
$var->setDefault($u_perms);
}
}
/* Groups permissions. */
$perm_val = $permission->getGroupPermissions();
$this->_form->setSection('groups', _("Groups"), Horde::img('group.png', '', '', $registry->getImageDir('horde')), false);
require_once 'Horde/Group.php';
$groups = &Group::singleton();
$group_list = $groups->listGroups();
if (!empty($group_list)) {
/* There is an available list of groups so set up an array which
* the matrix field type will recognise to set up an enum box for
* adding new groups to the permissions matrix. */
$new_groups = array();
foreach ($group_list as $groupId => $group) {
if (!isset($perm_val[$groupId])) {
$new_groups[$groupId] = $group;
}
}
} else {
/* Do not offer a text box to add new groups. */
$new_groups = false;
}
if ($this->_type == 'matrix') {
/* Set up the matrix array, break up each permission integer into
* an array. The keys of this array will be the row headers. */
$rows = array();
$matrix = array();
foreach ($perm_val as $g_id => $g_perms) {
$rows[$g_id] = isset($group_list[$g_id]) ? $group_list[$g_id] : $g_id;
$matrix[$g_id] = Perms::integerToArray($g_perms);
}
$this->_form->addVariable('', 'g', 'matrix', false, false, null, array($cols, $rows, $matrix, $new_groups));
} else {
if ($new_groups) {
if (is_array($new_groups)) {
$g_n = Util::getFormData('g_n');
$g_n = empty($g_n['g']) ? null : $g_n['g'];
$group_html = '<select name="g_n[g]"><option>' . _("-- select --") . '</option>';
foreach ($new_groups as $groupId => $group) {
$group_html .= '<option value="' . $groupId . '"';
$group_html .= $g_n == $groupId ? ' selected="selected"' : '';
$group_html .= '>' . htmlspecialchars($group) . '</option>';
}
$group_html .= '</select>';
} else {
$group_html = '<input type="text" name="g_n[g]" />';
}
$this->_form->addVariable($group_html, 'g_n[v]', $this->_type, false, false, null, $params);
}
foreach ($perm_val as $g_id => $g_perms) {
$var = &$this->_form->addVariable(isset($group_list[$g_id]) ? $group_list[$g_id] : $g_id, 'g_v[' . $g_id . ']', $this->_type, false, false, null, $params);
$var->setDefault($g_perms);
}
}
/* Set form title. */
$this->_form->setTitle(sprintf(_("Edit permissions for \"%s\""), $this->_perms->getTitle($permission->getName())));
}
/**
* Function to validate any edit form input.
*
* @return mixed Either false if the form does not validate correctly or
* an array with all the form values.
*/
function validateEditForm(&$info)
{
if (!$this->_form->validate($this->_vars)) {
return false;
}
$this->_form->getInfo($this->_vars, $info);
if ($this->_type == 'matrix') {
/* Collapse the array for default/guest/creator. */
$info['deflt'] = isset($info['deflt'][0]) ? $info['deflt'][0] : null;
$info['guest'] = isset($info['guest'][0]) ? $info['guest'][0] : null;
$info['creator'] = isset($info['creator'][0]) ? $info['creator'][0] : null;
} else {
$u_n = $this->_vars->get('u_n');
$info['u'] = array($u_n['u'] => $info['u_n']['v']);
unset($info['u_n']);
if (isset($info['u_v'])) {
$info['u'] = array_merge($info['u'], $info['u_v']);
unset($info['u_v']);
}
$g_n = $this->_vars->get('g_n');
$info['g'] = array($g_n['g'] => $info['g_n']['v']);
unset($info['g_n']);
if (isset($info['g_v'])) {
$info['g'] = array_merge($info['g'], $info['g_v']);
unset($info['g_v']);
}
}
$info['default'] = $info['deflt'];
unset($info['deflt']);
return true;
}
/**
* Create a permission deleting form.
*
* @param object $permission
*/
function setupDeleteForm($permission)
{
/* Initialise form if required. */
$this->_formInit();
$this->_form->setTitle(sprintf(_("Delete permissions for \"%s\""), $this->_perms->getTitle($permission->getName())));
$this->_form->setButtons(array(_("Delete"), _("Do not delete")));
$this->_form->addHidden('', 'perm_id', 'text', false);
$this->_form->addVariable(sprintf(_("Delete permissions for \"%s\" and any sub-permissions?"), $this->_perms->getTitle($permission->getName())), 'prompt', 'description', false);
}
/**
* Function to validate any delete form input.
*
* @return mixed If the delete button confirmation has been pressed return
* true, if any other submit button has been pressed return
* false. If form did not validate return null.
*/
function validateDeleteForm(&$info)
{
$form_submit = $this->_vars->get('submitbutton');
if ($form_submit == _("Delete")) {
if ($this->_form->validate($this->_vars)) {
$this->_form->getInfo($this->_vars, $info);
return true;
}
} elseif (!empty($form_submit)) {
return false;
}
return null;
}
/**
* Renders the edit form.
*/
function renderForm($form_script = 'edit.php')
{
require_once 'Horde/Form/Renderer.php';
$renderer = &new Horde_Form_Renderer();
$this->_form->renderActive($renderer, $this->_vars, $form_script, 'post');
}
/**
* Creates any form objects if they have not been initialised yet.
*
* @access private
*/
function _formInit()
{
if (is_null($this->_vars)) {
/* No existing vars set, get them now. */
require_once 'Horde/Variables.php';
$this->_vars = &Variables::getDefaultVariables();
}
if (!is_a($this->_form, 'Horde_Form')) {
/* No existing valid form object set so set up a new one. */
require_once 'Horde/Form.php';
$this->_form = &Horde_Form::singleton('', $this->_vars);
}
}
}
|