* @category Horde
* @package Perms
* @todo Remove $GLOBALS references here and in Sql backend for Horde 6.
*/
abstract class Horde_Perms_Base
{
/**
* Cache object.
*
* @var Horde_Cache
*/
protected $_cache;
/**
* Logger.
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Constructor.
*
* @param array $params Configuration parameters:
*
* 'cache' - (Horde_Cache) The object to use to cache perms.
* 'logger' - (Horde_Log_Logger) A logger object.
*
*
* @throws Horde_Perms_Exception
*/
public function __construct($params = array())
{
if (isset($params['cache'])) {
$this->_cache = $params['cache'];
}
if (isset($params['logger'])) {
$this->_logger = $params['logger'];
}
}
/**
* Returns the short name of an object, the last portion of the full name.
*
* @param string $name The name of the object.
*
* @return string The object's short name.
*/
public function getShortName($name)
{
/* If there are several components to the name, explode and
* get the last one, otherwise just return the name. */
if (strpos($name, ':') !== false) {
$tmp = explode(':', $name);
return array_pop($tmp);
}
return $name;
}
/**
* Returns a new permissions object.
*
* @param string $name The permission's name.
* @param string $type The permission type.
* @param array $params The permission parameters.
*
* @return Horde_Perms_Permission A new permissions object.
* @throws Horde_Perms_Exception
*/
abstract public function newPermission($name, $type = 'matrix', $params = null);
/**
* Returns an object corresponding to the named permission, with the users
* and other data retrieved appropriately.
*
* @param string $name The name of the permission to retrieve.
*
* @return Horde_Perms_Permission A permissions object.
* @throws Horde_Perms_Exception
*/
abstract public function getPermission($name);
/**
* Returns an object corresponding to the given unique ID, with the users
* and other data retrieved appropriately.
*
* @param integer $cid The unique ID of the permission to retrieve.
*
* @return Horde_Perms_Permission A permissions object.
* @throws Horde_Perms_Exception
*/
abstract public function getPermissionById($cid);
/**
* Adds a permission to the permissions system. The permission must first
* be created with newPermission(), and have any initial users added to
* it, before this function is called.
*
* @param Horde_Perms_Permission $perm The permissions object.
*
* @throws Horde_Perms_Exception
*/
abstract public function addPermission(Horde_Perms_Permission $perm);
/**
* Removes a permission from the permissions system permanently.
*
* @param Horde_Perms_Permission $perm The permission to remove.
* @param boolean $force Force to remove every child.
*
* @throws Horde_Perms_Exception
*/
abstract public function removePermission(Horde_Perms_Permission $perm,
$force = false);
/**
* Finds out what rights the given user has to this object.
*
* @param mixed $permission The full permission name of the object to
* check the permissions of, or the
* Horde_Permissions object.
* @param string $user The user to check for.
* @param string $creator The user who created the event.
*
* @return mixed A bitmask of permissions the user has, false if there
* are none.
*/
public function getPermissions($permission, $user, $creator = null)
{
if (is_string($permission)) {
try {
$permission = $this->getPermission($permission);
} catch (Horde_Perms_Exception $e) {
/* Ignore not exists errors. */
if ($this->_logger &&
($e->getCode() != Horde_Perms_Exception::NOT_EXIST)) {
$this->_logger->log($e, 'DEBUG');
}
return false;
}
}
// If this is a guest user, only check guest permissions.
if (empty($user)) {
return $permission->getGuestPermissions();
}
// Combine all other applicable permissions.
$type = $permission->get('type');
$composite_perm = ($type == 'matrix') ? 0 : array();
// If $creator was specified, check creator permissions.
// If the user is the creator of the event see if there are creator
// permissions.
if (!is_null($creator) &&
strlen($user) &&
($user === $creator) &&
(($perms = $permission->getCreatorPermissions()) !== null)) {
if ($type == 'matrix') {
$composite_perm |= $perms;
} else {
$composite_perm[] = $perms;
}
}
// Check user-level permissions.
$userperms = $permission->getUserPermissions();
if (isset($userperms[$user])) {
if ($type == 'matrix') {
$composite_perm |= $userperms[$user];
} else {
$composite_perm[] = $userperms[$user];
}
}
// If no user permissions are found, try group permissions.
if (isset($permission->data['groups']) &&
is_array($permission->data['groups']) &&
count($permission->data['groups'])) {
$groups = $GLOBALS['injector']
->getInstance('Horde_Group')
->listGroups($user);
foreach ($permission->data['groups'] as $group => $perms) {
if (isset($groups[$group])) {
if ($type == 'matrix') {
$composite_perm |= $perms;
} else {
$composite_perm[] = $perms;
}
}
}
}
// If there are default permissions, return them.
if (($perms = $permission->getDefaultPermissions()) !== null) {
if ($type == 'matrix') {
$composite_perm |= $perms;
} else {
$composite_perm[] = $perms;
}
}
// Return composed permissions.
if ($composite_perm) {
return $composite_perm;
}
// Otherwise, deny all permissions to the object.
return false;
}
/**
* Returns the unique identifier of this permission.
*
* @param Horde_Perms_Permission $permission The permission object to get
* the ID of.
*
* @return integer The unique id.
* @throws Horde_Perms_Exception
*/
abstract public function getPermissionId($permission);
/**
* Finds out if the user has the specified rights to the given object.
*
* @param string $permission The permission to check.
* @param string $user The user to check for.
* @param integer $perm The permission level that needs to be checked
* for.
* @param string $creator The creator of the event
*
* @return boolean Whether the user has the specified permissions.
*/
public function hasPermission($permission, $user, $perm, $creator = null)
{
return (bool)($this->getPermissions($permission, $user, $creator) & $perm);
}
/**
* Checks if a permission exists in the system.
*
* @param string $permission The permission to check.
*
* @return boolean True if the permission exists.
*/
abstract public function exists($permission);
/**
* Returns a list of parent permissions.
*
* @param string $child The name of the child to retrieve parents for.
*
* @return array A hash with all parents in a tree format.
* @throws Horde_Perms_Exception
*/
abstract public function getParents($child);
/**
* Returns all permissions of the system in a tree format.
*
* @return array A hash with all permissions in a tree format.
*/
abstract public function getTree();
}
Horde_Perms-2.1.7/lib/Horde/Perms/Exception.php 0000664 0001750 0001750 00000000702 12667632612 017412 0 ustar jan jan
* @category Horde
* @package Perms
*/
class Horde_Perms_Exception extends Horde_Exception_Wrapped
{
const NOT_EXIST = 1;
}
Horde_Perms-2.1.7/lib/Horde/Perms/Null.php 0000664 0001750 0001750 00000007375 12667632612 016403 0 ustar jan jan
* @category Horde
* @package Perms
*/
class Horde_Perms_Null extends Horde_Perms_Base
{
/**
* Returns a new permissions object.
*
* @param string $name The permission's name.
* @param string $type The permission type.
* @param array $params The permission parameters.
*
* @return Horde_Perms_Permission A new permissions object.
* @throws Horde_Perms_Exception
*/
public function newPermission($name, $type = 'matrix', $params = null)
{
throw new Horde_Perms_Exception();
}
/**
* Returns an object corresponding to the named permission, with the users
* and other data retrieved appropriately.
*
* @param string $name The name of the permission to retrieve.
*
* @return Horde_Perms_Permission A permissions object.
* @throws Horde_Perms_Exception
*/
public function getPermission($name)
{
throw new Horde_Perms_Exception();
}
/**
* Returns an object corresponding to the given unique ID, with the users
* and other data retrieved appropriately.
*
* @param integer $cid The unique ID of the permission to retrieve.
*
* @return Horde_Perms_Permission A permissions object.
* @throws Horde_Perms_Exception
*/
public function getPermissionById($cid)
{
throw new Horde_Perms_Exception();
}
/**
* Adds a permission to the permissions system. The permission must first
* be created with newPermission(), and have any initial users added to
* it, before this function is called.
*
* @param Horde_Perms_Permission $perm The permissions object.
*
* @throws Horde_Perms_Exception
*/
public function addPermission(Horde_Perms_Permission $perm)
{
throw new Horde_Perms_Exception();
}
/**
* Removes a permission from the permissions system permanently.
*
* @param Horde_Perms_Permission $perm The permission to remove.
* @param boolean $force Force to remove every child.
*
* @throws Horde_Perms_Exception
*/
public function removePermission(Horde_Perms_Permission $perm,
$force = false)
{
throw new Horde_Perms_Exception();
}
/**
* Returns the unique identifier of this permission.
*
* @param Horde_Perms_Permission $permission The permission object to get
* the ID of.
*
* @return integer The unique id.
* @throws Horde_Perms_Exception
*/
public function getPermissionId($permission)
{
throw new Horde_Perms_Exception();
}
/**
* Checks if a permission exists in the system.
*
* @param string $permission The permission to check.
*
* @return boolean True if the permission exists.
*/
public function exists($permission)
{
return false;
}
/**
* Returns a list of parent permissions.
*
* @param string $child The name of the child to retrieve parents for.
*
* @return array A hash with all parents in a tree format.
* @throws Horde_Perms_Exception
*/
public function getParents($child)
{
throw new Horde_Perms_Exception();
}
/**
* Returns all permissions of the system in a tree format.
*
* @return array A hash with all permissions in a tree format.
*/
public function getTree()
{
return array();
}
}
Horde_Perms-2.1.7/lib/Horde/Perms/Permission.php 0000664 0001750 0001750 00000044766 12667632612 017626 0 ustar jan jan
* @author Jan Schneider
* @category Horde
* @package Perms
*/
class Horde_Perms_Permission
{
/**
* TODO
*/
public $data;
/**
* TODO
*/
public $name;
/**
* Incrementing version number if cached classes change.
*
* @var integer
*/
protected $_cacheVersion;
/**
* Constructor.
*
* @param string $name The name of the perm.
* @param integer $cacheVersion The revision number of the class.
* @param string $type The permission type.
* @param array $params A hash with any parameters that the
* permission type needs.
*/
public function __construct($name, $cacheVersion = null, $type = 'matrix',
$params = null)
{
$this->setName($name);
$this->setCacheVersion($cacheVersion);
$this->data['type'] = $type;
if (is_array($params)) {
$this->data['params'] = $params;
}
}
/**
* Sets the revision number of the class.
*
* @param integer $cacheVersion The revision number of the class.
*/
public function setCacheVersion($cacheVersion)
{
$this->_cacheVersion = $cacheVersion;
}
/**
* Gets one of the attributes of the object, or null if it isn't defined.
*
* @param string $attribute The attribute to get.
*
* @return mixed The value of the attribute, or null.
*/
public function get($attribute)
{
if (isset($this->data[$attribute])) {
return $this->data[$attribute];
}
return ($attribute == 'type') ? 'matrix' : null;
}
/**
* Get permission name.
*
* @return string Permission name.
*/
public function getName()
{
return $this->name;
}
/**
* Set permission name
*
* @param string $name Permission name.
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get permission details.
*
* @return array Permission details.
*/
public function getData()
{
return $this->data;
}
/**
* Set permission details.
*
* @param string $data Permission details.
*/
public function setData($data)
{
$this->data = $data;
}
/**
* Updates the permissions based on data passed in the array.
*
* @param array $perms An array containing the permissions which are to
* be updated.
*/
public function updatePermissions($perms)
{
$type = $this->get('type');
if ($type == 'matrix') {
/* Array of permission types to iterate through. */
$perm_types = Horde_Perms::getPermsArray();
}
foreach ($perms as $perm_class => $perm_values) {
switch ($perm_class) {
case 'default':
case 'guest':
case 'creator':
if ($type == 'matrix') {
foreach ($perm_types as $val => $label) {
if (!empty($perm_values[$val])) {
$this->setPerm($perm_class, $val, false);
} else {
$this->unsetPerm($perm_class, $val, false);
}
}
} elseif (!empty($perm_values)) {
$this->setPerm($perm_class, $perm_values, false);
} else {
$this->unsetPerm($perm_class, null, false);
}
break;
case 'u':
case 'g':
$permId = array('class' => $perm_class == 'u' ? 'users' : 'groups');
/* Figure out what names that are stored in this permission
* class have not been submitted for an update, ie. have been
* removed entirely. */
$current_names = isset($this->data[$permId['class']])
? array_keys($this->data[$permId['class']])
: array();
$updated_names = array_keys($perm_values);
$removed_names = array_diff($current_names, $updated_names);
/* Remove any names that have been completely unset. */
foreach ($removed_names as $name) {
unset($this->data[$permId['class']][$name]);
}
/* If nothing to actually update finish with this case. */
if (is_null($perm_values)) {
continue;
}
/* Loop through the names and update permissions for each. */
// @todo for Horde 6 - allow integer 0 values?
foreach ($perm_values as $name => $name_values) {
$permId['name'] = $name;
if ($type == 'matrix') {
foreach ($perm_types as $val => $label) {
if ($name_values[$val] === '0' || !empty($name_values[$val])) {
$this->setPerm($permId, $val, false);
} else {
$this->unsetPerm($permId, $val, false);
}
}
} elseif ($name_values === '0' || !empty($name_values)) {
$this->setPerm($permId, $name_values, false);
} else {
$this->unsetPerm($permId, null, false);
}
}
break;
}
}
}
/**
* TODO
*/
public function setPerm($permId, $permission, $update = true)
{
if (is_array($permId)) {
if (empty($permId['name'])) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data[$permId['class']][$permId['name']])) {
$this->data[$permId['class']][$permId['name']] |= $permission;
} else {
$this->data[$permId['class']][$permId['name']] = $permission;
}
} else {
if ($this->get('type') == 'matrix' &&
isset($this->data[$permId])) {
$this->data[$permId] |= $permission;
} else {
$this->data[$permId] = $permission;
}
}
if ($update) {
$this->save();
}
}
/**
* TODO
*/
public function unsetPerm($permId, $permission, $update = true)
{
if (is_array($permId)) {
if (empty($permId['name'])) {
return;
}
if ($this->get('type') == 'matrix') {
if (isset($this->data[$permId['class']][$permId['name']])) {
$this->data[$permId['class']][$permId['name']] &= ~$permission;
if (empty($this->data[$permId['class']][$permId['name']])) {
unset($this->data[$permId['class']][$permId['name']]);
}
} else {
$update = false;
}
} else {
unset($this->data[$permId['class']][$permId['name']]);
}
} else {
if ($this->get('type') == 'matrix') {
if (isset($this->data[$permId])) {
$this->data[$permId] &= ~$permission;
} else {
$update = false;
}
} else {
unset($this->data[$permId]);
}
}
if ($update) {
$this->save();
}
}
/**
* Grants a user additional permissions to this object.
*
* @param string $uer The user to grant additional permissions
* to.
* @param integer $permission The permission (DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function addUserPermission($user, $permission, $update = true)
{
if (empty($user)) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data['users'][$user])) {
$this->data['users'][$user] |= $permission;
} else {
$this->data['users'][$user] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants guests additional permissions to this object.
*
* @param integer $permission The permission (DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function addGuestPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['guest'])) {
$this->data['guest'] |= $permission;
} else {
$this->data['guest'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants creators additional permissions to this object.
*
* @param integer $permission The permission (DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function addCreatorPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['creator'])) {
$this->data['creator'] |= $permission;
} else {
$this->data['creator'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants additional default permissions to this object.
*
* @param integer $permission The permission (DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function addDefaultPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['default'])) {
$this->data['default'] |= $permission;
} else {
$this->data['default'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants a group additional permissions to this object.
*
* @param integer $groupId The id of the group to grant additional
* permissions to.
* @param integer $permission The permission (DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function addGroupPermission($groupId, $permission, $update = true)
{
if (empty($groupId)) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data['groups'][$groupId])) {
$this->data['groups'][$groupId] |= $permission;
} else {
$this->data['groups'][$groupId] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that a user currently has on this object.
*
* @param string $user The user to remove the permission from.
* Defaults to all users.
* @param integer $permission The permission (DELETE, etc.) to
* remove. Defaults to all permissions.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function removeUserPermission($user = null, $permission = null,
$update = true)
{
if (is_null($user)) {
$this->data['users'] = array();
} else {
if (!isset($this->data['users'][$user])) {
return;
}
if ($permission && $this->get('type') == 'matrix') {
$this->data['users'][$user] &= ~$permission;
if (empty($this->data['users'][$user])) {
unset($this->data['users'][$user]);
}
} else {
unset($this->data['users'][$user]);
}
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that guests currently have on this object.
*
* @param integer $permission The permission (DELETE, etc.) to
* remove. Defaults to all permissions.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function removeGuestPermission($permission = null, $update = true)
{
if (!isset($this->data['guest'])) {
return;
}
if ($permission && $this->get('type') == 'matrix') {
$this->data['guest'] &= ~$permission;
} else {
unset($this->data['guest']);
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that creators currently have on this object.
*
* @param integer $permission The permission (DELETE, etc.) to
* remove. Defaults to all permissions.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function removeCreatorPermission($permission = null, $update = true)
{
if (!isset($this->data['creator'])) {
return;
}
if ($permission && $this->get('type') == 'matrix') {
$this->data['creator'] &= ~$permission;
} else {
unset($this->data['creator']);
}
if ($update) {
$this->save();
}
}
/**
* Removes a default permission on this object.
*
* @param integer $permission The permission (DELETE, etc.) to
* remove. Defaults to all permissions.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function removeDefaultPermission($permission = null, $update = true)
{
if (!isset($this->data['default'])) {
return;
}
if ($permission && $this->get('type') == 'matrix') {
$this->data['default'] &= ~$permission;
} else {
unset($this->data['default']);
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that a group currently has on this object.
*
* @param integer $groupId The id of the group to remove the
* permission from. Defaults to all groups.
* @param integer $permission The permission (DELETE, etc.) to
* remove. Defaults to all permissions.
* @param boolean $update Whether to automatically update the
* backend.
*/
public function removeGroupPermission($groupId = null, $permission = null,
$update = true)
{
if (is_null($groupId)) {
$this->data['groups'] = array();
} else {
if (!isset($this->data['groups'][$groupId])) {
return;
}
if ($permission && $this->get('type') == 'matrix') {
$this->data['groups'][$groupId] &= ~$permission;
if (empty($this->data['groups'][$groupId])) {
unset($this->data['groups'][$groupId]);
}
} else {
unset($this->data['groups'][$groupId]);
}
}
if ($update) {
$this->save();
}
}
/**
* Returns an array of all user permissions on this object.
*
* @param integer $perm List only users with this permission level.
* Defaults to all users.
*
* @return array All user permissions for this object, indexed by user.
*/
public function getUserPermissions($perm = null)
{
if (!isset($this->data['users']) || !is_array($this->data['users'])) {
return array();
} elseif (!$perm) {
return $this->data['users'];
}
$users = array();
foreach ($this->data['users'] as $user => $uperm) {
if ($uperm & $perm) {
$users[$user] = $uperm;
}
}
return $users;
}
/**
* Returns the guest permissions on this object.
*
* @return integer The guest permissions on this object.
*/
public function getGuestPermissions()
{
return empty($this->data['guest'])
? null
: $this->data['guest'];
}
/**
* Returns the creator permissions on this object.
*
* @return integer The creator permissions on this object.
*/
public function getCreatorPermissions()
{
return empty($this->data['creator'])
? null
: $this->data['creator'];
}
/**
* Returns the default permissions on this object.
*
* @return integer The default permissions on this object.
*/
public function getDefaultPermissions()
{
return empty($this->data['default'])
? null
: $this->data['default'];
}
/**
* Returns an array of all group permissions on this object.
*
* @param integer $perm List only users with this permission level.
* Defaults to all users.
*
* @return array All group permissions for this object, indexed by group.
*/
public function getGroupPermissions($perm = null)
{
if (!isset($this->data['groups']) ||
!is_array($this->data['groups'])) {
return array();
} elseif (!$perm) {
return $this->data['groups'];
}
$groups = array();
foreach ($this->data['groups'] as $group => $gperm) {
if ($gperm & $perm) {
$groups[$group] = $gperm;
}
}
return $groups;
}
/**
* TODO
*/
public function save()
{
}
}
Horde_Perms-2.1.7/lib/Horde/Perms/Sql.php 0000664 0001750 0001750 00000032115 12667632612 016216 0 ustar jan jan
* @category Horde
* @package Perms
*/
class Horde_Perms_Sql extends Horde_Perms_Base
{
/**
* Configuration parameters.
*
* @var array
*/
protected $_params = array();
/**
* Handle for the current database connection.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Incrementing version number if cached classes change.
*
* @var integer
*/
private $_cacheVersion = 2;
/**
* Cache of previously retrieved permissions.
*
* @var array
*/
protected $_permsCache = array();
/**
* Constructor.
*
* @param array $params Configuration parameters (in addition to base
* Horde_Perms parameters):
*
* 'db' - (Horde_Db_Adapter) [REQUIRED] The DB instance.
* 'table' - (string) The name of the perms table.
* DEFAULT: 'horde_perms'
*
*
* @throws Horde_Perms_Exception
*/
public function __construct($params = array())
{
if (!isset($params['db'])) {
throw new Horde_Perms_Exception('Missing db parameter.');
}
$this->_db = $params['db'];
unset($params['db']);
$this->_params = array_merge(array(
'table' => 'horde_perms'
), $this->_params, $params);
parent::__construct($params);
}
/**
* Returns a new permissions object.
*
* @param string $name The permission's name.
* @param string $type The permission type.
* @param array $params The permission parameters.
*
* @return Horde_Perms_Permission_Sql A new permissions object.
*/
public function newPermission($name, $type = 'matrix', $params = null)
{
$ob = new Horde_Perms_Permission_Sql($name, $this->_cacheVersion, $type, $params);
$ob->setObs($this->_cache, $this->_db);
return $ob;
}
/**
* Returns an object corresponding to the named permission, with the
* users and other data retrieved appropriately.
*
* @param string $name The name of the permission to retrieve.
*
* @return Horde_Perms_Permission_Sql TODO
* @throw Horde_Perms_Exception
*/
public function getPermission($name)
{
if (isset($this->_permsCache[$name])) {
return $this->_permsCache[$name];
}
$perm = $this->_cache->get('perm_sql_' . $this->_cacheVersion . $name, $GLOBALS['conf']['cache']['default_lifetime']);
if (!empty($perm)) {
$this->_permsCache[$name] = unserialize($perm);
}
if (empty($this->_permsCache[$name])) {
$query = 'SELECT perm_id, perm_data FROM ' .
$this->_params['table'] . ' WHERE perm_name = ?';
try {
$result = $this->_db->selectOne($query, array($name));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
if (empty($result)) {
throw new Horde_Perms_Exception('Does not exist', Horde_Perms_Exception::NOT_EXIST);
}
$object = new Horde_Perms_Permission_Sql($name, $this->_cacheVersion);
$object->setId($result['perm_id']);
$object->setData(unserialize($result['perm_data']));
$this->_cache->set('perm_sql_' . $this->_cacheVersion . $name, serialize($object));
$this->_permsCache[$name] = $object;
}
$this->_permsCache[$name]->setObs($this->_cache, $this->_db);
return $this->_permsCache[$name];
}
/**
* Returns a permission object corresponding to the given unique ID,
* with the users and other data retrieved appropriately.
*
* @param integer $id The unique ID of the permission to retrieve.
*
* @return Horde_Perms_Permission_Sql TODO
* @throws Horde_Perms_Exception
*/
public function getPermissionById($id)
{
if ($id == Horde_Perms::ROOT || empty($id)) {
$object = $this->newPermission(Horde_Perms::ROOT);
} else {
$query = 'SELECT perm_name, perm_data FROM ' .
$this->_params['table'] . ' WHERE perm_id = ?';
try {
$result = $this->_db->selectOne($query, array($id));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
if (empty($result)) {
throw new Horde_Perms_Exception('Does not exist', Horde_Perms_Exception::NOT_EXIST);
}
$object = new Horde_Perms_Permission_Sql($result['perm_name'], $this->_cacheVersion);
$object->setId($id);
$object->setData(unserialize($result['perm_data']));
$object->setObs($this->_cache, $this->_db);
}
return $object;
}
/**
* Adds a permission to the permissions system. The permission must first
* be created with newPermission(), and have any initial users added to
* it, before this function is called.
*
* @param Horde_Perms_Permission_Sql $perm The perm object.
*
* @return integer Permission ID in the database.
* @throws Horde_Perms_Exception
*/
public function addPermission(Horde_Perms_Permission $perm)
{
$name = $perm->getName();
if (empty($name)) {
throw new Horde_Perms_Exception('Permission name must be non-empty.');
}
$this->_cache->expire('perm_sql_' . $this->_cacheVersion . $name);
$this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
// remove root from the name
$root = Horde_Perms::ROOT . ':';
if (substr($name, 0, strlen($root)) == ($root)) {
$name = substr($name, strlen($root));
}
// build parents
$parents = null;
if (($pos = strrpos($name, ':')) !== false) {
$parent_name = substr($name, 0, $pos);
$query = 'SELECT perm_id, perm_parents FROM ' .
$this->_params['table'] . ' WHERE perm_name = ?';
$result = $this->_db->selectOne($query, array($parent_name));
if (empty($result)) {
throw new Horde_Perms_Exception(Horde_Perms_Translation::t("Trying to create sub permission of non-existent parent permission. Create parent permission(s) first."));
}
$parents = $result['perm_parents'] . ':' . $result['perm_id'];
}
$query = 'INSERT INTO ' . $this->_params['table'] .
' (perm_name, perm_parents) VALUES (?, ?)';
try {
$id = $this->_db->insert($query, array($name, $parents));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
$perm->setId($id);
$perm->save();
return $id;
}
/**
* Removes a permission from the permissions system permanently.
*
* @param Horde_Perms_Permission_Sql $perm The permission to
* remove.
* @param boolean $force Force to remove every
* child.
*
* @return boolean True if permission was deleted.
* @throws Horde_Perms_Exception
*/
public function removePermission(Horde_Perms_Permission $perm,
$force = false)
{
$name = $perm->getName();
$this->_cache->expire('perm_sql_' . $this->_cacheVersion . $name);
$this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE perm_name = ?';
try {
$result = $this->_db->delete($query, array($name));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
if (!$force) {
return (bool)$result;
}
/* Need to expire cache for all sub-permissions. */
try {
$sub = $this->_db->selectValues(
'SELECT perm_name FROM ' . $this->_params['table'] . ' WHERE perm_name LIKE ?',
array($name . ':%')
);
foreach ($sub as $val) {
$this->_cache->expire('perm_sql_' . $this->_cacheVersion . $val);
$this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $val);
}
} catch (Horde_Db_Exception $e) {}
$query = 'DELETE FROM ' . $this->_params['table'] .
' WHERE perm_name LIKE ?';
try {
return (bool)$this->_db->delete($query, array($name . ':%'));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
}
/**
* Returns the unique identifier of this permission.
*
* @param Horde_Perms_Permission_Sql $perm The permission object to
* get the ID of.
*
* @return integer The unique id.
* @throws Horde_Perms_Exception
*/
public function getPermissionId($permission)
{
if ($permission->getName() == Horde_Perms::ROOT) {
return Horde_Perms::ROOT;
}
$query = 'SELECT perm_id FROM ' . $this->_params['table'] .
' WHERE perm_name = ?';
try {
return $this->_db->selectValue($query, array($permission->getName()));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
}
/**
* Checks if a permission exists in the system.
*
* @param string $permission The permission to check.
*
* @return boolean True if the permission exists.
* @throws Horde_Perms_Exception
*/
public function exists($permission)
{
$key = 'perm_sql_exists_' . $this->_cacheVersion . $permission;
$exists = $this->_cache->get($key, $GLOBALS['conf']['cache']['default_lifetime']);
if ($exists === false) {
$query = 'SELECT COUNT(*) FROM ' . $this->_params['table'] .
' WHERE perm_name = ?';
try {
$exists = $this->_db->selectValue($query, array($permission));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
$this->_cache->set($key, (string)$exists);
}
return (bool)$exists;
}
/**
* Returns a child's direct parent ID.
*
* @param mixed $child The object name for which to look up the parent's
* ID.
*
* @return integer The unique ID of the parent.
* @throws Horde_Perms_Exception
*/
public function getParent($child)
{
$query = 'SELECT perm_parents FROM ' . $this->_params['table'] .
' WHERE perm_name = ?';
try {
$parents = $this->_db->selectValue($query, array($child));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
if (empty($parents)) {
return Horde_Perms::ROOT;
}
$parents = explode(':', $parents);
return array_pop($parents);
}
/**
* Returns a list of parent permissions.
*
* @param string $child The name of the child to retrieve parents for.
*
* @return array A hash with all parents in a tree format.
* @throws Horde_Perms_Exception
*/
public function getParents($child)
{
$query = 'SELECT perm_parents FROM ' . $this->_params['table'] .
' WHERE perm_name = ?';
try {
$result = $this->_db->selectValue($query, array($child));
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
if (empty($result)) {
throw new Horde_Perms_Exception('Does not exist', Horde_Perms_Exception::NOT_EXIST);
}
return $this->_getParents($result);
}
/**
* TODO
*/
protected function _getParents($parents)
{
if (empty($parents)) {
return array(Horde_Perms::ROOT => true);
}
$pname = $parents;
$parents = substr($parents, 0, strrpos($parents, ':'));
return array($pname => $this->_getParents($parents));
}
/**
* Returns all permissions of the system in a tree format.
*
* @return array A hash with all permissions in a tree format.
* @throws Horde_Perms_Exception
*/
public function getTree()
{
$query = 'SELECT perm_id, perm_name FROM ' . $this->_params['table'] .
' ORDER BY perm_name ASC';
try {
$tree = $this->_db->selectAssoc($query);
} catch (Horde_Db_Exception $e) {
throw new Horde_Perms_Exception($e);
}
$tree[Horde_Perms::ROOT] = Horde_Perms::ROOT;
return $tree;
}
}
Horde_Perms-2.1.7/lib/Horde/Perms/Translation.php 0000664 0001750 0001750 00000001365 12667632612 017760 0 ustar jan jan
* @package Perms
*/
class Horde_Perms_Translation extends Horde_Translation_Autodetect
{
/**
* The translation domain
*
* @var string
*/
protected static $_domain = 'Horde_Perms';
/**
* The absolute PEAR path to the translations for the default gettext handler.
*
* @var string
*/
protected static $_pearDirectory = '@data_dir@';
}
Horde_Perms-2.1.7/lib/Horde/Perms.php 0000664 0001750 0001750 00000004621 12667632612 015460 0 ustar jan jan
* @author Jan Schneider
* @category Horde
* @package Perms
*/
class Horde_Perms
{
/**
* Existence of object is known - object is shown to user.
*/
const SHOW = 2;
/**
* Contents of the object can be read.
*/
const READ = 4;
/**
* Contents of the object can be edited.
*/
const EDIT = 8;
/**
* The object can be deleted.
*/
const DELETE = 16;
/**
* A bitmask of all possible permission values.
*
* Useful for removeXxxPermission(), unsetPerm(), etc.
* 30 = SHOW | READ | EDIT | DELETE
*/
const ALL = 30;
/**
* The root permission.
*/
const ROOT = -1;
/**
* Cache for integerToArray().
*
* @var array
*/
protected static $_itaCache = array();
/**
* Returns an hash of the available permissions.
*
* @return array The available permissions as a hash.
*/
public static function getPermsArray()
{
return array(
self::SHOW => Horde_Perms_Translation::t("Show"),
self::READ => Horde_Perms_Translation::t("Read"),
self::EDIT => Horde_Perms_Translation::t("Edit"),
self::DELETE => Horde_Perms_Translation::t("Delete")
);
}
/**
* Given an integer value of permissions returns an array representation
* of the integer.
*
* @param integer $int The integer representation of permissions.
*
* @return TODO
*/
public static function integerToArray($int)
{
if (isset(self::$_itaCache[$int])) {
return self::$_itaCache[$int];
}
self::$_itaCache[$int] = array();
/* Get the available perms array. */
$perms = self::getPermsArray();
/* Loop through each perm and check if its value is included in the
* integer representation. */
foreach ($perms as $val => $label) {
if ($int & $val) {
self::$_itaCache[$int][$val] = true;
}
}
return self::$_itaCache[$int];
}
}
Horde_Perms-2.1.7/locale/ar/LC_MESSAGES/Horde_Perms.mo 0000664 0001750 0001750 00000000574 12667632612 020247 0 ustar jan jan $ , 8 B 9 Project-Id-Version: Horde_Perms
Report-Msgid-Bugs-To: dev@lists.horde.org
POT-Creation-Date: 2010-10-13 01:27+0200
PO-Revision-Date: 2010-10-13 01:27+0200
Last-Translator: Automatically generated
Language-Team: i18n@lists.horde.org
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Horde_Perms-2.1.7/locale/ar/LC_MESSAGES/Horde_Perms.po 0000664 0001750 0001750 00000001533 12667632612 020246 0 ustar jan jan # Arabic translations for Horde_Perms module.
# Copyright 2010-2016 Horde LLC (http://www.horde.org/)
# This file is distributed under the same license as the Horde_Perms module.
# Automatically generated, 2010.
#
msgid ""
msgstr ""
"Project-Id-Version: Horde_Perms\n"
"Report-Msgid-Bugs-To: dev@lists.horde.org\n"
"POT-Creation-Date: 2010-10-13 01:27+0200\n"
"PO-Revision-Date: 2010-10-13 01:27+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: i18n@lists.horde.org\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: lib/Horde/Perms.php:181
msgid "All Permissions"
msgstr ""
#: lib/Horde/Perms.php:527
msgid "Delete"
msgstr ""
#: lib/Horde/Perms.php:526
msgid "Edit"
msgstr ""
#: lib/Horde/Perms.php:525
msgid "Read"
msgstr ""
#: lib/Horde/Perms.php:524
msgid "Show"
msgstr ""
Horde_Perms-2.1.7/locale/bg/LC_MESSAGES/Horde_Perms.mo 0000664 0001750 0001750 00000001150 12667632612 020224 0 ustar jan jan L | B * 7 H U All Permissions Delete Edit Read Show Project-Id-Version: Horde_Perms
Report-Msgid-Bugs-To: dev@lists.horde.org
POT-Creation-Date: 2010-10-13 01:27+0200
PO-Revision-Date: 2010-10-13 01:27+0200
Last-Translator: Automatically generated
Language-Team: i18n@lists.horde.org
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Всички права Изтрий Редакция Четене Показване Horde_Perms-2.1.7/locale/bg/LC_MESSAGES/Horde_Perms.po 0000664 0001750 0001750 00000001657 12667632612 020243 0 ustar jan jan # Bulgarian translations for Horde_Perms module.
# Copyright 2010-2016 Horde LLC (http://www.horde.org/)
# This file is distributed under the same license as the Horde_Perms module.
# Automatically generated, 2010.
#
msgid ""
msgstr ""
"Project-Id-Version: Horde_Perms\n"
"Report-Msgid-Bugs-To: dev@lists.horde.org\n"
"POT-Creation-Date: 2010-10-13 01:27+0200\n"
"PO-Revision-Date: 2010-10-13 01:27+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: i18n@lists.horde.org\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: lib/Horde/Perms.php:181
msgid "All Permissions"
msgstr "Всички права"
#: lib/Horde/Perms.php:527
msgid "Delete"
msgstr "Изтрий"
#: lib/Horde/Perms.php:526
msgid "Edit"
msgstr "Редакция"
#: lib/Horde/Perms.php:525
msgid "Read"
msgstr "Четене"
#: lib/Horde/Perms.php:524
msgid "Show"
msgstr "Показване"
Horde_Perms-2.1.7/locale/bs/LC_MESSAGES/Horde_Perms.mo 0000664 0001750 0001750 00000000737 12667632612 020252 0 ustar jan jan <