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