Code

097c7ccdc90f2030cdd4cbc0d35b5e28ba0ff126
[gosa.git] / 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['ACL_CACHE']= array();
70     $this->reset_acl_cache();
71   }
74   public function reset_acl_cache()
75   {
76     /* Initialize ACL_CACHE */
77     $_SESSION['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     if (isset($_SESSION['ACL_CACHE']["$dn+$object+$attribute"])){
195       /* Remove write if needed */
196       if ($skip_write){
197         $ret = preg_replace('/w/', '', $_SESSION['ACL_CACHE']["$dn+$object+$attribute"]);
198       }else{
199         $ret = $_SESSION['ACL_CACHE']["$dn+$object+$attribute"];
200       } 
201       return($ret);
202     }
204     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
206     /* Build dn array */
207     $path= split(',', $dn);
208     $path= array_reverse($path);
210     /* Walk along the path to evaluate the acl */
211     $cpath= "";
212     foreach ($path as $element){
214       /* Clean potential ACLs for each level */
215       $acl= $this->cleanACL($acl);
217       if ($cpath == ""){
218         $cpath= $element;
219       } else {
220         $cpath= $element.','.$cpath;
221       }
222       if (isset($this->ACL[$cpath])){
224         /* Inspect this ACL, place the result into ACL */
225         foreach ($this->ACL[$cpath] as $subacl){
227           /* Reset? Just clean the ACL and turn over to the next one... */
228           if ($subacl['type'] == 'reset'){
229             $acl= $this->cleanACL($acl, TRUE);
230             continue;
231           }
233           if($subacl['type'] == "role") {
234             echo "role skipped";
235             continue;
236           }
238           /* Per attribute ACL? */
239           if (isset($subacl['acl'][$object][$attribute])){
240             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
241             continue;
242           }
244           /* Per object ACL? */
245           if (isset($subacl['acl'][$object][0])){
246             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
247             continue;
248           }
250           /* Global ACL? */
251           if (isset($subacl['acl']['all'][0])){
252             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
253             continue;
254           }
256           /* If attribute is "", we want to know, if we've *any* permissions here... */
257           if ($attribute == "" && isset($subacl['acl'][$object])){
258             foreach($subacl['acl'][$object] as $attr => $dummy){
259               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
260             }
261             continue;
262           }
264         }
265       }
266     }
268     /* Assemble string */
269     $ret= "";
270     foreach ($acl as $key => $value){
271       if ($value != ""){
272         $ret.= $key;
273       }
274     }
276     $_SESSION['ACL_CACHE']["$dn+$object+$attribute"]= $ret;
278     /* Remove write if needed */
279     if ($skip_write){
280       $ret= preg_replace('/w/', '', $ret);
281     }
282     return ($ret);
283   }
286   /* Extract all departments that are accessible (direct or 'on the way' to an
287      accessible department) */
288   function get_module_departments($module)
289   {
290     /* Use cached results if possilbe */
291     if(isset($_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)])){
292       return($_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)]);
293     }
295     global $plist;
297     $objects= array();
298     $deps= array();
300     /* Extract all relevant objects for this module from plist */
301     foreach ($plist->info as $object => $info){
302       if (!isset($info['plCategory'])){
303         continue;
304       }
305       foreach ($info['plCategory'] as $idx => $data){
306         if (preg_match('/^[0-9]+$/', $idx)){
307           if ($data == $module){
308             $objects[$object]= $object;
309           }
310         } else {
311           if ($idx == $module){
312             $objects[$object]= $object;
313           }
314         }
315       }
316     }
318     /* For all gosaDepartments */
319     foreach ($this->config->departments as $dn){
320       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
322       /* Build dn array */
323       $path= split(',', $dn);
324       $path= array_reverse($path);
326       /* Walk along the path to evaluate the acl */
327       $cpath= "";
328       foreach ($path as $element){
330         /* Clean potential ACLs for each level */
331         $acl= $this->cleanACL($acl);
333         if ($cpath == ""){
334           $cpath= $element;
335         } else {
336           $cpath= $element.','.$cpath;
337         }
338         if (isset($this->ACL[$cpath])){
340           /* Inspect this ACL, place the result into ACL */
341           foreach ($this->ACL[$cpath] as $subacl){
343             /* Reset? Just clean the ACL and turn over to the next one... */
344             if ($subacl['type'] == 'reset'){
345               $acl= $this->cleanACL($acl, TRUE);
346               continue;
347             }
348     
349             if($subacl['type'] == 'role'){
350               echo "role skipped";
351               continue;
352             }
354             /* Per object ACL? */
355             foreach ($objects as $object){
356               if (isset($subacl['acl']["$module/$object"])){
357                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
358                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
359                 }
360               }
361             }
363             /* Global ACL? */
364             if (isset($subacl['acl']["$module/all"][0])){
365               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
366               continue;
367             }
369             /* Global ACL? */
370             if (isset($subacl['acl']["all"][0])){
371               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
372               continue;
373             }
374           }
375         }
376       }
378       /* Add department, if we have (some) permissions for the required module */
379       foreach ($acl as $val){
380         if ($val != ""){
381           $deps[]= $dn;
382           break;
383         }
384       }
385     }
387     $_SESSION['ACL_CACHE']['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
388     return ($deps);
389   }
392   function mergeACL($acl, $type, $newACL)
393   {
394     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
395       $newACL .= "r";
396     }
398     foreach(str_split($newACL) as $char){
400       /* Ignore invalid characters */
401       if (!preg_match('/[rwcdm]/', $char)){
402         continue;
403       }
405       /* Skip permanent and subtree entries */
406       if (preg_match('/[sp]/', $acl[$char])){
407         continue;
408       }
410       switch ($type){
411         case 'psub':
412           $acl[$char]= 'p';
413           break;
415         case 'sub':
416           $acl[$char]= 's';
417           break;
419         case 'one':
420           $acl[$char]= 1;
421           break;
423         case 'base':
424           if ($acl[$char] != 1){
425             $acl[$char]= 0;
426           }
427           break;
428       }
429     }
431     return ($acl);
432   }
435   function cleanACL($acl, $reset= FALSE)
436   {
437     foreach ($acl as &$value){
439       /* Reset removes everything but 'p' */
440       if ($reset && $value != 'p'){
441         $value= "";
442         continue;
443       }
445       /* Decrease tree level */
446       if (is_int($value)){
447         if ($value){
448           $value--;
449         } else {
450           $value= "";
451         }
452       }
453     }
455     return ($acl);
456   }
459   /* #FIXME This could be logical wrong or could be optimized in the future
460      Return combined acls for a given category. 
461      All acls will be combined like boolean AND 
462       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
463     
464      Results will be cached in $this->result_cache.
465       $this->result_cache will be resetted if load_acls is called.
466   */
467   function has_complete_category_acls($dn,$category)
468   {
469     $acl    = "rwcdm";
470     $types  = "rwcdm";
472     if(!is_string($category)){
473       trigger_error("category must be string");   
474       $acl = "";
475     }else{
476       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
477         if (isset($this->ocMapping[$category])){
478           foreach($this->ocMapping[$category] as $oc){
480             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
481             if($oc == "0") continue;
482             $tmp =  $this->get_permissions($dn, $category."/".$oc);
483             for($i = 0 ; $i < strlen($types); $i++) {
484               if(!preg_match("/".$types[$i]."/",$tmp)){ 
485                 $acl = preg_replace("/".$types[$i]."/","",$acl);
486               }
487             }
488           }
489         }else{
490           trigger_error("Invalid type of category ".$category);
491           $acl = "";
492         }
493         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
494       }else{
495         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
496       }
497     }
498     return($acl);
499   }
502 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
503 ?>