Code

Fix one TODO 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();
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     /* Get list of objectClasses and get the permissions for it */
182     $acl= "";
183     if (isset($this->ocMapping[$category])){
184       foreach($this->ocMapping[$category] as $oc){
185         $acl.= $this->get_permissions($dn, $category."/".$oc);
186       }
187     }
189     return ($acl);
190   }
193   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
194   {
195     /* Push cache answer? */
196     $ACL_CACHE = &session::get('ACL_CACHE');
197     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
199       /* Remove write if needed */
200       if ($skip_write){
201         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
202       }else{
203         $ret = $ACL_CACHE["$dn+$object+$attribute"];
204       } 
205       return($ret);
206     }
208     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
210     /* Build dn array */
211     $path= split(',', $dn);
212     $path= array_reverse($path);
214     /* Walk along the path to evaluate the acl */
215     $cpath= "";
216     foreach ($path as $element){
218       /* Clean potential ACLs for each level */
219       $acl= $this->cleanACL($acl);
221       if ($cpath == ""){
222         $cpath= $element;
223       } else {
224         $cpath= $element.','.$cpath;
225       }
226       if (isset($this->ACL[$cpath])){
228         /* Inspect this ACL, place the result into ACL */
229         foreach ($this->ACL[$cpath] as $subacl){
231           /* Reset? Just clean the ACL and turn over to the next one... */
232           if ($subacl['type'] == 'reset'){
233             $acl= $this->cleanACL($acl, TRUE);
234             continue;
235           }
237           if($subacl['type'] == "role") {
238             echo "role skipped";
239             continue;
240           }
242           /* Per attribute ACL? */
243           if (isset($subacl['acl'][$object][$attribute])){
244             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
245             continue;
246           }
248           /* Per object ACL? */
249           if (isset($subacl['acl'][$object][0])){
250             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
251             continue;
252           }
254           /* Global ACL? */
255           if (isset($subacl['acl']['all'][0])){
256             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
257             continue;
258           }
260           /* If attribute is "", we want to know, if we've *any* permissions here... */
261           if ($attribute == "" && isset($subacl['acl'][$object])){
262             foreach($subacl['acl'][$object] as $attr => $dummy){
263               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
264             }
265             continue;
266           }
268         }
269       }
270     }
272     /* Assemble string */
273     $ret= "";
274     foreach ($acl as $key => $value){
275       if ($value != ""){
276         $ret.= $key;
277       }
278     }
280     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
282     /* Remove write if needed */
283     if ($skip_write){
284       $ret= preg_replace('/w/', '', $ret);
285     }
286     return ($ret);
287   }
290   /* Extract all departments that are accessible (direct or 'on the way' to an
291      accessible department) */
292   function get_module_departments($module)
293   {
294     /* Use cached results if possilbe */
295     $ACL_CACHE = session::get('ACL_CACHE');
296     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
297       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
298     }
300     global $plist;
302     $objects= array();
303     $deps= array();
305     /* Extract all relevant objects for this module from plist */
306     foreach ($plist->info as $object => $info){
307       if (!isset($info['plCategory'])){
308         continue;
309       }
310       foreach ($info['plCategory'] as $idx => $data){
311         if (preg_match('/^[0-9]+$/', $idx)){
312           if ($data == $module){
313             $objects[$object]= $object;
314           }
315         } else {
316           if ($idx == $module){
317             $objects[$object]= $object;
318           }
319         }
320       }
321     }
323     /* For all gosaDepartments */
324     foreach ($this->config->departments as $dn){
325       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
327       /* Build dn array */
328       $path= split(',', $dn);
329       $path= array_reverse($path);
331       /* Walk along the path to evaluate the acl */
332       $cpath= "";
333       foreach ($path as $element){
335         /* Clean potential ACLs for each level */
336         $acl= $this->cleanACL($acl);
338         if ($cpath == ""){
339           $cpath= $element;
340         } else {
341           $cpath= $element.','.$cpath;
342         }
343         if (isset($this->ACL[$cpath])){
345           /* Inspect this ACL, place the result into ACL */
346           foreach ($this->ACL[$cpath] as $subacl){
348             /* Reset? Just clean the ACL and turn over to the next one... */
349             if ($subacl['type'] == 'reset'){
350               $acl= $this->cleanACL($acl, TRUE);
351               continue;
352             }
353     
354             if($subacl['type'] == 'role'){
355               echo "role skipped";
356               continue;
357             }
359             /* Per object ACL? */
360             foreach ($objects as $object){
361               if (isset($subacl['acl']["$module/$object"])){
362                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
363                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
364                 }
365               }
366             }
368             /* Global ACL? */
369             if (isset($subacl['acl']["$module/all"][0])){
370               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
371               continue;
372             }
374             /* Global ACL? */
375             if (isset($subacl['acl']["all"][0])){
376               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
377               continue;
378             }
379           }
380         }
381       }
383       /* Add department, if we have (some) permissions for the required module */
384       foreach ($acl as $val){
385         if ($val != ""){
386           $deps[]= $dn;
387           break;
388         }
389       }
390     }
392     $ACL_CACHE = &session::get('ACL_CACHE');
393     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
394     return ($deps);
395   }
398   function mergeACL($acl, $type, $newACL)
399   {
400     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
401       $newACL .= "r";
402     }
404     foreach(str_split($newACL) as $char){
406       /* Ignore invalid characters */
407       if (!preg_match('/[rwcdm]/', $char)){
408         continue;
409       }
411       /* Skip permanent and subtree entries */
412       if (preg_match('/[sp]/', $acl[$char])){
413         continue;
414       }
416       switch ($type){
417         case 'psub':
418           $acl[$char]= 'p';
419           break;
421         case 'sub':
422           $acl[$char]= 's';
423           break;
425         case 'one':
426           $acl[$char]= 1;
427           break;
429         case 'base':
430           if ($acl[$char] != 1){
431             $acl[$char]= 0;
432           }
433           break;
434       }
435     }
437     return ($acl);
438   }
441   function cleanACL($acl, $reset= FALSE)
442   {
443     foreach ($acl as &$value){
445       /* Reset removes everything but 'p' */
446       if ($reset && $value != 'p'){
447         $value= "";
448         continue;
449       }
451       /* Decrease tree level */
452       if (is_int($value)){
453         if ($value){
454           $value--;
455         } else {
456           $value= "";
457         }
458       }
459     }
461     return ($acl);
462   }
465   /* #FIXME This could be logical wrong or could be optimized in the future
466      Return combined acls for a given category. 
467      All acls will be combined like boolean AND 
468       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
469     
470      Results will be cached in $this->result_cache.
471       $this->result_cache will be resetted if load_acls is called.
472   */
473   function has_complete_category_acls($dn,$category)
474   {
475     $acl    = "rwcdm";
476     $types  = "rwcdm";
478     if(!is_string($category)){
479       trigger_error("category must be string");   
480       $acl = "";
481     }else{
482       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
483         if (isset($this->ocMapping[$category])){
484           foreach($this->ocMapping[$category] as $oc){
486             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
487             if($oc == "0") continue;
488             $tmp =  $this->get_permissions($dn, $category."/".$oc);
489             for($i = 0 ; $i < strlen($types); $i++) {
490               if(!preg_match("/".$types[$i]."/",$tmp)){ 
491                 $acl = preg_replace("/".$types[$i]."/","",$acl);
492               }
493             }
494           }
495         }else{
496           trigger_error("Invalid type of category ".$category);
497           $acl = "";
498         }
499         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
500       }else{
501         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
502       }
503     }
504     return($acl);
505   }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>