Changeset 457 for trunk/lib/ACL.inc.php


Ignore:
Timestamp:
Jan 20, 2014 9:42:13 PM (10 years ago)
Author:
anonymous
Message:

Removed use of requireAccessClearance(). Adjusted sequence of sslOn() and requireLogin(). Added ACL::requireAllow() method. Added arguments to SortOrder::set(). Changed behavior of Validator::validateStrDate(). Added use of Validator::validateStrDate() to module maker templates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/ACL.inc.php

    r420 r457  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    2323/*
    2424* ACL.inc.php
    25 * 
     25*
    2626* Uses the ARO/ACO/AXO model of Access Control Lists.
    2727* Uses Modified Preorder Tree Traversal to maintain a tree-structure.
    2828* See: http://www.sitepoint.com/print/hierarchical-data-database
    2929* Includes a command-line tool for managing rights (codebase/bin/acl.cli.php).
    30 * 
     30*
    3131*
    3232* @author   Quinn Comendant <quinn@strangecode.com>
     
    4141    // Configuration parameters for this object.
    4242    var $_params = array(
    43        
     43
    4444        // If false nothing will be cached or retrieved. Useful for testing realtime data requests.
    4545        'enable_cache' => true,
     
    9494    {
    9595        $app =& App::getInstance();
    96    
     96
    9797        if (isset($params) && is_array($params)) {
    9898            // Merge new parameters with old overriding only those passed.
     
    113113    {
    114114        $app =& App::getInstance();
    115    
     115
    116116        if (isset($this->_params[$param])) {
    117117            return $this->_params[$param];
     
    145145                $app->logMsg(sprintf('Dropping and recreating tables acl_tbl, aro_tbl, aco_tbl, axo_tbl.', null), LOG_INFO, __FILE__, __LINE__);
    146146            }
    147            
     147
    148148            // acl_tbl
    149149            $db->query("
     
    171171                $qid = $db->query("SELECT 1 FROM acl_tbl");
    172172                if (mysql_num_rows($qid) == 0) {
    173                     $qid = $db->query("REPLACE INTO acl_tbl VALUES ('1', '1', '1', 'deny', NOW())");                   
    174                 }               
     173                    $qid = $db->query("REPLACE INTO acl_tbl VALUES ('1', '1', '1', 'deny', NOW())");
     174                }
    175175            }
    176176
     
    202202                    $qid = $db->query("SELECT 1 FROM {$a_o}_tbl WHERE name = 'root'");
    203203                    if (mysql_num_rows($qid) == 0) {
    204                         $qid = $db->query("REPLACE INTO {$a_o}_tbl (name, lft, rgt, added_datetime) VALUES ('root', 1, 2, NOW())");                   
    205                     }                   
     204                        $qid = $db->query("REPLACE INTO {$a_o}_tbl (name, lft, rgt, added_datetime) VALUES ('root', 1, 2, NOW())");
     205                    }
    206206                }
    207207
     
    228228        $app =& App::getInstance();
    229229        $db =& DB::getInstance();
    230        
     230
    231231        $this->initDB();
    232        
     232
    233233        switch ($type) {
    234234        case 'aro' :
     
    246246            break;
    247247        }
    248        
     248
    249249        // If $parent is null, use root object.
    250250        if (is_null($parent)) {
    251251            $parent = 'root';
    252252        }
    253        
     253
    254254        // Ensure node and parent name aren't empty.
    255255        if ('' == trim($name) || '' == trim($parent)) {
     
    257257            return false;
    258258        }
    259        
     259
    260260        // Ensure node is unique.
    261261        $qid = $db->query("SELECT 1 FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
     
    264264            return false;
    265265        }
    266        
     266
    267267        // Select the rgt of $parent.
    268268        $qid = $db->query("SELECT rgt FROM $tbl WHERE name = '" . $db->escapeString($parent) . "'");
     
    275275        $db->query("UPDATE $tbl SET lft = lft + 2 WHERE lft >= $parent_rgt");
    276276        $db->query("UPDATE $tbl SET rgt = rgt + 2 WHERE rgt >= $parent_rgt");
    277        
     277
    278278        // Insert new node just below parent. Lft is parent's old rgt.
    279279        $db->query("
    280             INSERT INTO $tbl (name, lft, rgt, added_datetime) 
     280            INSERT INTO $tbl (name, lft, rgt, added_datetime)
    281281            VALUES ('" . $db->escapeString($name) . "', $parent_rgt, $parent_rgt + 1, NOW())
    282282        ");
     
    315315        $app =& App::getInstance();
    316316        $db =& DB::getInstance();
    317        
     317
    318318        $this->initDB();
    319319
     
    336336            break;
    337337        }
    338        
     338
    339339        // Ensure node name isn't empty.
    340340        if ('' == trim($name)) {
     
    342342            return false;
    343343        }
    344        
     344
    345345        // Select the lft and rgt of $name to use for selecting children and reordering transversals.
    346346        $qid = $db->query("SELECT lft, rgt FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
     
    349349            return false;
    350350        }
    351        
     351
    352352        // Remove node and all children of node, as well as acl_tbl links.
    353353        $db->query("
    354             DELETE $tbl, acl_tbl 
     354            DELETE $tbl, acl_tbl
    355355            FROM $tbl
    356356            LEFT JOIN acl_tbl ON ($tbl.$primary_key = acl_tbl.$primary_key)
     
    366366        return true;
    367367    }
    368    
     368
    369369    // Alias functions for the different object types.
    370370    function removeRequestObject($name)
     
    397397        $app =& App::getInstance();
    398398        $db =& DB::getInstance();
    399        
     399
    400400        $this->initDB();
    401401
     
    418418            break;
    419419        }
    420        
     420
    421421        // If $new_parent is null, use root object.
    422422        if (is_null($new_parent)) {
    423423            $new_parent = 'root';
    424424        }
    425        
     425
    426426        // Ensure node and parent name aren't empty.
    427427        if ('' == trim($name) || '' == trim($new_parent)) {
     
    429429            return false;
    430430        }
    431        
     431
    432432        // Select the lft and rgt of $name to use for selecting children and reordering transversals.
    433433        $qid = $db->query("SELECT lft, rgt FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
     
    436436            return false;
    437437        }
    438        
     438
    439439        // Total number of transversal values (that is, the count of self plus all children times two).
    440440        $total_transversal_value = ($rgt - $lft + 1);
     
    446446            return false;
    447447        }
    448        
     448
    449449        // Ensure the new parent is not a child of the node being moved.
    450450        if ($new_parent_rgt <= $rgt && $new_parent_rgt >= $lft) {
     
    452452            return false;
    453453        }
    454        
     454
    455455        // Collect unique ids of all nodes being moved. The transversal numbers will become duplicated so these will be needed to identify these.
    456456        $qid = $db->query("
     
    472472        // Apply transformation to new parent rgt also.
    473473        $new_parent_rgt = $new_parent_rgt > $rgt ? $new_parent_rgt - $total_transversal_value : $new_parent_rgt;
    474        
     474
    475475        // Update transversal values of moved node and children.
    476476        $db->query("
    477             UPDATE $tbl SET 
     477            UPDATE $tbl SET
    478478                lft = lft - ($lft - $new_parent_rgt),
    479479                rgt = rgt - ($lft - $new_parent_rgt)
     
    488488        return true;
    489489    }
    490    
     490
    491491    // Alias functions for the different object types.
    492492    function moveRequestObject($name, $new_parent=null)
     
    502502        return $this->move($name, $new_parent, 'axo');
    503503    }
    504    
     504
    505505    /*
    506506    * Add an entry to the acl_tbl to allow (or deny) a truple with the specified
     
    528528        $aco = is_null($aco) ? 'root' : $aco;
    529529        $axo = is_null($axo) ? 'root' : $axo;
    530        
     530
    531531        // Flush old cached values.
    532532        $cache_hash = $aro . '|' . $aco . '|' . $axo;
     
    552552        // Access must be 'allow' or 'deny'.
    553553        $allow = 'allow' == $access ? 'allow' : 'deny';
    554        
     554
    555555        $db->query("REPLACE INTO acl_tbl VALUES ('$aro_id', '$aco_id', '$axo_id', '$allow', NOW())");
    556556        $app->logMsg(sprintf('Set %s: %s -> %s -> %s.', $allow, $aro, $aco, $axo), LOG_INFO, __FILE__, __LINE__);
    557        
     557
    558558        return true;
    559559    }
     
    577577        return $this->grant($aro, $aco, $axo, 'deny');
    578578    }
    579    
     579
    580580    /*
    581581    * Delete an entry from the acl_tbl completely to allow other permissions to cascade down.
     
    610610        $aco = is_null($aco) ? 'root' : $aco;
    611611        $axo = is_null($axo) ? 'root' : $axo;
    612        
     612
    613613        // Flush old cached values.
    614614        $cache_hash = $aro . '|' . $aco . '|' . $axo;
     
    621621            return false;
    622622        }
    623        
     623
    624624        $qid = $db->query("
    625625            DELETE acl_tbl
     
    632632
    633633        $app->logMsg(sprintf('Deleted %s acl_tbl links: %s -> %s -> %s', mysql_affected_rows($db->getDBH()), $aro, $aco, $axo), LOG_INFO, __FILE__, __LINE__);
    634        
     634
    635635        return true;
    636636    }
    637    
     637
    638638    /*
    639639    * Calculates the most specific cascading privilege found for a requested
    640     * ARO -> ACO -> AXO entry. Returns FALSE if the entry is denied. By default, 
     640    * ARO -> ACO -> AXO entry. Returns FALSE if the entry is denied. By default,
    641641    * all entries are denied, unless some point in the hierarchy is set to "allow."
    642642    *
     
    654654        $app =& App::getInstance();
    655655        $db =& DB::getInstance();
    656        
     656
    657657        $this->initDB();
    658658
     
    661661        $aco = is_null($aco) || '' == trim($aco) ? 'root' : $aco;
    662662        $axo = is_null($axo) || '' == trim($axo) ? 'root' : $axo;
    663        
     663
    664664        $cache_hash = $aro . '|' . $aco . '|' . $axo;
    665665        if ($this->cache->exists($cache_hash) && true === $this->getParam('enable_cache')) {
     
    687687            $this->cache->set($cache_hash, $access);
    688688        }
    689        
     689
    690690        if ('allow' == $access) {
    691691            $app->logMsg(sprintf('Access granted: %s -> %s -> %s.', $aro, $aco, $axo), LOG_DEBUG, __FILE__, __LINE__);
     
    697697    }
    698698
     699    /*
     700    * Bounce user if they are denied access. Because this function calls dieURL() it must be called before any other HTTP header output.
     701    *
     702    * @access   public
     703    * @param    string $aro Identifier of an existing ARO object.
     704    * @param    string $aco Identifier of an existing ACO object (or null to use root).
     705    * @param    string $axo Identifier of an existing AXO object (or null to use root).
     706    * @param    string $message The text description of a message to raise.
     707    * @param    int    $type    The type of message: MSG_NOTICE,
     708    *                           MSG_SUCCESS, MSG_WARNING, or MSG_ERR.
     709    * @param    string $file    __FILE__.
     710    * @param    string $line    __LINE__.
     711    * @author   Quinn Comendant <quinn@strangecode.com>
     712    * @version  1.0
     713    * @since    20 Jan 2014 12:09:03
     714    */
     715    function requireAllow($aro, $aco=null, $axo=null, $message='', $type=MSG_NOTICE, $file=null, $line=null)
     716    {
     717        $app =& App::getInstance();
     718
     719        if (!$this->check($aro, $aco, $axo)) {
     720            $message = '' == trim($message) ? sprintf(_("You have insufficient privileges to view <em>%s %s</em>"), $aco, $axo) : $message;
     721            $app->raiseMsg($message, $type, $file, $line);
     722            $app->dieBoomerangURL();
     723        }
     724    }
     725
    699726} // End class.
    700727
Note: See TracChangeset for help on using the changeset viewer.