Code

Added fall back to old style move method.
[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     }
176   }
179   function get_category_permissions($dn, $category)
180   {
181     /* If we are forced to skip ACLs checks for the current user 
182         then return all permissions.
183      */
184     if($this->ignore_acl_for_current_user()){
185       return("rwcdm");
186     }
188     /* Get list of objectClasses and get the permissions for it */
189     $acl= "";
190     if (isset($this->ocMapping[$category])){
191       foreach($this->ocMapping[$category] as $oc){
192         $acl.= $this->get_permissions($dn, $category."/".$oc);
193       }
194     }else{
195       trigger_error("ACL request for an invalid category (".$category.").");
196     }
198     return ($acl);
199   }
202   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
203   {
204     /* If we are forced to skip ACLs checks for the current user 
205         then return all permissions.
206      */
207     if($this->ignore_acl_for_current_user()){
208       return("rwcdm");
209     }
211     /* Push cache answer? */
212     $ACL_CACHE = &session::get('ACL_CACHE');
213     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
215       /* Remove write if needed */
216       if ($skip_write){
217         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
218       }else{
219         $ret = $ACL_CACHE["$dn+$object+$attribute"];
220       } 
221       return($ret);
222     }
224     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
226     /* Build dn array */
227     $path= split(',', $dn);
228     $path= array_reverse($path);
230     /* Walk along the path to evaluate the acl */
231     $cpath= "";
232     foreach ($path as $element){
234       /* Clean potential ACLs for each level */
235       $acl= $this->cleanACL($acl);
237       if ($cpath == ""){
238         $cpath= $element;
239       } else {
240         $cpath= $element.','.$cpath;
241       }
242       if (isset($this->ACL[$cpath])){
244         /* Inspect this ACL, place the result into ACL */
245         foreach ($this->ACL[$cpath] as $subacl){
247           /* Reset? Just clean the ACL and turn over to the next one... */
248           if ($subacl['type'] == 'reset'){
249             $acl= $this->cleanACL($acl, TRUE);
250             continue;
251           }
253           if($subacl['type'] == "role") {
254             echo "role skipped";
255             continue;
256           }
258           /* Per attribute ACL? */
259           if (isset($subacl['acl'][$object][$attribute])){
260             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
261             continue;
262           }
264           /* Per object ACL? */
265           if (isset($subacl['acl'][$object][0])){
266             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
267             continue;
268           }
270           /* Global ACL? */
271           if (isset($subacl['acl']['all'][0])){
272             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
273             continue;
274           }
276           /* If attribute is "", we want to know, if we've *any* permissions here... */
277           if ($attribute == "" && isset($subacl['acl'][$object])){
278             foreach($subacl['acl'][$object] as $attr => $dummy){
279               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
280             }
281             continue;
282           }
284         }
285       }
286     }
288     /* Assemble string */
289     $ret= "";
290     foreach ($acl as $key => $value){
291       if ($value != ""){
292         $ret.= $key;
293       }
294     }
296     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
298     /* Remove write if needed */
299     if ($skip_write){
300       $ret= preg_replace('/w/', '', $ret);
301     }
302     return ($ret);
303   }
306   /* Extract all departments that are accessible (direct or 'on the way' to an
307      accessible department) */
308   function get_module_departments($module)
309   {
310     
311     /* If we are forced to skip ACLs checks for the current user 
312         then return all departments as valid.
313      */
314     if($this->ignore_acl_for_current_user()){
315       return(array_keys($this->config->idepartments));
316     }
318     /* Use cached results if possilbe */
319     $ACL_CACHE = session::get('ACL_CACHE');
320     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
321       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
322     }
324     global $plist;
326     $objects= array();
327     $deps= array();
329     /* Extract all relevant objects for this module from plist */
330     foreach ($plist->info as $object => $info){
331       if (!isset($info['plCategory'])){
332         continue;
333       }
334       foreach ($info['plCategory'] as $idx => $data){
335         if (preg_match('/^[0-9]+$/', $idx)){
336           if ($data == $module){
337             $objects[$object]= $object;
338           }
339         } else {
340           if ($idx == $module){
341             $objects[$object]= $object;
342           }
343         }
344       }
345     }
347     /* For all gosaDepartments */
348     foreach ($this->config->departments as $dn){
349       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
351       /* Build dn array */
352       $path= split(',', $dn);
353       $path= array_reverse($path);
355       /* Walk along the path to evaluate the acl */
356       $cpath= "";
357       foreach ($path as $element){
359         /* Clean potential ACLs for each level */
360         $acl= $this->cleanACL($acl);
362         if ($cpath == ""){
363           $cpath= $element;
364         } else {
365           $cpath= $element.','.$cpath;
366         }
367         if (isset($this->ACL[$cpath])){
369           /* Inspect this ACL, place the result into ACL */
370           foreach ($this->ACL[$cpath] as $subacl){
372             /* Reset? Just clean the ACL and turn over to the next one... */
373             if ($subacl['type'] == 'reset'){
374               $acl= $this->cleanACL($acl, TRUE);
375               continue;
376             }
377     
378             if($subacl['type'] == 'role'){
379               echo "role skipped";
380               continue;
381             }
383             /* Per object ACL? */
384             foreach ($objects as $object){
385               if (isset($subacl['acl']["$module/$object"])){
386                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
387                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
388                 }
389               }
390             }
392             /* Global ACL? */
393             if (isset($subacl['acl']["$module/all"][0])){
394               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
395               continue;
396             }
398             /* Global ACL? */
399             if (isset($subacl['acl']["all"][0])){
400               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
401               continue;
402             }
403           }
404         }
405       }
407       /* Add department, if we have (some) permissions for the required module */
408       foreach ($acl as $val){
409         if ($val != ""){
410           $deps[]= $dn;
411           break;
412         }
413       }
414     }
416     $ACL_CACHE = &session::get('ACL_CACHE');
417     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
418     return ($deps);
419   }
422   function mergeACL($acl, $type, $newACL)
423   {
424     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
425       $newACL .= "r";
426     }
428     foreach(str_split($newACL) as $char){
430       /* Ignore invalid characters */
431       if (!preg_match('/[rwcdm]/', $char)){
432         continue;
433       }
435       /* Skip permanent and subtree entries */
436       if (preg_match('/[sp]/', $acl[$char])){
437         continue;
438       }
440       switch ($type){
441         case 'psub':
442           $acl[$char]= 'p';
443           break;
445         case 'sub':
446           $acl[$char]= 's';
447           break;
449         case 'one':
450           $acl[$char]= 1;
451           break;
453         case 'base':
454           if ($acl[$char] != 1){
455             $acl[$char]= 0;
456           }
457           break;
458       }
459     }
461     return ($acl);
462   }
465   function cleanACL($acl, $reset= FALSE)
466   {
467     foreach ($acl as &$value){
469       /* Reset removes everything but 'p' */
470       if ($reset && $value != 'p'){
471         $value= "";
472         continue;
473       }
475       /* Decrease tree level */
476       if (is_int($value)){
477         if ($value){
478           $value--;
479         } else {
480           $value= "";
481         }
482       }
483     }
485     return ($acl);
486   }
489   /* #FIXME This could be logical wrong or could be optimized in the future
490      Return combined acls for a given category. 
491      All acls will be combined like boolean AND 
492       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
493     
494      Results will be cached in $this->result_cache.
495       $this->result_cache will be resetted if load_acls is called.
496   */
497   function has_complete_category_acls($dn,$category)
498   {
499     $acl    = "rwcdm";
500     $types  = "rwcdm";
502     if(!is_string($category)){
503       trigger_error("category must be string");   
504       $acl = "";
505     }else{
506       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
507         if (isset($this->ocMapping[$category])){
508           foreach($this->ocMapping[$category] as $oc){
510             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
511             if($oc == "0") continue;
512             $tmp =  $this->get_permissions($dn, $category."/".$oc);
513             for($i = 0 ; $i < strlen($types); $i++) {
514               if(!preg_match("/".$types[$i]."/",$tmp)){ 
515                 $acl = preg_replace("/".$types[$i]."/","",$acl);
516               }
517             }
518           }
519         }else{
520           trigger_error("Invalid type of category ".$category);
521           $acl = "";
522         }
523         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
524       }else{
525         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
526       }
527     }
528     return($acl);
529   }
531  
532   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
533       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
534    */ 
535   function ignore_acl_for_current_user()
536   {
537     return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
538   }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>