Code

More cleanups
[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();
39   var $ignoreACl = FALSE;
40   var $ACLperPath = array();
41   var $ACLperPath_usesFilter = array();
43   /* get acl's an put them into the userinfo object
44      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
45   function userinfo(&$config, $userdn){
46     $this->config= &$config;
47     $ldap= $this->config->get_ldap_link();
48     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
49     $attrs= $ldap->fetch();
51     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
52       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
53     } else {
54       $this->cn= $attrs['uid'][0];
55     }
56     if (isset($attrs['gidNumber'][0])){
57       $this->gidNumber= $attrs['gidNumber'][0];
58     }
60     /* Assign user language */
61     if (isset($attrs['preferredLanguage'][0])){
62       $this->language= $attrs['preferredLanguage'][0];
63     }
65     if (isset($attrs['gosaUnitTag'][0])){
66       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
67     }
69     $this->dn= $userdn;
70     $this->uid= $attrs['uid'][0];
71     $this->ip= $_SERVER['REMOTE_ADDR'];
73     $this->ignoreACL = ($this->config->get_cfg_value("ignoreAcl") == $this->dn);
75     /* Initialize ACL_CACHE */
76     $this->reset_acl_cache();
77   }
80   public function reset_acl_cache()
81   {
82     /* Initialize ACL_CACHE */
83     session::set('ACL_CACHE',array());
84   }
86   function loadACL()
87   {
88     $this->ACL= array();    
89     $this->groups= array();    
90     $this->result_cache =array();
91     $this->reset_acl_cache();
92     $ldap= $this->config->get_ldap_link();
93     $ldap->cd($this->config->current['BASE']);
95     /* Get member groups... */
96     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
97     while ($attrs= $ldap->fetch()){
98       $this->groups[$attrs['dn']]= $attrs['dn'];
99     }
101     /* Crawl through ACLs and move relevant to the tree */
102     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
103     $aclp= array();
104     $aclc= array();
105     while ($attrs= $ldap->fetch()){
107       /* Insert links in ACL array */
108       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
109       $aclc[$attrs['dn']]= array();
110       $ol= array();
111       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
112         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
113       }
114       $aclc[$attrs['dn']]= $ol;
115     }
117     /* Resolve roles here. 
118      */
119     foreach($aclc as $dn => $data){
120       foreach($data as $prio => $aclc_value)  {
121         if($aclc_value['type'] == "role"){
123           unset($aclc[$dn][$prio]);
125           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
126           $attrs = $ldap->fetch();
128           if(isset($attrs['gosaAclTemplate'])){
129             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
130               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
132               foreach($tmp as $new_acl){
134                 /* Keep non role attributes here! */
135                 $new_acl['filter'] = $aclc_value['filter'];
136                 $new_acl['members'] = $aclc_value['members'];
137                 $aclc[$dn][] =$new_acl;
138               }
139             }      
140           }
141         }
142       }
143     }
145     /* ACL's read, sort for tree depth */
146     asort($aclp);
148     /* Sort in tree order */
149     foreach ($aclp as $dn => $acl){
150       /* Check if we need to keep this ACL */
151       foreach($aclc[$dn] as $idx => $type){
152         $interresting= FALSE;
153         
154         /* No members? This is good for all users... */
155         if (!count($type['members'])){
156           $interresting= TRUE;
157         } else {
159           /* Inspect members... */
160           foreach ($type['members'] as $grp => $grpdsc){
161             /* Some group inside the members that is relevant for us? */
162             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
163               $interresting= TRUE;
164             }
166             /* User inside the members? */
167             if (preg_replace('/^U:/', '', $grp) == $this->dn){
168               $interresting= TRUE;
169             }
170           }
171         }
173         if ($interresting){
174           if (!isset($this->ACL[$dn])){
175             $this->ACL[$dn]= array();
176           }
177           $this->ACL[$dn][$idx]= $type;
178         }
179       }
180     }
182     /* Create an array which represenet all relevant permissions settings 
183         per dn.
184      */
185     $tmp = array();
186     foreach($this->ACL as $dn => $acl){
187       $sdn =$dn;
188       while(strpos($dn,",") !== FALSE){
189         if(isset($this->ACL[$dn])){
190           $tmp[$sdn][$dn] = $this->ACL[$dn];
191           foreach($this->ACL[$dn] as $aclset){
192             if(isset($aclset['filter']{1})){
193               $this->ACLperPath_usesFilter[$sdn] = TRUE;
194             }
195           }
196         }
197         $dn = preg_replace("/^[^,]*+,/","",$dn);
198       }
199     } 
200     $this->ACLperPath =$tmp;
201   }
204   function get_category_permissions($dn, $category, $any_acl = FALSE)
205   {
206     return(@$this->get_permissions($dn,$category.'/0'));
207   }
209   
210   /*! \brief Check if the given object (dn) is copyable
211       @param  String The object dn 
212       @param  String The acl  category (e.g. users) 
213       @param  String The acl  class (e.g. user) 
214       @return Boolean   TRUE if the given object is copyable else FALSE 
215   */
216   function is_copyable($dn, $object, $class)
217   {
218     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
219   }
222   /*! \brief Check if the given object (dn) is cutable
223       @param  String The object dn 
224       @param  String The acl  category (e.g. users) 
225       @param  String The acl  class (e.g. user) 
226       @return Boolean   TRUE if the given object is cutable else FALSE 
227   */
228   function is_cutable($dn, $object, $class)
229   {
230     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
231     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
232     return($remove && $read);
233   }
236   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
237       @param  String The destination dn 
238       @param  String The acl  category (e.g. users) 
239       @param  String The acl  class (e.g. user) 
240       @return Boolean   TRUE if we are allowed to paste an object.
241   */
242   function is_pasteable($dn, $object)
243   {
244     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
245   }
248   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
249       @param  String The destination dn 
250       @param  String The acl  category (e.g. users) 
251       @return Boolean   TRUE if we are allowed to restore a snapshot.
252   */
253   function allow_snapshot_restore($dn, $object)
254   {
255     if(!is_array($object)){
256       $object = array($object);
257     }
258     $r = $w = TRUE;
259     foreach($object as $category){
260       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
261       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
262     }
263     return($r && $w ); 
264   }  
267   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
268       @param  String The source dn 
269       @param  String The acl category (e.g. users) 
270       @return Boolean   TRUE if we are allowed to restore a snapshot.
271   */
272   function allow_snapshot_create($dn, $object)
273   {
274     if(!is_array($object)){
275       $object = array($object);
276     }
277     $r = TRUE;
278     foreach($object as $category){
279       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
280     }
281     return($r) ; 
282   }  
285   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
286   {
287     /* If we are forced to skip ACLs checks for the current user 
288         then return all permissions.
289      */
290     if($this->ignore_acl_for_current_user()){
291       if($skip_write){
292         return("rcdm");
293       }
294       return("rwcdm");
295     }
297     /* Push cache answer? */
298     $ACL_CACHE = &session::get('ACL_CACHE');
299     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
300       $ret = $ACL_CACHE["$dn+$object+$attribute"];
301       if($skip_write){
302         $ret = str_replace('w', '',$ret);
303       }
304       return($ret);
305     }
307     /* Detect the set of ACLs we have to check for this object 
308      */
309     $adn = $dn;
310     while(!isset($this->ACLperPath[$adn]) && strpos($adn,",") !== FALSE){
311       $adn = preg_replace("/^[^,]*+,/","",$adn);
312     }
313     if(isset($this->ACLperPath[$adn])){
314       $ACL = $this->ACLperPath[$adn];
315     }else{
316       $ACL_CACHE["$dn+$object+$attribute"] = "";
317       return("");
318     }
319  
320     /* If we do not need to respect any user-filter settings 
321         we can skip the per object ACL checks.
322      */
323     $orig_dn= $dn;
324     if(!isset($this->ACLperPath_usesFilter[$adn])){
325       $dn = $adn;
326       if (isset($ACL_CACHE["$dn+$object+$attribute"])){
327         $ret = $ACL_CACHE["$dn+$object+$attribute"];
328         if(!isset($ACL_CACHE["$orig_dn+$object+$attribute"])){
329           $ACL_CACHE["$orig_dn+$object+$attribute"] = $ACL_CACHE["$dn+$object+$attribute"];
330         }
331         if($skip_write){
332           $ret = str_replace('w','',$ret);
333         }
334         return($ret);
335       }
336     }
337  
338     /* Get ldap object, for later filter checks 
339      */
340     $ldap = $this->config->get_ldap_link();
342     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
344     /* Build dn array */
345     $path= split(',', $dn);
346     $path= array_reverse($path);
348     /* Walk along the path to evaluate the acl */
349     $cpath= "";
350     foreach ($path as $element){
352       /* Clean potential ACLs for each level */
353                         if(isset($this->config->idepartments[$cpath])){
354         $acl= $this->cleanACL($acl);
355       }
357       if ($cpath == ""){
358         $cpath= $element;
359       } else {
360         $cpath= $element.','.$cpath;
361       }
363       if (isset($ACL[$cpath])){
365         /* Inspect this ACL, place the result into ACL */
366         foreach ($ACL[$cpath] as $subacl){
368           /* Reset? Just clean the ACL and turn over to the next one... */
369           if ($subacl['type'] == 'reset'){
370             $acl= $this->cleanACL($acl, TRUE);
371             continue;
372           }
374           if($subacl['type'] == "role") {
375             echo "role skipped";
376             continue;
377           }
379           /* With user filter */
380           if (isset($subacl['filter']) && !empty($subacl['filter'])){
381             $id = $dn."-".$subacl['filter'];
382             if(!isset($ACL_CACHE['FILTER'][$id])){
383               $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
384             }
385             if(!$ACL_CACHE['FILTER'][$id]){
386               continue;
387             }
388           }
390           /* Self ACLs? 
391            */
392           if($dn != $this->dn && isset($subacl['acl'][$object][0]) && strpos($subacl['acl'][$object][0],"s")){
393             continue;
394           }
396           /* If attribute is "", we want to know, if we've *any* permissions here... 
397               Merge global class ACLs [0] with attributes specific ACLs [attribute].
398            */
399           if ($attribute == "" && isset($subacl['acl'][$object])){
400             foreach($subacl['acl'][$object] as $attr => $dummy){
401               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
402             }
403             continue;
404           }
406           /* Per attribute ACL? */
407           if (isset($subacl['acl'][$object][$attribute])){
408             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
409             continue;
410           }
412           /* Per object ACL? */
413           if (isset($subacl['acl'][$object][0])){
414             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
415             continue;
416           }
418           /* Global ACL? */
419           if (isset($subacl['acl']['all'][0])){
420             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
421             continue;
422           }
424           /* Category ACLs */
425           if(strstr($object,"/0")){
426             $ocs = preg_replace("/\/0$/","",$object);
427             if(isset($this->ocMapping[$ocs]))
428             foreach($this->ocMapping[$ocs] as $oc){
429               if(isset($subacl['acl'][$ocs.'/'.$oc][0])){
430                 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][0]);
431               }
432             }
433             continue;
434           }
435         }
436       }
437     }
439     /* If the requested ACL is for a container object, then alter 
440         ACLs by applying cleanACL a last time.
441      */
442     if(isset($this->config->idepartments[$dn])){
443       $acl = $this->cleanACL($acl);
444     }
446     /* Assemble string */
447     $ret= "";
448     foreach ($acl as $key => $value){
449       if ($value !== ""){
450         $ret.= $key;
451       }
452     }
454     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
455     $ACL_CACHE["$orig_dn+$object+$attribute"]= $ret;
457     /* Remove write if needed */
458     if ($skip_write){
459       $ret= str_replace('w', '', $ret);
460     }
461     return ($ret);
462   }
465   /* Extract all departments that are accessible (direct or 'on the way' to an
466      accessible department) */
467   function get_module_departments($module, $skip_self_acls = FALSE )
468   {
469     /* If we are forced to skip ACLs checks for the current user 
470         then return all departments as valid.
471      */
472     if($this->ignore_acl_for_current_user()){
473       return(array_keys($this->config->idepartments));
474     }
476     /* Use cached results if possilbe */
477     $ACL_CACHE = &session::get('ACL_CACHE');
479     if(!is_array($module)){
480       $module = array($module);
481     }
483     global $plist;
484     $res = array();
485     foreach($module as $mod){
486       if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
487         $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
488         continue;
489       }
491       $deps = array();
493       /* Search for per object ACLs */
494       foreach($this->ACL as $dn => $infos){
495         foreach($infos as $info){
496           $found = FALSE;
497           foreach($info['acl'] as $cat => $data){
499             /* Skip self acls? */
500             if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
501             if(preg_match("/^".preg_quote($mod, '/')."/",$cat)){
502               $found =TRUE;
503               break;
504             }
505           } 
507           if($found && !isset($this->config->idepartments[$dn])){
508             while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
509               $dn = preg_replace("/^[^,]+,/","",$dn);
510             }
511             if(isset($this->config->idepartments[$dn])){
512               $deps[] = $dn;
513             }
514           }
515         }
516       }
518       /* For all gosaDepartments */
519       foreach ($this->config->departments as $dn){
520         if(in_array($dn,$deps)) continue;
521         $acl = "";
522         if(strpos($mod, '/')){
523           $acl.=  $this->get_permissions($dn,$mod);
524         }else{
525           $acl.=  $this->get_category_permissions($dn,$mod,TRUE);
526         }
527         if(!empty($acl)) {
528           $deps[] = $dn;
529         }
530       }
532       $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
533       $res = array_merge($res,$deps);
534     } 
535     return ($res);
536   }
539   function mergeACL($acl, $type, $newACL)
540   {
541                 $at= array("psub" => "p", "sub" => "s", "one" => "1");
543     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
544       $newACL .= "r";
545     }
547                 /* Ignore invalid characters */
548                 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
550     foreach(str_split($newACL) as $char){
552       /* Skip permanent and subtree entries */
553       if (!isset($acl[$char]) || preg_match('/[sp]/', $acl[$char])){
554         continue;
555       }
557                         if ($type == "base" && $acl[$char] != 1) {
558                                 $acl[$char]= 0;
559                         } else {
560         $acl[$char]= $at[$type];
561                         }
562     }
564     return ($acl);
565   }
568   function cleanACL($acl, $reset= FALSE)
569   {
570     foreach ($acl as $key => $value){
572       /* Continue, if value is empty or permanent */
573       if ($value == "" || $value == "p") {
574             continue;
575       }
577       /* Reset removes everything but 'p' */
578       if ($reset && $value != 'p'){
579         $acl[$key]= "";
580         continue;
581       }
583       /* Decrease tree level */
584       if (is_int($value)){
585         if ($value){
586           $acl[$key]--;
587         } else {
588           $acl[$key]= "";
589         }
590       }
591     }
593     return ($acl);
594   }
597   /* #FIXME This could be logical wrong or could be optimized in the future
598      Return combined acls for a given category. 
599      All acls will be combined like boolean AND 
600       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
601     
602      Results will be cached in $this->result_cache.
603       $this->result_cache will be resetted if load_acls is called.
604   */
605   function has_complete_category_acls($dn,$category)
606   {
607     $acl    = "rwcdm";
608     $types  = "rwcdm";
610     if(!is_string($category)){
611       trigger_error("category must be string");   
612       $acl = "";
613     }else{
614       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
615         if (isset($this->ocMapping[$category])){
616           foreach($this->ocMapping[$category] as $oc){
618             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
619             if($oc == "0") continue;
620             $tmp =  $this->get_permissions($dn, $category."/".$oc);
621             for($i = 0, $l= strlen($types); $i < $l; $i++) {
622               if(!preg_match("/".$types[$i]."/",$tmp)){ 
623                 $acl = preg_replace("/".$types[$i]."/","",$acl);
624               }
625             }
626           }
627         }else{
628           trigger_error("Invalid type of category ".$category);
629           $acl = "";
630         }
631         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
632       }else{
633         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
634       }
635     }
636     return($acl);
637   }
639  
640   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
641       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
642    */ 
643   function ignore_acl_for_current_user()
644   {
645     return($this->ignoreACL);
646   }
650 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
651 ?>