File: edit.php

package info (click to toggle)
php-horde 5.2.1%2Bdebian0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,196 kB
  • sloc: php: 11,089; xml: 6,460; sh: 96; makefile: 33; sql: 1
file content (156 lines) | stat: -rw-r--r-- 5,933 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * Copyright 1999-2014 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL-2). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl.
 *
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @author   Jan Schneider <jan@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/lgpl LGPL-2
 * @package  Horde
 */

require_once __DIR__ . '/../../lib/Application.php';
Horde_Registry::appInit('horde', array(
    'permission' => array('horde:administration:perms')
));

/* Set up the form variables. */
$vars = $injector->getInstance('Horde_Variables');
$perms = $injector->getInstance('Horde_Perms');
$corePerms = $injector->getInstance('Horde_Core_Perms');
$perm_id = $vars->get('perm_id');
$category = $vars->get('category');

/* See if we need to (and are supposed to) autocreate the permission. */
$redirect = false;
if ($category !== null) {
    try {
        $permission = $perms->getPermission($category);
        $perm_id = $perms->getPermissionId($permission);
    } catch (Horde_Perms_Exception $e) {
        if ($vars->autocreate) {
            /* Check to see if the permission we are copying from exists
             * before we autocreate. */
            $copyFrom = $vars->autocreate_copy;
            if ($copyFrom && !$perms->exists($copyFrom)) {
                $copyFrom = null;
            }

            /* Create parents if necessary. Remove root from the name. */
            $root = Horde_Perms::ROOT . ':';
            if (substr($category, 0, strlen($root)) == ($root)) {
                $category = substr($category, strlen($root));
            }
            $pos = -1;
            while (($pos = strpos($category, ':', $pos + 1)) !== false) {
                $parent = substr($category, 0, $pos);
                if (!$perms->exists($parent)) {
                    try {
                        $permission = $corePerms->newPermission($parent);
                        $perms->addPermission($permission);
                        $permission->addDefaultPermission(Horde_Perms::ALL);
                    } catch (Exception $e) {
                        $notification->push($e);
                        Horde::url('admin/perms/index.php', true)->redirect();
                    }
                }
            }

            try {
                $permission = $corePerms->newPermission($category);
                $perms->addPermission($permission);
                $form = 'edit.inc';
                $perm_id = $perms->getPermissionId($permission);

                if ($copyFrom) {
                    /* We have autocreated the permission and we have been told
                     * to copy an existing permission for the defaults. */
                    $copyFromObj = $perms->getPermission($copyFrom);
                    $permission->addGuestPermission($copyFromObj->getGuestPermissions(), false);
                    $permission->addDefaultPermission($copyFromObj->getDefaultPermissions(), false);
                    $permission->addCreatorPermission($copyFromObj->getCreatorPermissions(), false);
                    foreach ($copyFromObj->getUserPermissions() as $user => $uperm) {
                        $permission->addUserPermission($user, $uperm, false);
                    }
                    foreach ($copyFromObj->getGroupPermissions() as $group => $gperm) {
                        $permission->addGroupPermission($group, $gperm, false);
                    }
                } else {
                    /* We have autocreated the permission and we don't have an
                     * existing permission to copy.  See if some defaults were
                     * supplied. */
                    $addPerms = $vars->autocreate_guest;
                    if ($addPerms) {
                        $permission->addGuestPermission($addPerms, false);
                    }
                    $addPerms = $vars->autocreate_default;
                    if ($addPerms) {
                        $permission->addDefaultPermission($addPerms, false);
                    }
                    $addPerms = $vars->autocreate_creator;
                    if ($addPerms) {
                        $permission->addCreatorPermission($addPerms, false);
                    }
                }
                $permission->save();
            } catch (Exception $e) {
                $notification->push($e);
                Horde::url('admin/perms/index.php', true)->redirect();
            }
        } else {
            $redirect = true;
        }
    } catch (Exception $e) {
        $redirect = true;
    }
    $vars->set('perm_id', $perm_id);
} else {
    try {
        $permission = $perms->getPermissionById($perm_id);
    } catch (Exception $e) {
        $redirect = true;
    }
}

if ($redirect) {
    $notification->push(_("Attempt to edit a non-existent permission."), 'horde.error');
    Horde::url('admin/perms/index.php', true)->redirect();
}

$ui = new Horde_Core_Perms_Ui($perms, $corePerms);
$ui->setVars($vars);
$ui->setupEditForm($permission);

if ($ui->validateEditForm($info)) {
    /* Update and save the permissions. */
    $permission->updatePermissions($info);
    $permission->save();
    $notification->push(sprintf(_("Updated \"%s\"."), $corePerms->getTitle($permission->getName())), 'horde.success');
    Horde::url('admin/perms/edit.php', true)
        ->add('perm_id', $permission->getId())
        ->redirect();
}

// Buffer the tree rendering
Horde::startBuffer();
$ui->renderForm('edit.php');
echo '<br />';
$ui->renderTree($perm_id);
$tree_output = Horde::endBuffer();

// Buffer the menu output
Horde::startBuffer();
require HORDE_TEMPLATES . '/admin/menu.inc';
$menu_output = Horde::endBuffer();

$page_output->header(array(
    'title' => _("Permissions Administration")
));

/* Render the form and tree. */
echo $menu_output;
echo $tree_output;
$page_output->footer();