Code

Cache results from get_module_departments.
[gosa.git] / include / class_userinfo.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2005  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class userinfo
22 {
23   var $dn;
24   var $ip;
25   var $username;
26   var $cn;
27   var $uid;
28   var $gidNumber= -1;
29   var $language= "";
30   var $config;
31   var $gosaUnitTag= "";
32   var $subtreeACL= array();
33   var $ACL= array();
34   var $ocMapping= array();
35   var $groups= array();
36   var $result_cache =array();
38   /* get acl's an put them into the userinfo object
39      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
40   function userinfo(&$config, $userdn){
41     $this->config= &$config;
42     $ldap= $this->config->get_ldap_link();
43     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
44     $attrs= $ldap->fetch();
46     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
47       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
48     } else {
49       $this->cn= $attrs['uid'][0];
50     }
51     if (isset($attrs['gidNumber'][0])){
52       $this->gidNumber= $attrs['gidNumber'][0];
53     }
55     /* Assign user language */
56     if (isset($attrs['preferredLanguage'][0])){
57       $this->language= $attrs['preferredLanguage'][0];
58     }
60     if (isset($attrs['gosaUnitTag'][0])){
61       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
62     }
64     $this->dn= $userdn;
65     $this->uid= $attrs['uid'][0];
66     $this->ip= $_SERVER['REMOTE_ADDR'];
68     /* Initialize ACL_CACHE */
69     $_SESSION['ACL_CACHE']= array();
70     $this->reset_acl_cache();
71   }
74   public function reset_acl_cache()
75   {
76     /* Initialize ACL_CACHE */
77     $_SESSION['ACL_CACHE']= array();
78   }
80   function loadACL()
81   {
82     $this->ACL= array();    
83     $this->groups= array();    
84     $this->result_cache =array();
85     $this->reset_acl_cache();
86     $ldap= $this->config->get_ldap_link();
87     $ldap->cd($this->config->current['BASE']);
89     /* Get member groups... */
90     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
91     while ($attrs= $ldap->fetch()){
92       $this->groups[$attrs['dn']]= $attrs['dn'];
93     }
95     /* Crawl through ACLs and move relevant to the tree */
96     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
97     $aclp= array();
98     $aclc= array();
99     while ($attrs= $ldap->fetch()){
101       /* Insert links in ACL array */
102       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
103       $aclc[$attrs['dn']]= array();
104       $ol= array();
105       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
106         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
107       }
108       $aclc[$attrs['dn']]= $ol;
109     }
111     /* Resolve roles here. 
112      */
113     foreach($aclc as $dn => $data){
114       foreach($data as $prio => $aclc_value)  {
115         if($aclc_value['type'] == "role"){
117           unset($aclc[$dn][$prio]);
119           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
120           $attrs = $ldap->fetch();
122           if(isset($attrs['gosaAclTemplate'])){
123             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
124               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
126               foreach($tmp as $new_acl){
127                 $new_acl['members'] = $aclc_value['members'];
128                 $aclc[$dn][] =$new_acl;
129               }
130             }      
131           }
132         }
133       }
134     }
136     /* ACL's read, sort for tree depth */
137     asort($aclp);
139     /* Sort in tree order */
140     foreach ($aclp as $dn => $acl){
141       /* Check if we need to keep this ACL */
142       foreach($aclc[$dn] as $idx => $type){
143         $interresting= FALSE;
144         
145         /* No members? This is good for all users... */
146         if (!count($type['members'])){
147           $interresting= TRUE;
148         } else {
150           /* Inspect members... */
151           foreach ($type['members'] as $grp => $grpdsc){
152             /* Some group inside the members that is relevant for us? */
153             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
154               $interresting= TRUE;
155             }
157             /* User inside the members? */
158             if (preg_replace('/^U:/', '', $grp) == $this->dn){
159               $interresting= TRUE;
160             }
161           }
162         }
164         if ($interresting){
165           if (!isset($this->ACL[$dn])){
166             $this->ACL[$dn]= array();
167           }
168           $this->ACL[$dn][$idx]= $type;
169         }
170       }
172     }
173   }
176   function get_category_permissions($dn, $category)
177   {
178     /* Get list of objectClasses and get the permissions for it */
179     $acl= "";
180     if (isset($this->ocMapping[$category])){
181       foreach($this->ocMapping[$category] as $oc){
182         $acl.= $this->get_permissions($dn, $category."/".$oc);
183       }
184     }
186     return ($acl);
187   }
190   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
191   {
192     /* Push cache answer? */
193     if (isset($_SESSION['ACL_CACHE']["$dn+$object+$attribute"])){
194       return ($_SESSION['ACL_CACHE']["$dn+$object+$attribute"]);
195     }
197     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
199     /* Build dn array */
200     $path= split(',', $dn);
201     $path= array_reverse($path);
203     /* Walk along the path to evaluate the acl */
204     $cpath= "";
205     foreach ($path as $element){
207       /* Clean potential ACLs for each level */
208       $acl= $this->cleanACL($acl);
210       if ($cpath == ""){
211         $cpath= $element;
212       } else {
213         $cpath= $element.','.$cpath;
214       }
215       if (isset($this->ACL[$cpath])){
217         /* Inspect this ACL, place the result into ACL */
218         foreach ($this->ACL[$cpath] as $subacl){
220           /* Reset? Just clean the ACL and turn over to the next one... */
221           if ($subacl['type'] == 'reset'){
222             $acl= $this->cleanACL($acl, TRUE);
223             continue;
224           }
226           if($subacl['type'] == "role") {
227             echo "role skipped";
228             continue;
229           }
231           /* Per attribute ACL? */
232           if (isset($subacl['acl'][$object][$attribute])){
233             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
234             continue;
235           }
237           /* Per object ACL? */
238           if (isset($subacl['acl'][$object][0])){
239             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
240             continue;
241           }
243           /* Global ACL? */
244           if (isset($subacl['acl']['all'][0])){
245             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
246             continue;
247           }
249           /* If attribute is "", we want to know, if we've *any* permissions here... */
250           if ($attribute == "" && isset($subacl['acl'][$object])){
251             foreach($subacl['acl'][$object] as $attr => $dummy){
252               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
253             }
254             continue;
255           }
257         }
258       }
259     }
261     /* Assemble string */
262     $ret= "";
263     foreach ($acl as $key => $value){
264       if ($value != ""){
265         $ret.= $key;
266       }
267     }
269     /* Remove write if needed */
270     if ($skip_write){
271       $ret= preg_replace('/w/', '', $ret);
272     }
274     $_SESSION['ACL_CACHE']["$dn+$object+$attribute"]= $ret;
275     return ($ret);
276   }
279   /* Extract all departments that are accessible (direct or 'on the way' to an
280      accessible department) */
281   function get_module_departments($module)
282   {
283     /* Use cached results if possilbe */
284     if(isset($_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)])){
285       return($_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)]);
286     }
288     global $plist;
290     $objects= array();
291     $deps= array();
293     /* Extract all relevant objects for this module from plist */
294     foreach ($plist->info as $object => $info){
295       if (!isset($info['plCategory'])){
296         continue;
297       }
298       foreach ($info['plCategory'] as $idx => $data){
299         if (preg_match('/^[0-9]+$/', $idx)){
300           if ($data == $module){
301             $objects[$object]= $object;
302           }
303         } else {
304           if ($idx == $module){
305             $objects[$object]= $object;
306           }
307         }
308       }
309     }
311     /* For all gosaDepartments */
312     foreach ($this->config->departments as $dn){
313       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
315       /* Build dn array */
316       $path= split(',', $dn);
317       $path= array_reverse($path);
319       /* Walk along the path to evaluate the acl */
320       $cpath= "";
321       foreach ($path as $element){
323         /* Clean potential ACLs for each level */
324         $acl= $this->cleanACL($acl);
326         if ($cpath == ""){
327           $cpath= $element;
328         } else {
329           $cpath= $element.','.$cpath;
330         }
331         if (isset($this->ACL[$cpath])){
333           /* Inspect this ACL, place the result into ACL */
334           foreach ($this->ACL[$cpath] as $subacl){
336             /* Reset? Just clean the ACL and turn over to the next one... */
337             if ($subacl['type'] == 'reset'){
338               $acl= $this->cleanACL($acl, TRUE);
339               continue;
340             }
341     
342             if($subacl['type'] == 'role'){
343               echo "role skipped";
344               continue;
345             }
347             /* Per object ACL? */
348             foreach ($objects as $object){
349               if (isset($subacl['acl']["$module/$object"])){
350                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
351                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
352                 }
353               }
354             }
356             /* Global ACL? */
357             if (isset($subacl['acl']["$module/all"][0])){
358               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
359               continue;
360             }
362             /* Global ACL? */
363             if (isset($subacl['acl']["all"][0])){
364               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
365               continue;
366             }
367           }
368         }
369       }
371       /* Add department, if we have (some) permissions for the required module */
372       foreach ($acl as $val){
373         if ($val != ""){
374           $deps[]= $dn;
375           break;
376         }
377       }
378     }
380     $_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
381     return ($deps);
382   }
385   function mergeACL($acl, $type, $newACL)
386   {
387     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
388       $newACL .= "r";
389     }
391     foreach(str_split($newACL) as $char){
393       /* Ignore invalid characters */
394       if (!preg_match('/[rwcdm]/', $char)){
395         continue;
396       }
398       /* Skip permanent and subtree entries */
399       if (preg_match('/[sp]/', $acl[$char])){
400         continue;
401       }
403       switch ($type){
404         case 'psub':
405           $acl[$char]= 'p';
406           break;
408         case 'sub':
409           $acl[$char]= 's';
410           break;
412         case 'one':
413           $acl[$char]= 1;
414           break;
416         case 'base':
417           if ($acl[$char] != 1){
418             $acl[$char]= 0;
419           }
420           break;
421       }
422     }
424     return ($acl);
425   }
428   function cleanACL($acl, $reset= FALSE)
429   {
430     foreach ($acl as &$value){
432       /* Reset removes everything but 'p' */
433       if ($reset && $value != 'p'){
434         $value= "";
435         continue;
436       }
438       /* Decrease tree level */
439       if (is_int($value)){
440         if ($value){
441           $value--;
442         } else {
443           $value= "";
444         }
445       }
446     }
448     return ($acl);
449   }
452   /* #FIXME This could be logical wrong or could be optimized in the future
453      Return combined acls for a given category. 
454      All acls will be combined like boolean AND 
455       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
456     
457      Results will be cached in $this->result_cache.
458       $this->result_cache will be resetted if load_acls is called.
459   */
460   function has_complete_category_acls($dn,$category)
461   {
462     $acl    = "rwcdm";
463     $types  = "rwcdm";
465     if(!is_string($category)){
466       trigger_error("category must be string");   
467       $acl = "";
468     }else{
469       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
470         if (isset($this->ocMapping[$category])){
471           foreach($this->ocMapping[$category] as $oc){
473             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
474             if($oc == "0") continue;
475             $tmp =  $this->get_permissions($dn, $category."/".$oc);
476             for($i = 0 ; $i < strlen($types); $i++) {
477               if(!preg_match("/".$types[$i]."/",$tmp)){ 
478                 $acl = preg_replace("/".$types[$i]."/","",$acl);
479               }
480             }
481           }
482         }else{
483           trigger_error("Invalid type of category ".$category);
484           $acl = "";
485         }
486         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
487       }else{
488         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
489       }
490     }
491     return($acl);
492   }
495 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
496 ?>