Code

5f68ac99989044c9f93fe27fa87b582c264b41d0
[gosa.git] / gosa-core / include / class_userinfo.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
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  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class userinfo
24 {
25   var $dn;
26   var $ip;
27   var $username;
28   var $cn;
29   var $uid;
30   var $gidNumber= -1;
31   var $language= "";
32   var $config;
33   var $gosaUnitTag= "";
34   var $subtreeACL= array();
35   var $ACL= array();
36   var $ocMapping= array();
37   var $groups= array();
38   var $result_cache =array();
40   /* get acl's an put them into the userinfo object
41      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
42   function userinfo(&$config, $userdn){
43     $this->config= &$config;
44     $ldap= $this->config->get_ldap_link();
45     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
46     $attrs= $ldap->fetch();
48     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
49       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
50     } else {
51       $this->cn= $attrs['uid'][0];
52     }
53     if (isset($attrs['gidNumber'][0])){
54       $this->gidNumber= $attrs['gidNumber'][0];
55     }
57     /* Assign user language */
58     if (isset($attrs['preferredLanguage'][0])){
59       $this->language= $attrs['preferredLanguage'][0];
60     }
62     if (isset($attrs['gosaUnitTag'][0])){
63       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
64     }
66     $this->dn= $userdn;
67     $this->uid= $attrs['uid'][0];
68     $this->ip= $_SERVER['REMOTE_ADDR'];
70     /* Initialize ACL_CACHE */
71     session::set('ACL_CACHE',array());
72     $this->reset_acl_cache();
73   }
76   public function reset_acl_cache()
77   {
78     /* Initialize ACL_CACHE */
79     session::set('ACL_CACHE',array());
80   }
82   function loadACL()
83   {
84     $this->ACL= array();    
85     $this->groups= array();    
86     $this->result_cache =array();
87     $this->reset_acl_cache();
88     $ldap= $this->config->get_ldap_link();
89     $ldap->cd($this->config->current['BASE']);
91     /* Get member groups... */
92     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
93     while ($attrs= $ldap->fetch()){
94       $this->groups[$attrs['dn']]= $attrs['dn'];
95     }
97     /* Crawl through ACLs and move relevant to the tree */
98     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
99     $aclp= array();
100     $aclc= array();
101     while ($attrs= $ldap->fetch()){
103       /* Insert links in ACL array */
104       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
105       $aclc[$attrs['dn']]= array();
106       $ol= array();
107       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
108         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
109       }
110       $aclc[$attrs['dn']]= $ol;
111     }
113     /* Resolve roles here. 
114      */
115     foreach($aclc as $dn => $data){
116       foreach($data as $prio => $aclc_value)  {
117         if($aclc_value['type'] == "role"){
119           unset($aclc[$dn][$prio]);
121           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
122           $attrs = $ldap->fetch();
124           if(isset($attrs['gosaAclTemplate'])){
125             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
126               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
128               foreach($tmp as $new_acl){
129                 $new_acl['members'] = $aclc_value['members'];
130                 $aclc[$dn][] =$new_acl;
131               }
132             }      
133           }
134         }
135       }
136     }
138     /* ACL's read, sort for tree depth */
139     asort($aclp);
141     /* Sort in tree order */
142     foreach ($aclp as $dn => $acl){
143       /* Check if we need to keep this ACL */
144       foreach($aclc[$dn] as $idx => $type){
145         $interresting= FALSE;
146         
147         /* No members? This is good for all users... */
148         if (!count($type['members'])){
149           $interresting= TRUE;
150         } else {
152           /* Inspect members... */
153           foreach ($type['members'] as $grp => $grpdsc){
154             /* Some group inside the members that is relevant for us? */
155             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
156               $interresting= TRUE;
157             }
159             /* User inside the members? */
160             if (preg_replace('/^U:/', '', $grp) == $this->dn){
161               $interresting= TRUE;
162             }
163           }
164         }
166         if ($interresting){
167           if (!isset($this->ACL[$dn])){
168             $this->ACL[$dn]= array();
169           }
170           $this->ACL[$dn][$idx]= $type;
171         }
172       }
174     }
175   }
178   function get_category_permissions($dn, $category)
179   {
180     /* If we are forced to skip ACLs checks for the current user 
181         then return all permissions.
182      */
183     if($this->ignore_acl_for_current_user()){
184       return("rwcdm");
185     }
187     /* Get list of objectClasses and get the permissions for it */
188     $acl= "";
189     if (isset($this->ocMapping[$category])){
190       foreach($this->ocMapping[$category] as $oc){
191         $acl.= $this->get_permissions($dn, $category."/".$oc);
192       }
193     }else{
194       trigger_error("ACL request for an invalid category (".$category.").");
195     }
197     return ($acl);
198   }
201   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
202   {
203     /* If we are forced to skip ACLs checks for the current user 
204         then return all permissions.
205      */
206     if($this->ignore_acl_for_current_user()){
207       return("rwcdm");
208     }
210     /* Push cache answer? */
211     $ACL_CACHE = &session::get('ACL_CACHE');
212     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
214       /* Remove write if needed */
215       if ($skip_write){
216         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
217       }else{
218         $ret = $ACL_CACHE["$dn+$object+$attribute"];
219       } 
220       return($ret);
221     }
223     /* Get ldap object, for later filter checks 
224      */
225     $ldap = $this->config->get_ldap_link();
227     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
229     /* Build dn array */
230     $path= split(',', $dn);
231     $path= array_reverse($path);
233     /* Walk along the path to evaluate the acl */
234     $cpath= "";
235     foreach ($path as $element){
237       /* Clean potential ACLs for each level */
238       $acl= $this->cleanACL($acl);
240       if ($cpath == ""){
241         $cpath= $element;
242       } else {
243         $cpath= $element.','.$cpath;
244       }
245       if (isset($this->ACL[$cpath])){
247         /* Inspect this ACL, place the result into ACL */
248         foreach ($this->ACL[$cpath] as $subacl){
250           /* Reset? Just clean the ACL and turn over to the next one... */
251           if ($subacl['type'] == 'reset'){
252             $acl= $this->cleanACL($acl, TRUE);
253             continue;
254           }
256           if($subacl['type'] == "role") {
257             echo "role skipped";
258             continue;
259           }
261          /* With user filter */
262          if (isset($subacl['filter']) && !empty($subacl['filter'])){
263            $sdn = preg_replace("/^[^,]*+,/","",$dn);
264            $ldap->cd($sdn);
265            $ldap->ls($subacl['filter'],$sdn);
266            if(!$ldap->count()){
267              continue;
268            }else{
269              $found = FALSE; 
270              while($attrs = $ldap->fetch()){
271                if($attrs['dn'] == $dn){
272                  $found = TRUE;
273                  break;
274                }
275              }
276              if(!$found){
277                continue;
278              }
279            }
280          }
282           /* Per attribute ACL? */
283           if (isset($subacl['acl'][$object][$attribute])){
284             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
285             continue;
286           }
288           /* Per object ACL? */
289           if (isset($subacl['acl'][$object][0])){
290             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
291             continue;
292           }
294           /* Global ACL? */
295           if (isset($subacl['acl']['all'][0])){
296             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
297             continue;
298           }
300           /* If attribute is "", we want to know, if we've *any* permissions here... */
301           if ($attribute == "" && isset($subacl['acl'][$object])){
302             foreach($subacl['acl'][$object] as $attr => $dummy){
303               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
304             }
305             continue;
306           }
308         }
309       }
310     }
312     /* Assemble string */
313     $ret= "";
314     foreach ($acl as $key => $value){
315       if ($value !== ""){
316         $ret.= $key;
317       }
318     }
320     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
322     /* Remove write if needed */
323     if ($skip_write){
324       $ret= preg_replace('/w/', '', $ret);
325     }
326     return ($ret);
327   }
330   /* Extract all departments that are accessible (direct or 'on the way' to an
331      accessible department) */
332   function get_module_departments($module)
333   {
334     
335     /* If we are forced to skip ACLs checks for the current user 
336         then return all departments as valid.
337      */
338     if($this->ignore_acl_for_current_user()){
339       return(array_keys($this->config->idepartments));
340     }
342     /* Use cached results if possilbe */
343     $ACL_CACHE = session::get('ACL_CACHE');
344     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
345       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
346     }
348     global $plist;
350     $objects= array();
351     $deps= array();
353     /* Extract all relevant objects for this module from plist */
354     foreach ($plist->info as $object => $info){
355       if (!isset($info['plCategory'])){
356         continue;
357       }
358       foreach ($info['plCategory'] as $idx => $data){
359         if (preg_match('/^[0-9]+$/', $idx)){
360           if ($data == $module){
361             $objects[$object]= $object;
362           }
363         } else {
364           if ($idx == $module){
365             $objects[$object]= $object;
366           }
367         }
368       }
369     }
371     /* For all gosaDepartments */
372     foreach ($this->config->departments as $dn){
373       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
375       /* Build dn array */
376       $path= split(',', $dn);
377       $path= array_reverse($path);
379       /* Walk along the path to evaluate the acl */
380       $cpath= "";
381       foreach ($path as $element){
383         /* Clean potential ACLs for each level */
384         $acl= $this->cleanACL($acl);
386         if ($cpath == ""){
387           $cpath= $element;
388         } else {
389           $cpath= $element.','.$cpath;
390         }
391         if (isset($this->ACL[$cpath])){
393           /* Inspect this ACL, place the result into ACL */
394           foreach ($this->ACL[$cpath] as $subacl){
396             /* Reset? Just clean the ACL and turn over to the next one... */
397             if ($subacl['type'] == 'reset'){
398               $acl= $this->cleanACL($acl, TRUE);
399               continue;
400             }
401     
402             if($subacl['type'] == 'role'){
403               echo "role skipped";
404               continue;
405             }
407             /* Per object ACL? */
408             foreach ($objects as $object){
409               if (isset($subacl['acl']["$module/$object"])){
410                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
411                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
412                 }
413               }
414             }
416             /* Global ACL? */
417             if (isset($subacl['acl']["$module/all"][0])){
418               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
419               continue;
420             }
422             /* Global ACL? */
423             if (isset($subacl['acl']["all"][0])){
424               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
425               continue;
426             }
427           }
428         }
429       }
431       /* Add department, if we have (some) permissions for the required module */
432       foreach ($acl as $val){
433         if ($val != ""){
434           $deps[]= $dn;
435           break;
436         }
437       }
438     }
440     $ACL_CACHE = &session::get('ACL_CACHE');
441     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
442     return ($deps);
443   }
446   function mergeACL($acl, $type, $newACL)
447   {
448     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
449       $newACL .= "r";
450     }
452     foreach(str_split($newACL) as $char){
454       /* Ignore invalid characters */
455       if (!preg_match('/[rwcdm]/', $char)){
456         continue;
457       }
459       /* Skip permanent and subtree entries */
460       if (preg_match('/[sp]/', $acl[$char])){
461         continue;
462       }
464       switch ($type){
465         case 'psub':
466           $acl[$char]= 'p';
467           break;
469         case 'sub':
470           $acl[$char]= 's';
471           break;
473         case 'one':
474           $acl[$char]= 1;
475           break;
477         case 'base':
478           if ($acl[$char] != 1){
479             $acl[$char]= 0;
480           }
481           break;
482       }
483     }
485     return ($acl);
486   }
489   function cleanACL($acl, $reset= FALSE)
490   {
491     foreach ($acl as &$value){
493       /* Reset removes everything but 'p' */
494       if ($reset && $value != 'p'){
495         $value= "";
496         continue;
497       }
499       /* Decrease tree level */
500       if (is_int($value)){
501         if ($value){
502           $value--;
503         } else {
504           $value= "";
505         }
506       }
507     }
509     return ($acl);
510   }
513   /* #FIXME This could be logical wrong or could be optimized in the future
514      Return combined acls for a given category. 
515      All acls will be combined like boolean AND 
516       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
517     
518      Results will be cached in $this->result_cache.
519       $this->result_cache will be resetted if load_acls is called.
520   */
521   function has_complete_category_acls($dn,$category)
522   {
523     $acl    = "rwcdm";
524     $types  = "rwcdm";
526     if(!is_string($category)){
527       trigger_error("category must be string");   
528       $acl = "";
529     }else{
530       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
531         if (isset($this->ocMapping[$category])){
532           foreach($this->ocMapping[$category] as $oc){
534             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
535             if($oc == "0") continue;
536             $tmp =  $this->get_permissions($dn, $category."/".$oc);
537             for($i = 0 ; $i < strlen($types); $i++) {
538               if(!preg_match("/".$types[$i]."/",$tmp)){ 
539                 $acl = preg_replace("/".$types[$i]."/","",$acl);
540               }
541             }
542           }
543         }else{
544           trigger_error("Invalid type of category ".$category);
545           $acl = "";
546         }
547         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
548       }else{
549         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
550       }
551     }
552     return($acl);
553   }
555  
556   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
557       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
558    */ 
559   function ignore_acl_for_current_user()
560   {
561     return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
562   }
566 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
567 ?>