Code

Updated ACL template rendering, hide everything instead of gray-out
[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){
130                 /* Keep non role attributes here! */
131                 $new_acl['filter'] = $aclc_value['filter'];
132                 $new_acl['members'] = $aclc_value['members'];
133                 $aclc[$dn][] =$new_acl;
134               }
135             }      
136           }
137         }
138       }
139     }
141     /* ACL's read, sort for tree depth */
142     asort($aclp);
144     /* Sort in tree order */
145     foreach ($aclp as $dn => $acl){
146       /* Check if we need to keep this ACL */
147       foreach($aclc[$dn] as $idx => $type){
148         $interresting= FALSE;
149         
150         /* No members? This is good for all users... */
151         if (!count($type['members'])){
152           $interresting= TRUE;
153         } else {
155           /* Inspect members... */
156           foreach ($type['members'] as $grp => $grpdsc){
157             /* Some group inside the members that is relevant for us? */
158             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
159               $interresting= TRUE;
160             }
162             /* User inside the members? */
163             if (preg_replace('/^U:/', '', $grp) == $this->dn){
164               $interresting= TRUE;
165             }
166           }
167         }
169         if ($interresting){
170           if (!isset($this->ACL[$dn])){
171             $this->ACL[$dn]= array();
172           }
173           $this->ACL[$dn][$idx]= $type;
174         }
175       }
177     }
178   }
181   function get_category_permissions($dn, $category)
182   {
183     /* If we are forced to skip ACLs checks for the current user 
184         then return all permissions.
185      */
186     if($this->ignore_acl_for_current_user()){
187       return("rwcdm");
188     }
190     /* Get list of objectClasses and get the permissions for it */
191     $acl= "";
192     if (isset($this->ocMapping[$category])){
193       foreach($this->ocMapping[$category] as $oc){
194         $acl.= $this->get_permissions($dn, $category."/".$oc);
195       }
196     }else{
197       trigger_error("ACL request for an invalid category (".$category.").");
198     }
200     return ($acl);
201   }
203   
204   /*! \brief Check if the given object (dn) is copyable
205       @param  String The object dn 
206       @param  String The acl  category (e.g. users) 
207       @param  String The acl  class (e.g. user) 
208       @return Boolean   TRUE if the given object is copyable else FALSE 
209   */
210   function is_copyable($dn, $object, $class)
211   {
212     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
213   }
216   /*! \brief Check if the given object (dn) is cutable
217       @param  String The object dn 
218       @param  String The acl  category (e.g. users) 
219       @param  String The acl  class (e.g. user) 
220       @return Boolean   TRUE if the given object is cutable else FALSE 
221   */
222   function is_cutable($dn, $object, $class)
223   {
224     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
225     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
226     return($remove && $read);
227   }
230   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
231       @param  String The destination dn 
232       @param  String The acl  category (e.g. users) 
233       @param  String The acl  class (e.g. user) 
234       @return Boolean   TRUE if we are allowed to paste an object.
235   */
236   function is_pasteable($dn, $object)
237   {
238     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
239   }
242   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
243       @param  String The destination dn 
244       @param  String The acl  category (e.g. users) 
245       @return Boolean   TRUE if we are allowed to restore a snapshot.
246   */
247   function allow_snapshot_restore($dn, $object)
248   {
249     if(!is_array($object)){
250       $object = array($object);
251     }
252     $r = $w = $c = TRUE;
253     foreach($object as $category){
254       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
255       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
256       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
257 #     print_a(array($category => array($r.$w.$c)));
258     }
259     return($r && $w ); 
260   }  
263   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
264       @param  String The source dn 
265       @param  String The acl category (e.g. users) 
266       @return Boolean   TRUE if we are allowed to restore a snapshot.
267   */
268   function allow_snapshot_create($dn, $object)
269   {
270     if(!is_array($object)){
271       $object = array($object);
272     }
273     $r = $w = $c = TRUE;
274     foreach($object as $category){
275       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
276       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
277       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
278 #      print_a(array($category => array($r.$w.$c)));
279     }
280     return($r) ; 
281   }  
284   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
285   {
286     /* If we are forced to skip ACLs checks for the current user 
287         then return all permissions.
288      */
289     if($this->ignore_acl_for_current_user()){
290       if($skip_write){
291         return("rcdm");
292       }
293       return("rwcdm");
294     }
296     /* Push cache answer? */
297     $ACL_CACHE = &session::get('ACL_CACHE');
298     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
300       /* Remove write if needed */
301       if ($skip_write){
302         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
303       }else{
304         $ret = $ACL_CACHE["$dn+$object+$attribute"];
305       } 
306       return($ret);
307     }
309     /* Get ldap object, for later filter checks 
310      */
311     $ldap = $this->config->get_ldap_link();
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       if(in_array($cpath,$this->config->departments)){
325         $acl= $this->cleanACL($acl);
326       }
328       if ($cpath == ""){
329         $cpath= $element;
330       } else {
331         $cpath= $element.','.$cpath;
332       }
334       if (isset($this->ACL[$cpath])){
336         /* Inspect this ACL, place the result into ACL */
337         foreach ($this->ACL[$cpath] as $subacl){
339           /* Reset? Just clean the ACL and turn over to the next one... */
340           if ($subacl['type'] == 'reset'){
341             $acl= $this->cleanACL($acl, TRUE);
342             continue;
343           }
345           if($subacl['type'] == "role") {
346             echo "role skipped";
347             continue;
348           }
350           /* With user filter */
351           if (isset($subacl['filter']) && !empty($subacl['filter'])){
352             $sdn = preg_replace("/^[^,]*+,/","",$dn);
353             $ldap->cd($sdn);
354             $ldap->ls($subacl['filter'],$sdn);
355             if(!$ldap->count()){
356               continue;
357             }else{
358               $found = FALSE; 
359               while($attrs = $ldap->fetch()){
360                 if($attrs['dn'] == $dn){
361                   $found = TRUE;
362                   break;
363                 }
364               }
365               if(!$found){
366                 continue;
367               }
368             }
369           }
371           /* Self ACLs? 
372            */
373           if(isset($subacl['acl'][$object][0]) && preg_match("/s/",$subacl['acl'][$object][0]) && $dn != $this->dn){
374             continue;
375           }
377           /* If attribute is "", we want to know, if we've *any* permissions here... 
378               Merge global class ACLs [0] with attributes specific ACLs [attribute].
379            */
380           if ($attribute == "" && isset($subacl['acl'][$object])){
381             foreach($subacl['acl'][$object] as $attr => $dummy){
382               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
383             }
384             continue;
385           }
387           /* Per attribute ACL? */
388           if (isset($subacl['acl'][$object][$attribute])){
389             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
390             continue;
391           }
393           /* Per object ACL? */
394           if (isset($subacl['acl'][$object][0])){
395             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
396             continue;
397           }
399           /* Global ACL? */
400           if (isset($subacl['acl']['all'][0])){
401             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
402             continue;
403           }
404         }
405       }
406     }
408     /* If the requested ACL is for a container object, then alter 
409         ACLs by applying cleanACL a last time.
410      */
411     if(in_array($dn,$this->config->departments)){
412       $acl = $this->cleanACL($acl);
413     }
415     /* Assemble string */
416     $ret= "";
417     foreach ($acl as $key => $value){
418       if ($value !== ""){
419         $ret.= $key;
420       }
421     }
423     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
425     /* Remove write if needed */
426     if ($skip_write){
427       $ret= preg_replace('/w/', '', $ret);
428     }
429     return ($ret);
430   }
433   /* Extract all departments that are accessible (direct or 'on the way' to an
434      accessible department) */
435   function get_module_departments($module, $skip_self_acls = FALSE )
436   {
437     
438     /* If we are forced to skip ACLs checks for the current user 
439         then return all departments as valid.
440      */
441     if($this->ignore_acl_for_current_user()){
442       return(array_keys($this->config->idepartments));
443     }
445     /* Use cached results if possilbe */
446     $ACL_CACHE = session::get('ACL_CACHE');
447     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
448       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
449     }
451     global $plist;
453     $objects= array();
454     $deps= array();
456     /* Extract all relevant objects for this module from plist */
457     foreach ($plist->info as $object => $info){
458       if (!isset($info['plCategory'])){
459         continue;
460       }
461       foreach ($info['plCategory'] as $idx => $data){
462         if (preg_match('/^[0-9]+$/', $idx)){
463           if ($data == $module){
464             $objects[$object]= $object;
465           }
466         } else {
467           if ($idx == $module){
468             $objects[$object]= $object;
469           }
470         }
471       }
472     }
474     /* Search for per object ACLs. 
475      */
476     $this->config->get_departments();
477     $this->config->make_idepartments();
479     foreach($this->ACL as $dn => $infos){
480       foreach($infos as $info){
481         $found = FALSE;
482         foreach($info['acl'] as $cat => $data){
484           /* Skip self acls? */
485           if($skip_self_acls && isset($data['0']) && preg_match("//s",$data['0'])) continue;
487           if(is_array($module)){
488             foreach($module as $mod){
489               if(preg_match("/^".normalizePreg($mod)."/",$cat)){
490                 $found =TRUE;
491                 break;
492               }
493             }
494           }else{
495             if(preg_match("/^".normalizePreg($module)."/",$cat)){
496               $found =TRUE;
497               break;
498             }
499           }
500         } 
502         if($found && !isset($this->config->idepartments[$dn])){
503           while(!isset($this->config->idepartments[$dn]) && preg_match("/,/",$dn)){
504             $dn = preg_replace("/^[^,]+,/","",$dn);
505           }
506           if(isset($this->config->idepartments[$dn])){
507             $deps[] = $dn;
508           }
509         }
510       }
511     }
513     /* For all gosaDepartments */
514     foreach ($this->config->departments as $dn){
515       if(!is_array($module)){
516         $module = array($module);
517       }
518       $acl = "";
519       foreach($module as $mod){
520         if(preg_match("/\//",$mod)){
521           $acl.=  $this->get_permissions($dn,$mod);
522         }else{
523           $acl.=  $this->get_category_permissions($dn,$mod);
524         }
525       }
526       if($acl !== "") $deps[] = $dn;
527     }
528   
529     $ACL_CACHE = &session::get('ACL_CACHE');
530     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
531     return ($deps);
532   }
535   function mergeACL($acl, $type, $newACL)
536   {
537     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
538       $newACL .= "r";
539     }
541     foreach(str_split($newACL) as $char){
543       /* Ignore invalid characters */
544       if (!preg_match('/[rwcdm]/', $char)){
545         continue;
546       }
548       /* Skip permanent and subtree entries */
549       if (preg_match('/[sp]/', $acl[$char])){
550         continue;
551       }
553       switch ($type){
554         case 'psub':
555           $acl[$char]= 'p';
556           break;
558         case 'sub':
559           $acl[$char]= 's';
560           break;
562         case 'one':
563           $acl[$char]= 1;
564           break;
566         case 'base':
567           if ($acl[$char] != 1){
568             $acl[$char]= 0;
569           }
570           break;
571       }
572     }
574     return ($acl);
575   }
578   function cleanACL($acl, $reset= FALSE)
579   {
580     foreach ($acl as &$value){
582       /* Reset removes everything but 'p' */
583       if ($reset && $value != 'p'){
584         $value= "";
585         continue;
586       }
588       /* Decrease tree level */
589       if (is_int($value)){
590         if ($value){
591           $value--;
592         } else {
593           $value= "";
594         }
595       }
596     }
598     return ($acl);
599   }
602   /* #FIXME This could be logical wrong or could be optimized in the future
603      Return combined acls for a given category. 
604      All acls will be combined like boolean AND 
605       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
606     
607      Results will be cached in $this->result_cache.
608       $this->result_cache will be resetted if load_acls is called.
609   */
610   function has_complete_category_acls($dn,$category)
611   {
612     $acl    = "rwcdm";
613     $types  = "rwcdm";
615     if(!is_string($category)){
616       trigger_error("category must be string");   
617       $acl = "";
618     }else{
619       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
620         if (isset($this->ocMapping[$category])){
621           foreach($this->ocMapping[$category] as $oc){
623             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
624             if($oc == "0") continue;
625             $tmp =  $this->get_permissions($dn, $category."/".$oc);
626             for($i = 0 ; $i < strlen($types); $i++) {
627               if(!preg_match("/".$types[$i]."/",$tmp)){ 
628                 $acl = preg_replace("/".$types[$i]."/","",$acl);
629               }
630             }
631           }
632         }else{
633           trigger_error("Invalid type of category ".$category);
634           $acl = "";
635         }
636         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
637       }else{
638         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
639       }
640     }
641     return($acl);
642   }
644  
645   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
646       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
647    */ 
648   function ignore_acl_for_current_user()
649   {
650     return($this->config->get_cfg_value("ignoreAcl") == $this->dn);
651   }
655 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
656 ?>