home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / groupperm.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  14.5 KB  |  413 lines

  1. <?php
  2. // $Id: groupperm.php 1102 2007-10-19 02:55:52Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35.  
  36. /**
  37.  *
  38.  *
  39.  * @package     kernel
  40.  *
  41.  * @author        Kazumi Ono    <onokazu@xoops.org>
  42.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  43.  */
  44.  
  45. /**
  46.  * A group permission
  47.  *
  48.  * These permissions are managed through a {@link XoopsGroupPermHandler} object
  49.  *
  50.  * @package     kernel
  51.  *
  52.  * @author        Kazumi Ono    <onokazu@xoops.org>
  53.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  54.  */
  55. class XoopsGroupPerm extends XoopsObject
  56. {
  57.  
  58.     /**
  59.      * Constructor
  60.      *
  61.      */
  62.     function XoopsGroupPerm()
  63.     {
  64.         $this->XoopsObject();
  65.         $this->initVar('gperm_id', XOBJ_DTYPE_INT, null, false);
  66.         $this->initVar('gperm_groupid', XOBJ_DTYPE_INT, null, false);
  67.         $this->initVar('gperm_itemid', XOBJ_DTYPE_INT, null, false);
  68.         $this->initVar('gperm_modid', XOBJ_DTYPE_INT, 0, false);
  69.         $this->initVar('gperm_name', XOBJ_DTYPE_OTHER, null, false);
  70.     }
  71. }
  72.  
  73.  
  74. /**
  75. * XOOPS group permission handler class.
  76. *
  77. * This class is responsible for providing data access mechanisms to the data source
  78. * of XOOPS group permission class objects.
  79. * This class is an abstract class to be implemented by child group permission classes.
  80. *
  81. * @see          XoopsGroupPerm
  82. * @author       Kazumi Ono  <onokazu@xoops.org>
  83. * @copyright    copyright (c) 2000-2003 XOOPS.org
  84. */
  85. class XoopsGroupPermHandler extends XoopsObjectHandler
  86. {
  87.  
  88.     /**
  89.      * Create a new {@link XoopsGroupPerm}
  90.      *
  91.      * @return    bool    $isNew  Flag the object as "new"?
  92.      */
  93.     function &create($isNew = true)
  94.     {
  95.         $perm = new XoopsGroupPerm();
  96.         if ($isNew) {
  97.             $perm->setNew();
  98.         }
  99.         return $perm;
  100.     }
  101.  
  102.     /**
  103.      * Retrieve a group permission
  104.      *
  105.      * @param    int $id ID
  106.      *
  107.      * @return    object  {@link XoopsGroupPerm}, FALSE on fail
  108.      */
  109.     function &get($id)
  110.     {
  111.       $id = intval($id);
  112.       $perm = false;
  113.         if ($id > 0) {
  114.             $sql = sprintf("SELECT * FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $id);
  115.             if ( !$result = $this->db->query($sql) ) {
  116.                 return $perm;
  117.             }
  118.             $numrows = $this->db->getRowsNum($result);
  119.             if ( $numrows == 1 ) {
  120.                 $perm = new XoopsGroupPerm();
  121.                 $perm->assignVars($this->db->fetchArray($result));
  122.             }
  123.         }
  124.         return $perm;
  125.     }
  126.  
  127.     /**
  128.      * Store a {@link XoopsGroupPerm}
  129.      *
  130.      * @param    object  &$perm  {@link XoopsGroupPerm} object
  131.      *
  132.      * @return    bool    TRUE on success
  133.      */
  134.     function insert(&$perm)
  135.     {
  136.         /**
  137.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  138.         */
  139.         if (!is_a($perm, 'xoopsgroupperm')) {
  140.             return false;
  141.         }
  142.         if ( !$perm->isDirty() ) {
  143.             return true;
  144.         }
  145.         if (!$perm->cleanVars()) {
  146.             return false;
  147.         }
  148.         foreach ($perm->cleanVars as $k => $v) {
  149.             ${$k} = $v;
  150.         }
  151.         if ($perm->isNew()) {
  152.             $gperm_id = $this->db->genId('group_permission_gperm_id_seq');
  153.             $sql = sprintf("INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, %u, %u, %s)", $this->db->prefix('group_permission'), $gperm_id, $gperm_groupid, $gperm_itemid, $gperm_modid, $this->db->quoteString($gperm_name));
  154.         } else {
  155.             $sql = sprintf("UPDATE %s SET gperm_groupid = %u, gperm_itemid = %u, gperm_modid = %u WHERE gperm_id = %u", $this->db->prefix('group_permission'), $gperm_groupid, $gperm_itemid, $gperm_modid, $gperm_id);
  156.         }
  157.         if (!$result = $this->db->query($sql)) {
  158.             return false;
  159.         }
  160.         if (empty($gperm_id)) {
  161.             $gperm_id = $this->db->getInsertId();
  162.         }
  163.         $perm->assignVar('gperm_id', $gperm_id);
  164.         return true;
  165.     }
  166.  
  167.     /**
  168.      * Delete a {@link XoopsGroupPerm}
  169.      *
  170.      * @param    object  &$perm
  171.      *
  172.      * @return    bool    TRUE on success
  173.      */
  174.     function delete(&$perm)
  175.     {
  176.         /**
  177.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  178.         */
  179.         if (!is_a($perm, 'xoopsgroupperm')) {
  180.             return false;
  181.         }
  182.         $sql = sprintf("DELETE FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $perm->getVar('gperm_id'));
  183.         if (!$result = $this->db->query($sql)) {
  184.             return false;
  185.         }
  186.         return true;
  187.     }
  188.  
  189.     /**
  190.      * Retrieve multiple {@link XoopsGroupPerm}s
  191.      *
  192.      * @param    object  $criteria   {@link CriteriaElement}
  193.      * @param    bool    $id_as_key  Use IDs as array keys?
  194.      *
  195.      * @return    array   Array of {@link XoopsGroupPerm}s
  196.      */
  197.     function getObjects($criteria = null, $id_as_key = false)
  198.     {
  199.         $ret = array();
  200.         $limit = $start = 0;
  201.         $sql = 'SELECT * FROM '.$this->db->prefix('group_permission');
  202.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  203.             $sql .= ' '.$criteria->renderWhere();
  204.             $limit = $criteria->getLimit();
  205.             $start = $criteria->getStart();
  206.         }
  207.         $result = $this->db->query($sql, $limit, $start);
  208.         if (!$result) {
  209.             return $ret;
  210.         }
  211.         while ($myrow = $this->db->fetchArray($result)) {
  212.             $perm = new XoopsGroupPerm();
  213.             $perm->assignVars($myrow);
  214.             if (!$id_as_key) {
  215.                 $ret[] =& $perm;
  216.             } else {
  217.                 $ret[$myrow['gperm_id']] =& $perm;
  218.             }
  219.             unset($perm);
  220.         }
  221.         return $ret;
  222.     }
  223.  
  224.     /**
  225.      * Count some {@link XoopsGroupPerm}s
  226.      *
  227.      * @param    object  $criteria   {@link CriteriaElement}
  228.      *
  229.      * @return    int
  230.      */
  231.     function getCount($criteria = null)
  232.     {
  233.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('group_permission');
  234.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  235.             $sql .= ' '.$criteria->renderWhere();
  236.         }
  237.         $result = $this->db->query($sql);
  238.         if (!$result) {
  239.             return 0;
  240.         }
  241.         list($count) = $this->db->fetchRow($result);
  242.         return $count;
  243.     }
  244.  
  245.     /**
  246.      * Delete all permissions by a certain criteria
  247.      *
  248.      * @param    object  $criteria   {@link CriteriaElement}
  249.      *
  250.      * @return    bool    TRUE on success
  251.      */
  252.     function deleteAll($criteria = null)
  253.     {
  254.         $sql = sprintf("DELETE FROM %s", $this->db->prefix('group_permission'));        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  255.             $sql .= ' '.$criteria->renderWhere();
  256.         }
  257.         if (!$result = $this->db->query($sql)) {
  258.             return false;
  259.         }
  260.         return true;
  261.     }
  262.  
  263.     /**
  264.      * Delete all module specific permissions assigned for a group
  265.      *
  266.      * @param    int  $gperm_groupid ID of a group
  267.      * @param    int  $gperm_modid ID of a module
  268.      *
  269.      * @return    bool TRUE on success
  270.      */
  271.     function deleteByGroup($gperm_groupid, $gperm_modid = null)
  272.     {
  273.         $criteria = new CriteriaCompo(new Criteria('gperm_groupid', intval($gperm_groupid)));
  274.         if (isset($gperm_modid)) {
  275.             $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
  276.         }
  277.         return $this->deleteAll($criteria);
  278.     }
  279.  
  280.     /**
  281.      * Delete all module specific permissions
  282.      *
  283.      * @param    int  $gperm_modid ID of a module
  284.      * @param    string  $gperm_name Name of a module permission
  285.      * @param    int  $gperm_itemid ID of a module item
  286.      *
  287.      * @return    bool TRUE on success
  288.      */
  289.     function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
  290.     {
  291.         $criteria = new CriteriaCompo(new Criteria('gperm_modid', intval($gperm_modid)));
  292.         if (isset($gperm_name)) {
  293.             $criteria->add(new Criteria('gperm_name', $gperm_name));
  294.             if (isset($gperm_itemid)) {
  295.                 $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
  296.             }
  297.         }
  298.         return $this->deleteAll($criteria);
  299.     }
  300.     /**#@-*/
  301.  
  302.     /**
  303.      * Check permission
  304.      *
  305.      * @param    string    $gperm_name       Name of permission
  306.      * @param    int       $gperm_itemid     ID of an item
  307.      * @param    int/array $gperm_groupid    A group ID or an array of group IDs
  308.      * @param    int       $gperm_modid      ID of a module
  309.      *
  310.      * @return    bool    TRUE if permission is enabled
  311.      */
  312.     function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
  313.     {
  314.         $criteria = new CriteriaCompo(new Criteria('gperm_modid', $gperm_modid));
  315.         $criteria->add(new Criteria('gperm_name', $gperm_name));
  316.         $gperm_itemid = intval($gperm_itemid);
  317.         if ($gperm_itemid > 0) {
  318.             $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
  319.         }
  320.         if (is_array($gperm_groupid)) {
  321.             if (in_array(XOOPS_GROUP_ADMIN, $gperm_groupid)) {
  322.                 return true;
  323.             }
  324.             $criteria2 = new CriteriaCompo();
  325.             foreach ($gperm_groupid as $gid) {
  326.                 $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
  327.             }
  328.             $criteria->add($criteria2);
  329.         } else {
  330.             if (XOOPS_GROUP_ADMIN == $gperm_groupid) {
  331.                 return true;
  332.             }
  333.             $criteria->add(new Criteria('gperm_groupid', $gperm_groupid));
  334.         }
  335.         if ($this->getCount($criteria) > 0) {
  336.             return true;
  337.         }
  338.         return false;
  339.     }
  340.  
  341.     /**
  342.      * Add a permission
  343.      *
  344.      * @param    string  $gperm_name       Name of permission
  345.      * @param    int     $gperm_itemid     ID of an item
  346.      * @param    int     $gperm_groupid    ID of a group
  347.      * @param    int     $gperm_modid      ID of a module
  348.      *
  349.      * @return    bool    TRUE jf success
  350.      */
  351.     function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
  352.     {
  353.         $perm =& $this->create();
  354.         $perm->setVar('gperm_name', $gperm_name);
  355.         $perm->setVar('gperm_groupid', $gperm_groupid);
  356.         $perm->setVar('gperm_itemid', $gperm_itemid);
  357.         $perm->setVar('gperm_modid', $gperm_modid);
  358.         return $this->insert($perm);
  359.     }
  360.  
  361.     /**
  362.      * Get all item IDs that a group is assigned a specific permission
  363.      *
  364.      * @param    string    $gperm_name       Name of permission
  365.      * @param    int/array $gperm_groupid    A group ID or an array of group IDs
  366.      * @param    int       $gperm_modid      ID of a module
  367.      *
  368.      * @return  array     array of item IDs
  369.      */
  370.     function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
  371.     {
  372.         $ret = array();
  373.         $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
  374.         $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
  375.         if (is_array($gperm_groupid)) {
  376.             $criteria2 = new CriteriaCompo();
  377.             foreach ($gperm_groupid as $gid) {
  378.                 $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
  379.             }
  380.             $criteria->add($criteria2);
  381.         } else {
  382.             $criteria->add(new Criteria('gperm_groupid', intval($gperm_groupid)));
  383.         }
  384.         $perms = $this->getObjects($criteria, true);
  385.         foreach (array_keys($perms) as $i) {
  386.             $ret[] = $perms[$i]->getVar('gperm_itemid');
  387.         }
  388.         return array_unique($ret);
  389.     }
  390.  
  391.     /**
  392.      * Get all group IDs assigned a specific permission for a particular item
  393.      *
  394.      * @param    string  $gperm_name       Name of permission
  395.      * @param    int     $gperm_itemid     ID of an item
  396.      * @param    int     $gperm_modid      ID of a module
  397.      *
  398.      * @return  array   array of group IDs
  399.      */
  400.     function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
  401.     {
  402.         $ret = array();
  403.         $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
  404.         $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
  405.         $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
  406.         $perms = $this->getObjects($criteria, true);
  407.         foreach (array_keys($perms) as $i) {
  408.             $ret[] = $perms[$i]->getVar('gperm_groupid');
  409.         }
  410.         return $ret;
  411.     }
  412. }
  413. ?>