Code

45f6a1324ad44fa32f80de5c5e0f26ace5dec402
[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 is_copyable($dn, $object, $class)
202   {
203     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
204   }
206   function is_cutable($dn, $object, $class)
207   {
208     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
209     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
210     return($remove && $read);
211   }
213   function is_pasteable($dn, $object, $class)
214   {
215     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
216   }
218   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
219   {
220     /* If we are forced to skip ACLs checks for the current user 
221         then return all permissions.
222      */
223     if($this->ignore_acl_for_current_user()){
224       return("rwcdm");
225     }
227     /* Push cache answer? */
228     $ACL_CACHE = &session::get('ACL_CACHE');
229     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
231       /* Remove write if needed */
232       if ($skip_write){
233         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
234       }else{
235         $ret = $ACL_CACHE["$dn+$object+$attribute"];
236       } 
237       return($ret);
238     }
240     /* Get ldap object, for later filter checks 
241      */
242     $ldap = $this->config->get_ldap_link();
244     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
246     /* Build dn array */
247     $path= split(',', $dn);
248     $path= array_reverse($path);
250     /* Walk along the path to evaluate the acl */
251     $cpath= "";
252     foreach ($path as $element){
254       /* Clean potential ACLs for each level */
255       $acl= $this->cleanACL($acl);
257       if ($cpath == ""){
258         $cpath= $element;
259       } else {
260         $cpath= $element.','.$cpath;
261       }
262       if (isset($this->ACL[$cpath])){
264         /* Inspect this ACL, place the result into ACL */
265         foreach ($this->ACL[$cpath] as $subacl){
267           /* Reset? Just clean the ACL and turn over to the next one... */
268           if ($subacl['type'] == 'reset'){
269             $acl= $this->cleanACL($acl, TRUE);
270             continue;
271           }
273           if($subacl['type'] == "role") {
274             echo "role skipped";
275             continue;
276           }
278          /* With user filter */
279          if (isset($subacl['filter']) && !empty($subacl['filter'])){
280            $sdn = preg_replace("/^[^,]*+,/","",$dn);
281            $ldap->cd($sdn);
282            $ldap->ls($subacl['filter'],$sdn);
283            if(!$ldap->count()){
284              continue;
285            }else{
286              $found = FALSE; 
287              while($attrs = $ldap->fetch()){
288                if($attrs['dn'] == $dn){
289                  $found = TRUE;
290                  break;
291                }
292              }
293              if(!$found){
294                continue;
295              }
296            }
297          }
299           /* Per attribute ACL? */
300           if (isset($subacl['acl'][$object][$attribute])){
301             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
302             continue;
303           }
305           /* Per object ACL? */
306           if (isset($subacl['acl'][$object][0])){
307             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
308             continue;
309           }
311           /* Global ACL? */
312           if (isset($subacl['acl']['all'][0])){
313             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
314             continue;
315           }
317           /* If attribute is "", we want to know, if we've *any* permissions here... */
318           if ($attribute == "" && isset($subacl['acl'][$object])){
319             foreach($subacl['acl'][$object] as $attr => $dummy){
320               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
321             }
322             continue;
323           }
325         }
326       }
327     }
329     /* Assemble string */
330     $ret= "";
331     foreach ($acl as $key => $value){
332       if ($value !== ""){
333         $ret.= $key;
334       }
335     }
337     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
339     /* Remove write if needed */
340     if ($skip_write){
341       $ret= preg_replace('/w/', '', $ret);
342     }
343     return ($ret);
344   }
347   /* Extract all departments that are accessible (direct or 'on the way' to an
348      accessible department) */
349   function get_module_departments($module)
350   {
351     
352     /* If we are forced to skip ACLs checks for the current user 
353         then return all departments as valid.
354      */
355     if($this->ignore_acl_for_current_user()){
356       return(array_keys($this->config->idepartments));
357     }
359     /* Use cached results if possilbe */
360     $ACL_CACHE = session::get('ACL_CACHE');
361     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
362       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
363     }
365     global $plist;
367     $objects= array();
368     $deps= array();
370     /* Extract all relevant objects for this module from plist */
371     foreach ($plist->info as $object => $info){
372       if (!isset($info['plCategory'])){
373         continue;
374       }
375       foreach ($info['plCategory'] as $idx => $data){
376         if (preg_match('/^[0-9]+$/', $idx)){
377           if ($data == $module){
378             $objects[$object]= $object;
379           }
380         } else {
381           if ($idx == $module){
382             $objects[$object]= $object;
383           }
384         }
385       }
386     }
388     /* For all gosaDepartments */
389     foreach ($this->config->departments as $dn){
390       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
392       /* Build dn array */
393       $path= split(',', $dn);
394       $path= array_reverse($path);
396       /* Walk along the path to evaluate the acl */
397       $cpath= "";
398       foreach ($path as $element){
400         /* Clean potential ACLs for each level */
401         $acl= $this->cleanACL($acl);
403         if ($cpath == ""){
404           $cpath= $element;
405         } else {
406           $cpath= $element.','.$cpath;
407         }
408         if (isset($this->ACL[$cpath])){
410           /* Inspect this ACL, place the result into ACL */
411           foreach ($this->ACL[$cpath] as $subacl){
413             /* Reset? Just clean the ACL and turn over to the next one... */
414             if ($subacl['type'] == 'reset'){
415               $acl= $this->cleanACL($acl, TRUE);
416               continue;
417             }
418     
419             if($subacl['type'] == 'role'){
420               echo "role skipped";
421               continue;
422             }
424             /* Per object ACL? */
425             foreach ($objects as $object){
426               if (isset($subacl['acl']["$module/$object"])){
427                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
428                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
429                 }
430               }
431             }
433             /* Global ACL? */
434             if (isset($subacl['acl']["$module/all"][0])){
435               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
436               continue;
437             }
439             /* Global ACL? */
440             if (isset($subacl['acl']["all"][0])){
441               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
442               continue;
443             }
444           }
445         }
446       }
448       /* Add department, if we have (some) permissions for the required module */
449       foreach ($acl as $val){
450         if ($val != ""){
451           $deps[]= $dn;
452           break;
453         }
454       }
455     }
457     $ACL_CACHE = &session::get('ACL_CACHE');
458     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
459     return ($deps);
460   }
463   function mergeACL($acl, $type, $newACL)
464   {
465     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
466       $newACL .= "r";
467     }
469     foreach(str_split($newACL) as $char){
471       /* Ignore invalid characters */
472       if (!preg_match('/[rwcdm]/', $char)){
473         continue;
474       }
476       /* Skip permanent and subtree entries */
477       if (preg_match('/[sp]/', $acl[$char])){
478         continue;
479       }
481       switch ($type){
482         case 'psub':
483           $acl[$char]= 'p';
484           break;
486         case 'sub':
487           $acl[$char]= 's';
488           break;
490         case 'one':
491           $acl[$char]= 1;
492           break;
494         case 'base':
495           if ($acl[$char] != 1){
496             $acl[$char]= 0;
497           }
498           break;
499       }
500     }
502     return ($acl);
503   }
506   function cleanACL($acl, $reset= FALSE)
507   {
508     foreach ($acl as &$value){
510       /* Reset removes everything but 'p' */
511       if ($reset && $value != 'p'){
512         $value= "";
513         continue;
514       }
516       /* Decrease tree level */
517       if (is_int($value)){
518         if ($value){
519           $value--;
520         } else {
521           $value= "";
522         }
523       }
524     }
526     return ($acl);
527   }
530   /* #FIXME This could be logical wrong or could be optimized in the future
531      Return combined acls for a given category. 
532      All acls will be combined like boolean AND 
533       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
534     
535      Results will be cached in $this->result_cache.
536       $this->result_cache will be resetted if load_acls is called.
537   */
538   function has_complete_category_acls($dn,$category)
539   {
540     $acl    = "rwcdm";
541     $types  = "rwcdm";
543     if(!is_string($category)){
544       trigger_error("category must be string");   
545       $acl = "";
546     }else{
547       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
548         if (isset($this->ocMapping[$category])){
549           foreach($this->ocMapping[$category] as $oc){
551             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
552             if($oc == "0") continue;
553             $tmp =  $this->get_permissions($dn, $category."/".$oc);
554             for($i = 0 ; $i < strlen($types); $i++) {
555               if(!preg_match("/".$types[$i]."/",$tmp)){ 
556                 $acl = preg_replace("/".$types[$i]."/","",$acl);
557               }
558             }
559           }
560         }else{
561           trigger_error("Invalid type of category ".$category);
562           $acl = "";
563         }
564         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
565       }else{
566         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
567       }
568     }
569     return($acl);
570   }
572  
573   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
574       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
575    */ 
576   function ignore_acl_for_current_user()
577   {
578     return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
579   }
583 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
584 ?>