Code

8268e57b57eb80f9d58a2f5cc330be58fa7c77ff
[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'];
67   }
70   function loadACL()
71   {
72     $this->ACL= array();    
73     $this->groups= array();    
74     $this->result_cache =array();
75     $ldap= $this->config->get_ldap_link();
76     $ldap->cd($this->config->current['BASE']);
78     /* Get member groups... */
79     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
80     while ($attrs= $ldap->fetch()){
81       $this->groups[$attrs['dn']]= $attrs['dn'];
82     }
84     /* Crawl through ACLs and move relevant to the tree */
85     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
86     $aclp= array();
87     $aclc= array();
88     while ($attrs= $ldap->fetch()){
90       /* Insert links in ACL array */
91       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
92       $aclc[$attrs['dn']]= array();
93       $ol= array();
94       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
95         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
96       }
97       $aclc[$attrs['dn']]= $ol;
98     }
100     /* Resolve roles here. 
101      */
102     foreach($aclc as $dn => $data){
103       foreach($data as $prio => $aclc_value)  {
104         if($aclc_value['type'] == "role"){
106           unset($aclc[$dn][$prio]);
108           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
109           $attrs = $ldap->fetch();
110           for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
111             $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
113             foreach($tmp as $new_acl){
114               $new_acl['members'] = $aclc_value['members'];
115               $aclc[$dn][] =$new_acl;
116             }
117           }      
118         }
119       }
120     }
122     /* ACL's read, sort for tree depth */
123     asort($aclp);
125     /* Sort in tree order */
126     foreach ($aclp as $dn => $acl){
127       /* Check if we need to keep this ACL */
128       foreach($aclc[$dn] as $idx => $type){
129         $interresting= FALSE;
130         
131         /* No members? This is good for all users... */
132         if (!count($type['members'])){
133           $interresting= TRUE;
134         } else {
136           /* Inspect members... */
137           foreach ($type['members'] as $grp => $grpdsc){
138             /* Some group inside the members that is relevant for us? */
139             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
140               $interresting= TRUE;
141             }
143             /* User inside the members? */
144             if (preg_replace('/^U:/', '', $grp) == $this->dn){
145               $interresting= TRUE;
146             }
147           }
148         }
150         if ($interresting){
151           if (!isset($this->ACL[$dn])){
152             $this->ACL[$dn]= array();
153           }
154           $this->ACL[$dn][$idx]= $type;
155         }
156       }
158     }
159   }
162   function get_category_permissions($dn, $category)
163   {
164     /* Get list of objectClasses and get the permissions for it */
165     $acl= "";
166     if (isset($this->ocMapping[$category])){
167       foreach($this->ocMapping[$category] as $oc){
168         $acl.= $this->get_permissions($dn, $category."/".$oc);
169       }
170     }
172     return ($acl);
173   }
176   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
177   {
178     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
180     /* Build dn array */
181     $path= split(',', $dn);
182     $path= array_reverse($path);
184     /* Walk along the path to evaluate the acl */
185     $cpath= "";
186     foreach ($path as $element){
188       /* Clean potential ACLs for each level */
189       $acl= $this->cleanACL($acl);
191       if ($cpath == ""){
192         $cpath= $element;
193       } else {
194         $cpath= $element.','.$cpath;
195       }
196       if (isset($this->ACL[$cpath])){
198         /* Inspect this ACL, place the result into ACL */
199         foreach ($this->ACL[$cpath] as $subacl){
201           /* Reset? Just clean the ACL and turn over to the next one... */
202           if ($subacl['type'] == 'reset'){
203             $acl= $this->cleanACL($acl, TRUE);
204             continue;
205           }
207           if($subacl['type'] == "role") {
208             echo "role skipped";
209             continue;
210           }
212           /* Per attribute ACL? */
213           if (isset($subacl['acl'][$object][$attribute])){
214             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
215             continue;
216           }
218           /* Per object ACL? */
219           if (isset($subacl['acl'][$object][0])){
220             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
221             continue;
222           }
224           /* Global ACL? */
225           if (isset($subacl['acl']['all'][0])){
226             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
227             continue;
228           }
230           /* If attribute is "", we want to know, if we've *any* permissions here... */
231           if ($attribute == "" && isset($subacl['acl'][$object])){
232             foreach($subacl['acl'][$object] as $attr => $dummy){
233               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
234             }
235             continue;
236           }
238         }
239       }
240     }
242     /* Assemble string */
243     $ret= "";
244     foreach ($acl as $key => $value){
245       if ($value != ""){
246         $ret.= $key;
247       }
248     }
250     /* Remove write if needed */
251     if ($skip_write){
252       $ret= preg_replace('/w/', '', $ret);
253     }
255     return ($ret);
256   }
259   /* Extract all departments that are accessible (direct or 'on the way' to an
260      accessible department) */
261   function get_module_departments($module)
262   {
263     global $plist;
265     $objects= array();
266     $deps= array();
268     /* Extract all relevant objects for this module from plist */
269     foreach ($plist->info as $object => $info){
270       if (!isset($info['plCategory'])){
271         continue;
272       }
273       foreach ($info['plCategory'] as $idx => $data){
274         if (preg_match('/^[0-9]+$/', $idx)){
275           if ($data == $module){
276             $objects[$object]= $object;
277           }
278         } else {
279           if ($idx == $module){
280             $objects[$object]= $object;
281           }
282         }
283       }
284     }
286     /* For all gosaDepartments */
287     foreach ($this->config->departments as $dn){
288       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
290       /* Build dn array */
291       $path= split(',', $dn);
292       $path= array_reverse($path);
294       /* Walk along the path to evaluate the acl */
295       $cpath= "";
296       foreach ($path as $element){
298         /* Clean potential ACLs for each level */
299         $acl= $this->cleanACL($acl);
301         if ($cpath == ""){
302           $cpath= $element;
303         } else {
304           $cpath= $element.','.$cpath;
305         }
306         if (isset($this->ACL[$cpath])){
308           /* Inspect this ACL, place the result into ACL */
309           foreach ($this->ACL[$cpath] as $subacl){
311             /* Reset? Just clean the ACL and turn over to the next one... */
312             if ($subacl['type'] == 'reset'){
313               $acl= $this->cleanACL($acl, TRUE);
314               continue;
315             }
316     
317             if($subacl['type'] == 'role'){
318               echo "role skipped";
319               continue;
320             }
322             /* Per object ACL? */
323             foreach ($objects as $object){
324               if (isset($subacl['acl']["$module/$object"])){
325                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
326                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
327                 }
328               }
329             }
331             /* Global ACL? */
332             if (isset($subacl['acl']["$module/all"][0])){
333               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
334               continue;
335             }
337             /* Global ACL? */
338             if (isset($subacl['acl']["all"][0])){
339               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
340               continue;
341             }
342           }
343         }
344       }
346       /* Add department, if we have (some) permissions for the required module */
347       foreach ($acl as $val){
348         if ($val != ""){
349           $deps[]= $dn;
350           break;
351         }
352       }
353     }
355     return ($deps);
356   }
359   function mergeACL($acl, $type, $newACL)
360   {
361     if(preg_match("/w/",$newACL) && !preg_match("/r/",$newACL)){
362       $newACL .= "r";
363     }
364     foreach(str_split($newACL) as $char){
366       /* Ignore invalid characters */
367       if (!preg_match('/[rwcdm]/', $char)){
368         continue;
369       }
371       /* Skip permanent and subtree entries */
372       if (preg_match('/[sp]/', $acl[$char])){
373         continue;
374       }
376       switch ($type){
377         case 'psub':
378           $acl[$char]= 'p';
379           break;
381         case 'sub':
382           $acl[$char]= 's';
383           break;
385         case 'one':
386           $acl[$char]= 1;
387           break;
389         case 'base':
390           if ($acl[$char] != 1){
391             $acl[$char]= 0;
392           }
393           break;
394       }
395     }
397     return ($acl);
398   }
401   function cleanACL($acl, $reset= FALSE)
402   {
403     foreach ($acl as $key => $value){
405       /* Reset removes everything but 'p' */
406       if ($reset && $value != 'p'){
407         $acl[$key]= "";
408         continue;
409       }
411       /* Decrease tree level */
412       if (preg_match('/^[0-9]+$/', $value)){
413         if ($value > 0){
414           $acl[$key]= $value - 1;
415         } else {
416           $acl[$key]= "";
417         }
418       }
419     }
421     return ($acl);
422   }
425   /* #FIXME This could be logical wrong or could be optimized in the future
426      Return combined acls for a given category. 
427      All acls will be combined like boolean AND 
428       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
429     
430      Results will be cached in $this->result_cache.
431       $this->result_cache will be resetted if load_acls is called.
432   */
433   function has_complete_category_acls($dn,$category)
434   {
435     $acl    = "rwcdm";
436     $types  = "rwcdm";
439     if(!is_string($category)){
440       trigger_error("category must be string");   
441       $acl = "";
442     }else{
443       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
444         if (isset($this->ocMapping[$category])){
445           foreach($this->ocMapping[$category] as $oc){
447             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
448             if($oc == "0") continue;
449             $tmp =  $this->get_permissions($dn, $category."/".$oc);
450             for($i = 0 ; $i < strlen($types); $i++) {
451               if(!preg_match("/".$types[$i]."/",$tmp)){ 
452                 $acl = preg_replace("/".$types[$i]."/","",$acl);
453               }
454             }
455           }
456         }else{
457           trigger_error("Invalid type of category ".$category);
458           $acl = "";
459         }
460         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
461       }else{
462         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
463       }
464     }
465     return($acl);
466   }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>