Fix/cleanup app permissions checking
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 30 Jul 2010 20:29:55 +0000 (14:29 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 30 Jul 2010 21:04:34 +0000 (15:04 -0600)
12 files changed:
framework/Block/lib/Horde/Block/Layout/Manager.php
framework/Core/lib/Horde.php
framework/Perms/lib/Horde/Perms.php
horde/lib/Application.php
horde/templates/portal/edit.inc
imp/lib/Application.php
ingo/lib/Application.php
kronolith/lib/Application.php
mnemo/lib/Application.php
mnemo/lib/Mnemo.php
mnemo/memo.php
nag/lib/Application.php

index d42bc9f..743aae3 100644 (file)
@@ -235,12 +235,13 @@ class Horde_Block_Layout_Manager extends Horde_Block_Layout
                 !$this->rowExists($row) ||
                 !$this->colExists($col)) {
                 // Check permissions.
-                if (Horde::hasPermission('max_blocks') !== true &&
-                    Horde::hasPermission('max_blocks') <= $this->count()) {
+                $max_blocks = $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_blocks');
+                if (($max_blocks !== true) &&
+                    ($max_blocks <= $this->count())) {
                     try {
                         $message = Horde::callHook('perms_denied', array('horde:max_blocks'));
                     } catch (Horde_Exception_HookNotSet $e) {
-                        $message = @htmlspecialchars(sprintf(ngettext("You are not allowed to create more than %d block.", "You are not allowed to create more than %d blocks.", Horde::hasPermission('max_blocks')), Horde::hasPermission('max_blocks')), ENT_COMPAT, $GLOBALS['registry']->getCharset());
+                        $message = @htmlspecialchars(sprintf(ngettext("You are not allowed to create more than %d block.", "You are not allowed to create more than %d blocks.", $max_blocks), $max_blocks), ENT_COMPAT, $GLOBALS['registry']->getCharset());
                     }
                     $GLOBALS['notification']->push($message, 'horde.error', array('content.raw'));
                     break;
index 19e71b4..f67df8c 100644 (file)
@@ -1737,33 +1737,6 @@ HTML;
     }
 
     /**
-     * Returns the specified permission for the current user.
-     *
-     * @param string $permission  A permission, currently only 'max_blocks'.
-     *
-     * @return mixed  The value of the specified permission.
-     */
-    static public function hasPermission($permission)
-    {
-        $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
-
-        if (!$perms->exists('horde:' . $permission)) {
-            return true;
-        }
-
-        $allowed = $perms->getPermissions('horde:' . $permission);
-        if (is_array($allowed)) {
-            switch ($permission) {
-            case 'max_blocks':
-                $allowed = max($allowed);
-                break;
-            }
-        }
-
-        return $allowed;
-    }
-
-    /**
      * Utility function to send redirect headers to browser, handling any
      * browser quirks.
      *
index 427c528..8864473 100644 (file)
@@ -450,11 +450,17 @@ class Horde_Perms
             : $GLOBALS['registry']->getApp();
 
         if ($this->exists($app . ':' . $permission)) {
-            $args = array($this->getPermissions($app . ':' . $permission));
-            if (isset($opts['opts'])) {
-                $args[] = $opts['opts'];
+            $perms = $this->getPermissions($app . ':' . $permission);
+            if ($perms === false) {
+                return false;
             }
 
+            $args = array(
+                $permission,
+                $perms,
+                isset($opts['opts']) ? $opts['opts'] : array()
+            );
+
             try {
                 return $GLOBALS['registry']->callAppMethod($app, 'hasPermission', array('args' => $args));
             } catch (Horde_Exception $e) {}
index 2f9218f..912bdd5 100644 (file)
@@ -43,6 +43,26 @@ class Horde_Application extends Horde_Registry_Application
     }
 
     /**
+     * Returns the specified permission for the given app permission.
+     *
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options (NONE).
+     *
+     * @return mixed  The value of the specified permission.
+     */
+    public function hasPermission($permission, $allowed, $opts = array())
+    {
+        switch ($permission) {
+        case 'max_blocks':
+            $allowed = max($allowed);
+            break;
+        }
+
+        return $allowed;
+    }
+
+    /**
      * Populate dynamically-generated preference values.
      *
      * @param Horde_Core_Prefs_Ui $ui  The UI object.
index 4cf5265..811f8c6 100644 (file)
@@ -1,10 +1,11 @@
 <table class="nopadding" cellspacing="6" width="100%">
 <?php
 $rows = $layout->rows();
+$max_blocks = $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_blocks');
 list($current_row, $current_col) = $layout->getCurrentBlock();
 $allow_add = !empty($conf['hooks']['permsdenied']) ||
-    (Horde::hasPermission('max_blocks') === true) ||
-    (Horde::hasPermission('max_blocks') > $layout->count());
+    ($max_blocks === true) ||
+    ($max_blocks > $layout->count());
 $columns = 0;
 for ($row = 0; $row < $rows; ++$row):
 ?>
index abaef47..7c20488 100644 (file)
@@ -178,27 +178,26 @@ class IMP_Application extends Horde_Registry_Application
     }
 
     /**
-     * Returns the specified permission for the current user.
+     * Returns the specified permission for the given app permission.
      *
-     * @param mixed $allowed  The allowed permissions.
-     * @param array $opts     Additinal options ('value').
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options ('value').
      *
      * @return mixed  The value of the specified permission.
      */
-    public function hasPermission($allowed, $opts = array())
+    public function hasPermission($permission, $allowed, $opts = array())
     {
-        if (is_array($allowed)) {
-            switch ($permission) {
-            case 'create_folders':
-                $allowed = (bool)count(array_filter($allowed));
-                break;
-
-            case 'max_folders':
-            case 'max_recipients':
-            case 'max_timelimit':
-                $allowed = max($allowed);
-                break;
-            }
+        switch ($permission) {
+        case 'create_folders':
+            $allowed = (bool)count(array_filter($allowed));
+            break;
+
+        case 'max_folders':
+        case 'max_recipients':
+        case 'max_timelimit':
+            $allowed = max($allowed);
+            break;
         }
 
         return (($permission == 'max_folders') && empty($opts['value']))
index c573162..94042fc 100644 (file)
@@ -119,24 +119,24 @@ class Ingo_Application extends Horde_Registry_Application
     }
 
     /**
-     * Returns the specified permission for the current user.
+     * Returns the specified permission for the given app permission.
      *
-     * @param mixed $allowed  The allowed permissions.
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options (NONE).
      *
      * @return mixed  The value of the specified permission.
      */
-    public function hasPermission($allowed)
+    public function hasPermission($permission, $allowed, $opts = array())
     {
-        if (is_array($allowed)) {
-            switch ($permission) {
-            case 'allow_rules':
-                $allowed = (bool)count(array_filter($allowed));
-                break;
-
-            case 'max_rules':
-                $allowed = max($allowed);
-                break;
-            }
+        switch ($permission) {
+        case 'allow_rules':
+            $allowed = (bool)count(array_filter($allowed));
+            break;
+
+        case 'max_rules':
+            $allowed = max($allowed);
+            break;
         }
 
         return $allowed;
index fce6980..9963db8 100644 (file)
@@ -89,20 +89,20 @@ class Kronolith_Application extends Horde_Registry_Application
     }
 
     /**
-     * Returns the specified permission for the current user.
+     * Returns the specified permission for the given app permission.
      *
-     * @param mixed $allowed  The allowed permissions.
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options (NONE).
      *
      * @return mixed  The value of the specified permission.
      */
-    public function hasPermission($allowed)
+    public function hasPermission($permission, $allowed, $opts = array())
     {
-        if (is_array($allowed)) {
-            switch ($permission) {
-            case 'max_events':
-                $allowed = max($allowed);
-                break;
-            }
+        switch ($permission) {
+        case 'max_events':
+            $allowed = max($allowed);
+            break;
         }
 
         return $allowed;
index a7a00fe..a45754c 100644 (file)
@@ -64,7 +64,27 @@ class Mnemo_Application extends Horde_Registry_Application
         $perms['title']['mnemo:max_notes'] = _("Maximum Number of Notes");
         $perms['type']['mnemo:max_notes'] = 'int';
 
-        return $perms;      
+        return $perms;
+    }
+
+    /**
+     * Returns the specified permission for the given app permission.
+     *
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options (NONE).
+     *
+     * @return mixed  The value of the specified permission.
+     */
+    public function hasPermission($permission, $allowed, $opts = array())
+    {
+        switch ($permission) {
+        case 'max_notes':
+            $allowed = max($allowed);
+            break;
+        }
+
+        return $allowed;
     }
 
     /**
index 0ce11c9..9900824 100644 (file)
@@ -22,32 +22,32 @@ class Mnemo {
      * Sort by memo description.
      */
     const SORT_DESC = 0;
-    
+
     /**
      * Sort by memo category.
      */
     const SORT_CATEGORY = 1;
-    
+
     /**
      * Sort by notepad.
      */
     const SORT_NOTEPAD = 2;
-    
+
     /**
      * Sort in ascending order.
      */
     const SORT_ASCEND = 0;
-    
+
     /**
      * Sort in descending order.
      */
     const SORT_DESCEND = 1;
-    
+
     /**
      * No passphrase provided.
      */
     const ERR_NO_PASSPHRASE = 100;
-    
+
     /**
      * Decrypting failed
      */
index 767f986..df41463 100644 (file)
@@ -71,9 +71,9 @@ $cManager = new Horde_Prefs_CategoryManager();
 switch ($actionID) {
 case 'add_memo':
     /* Check permissions. */
-    if ($GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_notes') !== true &&
-        $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_notes') <= Mnemo::countMemos()) {
-        $message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d notes."), Mnemo::hasPermission('max_notes')), ENT_COMPAT, $GLOBALS['registry']->getCharset());
+    if ($injector->getInstance('Horde_Perms')->hasAppPermission('max_notes') !== true &&
+        $injector->getInstance('Horde_Perms')->hasAppPermission('max_notes') <= Mnemo::countMemos()) {
+        $message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d notes."), $injector->getInstance('Horde_Perms')->hasAppPermission('max_notes')), ENT_COMPAT, $registry->getCharset());
         if (!empty($conf['hooks']['permsdenied'])) {
             $message = Horde::callHook('_perms_hook_denied', array('mnemo:max_notes'), 'horde', $message);
         }
@@ -130,12 +130,12 @@ case 'save_memo':
     $memo_passphrase2 = Horde_Util::getFormData('memo_passphrase2');
 
     try {
-        $share = $GLOBALS['mnemo_shares']->getShare($notepad_target);
+        $share = $mnemo_shares->getShare($notepad_target);
     } catch (Horde_Share_Exception $e) {
         throw new Mnemo_Exception($e);
     }
 
-    if (!$share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
+    if (!$share->hasPermission($registry->getAuth(), Horde_Perms::EDIT)) {
         $notification->push(sprintf(_("Access denied saving note to %s."), $share->get('name')), 'horde.error');
     } elseif ($memo_passphrase != $memo_passphrase2) {
         $notification->push(_("The passwords don't match."), 'horde.error');
@@ -173,17 +173,17 @@ case 'save_memo':
             if ($memolist_original != $notepad_target) {
                 /* Moving the note to another notepad. */
                 try {
-                    $share = $GLOBALS['mnemo_shares']->getShare($memolist_original);
+                    $share = $mnemo_shares->getShare($memolist_original);
                 } catch (Horde_Share_Exception $e) {
                     throw new Mnemo_Exception($e);
                 }
-                if ($share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE)) {
+                if ($share->hasPermission($registry->getAuth(), Horde_Perms::DELETE)) {
                     try {
-                        $share = &$GLOBALS['mnemo_shares']->getShare($notepad_target);
+                        $share = &$mnemo_shares->getShare($notepad_target);
                     } catch (Horde_Share_Exception $e) {
                         throw new Mnemo_Exception($e);
                     }
-                    if ($share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
+                    if ($share->hasPermission($registry->getAuth(), Horde_Perms::EDIT)) {
                         $result = $storage->move($memo_id, $notepad_target);
                         $storage = &Mnemo_Driver::singleton($notepad_target);
                     } else {
@@ -201,8 +201,8 @@ case 'save_memo':
             $result = $storage->modify($memo_id, $memo_desc, $memo_body, $memo_category, $memo_passphrase);
         } else {
             /* Check permissions. */
-            if ($GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_notes') !== true &&
-                $GLOBALS['injector']->getInstance('Horde_Perms')->hasAppPermission('max_notes') <= Mnemo::countMemos()) {
+            if ($injector->getInstance('Horde_Perms')->hasAppPermission('max_notes') !== true &&
+                $injector->getInstance('Horde_Perms')->hasAppPermission('max_notes') <= Mnemo::countMemos()) {
                 header('Location: ' . Horde::applicationUrl('list.php', true));
                 exit;
             }
@@ -233,11 +233,11 @@ case 'delete_memos':
 
     if (!is_null($memo_id) && Mnemo::getMemo($memolist_id, $memo_id)) {
         try {
-            $share = $GLOBALS['mnemo_shares']->getShare($memolist_id);
+            $share = $mnemo_shares->getShare($memolist_id);
         } catch (Horde_Share_Exception $e) {
             throw new Mnemo_Exception($e);
         }
-        if ($share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE)) {
+        if ($share->hasPermission($registry->getAuth(), Horde_Perms::DELETE)) {
             $storage = &Mnemo_Driver::singleton($memolist_id);
             $result = $storage->delete($memo_id);
             if ($result instanceof PEAR_Error) {
index 38fb261..95ee7f8 100644 (file)
@@ -72,20 +72,20 @@ class Nag_Application extends Horde_Registry_Application
     }
 
     /**
-     * Returns the specified permission for the current user.
+     * Returns the specified permission for the given app permission.
      *
-     * @param mixed $allowed  The allowed permissions.
+     * @param string $permission  The permission to check.
+     * @param mixed $allowed      The allowed permissions.
+     * @param array $opts         Additional options (NONE).
      *
      * @return mixed  The value of the specified permission.
      */
-    public function hasPermission($allowed)
+    public function hasPermission($permission, $allowed, $opts = array())
     {
-       if (is_array($allowed)) {
-            switch ($permission) {
-            case 'max_tasks':
-                $allowed = max($allowed);
-                break;
-            }
+        switch ($permission) {
+        case 'max_tasks':
+            $allowed = max($allowed);
+            break;
         }
 
         return $allowed;