Code

Prepared plugins to support "read only" mode. Added third button to the "locked entry...
[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.
185       The array will look like this:
186       
187       .     ['ou=base']        ['ou=base']          = array(ACLs);
188       .     
189       .     ['ou=dep1,ou=base']['ou=dep1,ou=base']  = array(ACLs);
190       .                        ['ou=base']          = array(ACLs);
193       For object located in 'ou=dep1,ou=base' we have to both ACLs,
194        for objects in 'ou=base' we only have to apply on ACL.
195      */
196     $without_self_acl = $all_acl = array();
197     foreach($this->ACL as $dn => $acl){
198       $sdn =$dn;
199       while(strpos($dn,",") !== FALSE){
201         if(isset($this->ACL[$dn])){
202           $all_acl[$sdn][$dn] = $this->ACL[$dn];
203           $without_self_acl[$sdn][$dn] = $this->ACL[$dn]; 
204           foreach($without_self_acl[$sdn][$dn] as $acl_id => $acl_set){
205   
206             /* Remember which ACL set has speicial user filter 
207              */
208             if(isset($acl_set['filter']{1})){
209               $this->ACLperPath_usesFilter[$sdn] = TRUE;
210             }
211           
212             /* Remove all acl entries which are especially for the current user (self acl)
213              */
214             foreach($acl_set['acl'] as $object => $object_acls){
215               if(strpos($object_acls[0],"s")){
216                 unset($without_self_acl[$sdn][$dn][$acl_id]['acl'][$object]);
217               }
218             }
219           }
220         }
221         $dn = preg_replace("/^[^,]*+,/","",$dn);
222       }
223     } 
224     $this->ACLperPath =$without_self_acl;
226     /* Append Self entry */
227     $dn = $this->dn;
228     while(strpos($dn,",") && !isset($all_acl[$dn])){
229       $dn = preg_replace("/^[^,]*+,/","",$dn);
230     }
231     if(isset($all_acl[$dn])){
232       $this->ACLperPath[$this->dn] = $all_acl[$dn];
233     }
234   }
237   function get_category_permissions($dn, $category, $any_acl = FALSE)
238   {
239     return(@$this->get_permissions($dn,$category.'/0',""));
240   }
242   
243   /*! \brief Check if the given object (dn) is copyable
244       @param  String The object dn 
245       @param  String The acl  category (e.g. users) 
246       @param  String The acl  class (e.g. user) 
247       @return Boolean   TRUE if the given object is copyable else FALSE 
248   */
249   function is_copyable($dn, $object, $class)
250   {
251     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
252   }
255   /*! \brief Check if the given object (dn) is cutable
256       @param  String The object dn 
257       @param  String The acl  category (e.g. users) 
258       @param  String The acl  class (e.g. user) 
259       @return Boolean   TRUE if the given object is cutable else FALSE 
260   */
261   function is_cutable($dn, $object, $class)
262   {
263     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
264     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
265     return($remove && $read);
266   }
269   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
270       @param  String The destination dn 
271       @param  String The acl  category (e.g. users) 
272       @param  String The acl  class (e.g. user) 
273       @return Boolean   TRUE if we are allowed to paste an object.
274   */
275   function is_pasteable($dn, $object)
276   {
277     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
278   }
281   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
282       @param  String The destination dn 
283       @param  String The acl  category (e.g. users) 
284       @return Boolean   TRUE if we are allowed to restore a snapshot.
285   */
286   function allow_snapshot_restore($dn, $object)
287   {
288     if(!is_array($object)){
289       $object = array($object);
290     }
291     $r = $w = TRUE;
292     foreach($object as $category){
293       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
294       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
295     }
296     return($r && $w ); 
297   }  
300   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
301       @param  String The source dn 
302       @param  String The acl category (e.g. users) 
303       @return Boolean   TRUE if we are allowed to restore a snapshot.
304   */
305   function allow_snapshot_create($dn, $object)
306   {
307     if(!is_array($object)){
308       $object = array($object);
309     }
310     $r = TRUE;
311     foreach($object as $category){
312       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
313     }
314     return($r) ; 
315   }  
318   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
319   {
320     /* If we are forced to skip ACLs checks for the current user 
321         then return all permissions.
322      */
323     if($this->ignore_acl_for_current_user()){
324       if($skip_write){
325         return("rcdm");
326       }
327       return("rwcdm");
328     }
330     /* Push cache answer? */
331     $ACL_CACHE = &session::get('ACL_CACHE');
332     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
333       $ret = $ACL_CACHE["$dn+$object+$attribute"];
334       if($skip_write){
335         $ret = str_replace(array('w','c','d','m'), '',$ret);
336       }
337       return($ret);
338     }
340     /* Detect the set of ACLs we have to check for this object 
341      */
342     $adn = $dn;
343     while(!isset($this->ACLperPath[$adn]) && strpos($adn,",") !== FALSE){
344       $adn = preg_replace("/^[^,]*+,/","",$adn);
345     }
346     if(isset($this->ACLperPath[$adn])){
347       $ACL = $this->ACLperPath[$adn];
348     }else{
349       $ACL_CACHE["$dn+$object+$attribute"] = "";
350       return("");
351     }
352  
353     /* If we do not need to respect any user-filter settings 
354         we can skip the per object ACL checks.
355      */
356     $orig_dn= $dn;
357     if(!isset($this->ACLperPath_usesFilter[$adn])){
358       $dn = $adn;
359       if (isset($ACL_CACHE["$dn+$object+$attribute"])){
360         $ret = $ACL_CACHE["$dn+$object+$attribute"];
361         if(!isset($ACL_CACHE["$orig_dn+$object+$attribute"])){
362           $ACL_CACHE["$orig_dn+$object+$attribute"] = $ACL_CACHE["$dn+$object+$attribute"];
363         }
364         if($skip_write){
365           $ret = str_replace('w','',$ret);
366         }
367         return($ret);
368       }
369     }
370  
371     /* Get ldap object, for later filter checks 
372      */
373     $ldap = $this->config->get_ldap_link();
375     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
377     /* Build dn array */
378     $path= split(',', $dn);
379     $path= array_reverse($path);
381     /* Walk along the path to evaluate the acl */
382     $cpath= "";
383     foreach ($path as $element){
385       /* Clean potential ACLs for each level */
386                         if(isset($this->config->idepartments[$cpath])){
387         $acl= $this->cleanACL($acl);
388       }
390       if ($cpath == ""){
391         $cpath= $element;
392       } else {
393         $cpath= $element.','.$cpath;
394       }
396       if (isset($ACL[$cpath])){
398         /* Inspect this ACL, place the result into ACL */
399         foreach ($ACL[$cpath] as $subacl){
401           /* Reset? Just clean the ACL and turn over to the next one... */
402           if ($subacl['type'] == 'reset'){
403             $acl= $this->cleanACL($acl, TRUE);
404             continue;
405           }
407           if($subacl['type'] == "role") {
408             echo "role skipped";
409             continue;
410           }
412           /* With user filter */
413           if (isset($subacl['filter']) && !empty($subacl['filter'])){
414             $id = $dn."-".$subacl['filter'];
415             if(!isset($ACL_CACHE['FILTER'][$id])){
416               $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
417             }
418             if(!$ACL_CACHE['FILTER'][$id]){
419               continue;
420             }
421           }
423           /* Self ACLs? 
424            */
425           if($dn != $this->dn && isset($subacl['acl'][$object][0]) && strpos($subacl['acl'][$object][0],"s")){
426             continue;
427           }
429           /* If attribute is "", we want to know, if we've *any* permissions here... 
430               Merge global class ACLs [0] with attributes specific ACLs [attribute].
431            */
432           if ($attribute == "" && isset($subacl['acl'][$object])){
433             foreach($subacl['acl'][$object] as $attr => $dummy){
434               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
435             }
436             continue;
437           }
439           /* Per attribute ACL? */
440           if (isset($subacl['acl'][$object][$attribute])){
441             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
442             continue;
443           }
445           /* Per object ACL? */
446           if (isset($subacl['acl'][$object][0])){
447             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
448             continue;
449           }
451           /* Global ACL? */
452           if (isset($subacl['acl']['all'][0])){
453             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
454             continue;
455           }
457           /* Category ACLs    (e.g. $object = "user/0")
458            */
459           if(strstr($object,"/0")){
460             $ocs = preg_replace("/\/0$/","",$object);
461             if(isset($this->ocMapping[$ocs])){
463               /* if $attribute is "", then check every single attribute for this object.
464                  if it is 0, then just check the object category ACL.
465                */
466               if($attribute == ""){    
467                 foreach($this->ocMapping[$ocs] as $oc){
468                   if (isset($subacl['acl'][$ocs.'/'.$oc])){
469                     foreach($subacl['acl'][$ocs.'/'.$oc] as $attr => $dummy){
470                       $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][$attr]);
471                     }
472                     continue;
473                   }
474                 }
475               }else{
476                 if(isset($subacl['acl'][$ocs.'/'.$oc][0])){
477                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][0]);
478                 }
479               }
480             }
481             continue;
482           }
483         }
484       }
485     }
487     /* If the requested ACL is for a container object, then alter 
488         ACLs by applying cleanACL a last time.
489      */
490     if(isset($this->config->idepartments[$dn])){
491       $acl = $this->cleanACL($acl);
492     }
494     /* Assemble string */
495     $ret= "";
496     foreach ($acl as $key => $value){
497       if ($value !== ""){
498         $ret.= $key;
499       }
500     }
502     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
503     $ACL_CACHE["$orig_dn+$object+$attribute"]= $ret;
505     /* Remove write if needed */
506     if ($skip_write){
507       $ret = str_replace(array('w','c','d','m'), '',$ret);
508     }
509     return ($ret);
510   }
513   /* Extract all departments that are accessible (direct or 'on the way' to an
514      accessible department) */
515   function get_module_departments($module, $skip_self_acls = FALSE )
516   {
517     /* If we are forced to skip ACLs checks for the current user 
518         then return all departments as valid.
519      */
520     if($this->ignore_acl_for_current_user()){
521       return(array_keys($this->config->idepartments));
522     }
524     /* Use cached results if possilbe */
525     $ACL_CACHE = &session::get('ACL_CACHE');
527     if(!is_array($module)){
528       $module = array($module);
529     }
531     global $plist;
532     $res = array();
533     foreach($module as $mod){
534       if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
535         $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
536         continue;
537       }
539       $deps = array();
541       /* Search for per object ACLs */
542       foreach($this->ACL as $dn => $infos){
543         foreach($infos as $info){
544           $found = FALSE;
545           foreach($info['acl'] as $cat => $data){
547             /* Skip self acls? */
548             if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
549             if(preg_match("/^".preg_quote($mod, '/')."/",$cat)){
550               $found =TRUE;
551               break;
552             }
553           } 
555           if($found && !isset($this->config->idepartments[$dn])){
556             while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
557               $dn = preg_replace("/^[^,]+,/","",$dn);
558             }
559             if(isset($this->config->idepartments[$dn])){
560               $deps[$dn] = $dn;
561             }
562           }
563         }
564       }
566       /* For all gosaDepartments */
567       foreach ($this->config->departments as $dn){
568         if(isset($deps[$dn])) continue;
569         $acl = "";
570         if(strpos($mod, '/')){
571           $acl.=  $this->get_permissions($dn,$mod);
572         }else{
573           $acl.=  $this->get_category_permissions($dn,$mod,TRUE);
574         }
575         if(!empty($acl)) {
576           $deps[$dn] = $dn;
577         }
578       }
580       $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
581       $res = array_merge($res,$deps);
582     }
584     return (array_values($res));
585   }
588   function mergeACL($acl, $type, $newACL)
589   {
590                 $at= array("psub" => "p", "sub" => "s", "one" => "1");
592     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
593       $newACL .= "r";
594     }
596                 /* Ignore invalid characters */
597                 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
599     foreach(str_split($newACL) as $char){
601       /* Skip permanent and subtree entries */
602       if (preg_match('/[sp]/', $acl[$char])){
603         continue;
604       }
606                         if ($type == "base" && $acl[$char] != 1) {
607                                 $acl[$char]= 0;
608                         } else {
609         $acl[$char]= $at[$type];
610                         }
611     }
613     return ($acl);
614   }
617   function cleanACL($acl, $reset= FALSE)
618   {
619     foreach ($acl as $key => $value){
621       /* Continue, if value is empty or permanent */
622       if ($value == "" || $value == "p") {
623             continue;
624       }
626       /* Reset removes everything but 'p' */
627       if ($reset && $value != 'p'){
628         $acl[$key]= "";
629         continue;
630       }
632       /* Decrease tree level */
633       if (is_int($value)){
634         if ($value){
635           $acl[$key]--;
636         } else {
637           $acl[$key]= "";
638         }
639       }
640     }
642     return ($acl);
643   }
646   /* #FIXME This could be logical wrong or could be optimized in the future
647      Return combined acls for a given category. 
648      All acls will be combined like boolean AND 
649       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
650     
651      Results will be cached in $this->result_cache.
652       $this->result_cache will be resetted if load_acls is called.
653   */
654   function has_complete_category_acls($dn,$category)
655   {
656     $acl    = "rwcdm";
657     $types  = "rwcdm";
659     if(!is_string($category)){
660       trigger_error("category must be string");   
661       $acl = "";
662     }else{
663       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
664         if (isset($this->ocMapping[$category])){
665           foreach($this->ocMapping[$category] as $oc){
667             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
668             if($oc == "0") continue;
669             $tmp =  $this->get_permissions($dn, $category."/".$oc);
670             for($i = 0, $l= strlen($types); $i < $l; $i++) {
671               if(!preg_match("/".$types[$i]."/",$tmp)){ 
672                 $acl = preg_replace("/".$types[$i]."/","",$acl);
673               }
674             }
675           }
676         }else{
677           trigger_error("Invalid type of category ".$category);
678           $acl = "";
679         }
680         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
681       }else{
682         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
683       }
684     }
685     return($acl);
686   }
688  
689   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
690       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
691    */ 
692   function ignore_acl_for_current_user()
693   {
694     return($this->ignoreACL);
695   }
699 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
700 ?>