Horde_Perms is throwing exceptions.
authorJan Schneider <jan@horde.org>
Fri, 12 Mar 2010 16:22:09 +0000 (17:22 +0100)
committerJan Schneider <jan@horde.org>
Fri, 12 Mar 2010 16:22:09 +0000 (17:22 +0100)
horde/admin/perms/addchild.php
horde/admin/perms/delete.php
horde/admin/perms/edit.php

index bf6d9ef..9534404 100644 (file)
@@ -19,7 +19,7 @@ $perm_id = $vars->get('perm_id');
 
 try {
     $permission = $perms->getPermissionById($perm_id);
-} catch (Horde_Exception $e) {
+} catch (Exception $e) {
     $notification->push(_("Invalid parent permission."), 'horde.error');
     $url = Horde::applicationUrl('admin/perms/index.php', true);
     header('Location: ' . $url);
@@ -32,23 +32,23 @@ $ui->setVars($vars);
 $ui->setupAddForm($permission);
 
 if ($ui->validateAddForm($info)) {
-    if ($info['perm_id'] == Horde_Perms::ROOT) {
-        $child = $perms->newPermission($info['child']);
-        $result = $perms->addPermission($child);
-    } else {
-        $pOb = $perms->getPermissionById($info['perm_id']);
-        $name = $pOb->getName() . ':' . str_replace(':', '.', $info['child']);
-        $child = $perms->newPermission($name);
-        $result = $perms->addPermission($child);
-    }
-    if (is_a($result, 'PEAR_Error')) {
-        $notification->push(sprintf(_("\"%s\" was not created: %s."), $perms->getTitle($child->getName()), $result->getMessage()), 'horde.error');
-    } else {
+    try {
+        if ($info['perm_id'] == Horde_Perms::ROOT) {
+            $child = $perms->newPermission($info['child']);
+            $result = $perms->addPermission($child);
+        } else {
+            $pOb = $perms->getPermissionById($info['perm_id']);
+            $name = $pOb->getName() . ':' . str_replace(':', '.', $info['child']);
+            $child = $perms->newPermission($name);
+            $result = $perms->addPermission($child);
+        }
         $notification->push(sprintf(_("\"%s\" was added to the permissions system."), $perms->getTitle($child->getName())), 'horde.success');
         $url = Horde::applicationUrl('admin/perms/edit.php', true);
         $url = Horde_Util::addParameter($url, 'perm_id', $child->getId(), false);
         header('Location: ' . $url);
         exit;
+    } catch (Exception $e) {
+        $notification->push(sprintf(_("\"%s\" was not created: %s."), $perms->getTitle($child->getName()), $result->getMessage()), 'horde.error');
     }
 }
 
index 73ac744..35560b3 100644 (file)
@@ -17,10 +17,10 @@ $vars = Horde_Variables::getDefaultVariables();
 $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
 $perm_id = $vars->get('perm_id');
 $category = $vars->get('category');
-$permission = $perms->getPermissionById($perm_id);
-
-/* If the permission fetched is an error return to permissions list. */
-if (is_a($permission, 'PEAR_Error')) {
+try {
+    $permission = $perms->getPermissionById($perm_id);
+} catch (Exception $e) {
+    /* If the permission fetched is an error return to permissions list. */
     $notification->push(_("Attempt to delete a non-existent permission."), 'horde.error');
     $url = Horde::applicationUrl('admin/perms/index.php', true);
     header('Location: ' . $url);
@@ -33,14 +33,14 @@ $ui->setVars($vars);
 $ui->setupDeleteForm($permission);
 
 if ($confirmed = $ui->validateDeleteForm($info)) {
-    $result = $perms->removePermission($permission, true);
-    if (is_a($result, 'PEAR_Error')) {
-        $notification->push(sprintf(_("Unable to delete \"%s\": %s."), $perms->getTitle($permission->getName()), $result->getMessage()), 'horde.error');
-    } else {
+    try {
+        $result = $perms->removePermission($permission, true);
         $notification->push(sprintf(_("Successfully deleted \"%s\"."), $perms->getTitle($permission->getName())), 'horde.success');
         $url = Horde::applicationUrl('admin/perms/index.php', true);
         header('Location: ' . $url);
         exit;
+    } catch (Exception $e) {
+        $notification->push(sprintf(_("Unable to delete \"%s\": %s."), $perms->getTitle($permission->getName()), $result->getMessage()), 'horde.error');
     }
 } elseif ($confirmed === false) {
     $notification->push(sprintf(_("Permission \"%s\" not deleted."), $perms->getTitle($permission->getName())), 'horde.success');
index 9a61734..7fb2775 100644 (file)
@@ -9,6 +9,13 @@
  * @author Jan Schneider <jan@horde.org>
  */
 
+function _redirect()
+{
+    $GLOBALS['notification']->push(_("Attempt to edit a non-existent permission."), 'horde.error');
+    header('Location: ' . Horde::applicationUrl('admin/perms/index.php', true));
+    exit;
+}
+
 require_once dirname(__FILE__) . '/../../lib/Application.php';
 Horde_Registry::appInit('horde', array('admin' => true));
 
@@ -20,69 +27,71 @@ $category = $vars->get('category');
 
 /* See if we need to (and are supposed to) autocreate the permission. */
 if ($category !== null) {
-    $permission = $perms->getPermission($category);
-    if (is_a($permission, 'PEAR_Error') && Horde_Util::getFormData('autocreate')) {
-
-        /* Check to see if the permission we are copying from exists before we
-         * autocreate. */
-        $copyFrom = Horde_Util::getFormData('autocreate_copy');
-        if ($copyFrom && !$perms->exists($copyFrom)) {
-            $copyFrom = null;
-        }
-
-        $parent = $vars->get('parent');
-        $permission = $perms->newPermission($category);
-        $result = $perms->addPermission($permission, $parent);
-        if (!is_a($result, 'PEAR_Error')) {
-            $form = 'edit.inc';
-            $perm_id = $perms->getPermissionId($permission);
-        }
+    try {
+        $permission = $perms->getPermission($category);
+        $perm_id = $perms->getPermissionId($permission);
+    } catch (Horde_Perms_Exception $e) {
+        if (Horde_Util::getFormData('autocreate')) {
+            /* Check to see if the permission we are copying from exists
+             * before we autocreate. */
+            $copyFrom = Horde_Util::getFormData('autocreate_copy');
+            if ($copyFrom && !$perms->exists($copyFrom)) {
+                $copyFrom = null;
+            }
 
-        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);
+            $parent = $vars->get('parent');
+            $permission = $perms->newPermission($category);
+            try {
+                $result = $perms->addPermission($permission, $parent);
+                $form = 'edit.inc';
+                $perm_id = $perms->getPermissionId($permission);
+            } catch (Exception $e) {
             }
-            foreach ($copyFromObj->getGroupPermissions() as $group => $gperm) {
-                $permission->addGroupPermission($group, $gperm, false);
+
+            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 = Horde_Util::getFormData('autocreate_guest');
+                if ($addPerms) {
+                    $permission->addGuestPermission($addPerms, false);
+                }
+                $addPerms = Horde_Util::getFormData('autocreate_default');
+                if ($addPerms) {
+                    $permission->addDefaultPermission($addPerms, false);
+                }
+                $addPerms = Horde_Util::getFormData('autocreate_creator');
+                if ($addPerms) {
+                    $permission->addCreatorPermission($addPerms, false);
+                }
             }
+            $permission->save();
         } else {
-            /* We have autocreated the permission and we don't have an
-             * existing permission to copy.  See if some defaults were
-             * supplied. */
-            $addPerms = Horde_Util::getFormData('autocreate_guest');
-            if ($addPerms) {
-                $permission->addGuestPermission($addPerms, false);
-            }
-            $addPerms = Horde_Util::getFormData('autocreate_default');
-            if ($addPerms) {
-                $permission->addDefaultPermission($addPerms, false);
-            }
-            $addPerms = Horde_Util::getFormData('autocreate_creator');
-            if ($addPerms) {
-                $permission->addCreatorPermission($addPerms, false);
-            }
+            _redirect();
         }
-        $permission->save();
-    } else {
-        $perm_id = $perms->getPermissionId($permission);
+    } catch (Exception $e) {
+        _redirect();
     }
     $vars->set('perm_id', $perm_id);
 } else {
-    $permission = $perms->getPermissionById($perm_id);
-}
-
-/* If the permission fetched is an error return to the permissions list. */
-if (is_a($permission, 'PEAR_Error')) {
-    $notification->push(_("Attempt to edit a non-existent permission."), 'horde.error');
-    $url = Horde::applicationUrl('admin/perms/index.php', true);
-    header('Location: ' . $url);
-    exit;
+    try {
+        $permission = $perms->getPermissionById($perm_id);
+    } catch (Exception $e) {
+        _redirect();
+    }
 }
 
 $ui = new Horde_Perms_Ui($perms);