Code

Replaced print_red with msg_dialog, for testing
[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"])){
194       return ($_SESSION['ACL_CACHE']["$dn+$object+$attribute"]);
195     }
197     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
199     /* Build dn array */
200     $path= split(',', $dn);
201     $path= array_reverse($path);
203     /* Walk along the path to evaluate the acl */
204     $cpath= "";
205     foreach ($path as $element){
207       /* Clean potential ACLs for each level */
208       $acl= $this->cleanACL($acl);
210       if ($cpath == ""){
211         $cpath= $element;
212       } else {
213         $cpath= $element.','.$cpath;
214       }
215       if (isset($this->ACL[$cpath])){
217         /* Inspect this ACL, place the result into ACL */
218         foreach ($this->ACL[$cpath] as $subacl){
220           /* Reset? Just clean the ACL and turn over to the next one... */
221           if ($subacl['type'] == 'reset'){
222             $acl= $this->cleanACL($acl, TRUE);
223             continue;
224           }
226           if($subacl['type'] == "role") {
227             echo "role skipped";
228             continue;
229           }
231           /* Per attribute ACL? */
232           if (isset($subacl['acl'][$object][$attribute])){
233             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
234             continue;
235           }
237           /* Per object ACL? */
238           if (isset($subacl['acl'][$object][0])){
239             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
240             continue;
241           }
243           /* Global ACL? */
244           if (isset($subacl['acl']['all'][0])){
245             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
246             continue;
247           }
249           /* If attribute is "", we want to know, if we've *any* permissions here... */
250           if ($attribute == "" && isset($subacl['acl'][$object])){
251             foreach($subacl['acl'][$object] as $attr => $dummy){
252               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
253             }
254             continue;
255           }
257         }
258       }
259     }
261     /* Assemble string */
262     $ret= "";
263     foreach ($acl as $key => $value){
264       if ($value != ""){
265         $ret.= $key;
266       }
267     }
269     /* Remove write if needed */
270     if ($skip_write){
271       $ret= preg_replace('/w/', '', $ret);
272     }
274     $_SESSION['ACL_CACHE']["$dn+$object+$attribute"]= $ret;
275     return ($ret);
276   }
279   /* Extract all departments that are accessible (direct or 'on the way' to an
280      accessible department) */
281   function get_module_departments($module)
282   {
283     global $plist;
285     $objects= array();
286     $deps= array();
288     /* Extract all relevant objects for this module from plist */
289     foreach ($plist->info as $object => $info){
290       if (!isset($info['plCategory'])){
291         continue;
292       }
293       foreach ($info['plCategory'] as $idx => $data){
294         if (preg_match('/^[0-9]+$/', $idx)){
295           if ($data == $module){
296             $objects[$object]= $object;
297           }
298         } else {
299           if ($idx == $module){
300             $objects[$object]= $object;
301           }
302         }
303       }
304     }
306     /* For all gosaDepartments */
307     foreach ($this->config->departments as $dn){
308       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
310       /* Build dn array */
311       $path= split(',', $dn);
312       $path= array_reverse($path);
314       /* Walk along the path to evaluate the acl */
315       $cpath= "";
316       foreach ($path as $element){
318         /* Clean potential ACLs for each level */
319         $acl= $this->cleanACL($acl);
321         if ($cpath == ""){
322           $cpath= $element;
323         } else {
324           $cpath= $element.','.$cpath;
325         }
326         if (isset($this->ACL[$cpath])){
328           /* Inspect this ACL, place the result into ACL */
329           foreach ($this->ACL[$cpath] as $subacl){
331             /* Reset? Just clean the ACL and turn over to the next one... */
332             if ($subacl['type'] == 'reset'){
333               $acl= $this->cleanACL($acl, TRUE);
334               continue;
335             }
336     
337             if($subacl['type'] == 'role'){
338               echo "role skipped";
339               continue;
340             }
342             /* Per object ACL? */
343             foreach ($objects as $object){
344               if (isset($subacl['acl']["$module/$object"])){
345                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
346                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
347                 }
348               }
349             }
351             /* Global ACL? */
352             if (isset($subacl['acl']["$module/all"][0])){
353               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
354               continue;
355             }
357             /* Global ACL? */
358             if (isset($subacl['acl']["all"][0])){
359               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
360               continue;
361             }
362           }
363         }
364       }
366       /* Add department, if we have (some) permissions for the required module */
367       foreach ($acl as $val){
368         if ($val != ""){
369           $deps[]= $dn;
370           break;
371         }
372       }
373     }
375     return ($deps);
376   }
379   function mergeACL($acl, $type, $newACL)
380   {
381     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
382       $newACL .= "r";
383     }
385     foreach(str_split($newACL) as $char){
387       /* Ignore invalid characters */
388       if (!preg_match('/[rwcdm]/', $char)){
389         continue;
390       }
392       /* Skip permanent and subtree entries */
393       if (preg_match('/[sp]/', $acl[$char])){
394         continue;
395       }
397       switch ($type){
398         case 'psub':
399           $acl[$char]= 'p';
400           break;
402         case 'sub':
403           $acl[$char]= 's';
404           break;
406         case 'one':
407           $acl[$char]= 1;
408           break;
410         case 'base':
411           if ($acl[$char] != 1){
412             $acl[$char]= 0;
413           }
414           break;
415       }
416     }
418     return ($acl);
419   }
422   function cleanACL($acl, $reset= FALSE)
423   {
424     foreach ($acl as &$value){
426       /* Reset removes everything but 'p' */
427       if ($reset && $value != 'p'){
428         $value= "";
429         continue;
430       }
432       /* Decrease tree level */
433       if (is_int($value)){
434         if ($value){
435           $value--;
436         } else {
437           $value= "";
438         }
439       }
440     }
442     return ($acl);
443   }
446   /* #FIXME This could be logical wrong or could be optimized in the future
447      Return combined acls for a given category. 
448      All acls will be combined like boolean AND 
449       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
450     
451      Results will be cached in $this->result_cache.
452       $this->result_cache will be resetted if load_acls is called.
453   */
454   function has_complete_category_acls($dn,$category)
455   {
456     $acl    = "rwcdm";
457     $types  = "rwcdm";
459     if(!is_string($category)){
460       trigger_error("category must be string");   
461       $acl = "";
462     }else{
463       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
464         if (isset($this->ocMapping[$category])){
465           foreach($this->ocMapping[$category] as $oc){
467             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
468             if($oc == "0") continue;
469             $tmp =  $this->get_permissions($dn, $category."/".$oc);
470             for($i = 0 ; $i < strlen($types); $i++) {
471               if(!preg_match("/".$types[$i]."/",$tmp)){ 
472                 $acl = preg_replace("/".$types[$i]."/","",$acl);
473               }
474             }
475           }
476         }else{
477           trigger_error("Invalid type of category ".$category);
478           $acl = "";
479         }
480         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
481       }else{
482         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
483       }
484     }
485     return($acl);
486   }
489 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
490 ?>