/**
* 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 = '';
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;
}