Code

Removed get_sub_list when not in subsearch
[gosa.git] / gosa-core / 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::set('ACL_CACHE',array());
70     $this->reset_acl_cache();
71   }
74   public function reset_acl_cache()
75   {
76     /* Initialize ACL_CACHE */
77     session::set('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     $ACL_CACHE = session::get('ACL_CACHE');
194     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
196       /* Remove write if needed */
197       if ($skip_write){
198         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
199       }else{
200         $ret = $ACL_CACHE["$dn+$object+$attribute"];
201       } 
202       return($ret);
203     }
205     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
207     /* Build dn array */
208     $path= split(',', $dn);
209     $path= array_reverse($path);
211     /* Walk along the path to evaluate the acl */
212     $cpath= "";
213     foreach ($path as $element){
215       /* Clean potential ACLs for each level */
216       $acl= $this->cleanACL($acl);
218       if ($cpath == ""){
219         $cpath= $element;
220       } else {
221         $cpath= $element.','.$cpath;
222       }
223       if (isset($this->ACL[$cpath])){
225         /* Inspect this ACL, place the result into ACL */
226         foreach ($this->ACL[$cpath] as $subacl){
228           /* Reset? Just clean the ACL and turn over to the next one... */
229           if ($subacl['type'] == 'reset'){
230             $acl= $this->cleanACL($acl, TRUE);
231             continue;
232           }
234           if($subacl['type'] == "role") {
235             echo "role skipped";
236             continue;
237           }
239           /* Per attribute ACL? */
240           if (isset($subacl['acl'][$object][$attribute])){
241             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
242             continue;
243           }
245           /* Per object ACL? */
246           if (isset($subacl['acl'][$object][0])){
247             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
248             continue;
249           }
251           /* Global ACL? */
252           if (isset($subacl['acl']['all'][0])){
253             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
254             continue;
255           }
257           /* If attribute is "", we want to know, if we've *any* permissions here... */
258           if ($attribute == "" && isset($subacl['acl'][$object])){
259             foreach($subacl['acl'][$object] as $attr => $dummy){
260               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
261             }
262             continue;
263           }
265         }
266       }
267     }
269     /* Assemble string */
270     $ret= "";
271     foreach ($acl as $key => $value){
272       if ($value != ""){
273         $ret.= $key;
274       }
275     }
277     $ACL_CACHE = session::get('ACL_CACHE');
278     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
279     session::set('ACL_CACHE',$ACL_CACHE);
281     /* Remove write if needed */
282     if ($skip_write){
283       $ret= preg_replace('/w/', '', $ret);
284     }
285     return ($ret);
286   }
289   /* Extract all departments that are accessible (direct or 'on the way' to an
290      accessible department) */
291   function get_module_departments($module)
292   {
293     /* Use cached results if possilbe */
294     $ACL_CACHE = session::get('ACL_CACHE');
295     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
296       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
297     }
299     global $plist;
301     $objects= array();
302     $deps= array();
304     /* Extract all relevant objects for this module from plist */
305     foreach ($plist->info as $object => $info){
306       if (!isset($info['plCategory'])){
307         continue;
308       }
309       foreach ($info['plCategory'] as $idx => $data){
310         if (preg_match('/^[0-9]+$/', $idx)){
311           if ($data == $module){
312             $objects[$object]= $object;
313           }
314         } else {
315           if ($idx == $module){
316             $objects[$object]= $object;
317           }
318         }
319       }
320     }
322     /* For all gosaDepartments */
323     foreach ($this->config->departments as $dn){
324       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
326       /* Build dn array */
327       $path= split(',', $dn);
328       $path= array_reverse($path);
330       /* Walk along the path to evaluate the acl */
331       $cpath= "";
332       foreach ($path as $element){
334         /* Clean potential ACLs for each level */
335         $acl= $this->cleanACL($acl);
337         if ($cpath == ""){
338           $cpath= $element;
339         } else {
340           $cpath= $element.','.$cpath;
341         }
342         if (isset($this->ACL[$cpath])){
344           /* Inspect this ACL, place the result into ACL */
345           foreach ($this->ACL[$cpath] as $subacl){
347             /* Reset? Just clean the ACL and turn over to the next one... */
348             if ($subacl['type'] == 'reset'){
349               $acl= $this->cleanACL($acl, TRUE);
350               continue;
351             }
352     
353             if($subacl['type'] == 'role'){
354               echo "role skipped";
355               continue;
356             }
358             /* Per object ACL? */
359             foreach ($objects as $object){
360               if (isset($subacl['acl']["$module/$object"])){
361                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
362                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
363                 }
364               }
365             }
367             /* Global ACL? */
368             if (isset($subacl['acl']["$module/all"][0])){
369               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
370               continue;
371             }
373             /* Global ACL? */
374             if (isset($subacl['acl']["all"][0])){
375               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
376               continue;
377             }
378           }
379         }
380       }
382       /* Add department, if we have (some) permissions for the required module */
383       foreach ($acl as $val){
384         if ($val != ""){
385           $deps[]= $dn;
386           break;
387         }
388       }
389     }
391     $ACL_CACHE = session::get('ACL_CACHE');
392     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
393     session::set('ACL_CACHE',$ACL_CACHE);
394     return ($deps);
395   }
398   function mergeACL($acl, $type, $newACL)
399   {
400     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
401       $newACL .= "r";
402     }
404     foreach(str_split($newACL) as $char){
406       /* Ignore invalid characters */
407       if (!preg_match('/[rwcdm]/', $char)){
408         continue;
409       }
411       /* Skip permanent and subtree entries */
412       if (preg_match('/[sp]/', $acl[$char])){
413         continue;
414       }
416       switch ($type){
417         case 'psub':
418           $acl[$char]= 'p';
419           break;
421         case 'sub':
422           $acl[$char]= 's';
423           break;
425         case 'one':
426           $acl[$char]= 1;
427           break;
429         case 'base':
430           if ($acl[$char] != 1){
431             $acl[$char]= 0;
432           }
433           break;
434       }
435     }
437     return ($acl);
438   }
441   function cleanACL($acl, $reset= FALSE)
442   {
443     foreach ($acl as &$value){
445       /* Reset removes everything but 'p' */
446       if ($reset && $value != 'p'){
447         $value= "";
448         continue;
449       }
451       /* Decrease tree level */
452       if (is_int($value)){
453         if ($value){
454           $value--;
455         } else {
456           $value= "";
457         }
458       }
459     }
461     return ($acl);
462   }
465   /* #FIXME This could be logical wrong or could be optimized in the future
466      Return combined acls for a given category. 
467      All acls will be combined like boolean AND 
468       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
469     
470      Results will be cached in $this->result_cache.
471       $this->result_cache will be resetted if load_acls is called.
472   */
473   function has_complete_category_acls($dn,$category)
474   {
475     $acl    = "rwcdm";
476     $types  = "rwcdm";
478     if(!is_string($category)){
479       trigger_error("category must be string");   
480       $acl = "";
481     }else{
482       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
483         if (isset($this->ocMapping[$category])){
484           foreach($this->ocMapping[$category] as $oc){
486             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
487             if($oc == "0") continue;
488             $tmp =  $this->get_permissions($dn, $category."/".$oc);
489             for($i = 0 ; $i < strlen($types); $i++) {
490               if(!preg_match("/".$types[$i]."/",$tmp)){ 
491                 $acl = preg_replace("/".$types[$i]."/","",$acl);
492               }
493             }
494           }
495         }else{
496           trigger_error("Invalid type of category ".$category);
497           $acl = "";
498         }
499         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
500       }else{
501         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
502       }
503     }
504     return($acl);
505   }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>