Source for file OCSP_GROUPTREE.phpclass

Documentation is available at OCSP_GROUPTREE.phpclass

  1. <?php
  2. /**
  3.   * Class file OCSP_GROUPTREE.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    user
  7.   *
  8.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @version    pk-03-12-30
  12.   * @version    $Id: OCSP_GROUPTREE.phpclass,v 1.13 2008/12/02 07:36:22 pitlinz Exp $
  13.   */
  14.  
  15.  
  16. if (!class_exists('OCSP_OBJ'))
  17. {
  18.     require_once __OCSP_PHPINCPATH__ "common" _OCSP_DIRSEP_ "OCSP_OBJ.phpclass";
  19. }
  20.  
  21. if (!class_exists('OCSP_USER'))
  22. {
  23.     require_once dirname(__FILE___OCSP_DIRSEP_ "OCSP_USER.phpclass";
  24. }
  25.  
  26. /**#@+
  27.   * Constants for system group id's
  28.   * @since pk-08-03-13
  29.   */
  30.     define('_OCSP_GROUP_PUBLIC_',0);
  31.     define('_OCSP_GROUP_LOGGEDIN_',-1);
  32.     define('_OCSP_GROUP_ITEMAUTHOR_',-2);
  33.     
  34.     define('_OCSP_GROUP_PROJ_ADMINGRP_',-80);
  35.     define('_OCSP_GROUP_PROJ_EDITOR_GRP_',-81);
  36.     define('_OCSP_GROUP_PROJ_AUTHOR_GRP_',-82);
  37.     define('_OCSP_GROUP_PROJ_USER_GRP_',-83);
  38.             
  39.     define('_OCSP_GROUP_NOTLOGGEDIN_',-99);
  40.     define('_OCSP_GROUP_SYSTEMJOB_',-98);
  41.     define('_OCSP_GROUP_TERMINATOR_',-89)// don't check parents lower then this
  42.     
  43.     define('_OCSP_GROUP_ADMIN_',1);
  44.     define('_OCSP_GROUP_DBMS_',2);
  45.     
  46.     define('_OCSP_GROUP_CMSADMIN_',3);
  47.     define('_OCSP_GROUP_CMSEDITOR_',5);
  48.     define('_OCSP_GROUP_CMSAUTHOR_',6);
  49.     
  50.     define('_OCSP_GROUP_PRODUCTS_',7);
  51.     
  52.     define('_OCSP_GROUP_TEMPLATE_',4);
  53.     
  54.     define('_OCSP_GROUP_CLIADMIN_',10);
  55.     
  56.     define('_OCSP_GROUPRIGHTS_INSERT',32);
  57.     define('_OCSP_GROUPRIGHTS_EDIT',64);
  58.     define('_OCSP_GROUPRIGHTS_DELETE',96);
  59.     
  60. /**#@-*/ 
  61.     
  62. /**
  63.   * handels groups
  64.   *
  65.   * @project    Open CSP-Management
  66.   * @package    user
  67.   *
  68.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  69.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  70.   *
  71.   * @since pk-08-03-12
  72.   * @version    $Id: OCSP_GROUPTREE.phpclass,v 1.13 2008/12/02 07:36:22 pitlinz Exp $
  73.   * 
  74.   */
  75.     extends OCSP_OBJ 
  76. {
  77.  
  78.     /*** class constants  --------------------------------------------- */
  79.  
  80.     /**
  81.       * @constant string CLASS_SRC_FILE
  82.       * 
  83.       * @abstract
  84.       */
  85.     const CLASS_SRC_FILE = __FILE__;
  86.  
  87.     /*** class variables  --------------------------------------------- */
  88.            
  89.     /**
  90.      * singleton instance
  91.      *
  92.      * @var OCSP_GROUPTREE $instance 
  93.      */
  94.     private static $instance;
  95.  
  96.     
  97.     /*** attrubtes       --------------------------------------------- */
  98.     
  99.     /**
  100.      * @var array $myGroups 
  101.      */
  102.     protected $myGroups = array();
  103.     
  104.     /**
  105.      * @var array $groupChildren 
  106.      */
  107.     protected $myChildTree = array();
  108.      
  109.     
  110.     /**
  111.      * array with already seen group members
  112.      *
  113.      * @var array $myGroupMembers 
  114.      */
  115.     protected $myGroupMembers = array();
  116.  
  117.     /**
  118.      * array with already seen group admins
  119.      *
  120.      * @var array $myGroupAdmins 
  121.      */    
  122.     protected $myGroupAdmins  = array();
  123.     
  124.     /**
  125.      * flag to notice if the class attributes has changed
  126.      *
  127.      * @var boolean $hasChanged 
  128.      */
  129.     protected $hasChanged = false;
  130.     
  131.     /**
  132.      * array with checked groups for a user
  133.      * 
  134.      * $this->checkedgroups[M|A][usrId][grpId] = True
  135.      *
  136.      * @var array $checkedgroups 
  137.      */
  138.     private $checkedgroups = array();
  139.     
  140.     
  141.     /**
  142.      * returns the object instance
  143.      *
  144.      * @return OCSP_GROUPTREE 
  145.      */
  146.     public static function getInstance()
  147.     {
  148.         if (!self::$instance)
  149.         {
  150.             self::$instance new OCSP_GROUPTREE();
  151.         }
  152.         return self::$instance;
  153.     }
  154.     
  155.       /**
  156.        * constructor
  157.        *
  158.        */
  159.       protected function __construct()
  160.       
  161.         $this->populateGroups();
  162.       }
  163.       
  164.       public function populateGroups($debug=false)
  165.       {
  166.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::populateGroups();");
  167.           
  168.           $this->myGroupAdmins     = array();
  169.           $this->myGroupMembers    = array();
  170.           
  171.           if (!($obj_db OCSP_OBJ::currentUser()->getMyDBObj()))
  172.           {
  173.               throw new Exception(_OCSP_EXCEP_NODBCONN_);
  174.           }
  175.           $str_query "SELECT * FROM T_SYS_GROUP ORDER BY GRP_PARENT";
  176.         if ($obj_cursor $obj_db->query($str_query))
  177.         {
  178.             while($arr_row $obj_cursor->fetchArrayFld())
  179.             {
  180.                 $this->myGroups[$arr_row['GRP_ID']]=$arr_row;
  181.                 $this->myChildTree[$arr_row['GRP_PARENT']][$arr_row['GRP_ID']
  182.             }
  183.         else {
  184.             throw new Exception(_OCSP_EXCEP_QUERYFAILED_);    
  185.         }    
  186.       }
  187.  
  188.       /**
  189.        * returns a group row also for system groups
  190.        *
  191.        * @param int $aId 
  192.        * @param boolean $debug 
  193.        * 
  194.        * @return array 
  195.        */
  196.       public function getGroupRow($aId,$debug=False)
  197.       {
  198.            
  199.           switch (intval($aId))
  200.           {
  201.               case 0:
  202.                   return array('GRP_ID'=>0,'GRP_NAME'=>'[Public]');
  203.               case _OCSP_GROUP_LOGGEDIN_:
  204.                   return array('GRP_ID'=>_OCSP_GROUP_LOGGEDIN_,'GRP_NAME'=>'[Angemeldete Benutzer]','GRP_PARENT'=>_OCSP_GROUP_ADMIN_);
  205.               case _OCSP_GROUP_NOTLOGGEDIN_:
  206.                   return array('GRP_ID'=>_OCSP_GROUP_LOGGEDIN_,'GRP_NAME'=>'[nicht angemeldete Benutzer]','GRP_PARENT'=>_OCSP_GROUP_ADMIN_);                  
  207.               default:
  208.                   if (!isset($this->myGroups[$aId]))
  209.                   {
  210.                     if ($aId 0{
  211.                         return array('GRP_ID'=>$aId,'GRP_NAME'=>'[Systemgruppe]','GRP_PARENT'=>_OCSP_GROUP_ADMIN_);                      
  212.                     else {
  213.                         throw new Exception(_OCSP_EXCEP_NOID_);
  214.                         return false;
  215.                     }
  216.                   else {
  217.                       if ($this->userIsMember($aId,OCSP_OBJ::currentUser()->getId(),True,$debug))
  218.                       {
  219.                           return $this->myGroups[$aId];
  220.                       else {
  221.                           return array('GRP_ID'=>$aId,'GRP_NAME'=>$this->myGroups[$aId]['GRP_NAME']);
  222.                       }
  223.                   }
  224.                   
  225.           }
  226.           return NULL;
  227.       }
  228.  
  229.       public function getGroupName($aId)
  230.       {
  231.           if ($arr_row $this->getGroupRow($aId))
  232.           {
  233.               return $arr_row['GRP_NAME'];
  234.           }
  235.       }
  236.       
  237.       /**
  238.        * appends a child tree to $retArr
  239.        *
  240.        * @param array &$retArr 
  241.        * @param int $parentId 
  242.        * @param string $keyPrepend 
  243.        * @param boolean $debug 
  244.        * 
  245.        * @return array 
  246.        */
  247.       protected function _addChildGroupToArray(&$retArr,$parentId,$keyPrepend,$debug=false)
  248.       {
  249.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::_addChildGroupArray(Parent: " $parentId ")");
  250.           if (isset($this->myChildTree[$parentId]))
  251.           {
  252.               foreach($this->myChildTree[$parentIdas $int_grpId)
  253.               {
  254.                   if (!in_array($this->myGroups[$int_grpId],$retArr))
  255.                   {
  256.                         if (!($int_idx intval($this->myGroups[$int_grpId]['GRP_SORTORDER'])))
  257.                       {
  258.                           $int_idx 1;
  259.                       }      
  260.                       while(isset($retArr[$keyPrepend sprintf("%04d",$int_idx)]))
  261.                       {
  262.                           $int_idx++;        
  263.                       }
  264.                     $retArr[$keyPrepend sprintf("%04d",$int_idx)$this->myGroups[$int_grpId];
  265.                     $this->_addChildGroupToArray($retArr,$int_grpId,$keyPrepend sprintf("%04d",$int_idx".",$debug);
  266.                   else {
  267.                       if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>" print_r($this->myGroups[$int_grpId],True"</pre>");
  268.                                                                      
  269.                   }
  270.               }
  271.               return True;
  272.           else {
  273.               return false;
  274.           }
  275.       }
  276.  
  277.       /**
  278.        * returns an array with all groups
  279.        *
  280.        * @param boolean $withSystemGroups 
  281.        * @param boolean $debug 
  282.        * 
  283.        * @return array 
  284.        */
  285.       public function getGroupTreeArray($debug=false)
  286.       {                    
  287.           if ($debugechoDebugMethod(__FILE__,get_class($this),'OCSP_GROUPTREE::getGroupArray()');
  288.                     
  289.           foreach($this->myChildTree[0as $int_grpId
  290.           {
  291.               if (!($int_idx intval($this->myGroups[$int_grpId]['GRP_SORTORDER'])))
  292.               {
  293.                   $int_idx 1;
  294.               }
  295.             while(isset($arr_ret[sprintf("%04d",$int_idx)]))
  296.             {
  297.                 $int_idx++;
  298.             }
  299.             $arr_ret[sprintf("%04d",$int_idx)$this->myGroups[$int_grpId];
  300.             $this->_addChildGroupToArray($arr_ret,$int_grpId,sprintf("%04d",$int_idx".",$debug);            
  301.           }
  302.           ksort($arr_ret);
  303.           return $arr_ret;
  304.       }
  305.       
  306.       /**
  307.        * returns an array with the 1st level children of a group
  308.        * 
  309.        * @param int $parentId 
  310.        * @param boolean $debug 
  311.        * 
  312.        * @return array 
  313.        * 
  314.        * @since pk-08-03-27
  315.        */
  316.       public function getChildGroups($parentId,$debug=false)
  317.       {
  318.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::getChildGroups(".intval($parentId)")");
  319.           if (isset($this->myChildTree[$parentId]))
  320.           {
  321.               $arr_ret array();
  322.               foreach($this->myChildTree[$parentIdas $int_grpId)
  323.               {
  324.                   $arr_ret[$int_grpId$this->myGroups[$int_grpId];
  325.               }
  326.               return $arr_ret;
  327.           else {
  328.               return array();
  329.           }
  330.       }
  331.       
  332.       /**
  333.        * returns an array with all children (including childrens of children) of a group
  334.        *
  335.        * @param int $parentId 
  336.        * @param array $setGroups already set groups to avoid loops on misconfigured groups
  337.        * @param boolean $debug 
  338.        * 
  339.        * @return array 
  340.        * 
  341.        * @since pk-08-06-05
  342.        */
  343.       public function getAllChildGroups($parentId,$setGroups=Null,$debug=False)
  344.       {
  345.           $arr_ret=$this->getChildGroups($parentId,$debug);
  346.           
  347.           foreach($arr_ret as $grp_id => $arr_grps)
  348.           {
  349.               if (!isset($setGroups[$grp_id]))
  350.               {
  351.                   $arr_ret array_merge($arr_ret,$this->getAllChildGroups($grp_id,$arr_ret,$debug));
  352.               }
  353.           }          
  354.           return $arr_ret;
  355.       }
  356.       
  357.       private function _usrGroupTreeSetShow(&$arr_groups,$grpId,$debug)
  358.       {
  359.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::_usrGroupTreeSetShow();");
  360.           if ($int_parent intval($this->myGroups[$grpId]['GRP_PARENT']))
  361.           {
  362.               if (!$arr_groups[$int_parent]['SHOW'])
  363.               {
  364.                   $arr_groups[$int_parent]['SHOW']=True;
  365.                   if (($int_parent _OCSP_GROUP_TERMINATOR_ && ($int_parent != _OCSP_GROUP_ADMIN_))
  366.                   {
  367.                       $this->_usrGroupTreeSetShow($arr_groups,$int_parent,$debug)
  368.                   }
  369.               }
  370.           }
  371.       }
  372.       
  373.       /**
  374.        * returns the group tree with additional informations for the current user
  375.        *
  376.        * the return array looks like:
  377.        *     - GRP_ID
  378.        *         - TREEIDX (0001.0001. ...)
  379.        *         - ROW (the database row of the group)
  380.        *         - MEMBER (the user is member of the group)
  381.        *         - ADMIN (the user is admin of the group)
  382.        *         - SHOW (the group is on the pass to a group the user is member or admin)
  383.        * 
  384.        * @param boolean $debug 
  385.        * 
  386.        * @return array 
  387.        */
  388.       public function getUserGroupTreeArray($debug=false)
  389.       {
  390.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::getUserGroupTreeArray()");
  391.           
  392.           $arr_grps $this->getGroupTreeArray($debug);
  393.         $arr_ret array();          
  394.           foreach($arr_grps as $str_key => $arr_row)
  395.           {
  396.               $arr_ret[$arr_row['GRP_ID']]['TREEIDX'$str_key;
  397.               $arr_ret[$arr_row['GRP_ID']]['ROW'$arr_row;
  398.               if ($this->userIsMember($arr_row['GRP_ID'],OCSP_OBJ::currentUser()->getId(),false,$debug))
  399.               {
  400.                   $arr_ret[$arr_row['GRP_ID']]['MEMBER']=True;
  401.               else {
  402.                   $arr_ret[$arr_row['GRP_ID']]['MEMBER']=false;
  403.               }
  404.               if ($this->userIsAdmin($arr_row['GRP_ID'],OCSP_OBJ::currentUser()->getId(),false,$debug))
  405.               {
  406.                   $arr_ret[$arr_row['GRP_ID']]['ADMIN']=True;
  407.               else {
  408.                   $arr_ret[$arr_row['GRP_ID']]['ADMIN']=false;
  409.               }
  410.               
  411.               if ($arr_ret[$arr_row['GRP_ID']]['MEMBER'|| $arr_ret[$arr_row['GRP_ID']]['ADMIN'])
  412.               {              
  413.                   $arr_ret[$arr_row['GRP_ID']]['SHOW']=True;
  414.                   $this->_usrGroupTreeSetShow($arr_ret,$arr_row['GRP_ID'],$debug);
  415.               else {
  416.                   $arr_ret[$arr_row['GRP_ID']]['SHOW']=false;
  417.               }
  418.           }
  419.           
  420.           return $arr_ret;
  421.       }
  422.       
  423.       // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  424.       // user methods
  425.       // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
  426.       
  427.       /**
  428.        * returns if a user is member of a group
  429.        * 
  430.        * - an entry in T_SYS_GROUPMEMBER is found
  431.        * - the user is member of a parent group
  432.        * - GRP_PASSDOWN_RIGHTS = 1 and user is member of a direct children
  433.        *
  434.        * @param int $grpId 
  435.        * @param int $usrId 
  436.        * @param boolean $withAdmin (include group admins)
  437.        * @param boolean $debug 
  438.         * 
  439.        * @global $OCSP_OBJ 
  440.        * 
  441.        * @return boolean 
  442.        * 
  443.        * @version pk-08-03-29
  444.        */
  445.       public function userIsMember($grpId,$usrId,$withAdmin=false,$debug=false)
  446.       {                   
  447.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::userIsMember(".$grpId.",".$usrId."," ($withAdmin "True" "False"")");
  448.                 
  449.           if (!($grpId intval($grpId)))
  450.           {
  451.               return True// everybody is member of puplic
  452.           }
  453.           
  454.           if (!($usrId)) // <pk-08-03-29>
  455.           {
  456.             if ($grpId == _OCSP_GROUP_NOTLOGGEDIN_{
  457.                   return True;
  458.               else {
  459.                   if ($debugechoDebugLine(__FILE__,__LINE__,"return false");
  460.                   return False;
  461.               }
  462.           }
  463.           
  464.           
  465.           
  466.           if (isset($this->checkedgroups['M'][$usrId][$grpId][intval($withAdmin)]))
  467.           {
  468.               if ($debugechoDebugLine(__FILE__,__LINE__,"group {$grpId} already checked");
  469.               if ($withAdmin)
  470.               {
  471.                   if (isset($this->myGroupMembers[$grpId]&& is_array($this->myGroupMembers[$grpId]))
  472.                   {
  473.                        if (in_array($usrId,$this->myGroupMembers[$grpId]))
  474.                       {
  475.                           return True;
  476.                       }    
  477.                   else if (isset($this->myGroupAdmins[$grpId]&& is_array($this->myGroupAdmins[$grpId])) {
  478.                       return (in_array($usrId,$this->myGroupAdmins[$grpId]));                      
  479.                   else {
  480.                       if ($debugechoDebugLine(__FILE__,__LINE__,"return false");
  481.                       return False;
  482.                   }
  483.               else {
  484.                   if (isset($this->myGroupMembers[$grpId]&& is_array($this->myGroupMembers[$grpId]))
  485.                   {
  486.                       return (in_array($usrId,$this->myGroupMembers[$grpId]));
  487.                   else {
  488.                       if ($debugechoDebugLine(__FILE__,__LINE__,"return false");
  489.                       return False;
  490.                   }
  491.               }
  492.           }
  493.           
  494.           $this->checkedgroups['M'][$usrId][$grpId][intval($withAdmin)True;
  495.           
  496.           if ($grpId 0)
  497.           {
  498.               if (OCSP_OBJ::currentUser()->getId(== $usrId)
  499.               {
  500.                   $obj_user OCSP_OBJ::currentUser();
  501.               else if (!($obj_user OCSP_USER::factoryFromId($usrId,$debug))) {
  502.                   throw new Exception(_OCSP_EXCEP_NOUSER_);
  503.               }
  504.  
  505.               switch($grpId)
  506.               {
  507.                   case _OCSP_GROUP_LOGGEDIN_:
  508.                       return (intval(OCSP_OBJ::currentUser()->getId()) True false);
  509.                   case _OCSP_GROUP_NOTLOGGEDIN_:
  510.                       return (intval(OCSP_OBJ::currentUser()->getId()) false True);                  
  511.               }
  512.               
  513.               if ($debugechoDebugLine(__FILE__,__LINE__,"return false");
  514.               return False;
  515.           }
  516.                     
  517.           if (isset($this->myGroupMembers[$grpId]&& is_array($this->myGroupMembers[$grpId]&& in_array($usrId,$this->myGroupMembers[$grpId]))
  518.           {              
  519.               return True;
  520.           }
  521.           
  522.           if ($withAdmin && isset($this->myGroupAdmins[$grpId]&& is_array($this->myGroupAdmins[$grpId]&& in_array($usrId,$this->myGroupAdmins[$grpId]))
  523.           {
  524.               return True;    
  525.           }                    
  526.           
  527.           // nothing cached here ask the user for direct assignment
  528.           if (OCSP_OBJ::currentUser()->getId(== $usrId)
  529.           {
  530.               $obj_user OCSP_OBJ::currentUser();
  531.           else if (!($obj_user OCSP_USER::factoryFromId($usrId,$debug))) {
  532.               throw new Exception(_OCSP_EXCEP_NOUSER_);
  533.           }
  534.           
  535.           if ($withAdmin && ($grpId != _OCSP_GROUP_ADMIN_&& $obj_user->isAdmin())
  536.           {
  537.               $this->myGroupAdmins[$grpId][$obj_user->getId();
  538.               return True;
  539.           }
  540.                     
  541.           try {
  542.               $arr_usrGrps $obj_user->getDirectAssignedGroups(false,$debug);
  543.           catch (Exception $e{
  544.             if ($debugechoDebugLine(__FILE__,__LINE__,"Exception: " $e->getMessage());
  545.           }
  546.  
  547.         if (is_array($arr_usrGrps))
  548.         {
  549.               $b_found false;
  550.               foreach($arr_usrGrps as $int_grpId)
  551.               {
  552.                 if ($int_grpId == $grpId
  553.                 {
  554.                     $b_found=True;
  555.                 }
  556.                 if (!isset($this->myGroupMembers[$int_grpId]|| !is_array($this->myGroupMembers[$int_grpId]))
  557.                 {
  558.                     $this->myGroupMembers[$int_grpId]     array();
  559.                     $this->myGroupMembers[$int_grpId][$obj_user->getId();
  560.                 }
  561.                 else if (!in_array($obj_user->getId(),$this->myGroupMembers[$int_grpId]))
  562.                 {                
  563.                     $this->myGroupMembers[$int_grpId][$obj_user->getId();
  564.                 }
  565.               }
  566.               $this->hasChanged = True;
  567.               if ($b_found
  568.               {
  569.                   return True;
  570.               }
  571.         }
  572.  
  573.         if ($withAdmin)
  574.         {
  575.                 try {
  576.                   $arr_adminGrps $obj_user->getDirectAssignedAdminGroups(false,$debug);
  577.               catch (Exception $e{
  578.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Exception: " $e->getMessage());
  579.               }
  580.               
  581.             if (is_array($arr_adminGrps))
  582.             {
  583.                   $b_found false;
  584.                   foreach($arr_adminGrps as $int_grpId)
  585.                   {
  586.                     if ($int_grpId == $grpId
  587.                     {
  588.                         $b_found=True;
  589.                     }
  590.                     if (!isset($this->myGroupAdmins[$int_grpId]|| !is_array($this->myGroupAdmins[$int_grpId]|| !in_array($obj_user->getId(),$this->myGroupAdmins[$int_grpId]))
  591.                     {
  592.                         $this->myGroupAdmins[$int_grpId][$obj_user->getId();
  593.                     }
  594.                   }
  595.                   $this->hasChanged = True;
  596.                   if ($b_found
  597.                   {
  598.                       return True;
  599.                   }
  600.             }              
  601.         }        
  602.         
  603.         // no direct assignment -> check children
  604.         
  605.         if (intval($this->myGroups[$grpId]['GRP_PASSDOWN_RIGHTS']))
  606.         {
  607.             if (isset($this->myChildTree[$grpId]))
  608.             {
  609.                 foreach($this->myChildTree[$grpIdas $int_grpId)
  610.                 {
  611.                     if (!isset($this->checkedgroups['M'][$usrId][$grpId][intval($withAdmin)]))
  612.                     {
  613.                         if ($this->userIsMember($int_grpId,$usrId,$withAdmin,$debug))
  614.                         {
  615.                             $this->myGroupMembers[$grpId]=$usrId;
  616.                             $this->hasChanged = True;
  617.                             return True;
  618.                         }
  619.                         $checkedGroups[$int_grpIdTrue;
  620.                     }
  621.                 }
  622.             }
  623.         }
  624.         
  625.         // no direct assignment -> check parents
  626.         
  627.         if (($int_parentId intval($this->myGroups[$grpId]['GRP_PARENT'])) && ($int_parentId 0))
  628.         {
  629.             if ($debugechoDebugLine(__FILE__,__LINE__,"checking parent group: {$int_parentId}");
  630.             if ($this->userIsMember($int_parentId,$usrId,$withAdmin,$debug))
  631.             {
  632.                 $this->myGroupMembers[$grpId][$usrId;
  633.                 $this->hasChanged = True;
  634.                 return True;
  635.             }
  636.         }
  637.                           
  638.         // finaly check for admin
  639.         
  640.         if ($withAdmin && $int_admGrp intval($this->myGroups[$grpId]['GRP_PARENT']))
  641.         {
  642.             if ($this->userIsMember($int_admGrp,$usrId,True,$debug))
  643.             {
  644.                 $this->myGroupAdmins[$grpId][]=$usrId;
  645.                 $this->hasChanged = True;
  646.                 return True;
  647.             }
  648.         }
  649.         
  650.         return false;
  651.       }
  652.       
  653.     /**
  654.      * returns if a user is admin of $grpId
  655.      *
  656.      * @param int $grpId 
  657.      * @param int $usrId 
  658.      * @param boolean $debug 
  659.      * 
  660.      * @global array $OCSP_OBJ 
  661.      * 
  662.      * @return boolean 
  663.      * 
  664.      * @version pk-08-03-29
  665.      */
  666.       public function userIsAdmin($grpId,$usrId,$debug=false)
  667.       {                    
  668.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::userIsAdmin("$grpId "," $usrId ")");
  669.           
  670.             if (!($usrId)) // <pk-08-03-29>
  671.           {
  672.               return False;
  673.           }          
  674.           
  675.           $this->checkedgroups['A'][$usrId][$grpId]=True;
  676.           
  677.           if (isset($this->myGroupAdmins[$grpId]&& is_array($this->myGroupAdmins[$grpId]&& in_array($usrId,$this->myGroupAdmins[$grpId]))
  678.           {
  679.               if ($debugechoDebugLine(__FILE__,__LINE__,"returning True");
  680.               return True;
  681.           }
  682.           
  683.           // nothing cached here ask the user for direct assignment
  684.           if (OCSP_OBJ::currentUser()->getId(== $usrId)
  685.           {
  686.               $obj_user OCSP_OBJ::currentUser();
  687.           else if (!($obj_user OCSP_USER::factoryFromId($usrId,$debug))) {
  688.               throw new Exception(_OCSP_EXCEP_NOUSER_);
  689.               if ($debugechoDebugLine(__FILE__,__LINE__,"returning FALSE");
  690.               return false;
  691.           }          
  692.           
  693.           try {
  694.               $arr_adminGrps $obj_user->getDirectAssignedAdminGroups(false,$debug);
  695.           catch (Exception $e{
  696.             if ($debugechoDebugLine(__FILE__,__LINE__,"Exception: " $e->getMessage());
  697.             $arr_adminGrps NULL;
  698.           }
  699.               
  700.         if (is_array($arr_adminGrps))
  701.         {
  702.               $b_found false;
  703.               foreach($arr_adminGrps as $int_grpId)
  704.               {
  705.                 if ($int_grpId == $grpId
  706.                 {
  707.                     $b_found=True;
  708.                 }
  709.                 if (!isset($this->myGroupAdmins[$int_grpId]|| !is_array($this->myGroupAdmins[$int_grpId]|| !in_array($obj_user->getId(),$this->myGroupAdmins[$int_grpId]))
  710.                 {
  711.                     $this->myGroupAdmins[$int_grpId][$obj_user->getId();
  712.                 }
  713.               }
  714.               if ($debugechoDebugLine(__FILE__,__LINE__,"cached: <pre>" print_r($this->myGroupAdmins,True"</pre>");
  715.               $this->hasChanged = True;
  716.               if ($b_found
  717.               {
  718.                   if ($debugechoDebugLine(__FILE__,__LINE__,"returning True");
  719.                   return True;
  720.               }
  721.         }  
  722.  
  723.         if ($int_admGrp intval($this->myGroups[$grpId]['GRP_ADMINGRP']))
  724.         {
  725.             if ($debugecho "<p>checking admingroup " $int_admGrp " for " $grpId "</p>";                
  726.             if ($this->userIsMember($int_admGrp,$usrId,True,$debug))
  727.             {
  728.                 $this->hasChanged = True;
  729.                 $this->myGroupAdmins[$grpId][$usrId;
  730.                 return True;
  731.             }
  732.         }
  733.         
  734.           if ($int_parentId intval($this->myGroups[$grpId]['GRP_PARENT']))
  735.         {
  736.             if ($this->userIsAdmin($int_parentId,$usrId,$debug))
  737.             {
  738.                 $this->myGroupAdmins[$grpId][$usrId;
  739.                 $this->hasChanged = True;
  740.                 return True;
  741.             }
  742.         }        
  743.         
  744.         return $this->userIsMember(1,$usrId)// <pk-08-06-12 />
  745.  
  746.         //return false;
  747.       }
  748.                   
  749.       // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  750.       // db methods
  751.       // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  752.       
  753.       /**
  754.        * adds a user to a group
  755.        *
  756.        * @param int $usrId 
  757.        * @param int $grpId 
  758.        * @param boolean $readOnlay 
  759.        * @param boolean $asAdmin 
  760.        * @param boolean $debug 
  761.        * 
  762.        * @version pk-08-06-12
  763.        */
  764.       public function addUserToGroup($usrId,$grpId,$readOnlay=False,$asAdmin=False,$debug=False)
  765.       {
  766.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::addUserToGroup();");
  767.               
  768.           $bol_canChange False;
  769.           if ($arr_grpRow $this->getGroupRow($grpId))
  770.           {
  771.               if ($this->userIsAdmin($grpId,OCSP_OBJ::currentUser()->getId()))
  772.               {
  773.                   $bol_canChange=True;
  774.               else {
  775.                   if (($arr_grpRow['GRP_SELFSELECT']&& (OCSP_OBJ::currentUser(True)->getId(== $usrId)) 
  776.                   {
  777.                       $bol_canChange=True;
  778.                   }
  779.               }
  780.               
  781.               if ($bol_canChange)
  782.               {
  783.                   $this->myGroupMembers[$grpId][]=$usrId;
  784.                   $arr_row array(
  785.                           'GRP_ID'         => $grpId,
  786.                           'USR_ID'         => $usrId,
  787.                           'GRP_ISADMIN'     => ($asAdmin 0),
  788.                           'GRP_READONLY'     => ($readOnly 0)
  789.                       );
  790.                   $obj_db OCSP_OBJ::currentUser()->getMyDBObj();
  791.                 return $obj_db->replaceArray('T_SYS_GROUPMEMBER',$arr_row);
  792.               else {
  793.                   throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  794.               }
  795.           }          
  796.       }
  797.       
  798.       /**
  799.        * removes a user from a group
  800.        *
  801.        * @param int $usrId 
  802.        * @param int $grpId 
  803.        */
  804.       public function removeUserFromGroup($usrId,$grpId)
  805.       {
  806.           if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::addUserToGroup();");
  807.           
  808.           $bol_canChange False;
  809.           if ($arr_grpRow $this->getGroupRow($grpId))
  810.           {
  811.               if ($this->userIsAdmin($grpId,OCSP_OBJ::currentUser()->getId()))
  812.               {
  813.                   $bol_canChange=True;
  814.               else {
  815.                   if (($arr_grpRow['GRP_SELFSELECT']&& (OCSP_OBJ::currentUser(True)->getId(== $usrId)) 
  816.                   {
  817.                       $bol_canChange=True;
  818.                   }
  819.               }
  820.               
  821.               if ($bol_canChange)
  822.               {
  823.                   $str_cmd "DELETE FROM T_SYS_GROUPMEMBER ";
  824.                   $str_cmd .= " WHERE GRP_ID=" intval($grpId);
  825.                   $str_cmd .= "   AND USR_ID=" intval($usrId);
  826.                   $obj_db OCSP_OBJ::currentUser()->getMyDBObj();
  827.                   $obj_db->executeCmd($str_cmd);
  828.               }
  829.           else {
  830.               throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  831.           }          
  832.       }      
  833.       
  834.  
  835.     /**
  836.      * sets a users admin flag
  837.      *
  838.      * @param int $usrId 
  839.      * @param int $grpId 
  840.      * @param boolean $asAdmin 
  841.      * @param boolean $debug 
  842.      *
  843.      * @since pk-08-06-12
  844.      *
  845.      */
  846.     public function setUsersAdminFlag($usrId,$grpId,$asAdmin,$debug=False)
  847.     {
  848.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::setUsersAdminFlag({$usrId},{$grpId},($asAdimn "True" "False")")");
  849.           $bol_canChange False;
  850.           if ((intval($grpId0&& ($arr_grpRow $this->getGroupRow($grpId)))
  851.           {
  852.               if ($this->userIsAdmin($grpId,OCSP_OBJ::currentUser()->getId()))
  853.               {
  854.                   $bol_canChange=True;
  855.               else {
  856.                   if (($arr_grpRow['GRP_SELFSELECT']&& (OCSP_OBJ::currentUser(True)->getId(== $usrId)) 
  857.                   {
  858.                       $bol_canChange=True;
  859.                   }
  860.               }
  861.               
  862.               if ($bol_canChange)
  863.               {        
  864.                   $str_cmd "UPDATE T_SYS_GROUPMEMBER ";
  865.                 $str_cmd .= " SET GRP_ISADMIN=" ($asAdmin "1" "0");
  866.                   $str_cmd .= " WHERE GRP_ID=" intval($grpId);
  867.                   $str_cmd .= "   AND USR_ID=" intval($usrId);
  868.                   $obj_db OCSP_OBJ::currentUser()->getMyDBObj();
  869.                   $obj_db->executeCmd($str_cmd);
  870.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Command: " $str_cmd);
  871.  
  872.                 if ($asAdmin)
  873.                 {
  874.                     $this->myGroupAdmins[$grpId][]$usrId;
  875.                 else if(isset($this->myGroupAdmins[$grpId])) {
  876.                     $arr_keys array_flip($this->myGroupAdmins[$grpId]);
  877.                     if (isset($arr_keys[$usrId]))
  878.                     {
  879.                         unset($this->myGroupAdmins[$grpId][$arr_keys[$usrId]]);
  880.                     }
  881.                 }
  882.               else {
  883.                   throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  884.               }          
  885.           else {
  886.               throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  887.           }          
  888.       }      
  889.  
  890.     /**
  891.      * sets a users read only flag
  892.      *
  893.      * @param int $usrId 
  894.      * @param int $grpId 
  895.      * @param boolean $asAdmin 
  896.      * @param boolean $debug 
  897.      *
  898.      * @since pk-08-06-12
  899.      *
  900.      */
  901.     public function setUsersROFlag($usrId,$grpId,$readOnly,$debug=False)
  902.     {
  903.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_GROUPTREE::setUsersAdminFlag({$usrId},{$grpId},($readOnly "True" "False")")");
  904.           $bol_canChange False;
  905.           if ((intval($grpId0&& ($arr_grpRow $this->getGroupRow($grpId)))
  906.           {
  907.               if ($this->userIsAdmin($grpId,OCSP_OBJ::currentUser()->getId()))
  908.               {
  909.                   $bol_canChange=True;
  910.               else {
  911.                   if (($arr_grpRow['GRP_SELFSELECT']&& (OCSP_OBJ::currentUser(True)->getId(== $usrId)) 
  912.                   {
  913.                       $bol_canChange=True;
  914.                   }
  915.               }
  916.               
  917.               if ($bol_canChange)
  918.               {        
  919.                   $str_cmd "UPDATE T_SYS_GROUPMEMBER ";
  920.                 $str_cmd .= " SET GRP_READONLY=" ($readOnly "1" "0");
  921.                   $str_cmd .= " WHERE GRP_ID=" intval($grpId);
  922.                   $str_cmd .= "   AND USR_ID=" intval($usrId);
  923.                   $obj_db OCSP_OBJ::currentUser()->getMyDBObj();
  924.                   $obj_db->executeCmd($str_cmd);
  925.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Command: " $str_cmd);
  926.  
  927.               else {
  928.                   throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  929.               }          
  930.           else {
  931.               throw new Exception('_OCSP_EXCEP_NORIGHTS_');
  932.           }          
  933.       }      
  934.  
  935.  
  936. }
  937. ?>

Documentation generated on Thu, 08 Jan 2009 17:46:49 +0100 by phpDocumentor 1.4.0a2