[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 /** Existence of object is known - object is shown to user. */ 4 define('PERMS_SHOW', 2); 5 6 /** Contents of the object can be read. */ 7 define('PERMS_READ', 4); 8 9 /** Contents of the object can be edited. */ 10 define('PERMS_EDIT', 8); 11 12 /** The object can be deleted. */ 13 define('PERMS_DELETE', 16); 14 15 /** 16 * A bitmask of all possible permission values. Useful for 17 * removeXxxPermission(), unsetPerm(), etc. 18 */ 19 define('PERMS_ALL', PERMS_SHOW | PERMS_READ | PERMS_EDIT | PERMS_DELETE); 20 21 /** 22 * The Perms:: class provides the Horde permissions system. 23 * 24 * $Horde: framework/Perms/Perms.php,v 1.80.10.9 2006/05/05 04:08:59 selsky Exp $ 25 * 26 * Copyright 2001-2006 Chuck Hagenbuch <chuck@horde.org> 27 * Copyright 2004-2006 Jan Schneider <jan@horde.org> 28 * 29 * See the enclosed file COPYING for license information (LGPL). If you 30 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 31 * 32 * @author Chuck Hagenbuch <chuck@horde.org> 33 * @author Jan Schneider <jan@horde.org> 34 * @since Horde 2.1 35 * @package Horde_Perms 36 */ 37 class Perms { 38 39 /** 40 * Caches information about application permissions. 41 * 42 * @var array 43 */ 44 var $_applicationPermissions; 45 46 /** 47 * Returns the available permissions for a given level. 48 * 49 * @param string $name The permission's name. 50 * 51 * @return array An array of available permissions and their titles or 52 * false if not sub permissions exist for this level. 53 */ 54 function getAvailable($name) 55 { 56 global $registry; 57 58 if (empty($name)) { 59 /* No name passed, so top level permissions are requested. These 60 * can only be applications. */ 61 $apps = $registry->listApps(array('notoolbar', 'active', 'hidden'), true); 62 foreach (array_keys($apps) as $app) { 63 $apps[$app] = $registry->get('name', $app) . ' (' . $app . ')'; 64 } 65 asort($apps); 66 return $apps; 67 } else { 68 /* Name has been passed, explode the name to get all the levels in 69 * permission being requisted, with the app as the first level. */ 70 $levels = array(); 71 $levels = explode(':', $name); 72 73 /* First level is always app. */ 74 $app = $levels[0]; 75 76 /* Return empty if no app defined API method for providing 77 * permission information. */ 78 if (!$registry->hasMethod('perms', $app)) { 79 return false; 80 } 81 82 /* Call the app's permission function to return the permissions 83 * specific to this app. */ 84 $perms = $this->getApplicationPermissions($app); 85 if (is_a($perms, 'PEAR_Error')) { 86 return $perms; 87 } 88 89 require_once 'Horde/Array.php'; 90 /* Get the part of the app's permissions based on the permission 91 * name requested. */ 92 $children = Horde_Array::getElement($perms['tree'], $levels); 93 if ($children === false || 94 !is_array($children) || 95 !count($children)) { 96 /* No array of children available for this permission name. */ 97 return false; 98 } 99 100 $perms_list = array(); 101 foreach ($children as $perm_key => $perm_val) { 102 $perms_list[$perm_key] = $perms['title'][$name . ':' . $perm_key]; 103 } 104 return $perms_list; 105 } 106 } 107 108 /** 109 * Returns the short name of an object, the last portion of the full name. 110 * 111 * @static 112 * 113 * @param string $name The name of the object. 114 * 115 * @return string The object's short name. 116 */ 117 function getShortName($name) 118 { 119 /* If there are several components to the name, explode and 120 * get the last one, otherwise just return the name. */ 121 if (strpos($name, ':') !== false) { 122 $tmp = explode(':', $name); 123 return array_pop($tmp); 124 } else { 125 return $name; 126 } 127 } 128 129 /** 130 * Given a permission name, returns the title for that permission by 131 * looking it up in the applications's permission api. 132 * 133 * @param string $name The permissions's name. 134 * 135 * @return string The title for the permission. 136 */ 137 function getTitle($name) 138 { 139 global $registry; 140 141 $levels = explode(':', $name); 142 if (count($levels) == 1) { 143 return $registry->get('name', $name) . ' (' . $name . ')'; 144 } 145 $perm = array_pop($levels); 146 147 /* First level is always app. */ 148 $app = $levels[0]; 149 150 /* Return empty if no app defined API method for providing permission 151 * information. */ 152 if (!$registry->hasMethod('perms', $app)) { 153 return Perms::getShortName($name); 154 } 155 156 $app_perms = $this->getApplicationPermissions($app); 157 158 return isset($app_perms['title'][$name]) 159 ? $app_perms['title'][$name] . ' (' . DataTree::getShortName($name) . ')' 160 : DataTree::getShortName($name); 161 } 162 163 /** 164 * Returns information about permissions implemented by an application. 165 * 166 * @since Horde 3.1 167 * 168 * @param string $app An application name. 169 * 170 * @return array Hash with permissions information. 171 */ 172 function getApplicationPermissions($app) 173 { 174 if (!isset($this->_applicationPermissions[$app])) { 175 $this->_applicationPermissions[$app] = $GLOBALS['registry']->callByPackage($app, 'perms'); 176 } 177 178 return $this->_applicationPermissions[$app]; 179 } 180 181 /** 182 * Returns a new permissions object. 183 * 184 * @param string $name The permission's name. 185 * 186 * @return Permissions A new permissions object. 187 */ 188 function &newPermission($name) 189 { 190 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 191 } 192 193 /** 194 * Returns a Permission object corresponding to the named permission, 195 * with the users and other data retrieved appropriately. 196 * 197 * @param string $name The name of the permission to retrieve. 198 */ 199 function &getPermission($name) 200 { 201 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 202 } 203 204 /** 205 * Returns a Permission object corresponding to the given unique ID, with 206 * the users and other data retrieved appropriately. 207 * 208 * @param integer $cid The unique ID of the permission to retrieve. 209 */ 210 function &getPermissionById($cid) 211 { 212 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 213 } 214 215 /** 216 * Adds a permission to the permissions system. The permission must first 217 * be created with Perm::newPermission(), and have any initial users 218 * added to it, before this function is called. 219 * 220 * @param Permission $perm The new perm object. 221 */ 222 function addPermission(&$perm) 223 { 224 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 225 } 226 227 /** 228 * Removes a permission from the permissions system permanently. 229 * 230 * @param Permission $perm The permission to remove. 231 * @param boolean $force Force to remove every child. 232 */ 233 function removePermission(&$perm, $force = false) 234 { 235 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 236 } 237 238 /** 239 * Finds out what rights the given user has to this object. 240 * 241 * @param mixed $permission The full permission name of the object to 242 * check the permissions of, or the Permission 243 * object. 244 * @param string $user The user to check for. Defaults to the current 245 * user. 246 * @param string $creator The user who created the event. 247 * 248 * @return mixed A bitmask of permissions the user has, false if there 249 * are none. 250 */ 251 function getPermissions($permission, $user = null, $creator = null) 252 { 253 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 254 } 255 256 /** 257 * Returns the unique identifier of this permission. 258 * 259 * @param Permission $permission The permission object to get the ID of. 260 * 261 * @return integer The unique id. 262 */ 263 function getPermissionId($permission) 264 { 265 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 266 } 267 268 /** 269 * Finds out if the user has the specified rights to the given object. 270 * 271 * @param string $permission The permission to check. 272 * @param string $user The user to check for. 273 * @param integer $perm The permission level needed for access. 274 * @param string $creator The creator of the event 275 * 276 * @return boolean True if the user has the specified permissions. 277 */ 278 function hasPermission($permission, $user, $perm, $creator = null) 279 { 280 return false; 281 } 282 283 /** 284 * Checks if a permission exists in the system. 285 * 286 * @param string $permission The permission to check. 287 * 288 * @return boolean True if the permission exists. 289 */ 290 function exists($permission) 291 { 292 return false; 293 } 294 295 /** 296 * Returns a list of parent permissions. 297 * 298 * @param string $child The name of the child to retrieve parents for. 299 * 300 * @return array A hash with all parents in a tree format. 301 */ 302 function getParents($child) 303 { 304 return PEAR::raiseError(_("The administrator needs to configure a permanent Permissions backend if you want to use Permissions.")); 305 } 306 307 /** 308 * Returns all permissions of the system in a tree format. 309 * 310 * @return array A hash with all permissions in a tree format. 311 */ 312 function getTree() 313 { 314 return array(); 315 } 316 317 /** 318 * Returns an hash of the available permissions. 319 * 320 * @return array The available permissions as a hash. 321 */ 322 function getPermsArray() 323 { 324 return array(PERMS_SHOW => _("Show"), 325 PERMS_READ => _("Read"), 326 PERMS_EDIT => _("Edit"), 327 PERMS_DELETE => _("Delete")); 328 } 329 330 /** 331 * Given an integer value of permissions returns an array 332 * representation of the integer. 333 * 334 * @param integer $int The integer representation of permissions. 335 */ 336 function integerToArray($int) 337 { 338 static $array = array(); 339 if (isset($array[$int])) { 340 return $array[$int]; 341 } 342 343 $array[$int] = array(); 344 345 /* Get the available perms array. */ 346 $perms = Perms::getPermsArray(); 347 348 /* Loop through each perm and check if its value is included in the 349 * integer representation. */ 350 foreach ($perms as $val => $label) { 351 if ($int & $val) { 352 $array[$int][$val] = true; 353 } 354 } 355 356 return $array[$int]; 357 } 358 359 /** 360 * Attempts to return a concrete Perms instance based on $driver. 361 * 362 * @param string $driver The type of the concrete Perms subclass 363 * to return. The class name is based on the 364 * perms driver ($driver). The code is 365 * dynamically included. 366 * 367 * @return Perms|boolean The newly created concrete Perms instance, or 368 * false on an error. 369 */ 370 function &factory($driver = null) 371 { 372 if (is_null($driver)) { 373 $perms = &new Perms(); 374 } else { 375 include_once 'Horde/Perms/' . $driver . '.php'; 376 $class = 'Perms_' . $driver; 377 if (class_exists($class)) { 378 $perms = &new $class(); 379 } else { 380 $perms = false; 381 } 382 } 383 384 return $perms; 385 } 386 387 /** 388 * Attempts to return a reference to a concrete Perms instance. 389 * It will only create a new instance if no Perms instance 390 * currently exists. 391 * 392 * This method must be invoked as: $var = &Perms::singleton() 393 * 394 * @return Perms|boolean The concrete Perm reference, or false on error. 395 */ 396 function &singleton() 397 { 398 static $perm; 399 400 if (!isset($perm)) { 401 $perm = &Perms::factory(!empty($GLOBALS['conf']['datatree']['driver']) ? 'datatree' : null); 402 } 403 404 return $perm; 405 } 406 407 }
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 |