Code

Corrected tag check.
[gosa.git] / gosa-core / include / class_userinfo.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2005  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class userinfo
22 {
23   var $dn;
24   var $ip;
25   var $username;
26   var $cn;
27   var $uid;
28   var $gidNumber= -1;
29   var $language= "";
30   var $config;
31   var $gosaUnitTag= "";
32   var $subtreeACL= array();
33   var $ACL= array();
34   var $ocMapping= array();
35   var $groups= array();
36   var $result_cache =array();
38   /* get acl's an put them into the userinfo object
39      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
40   function userinfo(&$config, $userdn){
41     $this->config= &$config;
42     $ldap= $this->config->get_ldap_link();
43     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
44     $attrs= $ldap->fetch();
46     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
47       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
48     } else {
49       $this->cn= $attrs['uid'][0];
50     }
51     if (isset($attrs['gidNumber'][0])){
52       $this->gidNumber= $attrs['gidNumber'][0];
53     }
55     /* Assign user language */
56     if (isset($attrs['preferredLanguage'][0])){
57       $this->language= $attrs['preferredLanguage'][0];
58     }
60     if (isset($attrs['gosaUnitTag'][0])){
61       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
62     }
64     $this->dn= $userdn;
65     $this->uid= $attrs['uid'][0];
66     $this->ip= $_SERVER['REMOTE_ADDR'];
68     /* Initialize ACL_CACHE */
69     session::set('ACL_CACHE',array());
70     $this->reset_acl_cache();
71   }
74   public function reset_acl_cache()
75   {
76     /* Initialize ACL_CACHE */
77     session::set('ACL_CACHE',array());
78   }
80   function loadACL()
81   {
82     $this->ACL= array();    
83     $this->groups= array();    
84     $this->result_cache =array();
85     $this->reset_acl_cache();
86     $ldap= $this->config->get_ldap_link();
87     $ldap->cd($this->config->current['BASE']);
89     /* Get member groups... */
90     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
91     while ($attrs= $ldap->fetch()){
92       $this->groups[$attrs['dn']]= $attrs['dn'];
93     }
95     /* Crawl through ACLs and move relevant to the tree */
96     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
97     $aclp= array();
98     $aclc= array();
99     while ($attrs= $ldap->fetch()){
101       /* Insert links in ACL array */
102       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
103       $aclc[$attrs['dn']]= array();
104       $ol= array();
105       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
106         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
107       }
108       $aclc[$attrs['dn']]= $ol;
109     }
111     /* Resolve roles here. 
112      */
113     foreach($aclc as $dn => $data){
114       foreach($data as $prio => $aclc_value)  {
115         if($aclc_value['type'] == "role"){
117           unset($aclc[$dn][$prio]);
119           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
120           $attrs = $ldap->fetch();
122           if(isset($attrs['gosaAclTemplate'])){
123             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
124               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
126               foreach($tmp as $new_acl){
127                 $new_acl['members'] = $aclc_value['members'];
128                 $aclc[$dn][] =$new_acl;
129               }
130             }      
131           }
132         }
133       }
134     }
136     /* ACL's read, sort for tree depth */
137     asort($aclp);
139     /* Sort in tree order */
140     foreach ($aclp as $dn => $acl){
141       /* Check if we need to keep this ACL */
142       foreach($aclc[$dn] as $idx => $type){
143         $interresting= FALSE;
144         
145         /* No members? This is good for all users... */
146         if (!count($type['members'])){
147           $interresting= TRUE;
148         } else {
150           /* Inspect members... */
151           foreach ($type['members'] as $grp => $grpdsc){
152             /* Some group inside the members that is relevant for us? */
153             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
154               $interresting= TRUE;
155             }
157             /* User inside the members? */
158             if (preg_replace('/^U:/', '', $grp) == $this->dn){
159               $interresting= TRUE;
160             }
161           }
162         }
164         if ($interresting){
165           if (!isset($this->ACL[$dn])){
166             $this->ACL[$dn]= array();
167           }
168           $this->ACL[$dn][$idx]= $type;
169         }
170       }
172     }
173   }
176   function get_category_permissions($dn, $category)
177   {
178     /* Get list of objectClasses and get the permissions for it */
179     $acl= "";
180     if (isset($this->ocMapping[$category])){
181       foreach($this->ocMapping[$category] as $oc){
182         $acl.= $this->get_permissions($dn, $category."/".$oc);
183       }
184     }
186     return ($acl);
187   }
190   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
191   {
192     /* Push cache answer? */
193     $ACL_CACHE = &session::get('ACL_CACHE');
194     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
196       /* Remove write if needed */
197       if ($skip_write){
198         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
199       }else{
200         $ret = $ACL_CACHE["$dn+$object+$attribute"];
201       } 
202       return($ret);
203     }
205     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
207     /* Build dn array */
208     $path= split(',', $dn);
209     $path= array_reverse($path);
211     /* Walk along the path to evaluate the acl */
212     $cpath= "";
213     foreach ($path as $element){
215       /* Clean potential ACLs for each level */
216       $acl= $this->cleanACL($acl);
218       if ($cpath == ""){
219         $cpath= $element;
220       } else {
221         $cpath= $element.','.$cpath;
222       }
223       if (isset($this->ACL[$cpath])){
225         /* Inspect this ACL, place the result into ACL */
226         foreach ($this->ACL[$cpath] as $subacl){
228           /* Reset? Just clean the ACL and turn over to the next one... */
229           if ($subacl['type'] == 'reset'){
230             $acl= $this->cleanACL($acl, TRUE);
231             continue;
232           }
234           if($subacl['type'] == "role") {
235             echo "role skipped";
236             continue;
237           }
239           /* Per attribute ACL? */
240           if (isset($subacl['acl'][$object][$attribute])){
241             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
242             continue;
243           }
245           /* Per object ACL? */
246           if (isset($subacl['acl'][$object][0])){
247             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
248             continue;
249           }
251           /* Global ACL? */
252           if (isset($subacl['acl']['all'][0])){
253             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
254             continue;
255           }
257           /* If attribute is "", we want to know, if we've *any* permissions here... */
258           if ($attribute == "" && isset($subacl['acl'][$object])){
259             foreach($subacl['acl'][$object] as $attr => $dummy){
260               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
261             }
262             continue;
263           }
265         }
266       }
267     }
269     /* Assemble string */
270     $ret= "";
271     foreach ($acl as $key => $value){
272       if ($value != ""){
273         $ret.= $key;
274       }
275     }
277     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
279     /* Remove write if needed */
280     if ($skip_write){
281       $ret= preg_replace('/w/', '', $ret);
282     }
283     return ($ret);
284   }
287   /* Extract all departments that are accessible (direct or 'on the way' to an
288      accessible department) */
289   function get_module_departments($module)
290   {
291     /* Use cached results if possilbe */
292     $ACL_CACHE = session::get('ACL_CACHE');
293     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
294       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
295     }
297     global $plist;
299     $objects= array();
300     $deps= array();
302     /* Extract all relevant objects for this module from plist */
303     foreach ($plist->info as $object => $info){
304       if (!isset($info['plCategory'])){
305         continue;
306       }
307       foreach ($info['plCategory'] as $idx => $data){
308         if (preg_match('/^[0-9]+$/', $idx)){
309           if ($data == $module){
310             $objects[$object]= $object;
311           }
312         } else {
313           if ($idx == $module){
314             $objects[$object]= $object;
315           }
316         }
317       }
318     }
320     /* For all gosaDepartments */
321     foreach ($this->config->departments as $dn){
322       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
324       /* Build dn array */
325       $path= split(',', $dn);
326       $path= array_reverse($path);
328       /* Walk along the path to evaluate the acl */
329       $cpath= "";
330       foreach ($path as $element){
332         /* Clean potential ACLs for each level */
333         $acl= $this->cleanACL($acl);
335         if ($cpath == ""){
336           $cpath= $element;
337         } else {
338           $cpath= $element.','.$cpath;
339         }
340         if (isset($this->ACL[$cpath])){
342           /* Inspect this ACL, place the result into ACL */
343           foreach ($this->ACL[$cpath] as $subacl){
345             /* Reset? Just clean the ACL and turn over to the next one... */
346             if ($subacl['type'] == 'reset'){
347               $acl= $this->cleanACL($acl, TRUE);
348               continue;
349             }
350     
351             if($subacl['type'] == 'role'){
352               echo "role skipped";
353               continue;
354             }
356             /* Per object ACL? */
357             foreach ($objects as $object){
358               if (isset($subacl['acl']["$module/$object"])){
359                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
360                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
361                 }
362               }
363             }
365             /* Global ACL? */
366             if (isset($subacl['acl']["$module/all"][0])){
367               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
368               continue;
369             }
371             /* Global ACL? */
372             if (isset($subacl['acl']["all"][0])){
373               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
374               continue;
375             }
376           }
377         }
378       }
380       /* Add department, if we have (some) permissions for the required module */
381       foreach ($acl as $val){
382         if ($val != ""){
383           $deps[]= $dn;
384           break;
385         }
386       }
387     }
389     $ACL_CACHE = &session::get('ACL_CACHE');
390     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
391     return ($deps);
392   }
395   function mergeACL($acl, $type, $newACL)
396   {
397     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
398       $newACL .= "r";
399     }
401     foreach(str_split($newACL) as $char){
403       /* Ignore invalid characters */
404       if (!preg_match('/[rwcdm]/', $char)){
405         continue;
406       }
408       /* Skip permanent and subtree entries */
409       if (preg_match('/[sp]/', $acl[$char])){
410         continue;
411       }
413       switch ($type){
414         case 'psub':
415           $acl[$char]= 'p';
416           break;
418         case 'sub':
419           $acl[$char]= 's';
420           break;
422         case 'one':
423           $acl[$char]= 1;
424           break;
426         case 'base':
427           if ($acl[$char] != 1){
428             $acl[$char]= 0;
429           }
430           break;
431       }
432     }
434     return ($acl);
435   }
438   function cleanACL($acl, $reset= FALSE)
439   {
440     foreach ($acl as &$value){
442       /* Reset removes everything but 'p' */
443       if ($reset && $value != 'p'){
444         $value= "";
445         continue;
446       }
448       /* Decrease tree level */
449       if (is_int($value)){
450         if ($value){
451           $value--;
452         } else {
453           $value= "";
454         }
455       }
456     }
458     return ($acl);
459   }
462   /* #FIXME This could be logical wrong or could be optimized in the future
463      Return combined acls for a given category. 
464      All acls will be combined like boolean AND 
465       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
466     
467      Results will be cached in $this->result_cache.
468       $this->result_cache will be resetted if load_acls is called.
469   */
470   function has_complete_category_acls($dn,$category)
471   {
472     $acl    = "rwcdm";
473     $types  = "rwcdm";
475     if(!is_string($category)){
476       trigger_error("category must be string");   
477       $acl = "";
478     }else{
479       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
480         if (isset($this->ocMapping[$category])){
481           foreach($this->ocMapping[$category] as $oc){
483             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
484             if($oc == "0") continue;
485             $tmp =  $this->get_permissions($dn, $category."/".$oc);
486             for($i = 0 ; $i < strlen($types); $i++) {
487               if(!preg_match("/".$types[$i]."/",$tmp)){ 
488                 $acl = preg_replace("/".$types[$i]."/","",$acl);
489               }
490             }
491           }
492         }else{
493           trigger_error("Invalid type of category ".$category);
494           $acl = "";
495         }
496         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
497       }else{
498         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
499       }
500     }
501     return($acl);
502   }
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>