1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Perms_Sql extends Horde_Perms_Base
16: {
17: 18: 19: 20: 21:
22: protected $_params = array();
23:
24: 25: 26: 27: 28:
29: protected $_db;
30:
31: 32: 33: 34: 35:
36: private $_cacheVersion = 2;
37:
38: 39: 40: 41: 42:
43: protected $_permsCache = array();
44:
45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function __construct($params = array())
59: {
60: if (!isset($params['db'])) {
61: throw new Horde_Perms_Exception('Missing db parameter.');
62: }
63: $this->_db = $params['db'];
64: unset($params['db']);
65:
66: $this->_params = array_merge(array(
67: 'table' => 'horde_perms'
68: ), $this->_params, $params);
69:
70: parent::__construct($params);
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80: 81:
82: public function newPermission($name, $type = 'matrix', $params = null)
83: {
84: $ob = new Horde_Perms_Permission_Sql($name, $this->_cacheVersion, $type, $params);
85: $ob->setObs($this->_cache, $this->_db);
86: return $ob;
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function getPermission($name)
99: {
100: if (isset($this->_permsCache[$name])) {
101: return $this->_permsCache[$name];
102: }
103:
104: $perm = $this->_cache->get('perm_sql_' . $this->_cacheVersion . $name, $GLOBALS['conf']['cache']['default_lifetime']);
105: if (empty($perm)) {
106: $query = 'SELECT perm_id, perm_data FROM ' .
107: $this->_params['table'] . ' WHERE perm_name = ?';
108:
109: try {
110: $result = $this->_db->selectOne($query, array($name));
111: } catch (Horde_Db_Exception $e) {
112: throw new Horde_Perms_Exception($e);
113: }
114:
115: if (empty($result)) {
116: throw new Horde_Perms_Exception('Does not exist');
117: }
118:
119: $object = new Horde_Perms_Permission_Sql($name, $this->_cacheVersion);
120: $object->setId($result['perm_id']);
121: $object->setData(unserialize($result['perm_data']));
122:
123: $this->_cache->set('perm_sql_' . $this->_cacheVersion . $name, serialize($object));
124:
125: $this->_permsCache[$name] = $object;
126: } else {
127: $this->_permsCache[$name] = unserialize($perm);
128: }
129:
130: $this->_permsCache[$name]->setObs($this->_cache, $this->_db);
131:
132: return $this->_permsCache[$name];
133: }
134:
135: 136: 137: 138: 139: 140: 141: 142: 143:
144: public function getPermissionById($id)
145: {
146: if ($id == Horde_Perms::ROOT || empty($id)) {
147: $object = $this->newPermission(Horde_Perms::ROOT);
148: } else {
149: $query = 'SELECT perm_name, perm_data FROM ' .
150: $this->_params['table'] . ' WHERE perm_id = ?';
151:
152: try {
153: $result = $this->_db->selectOne($query, array($id));
154: } catch (Horde_Db_Exception $e) {
155: throw new Horde_Perms_Exception($e);
156: }
157:
158: if (empty($result)) {
159: throw new Horde_Perms_Exception('Does not exist');
160: }
161:
162: $object = new Horde_Perms_Permission_Sql($result['perm_name'], $this->_cacheVersion);
163: $object->setId($id);
164: $object->setData(unserialize($result['perm_data']));
165: $object->setObs($this->_cache, $this->_db);
166: }
167:
168: return $object;
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179: 180:
181: public function addPermission(Horde_Perms_Permission $perm)
182: {
183: $name = $perm->getName();
184: if (empty($name)) {
185: throw new Horde_Perms_Exception('Permission name must be non-empty.');
186: }
187:
188: $this->_cache->expire('perm_sql_' . $this->_cacheVersion . $name);
189: $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
190:
191:
192: $root = Horde_Perms::ROOT . ':';
193: if (substr($name, 0, strlen($root)) == ($root)) {
194: $name = substr($name, strlen($root));
195: }
196:
197:
198: $parents = '';
199: if (($pos = strrpos($name, ':')) !== false) {
200: $parent_name = substr($name, 0, $pos);
201: $query = 'SELECT perm_id, perm_parents FROM ' .
202: $this->_params['table'] . ' WHERE perm_name = ?';
203: $result = $this->_db->selectOne($query, array($parent_name));
204: if (empty($result)) {
205: throw new Horde_Perms_Exception(Horde_Perms_Translation::t("Trying to create sub permission of non-existant parent permission. Create parent permission(s) first."));
206: }
207: $parents = $result['perm_parents'] . ':' . $result['perm_id'];
208: }
209:
210: $query = 'INSERT INTO ' . $this->_params['table'] .
211: ' (perm_name, perm_parents) VALUES (?, ?)';
212:
213: try {
214: $id = $this->_db->insert($query, array($name, $parents));
215: } catch (Horde_Db_Exception $e) {
216: throw new Horde_Perms_Exception($e);
217: }
218:
219: $perm->setId($id);
220: $perm->save();
221:
222: return $id;
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function removePermission(Horde_Perms_Permission $perm,
237: $force = false)
238: {
239: $name = $perm->getName();
240: $this->_cache->expire('perm_sql_' . $this->_cacheVersion . $name);
241: $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
242:
243: $query = 'DELETE FROM ' . $this->_params['table'] .
244: ' WHERE perm_name = ?';
245:
246: try {
247: $result = $this->_db->delete($query, array($name));
248: } catch (Horde_Db_Exception $e) {
249: throw new Horde_Perms_Exception($e);
250: }
251:
252: if (!$force) {
253: return (bool)$result;
254: }
255:
256: $query = 'DELETE FROM ' . $this->_params['table'] .
257: ' WHERE perm_name LIKE ?';
258:
259: try {
260: return (bool)$this->_db->delete($query, array($name . ':%'));
261: } catch (Horde_Db_Exception $e) {
262: throw new Horde_Perms_Exception($e);
263: }
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273: 274:
275: public function getPermissionId($permission)
276: {
277: if ($permission->getName() == Horde_Perms::ROOT) {
278: return Horde_Perms::ROOT;
279: }
280:
281: $query = 'SELECT perm_id FROM ' . $this->_params['table'] .
282: ' WHERE perm_name = ?';
283:
284: try {
285: return $this->_db->selectValue($query, array($permission->getName()));
286: } catch (Horde_Db_Exception $e) {
287: throw new Horde_Perms_Exception($e);
288: }
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298:
299: public function exists($permission)
300: {
301: $key = 'perm_sql_exists_' . $this->_cacheVersion . $permission;
302: $exists = $this->_cache->get($key, $GLOBALS['conf']['cache']['default_lifetime']);
303: if ($exists === false) {
304: $query = 'SELECT COUNT(*) FROM ' . $this->_params['table'] .
305: ' WHERE perm_name = ?';
306:
307: try {
308: $exists = $this->_db->selectValue($query, array($permission));
309: } catch (Horde_Db_Exception $e) {
310: throw new Horde_Perms_Exception($e);
311: }
312:
313: $this->_cache->set($key, (string)$exists);
314: }
315:
316: return (bool)$exists;
317: }
318:
319: 320: 321: 322: 323: 324: 325: 326: 327:
328: public function getParent($child)
329: {
330: $query = 'SELECT perm_parents FROM ' . $this->_params['table'] .
331: ' WHERE perm_name = ?';
332:
333: try {
334: $parents = $this->_db->selectValue($query, array($child));
335: } catch (Horde_Db_Exception $e) {
336: throw new Horde_Perms_Exception($e);
337: }
338:
339: if (empty($parents)) {
340: return Horde_Perms::ROOT;
341: }
342:
343: $parents = explode(':', $parents);
344:
345: return array_pop($parents);
346: }
347:
348: 349: 350: 351: 352: 353: 354: 355:
356: public function getParents($child)
357: {
358: $query = 'SELECT perm_parents FROM ' . $this->_params['table'] .
359: ' WHERE perm_name = ?';
360:
361: try {
362: $result = $this->_db->selectValue($query, array($child));
363: } catch (Horde_Db_Exception $e) {
364: throw new Horde_Perms_Exception($e);
365: }
366:
367: if (empty($result)) {
368: throw new Horde_Perms_Exception('Does not exist');
369: }
370:
371: return $this->_getParents($result);
372: }
373:
374: 375: 376:
377: protected function _getParents($parents)
378: {
379: if (empty($parents)) {
380: return array(Horde_Perms::ROOT => true);
381: }
382:
383: $pname = $parents;
384: $parents = substr($parents, 0, strrpos($parents, ':'));
385:
386: return array($pname => $this->_getParents($parents));
387: }
388:
389: 390: 391: 392: 393: 394:
395: public function getTree()
396: {
397: $query = 'SELECT perm_id, perm_name FROM ' . $this->_params['table'] .
398: ' ORDER BY perm_name ASC';
399:
400: try {
401: $tree = $this->_db->selectAssoc($query);
402: } catch (Horde_Db_Exception $e) {
403: throw new Horde_Perms_Exception($e);
404: }
405:
406: $tree[Horde_Perms::ROOT] = Horde_Perms::ROOT;
407:
408: return $tree;
409: }
410: }
411: