1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Core_Perms_Ui
16: {
17: 18: 19: 20: 21:
22: protected $_perms;
23:
24: 25: 26: 27: 28:
29: protected $_corePerms;
30:
31: 32: 33: 34: 35:
36: protected $_form = null;
37:
38: 39: 40: 41: 42:
43: protected $_vars = null;
44:
45: 46: 47: 48: 49:
50: protected $_type = 'matrix';
51:
52: 53: 54: 55: 56: 57:
58: public function __construct(Horde_Perms_Base $perms,
59: Horde_Core_Perms $corePerms)
60: {
61: $this->_perms = $perms;
62: $this->_corePerms = $corePerms;
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function renderTree($current = Horde_Perms::ROOT)
72: {
73: global $registry;
74:
75:
76: $nodes = $this->_perms->getTree();
77:
78: $perms_node = array('icon' => Horde_Themes::img('perms.png'));
79: $add = Horde::url('admin/perms/addchild.php');
80: $add_img = Horde::img('add_perm.png', Horde_Core_Translation::t("Add Permission"));
81: $edit = Horde::url('admin/perms/edit.php');
82: $delete = Horde::url('admin/perms/delete.php');
83: $edit_img = Horde::img('edit.png', Horde_Core_Translation::t("Edit Permission"));
84: $delete_img = Horde::img('delete.png', Horde_Core_Translation::t("Delete Permission"));
85: $blank_img = Horde::img('blank.gif', '', array('width' => 16, 'height' => 16));
86:
87:
88: $tree = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Tree')->create('perms_ui', 'Javascript', array(
89: 'alternate' => true,
90: 'hideHeaders' => true
91: ));
92: $tree->setHeader(array(
93: array(
94: 'class' => 'treeHdrSpacer'
95: )
96: ));
97:
98: foreach ($nodes as $perm_id => $node) {
99: $node_class = ($current == $perm_id)
100: ? array('class' => 'selected')
101: : array();
102: if ($perm_id == Horde_Perms::ROOT) {
103: $add_link = $add->add('perm_id', $perm_id)->link(array('class' => 'permsAdd', 'title' => Horde_Core_Translation::t("Add New Permission"))) . $add_img . '</a>';
104: $base_node_params = array('icon' => Horde_Themes::img('administration.png'));
105:
106: $tree->addNode($perm_id, null, Horde_Core_Translation::t("All Permissions"), 0, true,
107: $base_node_params + $node_class,
108: array($add_link));
109: } else {
110: $parent_id = $this->_perms->getParent($node);
111:
112: $perms_extra = array();
113: $parents = explode(':', $node);
114:
115: if (!in_array($parents[0], $GLOBALS['registry']->listApps(array('notoolbar', 'active', 'hidden')))) {
116:
117:
118:
119:
120: continue;
121: }
122:
123: try {
124: $app_perms = $this->_corePerms->getApplicationPermissions($parents[0]);
125: } catch (Horde_Perms_Exception $e) {
126: $GLOBALS['notification']->push($e);
127: continue;
128: }
129:
130: if (isset($app_perms['tree']) &&
131: is_array(Horde_Array::getElement($app_perms['tree'], $parents))) {
132: $add_link = $add->add('perm_id', $perm_id)->link(array('class' => 'permsAdd', 'title' => Horde_Core_Translation::t("Add Child Permission"))) . $add_img . '</a>';
133: $perms_extra[] = $add_link;
134: } else {
135: $perms_extra[] = $blank_img;
136: }
137:
138: $edit_link = $edit->add('perm_id', $perm_id)->link(array('class' => 'permsEdit', 'title' => Horde_Core_Translation::t("Edit Permission"))) . $edit_img . '</a>';
139: $perms_extra[] = $edit_link;
140: $delete_link = $delete->add('perm_id', $perm_id)->link(array('class' => 'permsDelete', 'title' => Horde_Core_Translation::t("Delete Permission"))) . $delete_img . '</a>';
141: $perms_extra[] = $delete_link;
142: $name = $this->_corePerms->getTitle($node);
143:
144: $expanded = isset($nodes[$current]) &&
145: strpos($nodes[$current], $node) === 0 &&
146: $nodes[$current] != $node;
147: $tree->addNode($perm_id, $parent_id, $name,
148: substr_count($node, ':') + 1, $expanded,
149: $perms_node + $node_class, $perms_extra);
150: }
151: }
152:
153: $tree->sort('label');
154:
155: return $tree->renderTree();
156: }
157:
158: 159: 160: 161: 162:
163: public function setForm(&$form)
164: {
165: $this->_form = $form;
166: }
167:
168: 169: 170: 171: 172: 173:
174: public function setVars($vars)
175: {
176: $this->_vars = $vars;
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186: 187:
188: public function setupAddForm($permission, $force_choice = null)
189: {
190:
191: $this->_formInit();
192:
193: $this->_form->setTitle(sprintf(Horde_Core_Translation::t("Add a child permission to \"%s\""), $this->_corePerms->getTitle($permission->getName())));
194: $this->_form->setButtons(Horde_Core_Translation::t("Add"));
195: $this->_vars->set('perm_id', $this->_perms->getPermissionId($permission));
196: $this->_form->addHidden('', 'perm_id', 'text', false);
197:
198:
199: $child_perms = $this->_corePerms->getAvailable($permission->getName());
200: if ($child_perms === false) {
201:
202: $this->_form->addVariable(Horde_Core_Translation::t("Permission"), 'child', 'invalid', true, false, null, array(Horde_Core_Translation::t("No children can be added to this permission.")));
203: } elseif (is_array($child_perms)) {
204: if (!empty($force_choice)) {
205:
206: $this->_vars->set('child', $force_choice);
207: $this->_form->addVariable(Horde_Core_Translation::t("Permissions"), 'child', 'enum', true, true, null, array($child_perms));
208: } else {
209:
210: $prefix = $permission->getName() . ':';
211: $length = strlen($prefix);
212: foreach ($this->_perms->getTree() as $name) {
213: if (strpos($name, $prefix) === 0) {
214: unset($child_perms[substr($name, $length)]);
215: }
216: }
217: $this->_form->addVariable(Horde_Core_Translation::t("Permissions"), 'child', 'enum', true, false, null, array($child_perms));
218: }
219: }
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229:
230: public function validateAddForm(&$info)
231: {
232: if (!$this->_form->validate($this->_vars)) {
233: return false;
234: }
235:
236: $this->_form->getInfo($this->_vars, $info);
237: return true;
238: }
239:
240: 241: 242: 243: 244:
245: public function setupEditForm($permission)
246: {
247: global $registry;
248:
249:
250: $this->_formInit();
251:
252: $this->_form->setButtons(Horde_Core_Translation::t("Update"), true);
253: $perm_id = $this->_perms->getPermissionId($permission);
254: $this->_form->addHidden('', 'perm_id', 'text', false);
255:
256:
257: $this->_type = $permission->get('type');
258: $params = $permission->get('params');
259:
260:
261: $perm_val = $permission->getDefaultPermissions();
262: $this->_form->setSection('default', Horde_Core_Translation::t("All Authenticated Users"), Horde::img('perms.png'), false);
263:
264: 265:
266: if ($this->_type == 'matrix') {
267:
268: $cols = Horde_Perms::getPermsArray();
269:
270:
271: $matrix = array(Horde_Perms::integerToArray($perm_val));
272: $this->_form->addVariable('', 'deflt', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
273: } else {
274: $var = $this->_form->addVariable('', 'deflt', $this->_type, false, false, null, $params);
275: $var->setDefault($perm_val);
276: }
277:
278:
279: $perm_val = $permission->getGuestPermissions();
280: $this->_form->setSection('guest', Horde_Core_Translation::t("Guest Permissions"), '', false);
281:
282: if ($this->_type == 'matrix') {
283:
284: $matrix = array(Horde_Perms::integerToArray($perm_val));
285: $this->_form->addVariable('', 'guest', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
286: } else {
287: $var = $this->_form->addVariable('', 'guest', $this->_type, false, false, null, $params);
288: $var->setDefault($perm_val);
289: }
290:
291:
292: $perm_val = $permission->getCreatorPermissions();
293: $this->_form->setSection('creator', Horde_Core_Translation::t("Creator Permissions"), Horde::img('user.png'), false);
294:
295: if ($this->_type == 'matrix') {
296:
297: $matrix = array(Horde_Perms::integerToArray($perm_val));
298: $this->_form->addVariable('', 'creator', 'matrix', false, false, null, array($cols, array(0 => ''), $matrix));
299: } else {
300: $var = $this->_form->addVariable('', 'creator', $this->_type, false, false, null, $params);
301: $var->setDefault($perm_val);
302: }
303:
304:
305: $perm_val = $permission->getUserPermissions();
306: $this->_form->setSection('users', Horde_Core_Translation::t("Individual Users"), Horde::img('user.png'), false);
307: $auth = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Auth')->create();
308: if ($auth->hasCapability('list')) {
309: 310: 311:
312: $new_users = array();
313:
314: try {
315: $user_list = $auth->listUsers();
316: sort($user_list);
317: foreach ($user_list as $user) {
318: if (!isset($perm_val[$user])) {
319: $new_users[$user] = $user;
320: }
321: }
322: } catch (Horde_Auth_Exception $e) {
323: $new_users = true;
324: }
325: } else {
326: 327:
328: $new_users = true;
329: }
330:
331: if ($this->_type == 'matrix') {
332: 333: 334:
335: $rows = array();
336: $matrix = array();
337: foreach ($perm_val as $u_id => $u_perms) {
338: $rows[$u_id] = $u_id;
339: $matrix[$u_id] = Horde_Perms::integerToArray($u_perms);
340: }
341: $this->_form->addVariable('', 'u', 'matrix', false, false, null, array($cols, $rows, $matrix, $new_users));
342: } else {
343: if ($new_users) {
344: if (is_array($new_users)) {
345: $u_n = Horde_Util::getFormData('u_n');
346: $u_n = empty($u_n['u']) ? null : $u_n['u'];
347: $user_html = '<select name="u_n[u]"><option value="">' . Horde_Core_Translation::t("-- select --") . '</option>';
348: foreach ($new_users as $new_user) {
349: $user_html .= '<option value="' . $new_user . '"';
350: $user_html .= $u_n == $new_user ? ' selected="selected"' : '';
351: $user_html .= '>' . htmlspecialchars($new_user) . '</option>';
352: }
353: $user_html .= '</select>';
354: } else {
355: $user_html = '<input type="text" name="u_n[u]" />';
356: }
357: $this->_form->addVariable($user_html, 'u_n[v]', $this->_type, false, false, null, $params);
358: }
359: foreach ($perm_val as $u_id => $u_perms) {
360: $var = $this->_form->addVariable($u_id, 'u_v[' . $u_id . ']', $this->_type, false, false, null, $params);
361: $var->setDefault($u_perms);
362: }
363: }
364:
365:
366: $perm_val = $permission->getGroupPermissions();
367: $this->_form->setSection('groups', Horde_Core_Translation::t("Groups"), Horde::img('group.png'), false);
368: try {
369: $group_list = $GLOBALS['injector']
370: ->getInstance('Horde_Group')
371: ->listAll();
372: } catch (Horde_Group_Exception $e) {
373: $GLOBALS['notification']->push($e);
374: $group_list = array();
375: }
376:
377: if (!empty($group_list)) {
378: 379: 380:
381: $new_groups = array();
382: foreach ($group_list as $groupId => $group) {
383: if (!isset($perm_val[$groupId])) {
384: $new_groups[$groupId] = $group;
385: }
386: }
387: } else {
388:
389: $new_groups = false;
390: }
391:
392: if ($this->_type == 'matrix') {
393: 394:
395: $rows = array();
396: $matrix = array();
397: foreach ($perm_val as $g_id => $g_perms) {
398: $rows[$g_id] = isset($group_list[$g_id]) ? $group_list[$g_id] : $g_id;
399: $matrix[$g_id] = Horde_Perms::integerToArray($g_perms);
400: }
401: $this->_form->addVariable('', 'g', 'matrix', false, false, null, array($cols, $rows, $matrix, $new_groups));
402: } else {
403: if ($new_groups) {
404: if (is_array($new_groups)) {
405: $g_n = Horde_Util::getFormData('g_n');
406: $g_n = empty($g_n['g']) ? null : $g_n['g'];
407: $group_html = '<select name="g_n[g]"><option value="">' . Horde_Core_Translation::t("-- select --") . '</option>';
408: foreach ($new_groups as $groupId => $group) {
409: $group_html .= '<option value="' . $groupId . '"';
410: $group_html .= $g_n == $groupId ? ' selected="selected"' : '';
411: $group_html .= '>' . htmlspecialchars($group) . '</option>';
412: }
413: $group_html .= '</select>';
414: } else {
415: $group_html = '<input type="text" name="g_n[g]" />';
416: }
417: $this->_form->addVariable($group_html, 'g_n[v]', $this->_type, false, false, null, $params);
418: }
419: foreach ($perm_val as $g_id => $g_perms) {
420: $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);
421: $var->setDefault($g_perms);
422: }
423: }
424:
425:
426: $this->_form->setTitle(sprintf(Horde_Core_Translation::t("Edit permissions for \"%s\""), $this->_corePerms->getTitle($permission->getName())));
427: }
428:
429: 430: 431: 432: 433: 434:
435: public function validateEditForm(&$info)
436: {
437: if (!$this->_form->validate($this->_vars)) {
438: return false;
439: }
440:
441: $this->_form->getInfo($this->_vars, $info);
442:
443: if ($this->_type == 'matrix') {
444:
445: $info['deflt'] = isset($info['deflt'][0])
446: ? $info['deflt'][0]
447: : null;
448: $info['guest'] = isset($info['guest'][0])
449: ? $info['guest'][0]
450: : null;
451: $info['creator'] = isset($info['creator'][0])
452: ? $info['creator'][0]
453: : null;
454: } else {
455: $u_n = $this->_vars->get('u_n');
456: $info['u'] = array();
457: if (!empty($u_n['u'])) {
458: $info['u'][$u_n['u']] = $info['u_n']['v'];
459: }
460: unset($info['u_n']);
461: if (isset($info['u_v'])) {
462: $info['u'] += $info['u_v'];
463: unset($info['u_v']);
464: }
465: $g_n = $this->_vars->get('g_n');
466: $info['g'] = array();
467: if (!empty($g_n['g'])) {
468: $info['g'][$g_n['g']] = $info['g_n']['v'];
469: }
470: unset($info['g_n']);
471: if (isset($info['g_v'])) {
472: $info['g'] += $info['g_v'];
473: unset($info['g_v']);
474: }
475: }
476: $info['default'] = $info['deflt'];
477: unset($info['deflt']);
478:
479: return true;
480: }
481:
482: 483: 484: 485: 486:
487: public function setupDeleteForm($permission)
488: {
489:
490: $this->_formInit();
491:
492: $this->_form->setTitle(sprintf(Horde_Core_Translation::t("Delete permissions for \"%s\""), $this->_corePerms->getTitle($permission->getName())));
493: $this->_form->setButtons(array(Horde_Core_Translation::t("Delete"), Horde_Core_Translation::t("Do not delete")));
494: $this->_form->addHidden('', 'perm_id', 'text', false);
495: $this->_form->addVariable(sprintf(Horde_Core_Translation::t("Delete permissions for \"%s\" and any sub-permissions?"), $this->_corePerms->getTitle($permission->getName())), 'prompt', 'description', false);
496: }
497:
498: 499: 500: 501: 502: 503: 504: 505: 506:
507: public function validateDeleteForm(&$info)
508: {
509: $form_submit = $this->_vars->get('submitbutton');
510:
511: if ($form_submit == Horde_Core_Translation::t("Delete")) {
512: if ($this->_form->validate($this->_vars)) {
513: $this->_form->getInfo($this->_vars, $info);
514: return true;
515: }
516: } elseif (!empty($form_submit)) {
517: return false;
518: }
519:
520: return null;
521: }
522:
523: 524: 525:
526: public function renderForm($form_script = 'edit.php')
527: {
528: $renderer = new Horde_Form_Renderer();
529: $this->_form->renderActive($renderer, $this->_vars, $form_script, 'post');
530: }
531:
532: 533: 534:
535: protected function _formInit()
536: {
537: if (is_null($this->_vars)) {
538:
539: $this->_vars = Horde_Variables::getDefaultVariables();
540: }
541:
542: if (!($this->_form instanceof Horde_Form)) {
543:
544: $this->_form = new Horde_Form($this->_vars);
545: }
546: }
547:
548: }
549: