Code

8ac07ad71fbc2e2264d559f36f7d6172d6d9ab9f
[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();
111           if(isset($attrs['gosaAclTemplate'])){
112             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
113               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
115               foreach($tmp as $new_acl){
116                 $new_acl['members'] = $aclc_value['members'];
117                 $aclc[$dn][] =$new_acl;
118               }
119             }      
120           }
121         }
122       }
123     }
125     /* ACL's read, sort for tree depth */
126     asort($aclp);
128     /* Sort in tree order */
129     foreach ($aclp as $dn => $acl){
130       /* Check if we need to keep this ACL */
131       foreach($aclc[$dn] as $idx => $type){
132         $interresting= FALSE;
133         
134         /* No members? This is good for all users... */
135         if (!count($type['members'])){
136           $interresting= TRUE;
137         } else {
139           /* Inspect members... */
140           foreach ($type['members'] as $grp => $grpdsc){
141             /* Some group inside the members that is relevant for us? */
142             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
143               $interresting= TRUE;
144             }
146             /* User inside the members? */
147             if (preg_replace('/^U:/', '', $grp) == $this->dn){
148               $interresting= TRUE;
149             }
150           }
151         }
153         if ($interresting){
154           if (!isset($this->ACL[$dn])){
155             $this->ACL[$dn]= array();
156           }
157           $this->ACL[$dn][$idx]= $type;
158         }
159       }
161     }
162   }
165   function get_category_permissions($dn, $category)
166   {
167     /* Get list of objectClasses and get the permissions for it */
168     $acl= "";
169     if (isset($this->ocMapping[$category])){
170       foreach($this->ocMapping[$category] as $oc){
171         $acl.= $this->get_permissions($dn, $category."/".$oc);
172       }
173     } else {
174       if (isset($this->ocMapping['all'])){
175         foreach($this->ocMapping['all'] as $oc){
176           $acl.= $this->get_permissions($dn, $category."/".$oc);
177         }
178       }
179     }
181     return ($acl);
182   }
185   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
186   {
187     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
189     /* Build dn array */
190     $path= split(',', $dn);
191     $path= array_reverse($path);
193     /* Walk along the path to evaluate the acl */
194     $cpath= "";
195     foreach ($path as $element){
197       /* Clean potential ACLs for each level */
198       $acl= $this->cleanACL($acl);
200       if ($cpath == ""){
201         $cpath= $element;
202       } else {
203         $cpath= $element.','.$cpath;
204       }
205       if (isset($this->ACL[$cpath])){
207         /* Inspect this ACL, place the result into ACL */
208         foreach ($this->ACL[$cpath] as $subacl){
210           /* Reset? Just clean the ACL and turn over to the next one... */
211           if ($subacl['type'] == 'reset'){
212             $acl= $this->cleanACL($acl, TRUE);
213             continue;
214           }
216           if($subacl['type'] == "role") {
217             echo "role skipped";
218             continue;
219           }
221           /* Per attribute ACL? */
222           if (isset($subacl['acl'][$object][$attribute])){
223             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
224             continue;
225           }
227           /* Per object ACL? */
228           if (isset($subacl['acl'][$object][0])){
229             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
230             continue;
231           }
233           /* Global ACL? */
234           if (isset($subacl['acl']['all'][0])){
235             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
236             continue;
237           }
239           /* If attribute is "", we want to know, if we've *any* permissions here... */
240           if ($attribute == "" && isset($subacl['acl'][$object])){
241             foreach($subacl['acl'][$object] as $attr => $dummy){
242               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
243             }
244             continue;
245           }
247         }
248       }
249     }
251     /* Assemble string */
252     $ret= "";
253     foreach ($acl as $key => $value){
254       if ($value != ""){
255         $ret.= $key;
256       }
257     }
259     /* Remove write if needed */
260     if ($skip_write){
261       $ret= preg_replace('/w/', '', $ret);
262     }
264     return ($ret);
265   }
268   /* Extract all departments that are accessible (direct or 'on the way' to an
269      accessible department) */
270   function get_module_departments($module)
271   {
272     global $plist;
274     $objects= array();
275     $deps= array();
277     /* Extract all relevant objects for this module from plist */
278     foreach ($plist->info as $object => $info){
279       if (!isset($info['plCategory'])){
280         continue;
281       }
282       foreach ($info['plCategory'] as $idx => $data){
283         if (preg_match('/^[0-9]+$/', $idx)){
284           if ($data == $module){
285             $objects[$object]= $object;
286           }
287         } else {
288           if ($idx == $module){
289             $objects[$object]= $object;
290           }
291         }
292       }
293     }
295     /* For all gosaDepartments */
296     foreach ($this->config->departments as $dn){
297       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
299       /* Build dn array */
300       $path= split(',', $dn);
301       $path= array_reverse($path);
303       /* Walk along the path to evaluate the acl */
304       $cpath= "";
305       foreach ($path as $element){
307         /* Clean potential ACLs for each level */
308         $acl= $this->cleanACL($acl);
310         if ($cpath == ""){
311           $cpath= $element;
312         } else {
313           $cpath= $element.','.$cpath;
314         }
315         if (isset($this->ACL[$cpath])){
317           /* Inspect this ACL, place the result into ACL */
318           foreach ($this->ACL[$cpath] as $subacl){
320             /* Reset? Just clean the ACL and turn over to the next one... */
321             if ($subacl['type'] == 'reset'){
322               $acl= $this->cleanACL($acl, TRUE);
323               continue;
324             }
325     
326             if($subacl['type'] == 'role'){
327               echo "role skipped";
328               continue;
329             }
331             /* Per object ACL? */
332             foreach ($objects as $object){
333               if (isset($subacl['acl']["$module/$object"])){
334                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
335                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
336                 }
337               }
338             }
340             /* Global ACL? */
341             if (isset($subacl['acl']["$module/all"][0])){
342               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
343               continue;
344             }
346             /* Global ACL? */
347             if (isset($subacl['acl']["all"][0])){
348               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
349               continue;
350             }
351           }
352         }
353       }
355       /* Add department, if we have (some) permissions for the required module */
356       foreach ($acl as $val){
357         if ($val != ""){
358           $deps[]= $dn;
359           break;
360         }
361       }
362     }
364     return ($deps);
365   }
368   function mergeACL($acl, $type, $newACL)
369   {
370     if(preg_match("/w/",$newACL) && !preg_match("/r/",$newACL)){
371       $newACL .= "r";
372     }
373     foreach(str_split($newACL) as $char){
375       /* Ignore invalid characters */
376       if (!preg_match('/[rwcdm]/', $char)){
377         continue;
378       }
380       /* Skip permanent and subtree entries */
381       if (preg_match('/[sp]/', $acl[$char])){
382         continue;
383       }
385       switch ($type){
386         case 'psub':
387           $acl[$char]= 'p';
388           break;
390         case 'sub':
391           $acl[$char]= 's';
392           break;
394         case 'one':
395           $acl[$char]= 1;
396           break;
398         case 'base':
399           if ($acl[$char] != 1){
400             $acl[$char]= 0;
401           }
402           break;
403       }
404     }
406     return ($acl);
407   }
410   function cleanACL($acl, $reset= FALSE)
411   {
412     foreach ($acl as &$value){
414       /* Reset removes everything but 'p' */
415       if ($reset && $value != 'p'){
416         $value= "";
417         continue;
418       }
420       /* Decrease tree level */
421       if (is_int($value)){
422         if ($value){
423           $value--;
424         } else {
425           $value= "";
426         }
427       }
428     }
430     return ($acl);
431   }
434   /* #FIXME This could be logical wrong or could be optimized in the future
435      Return combined acls for a given category. 
436      All acls will be combined like boolean AND 
437       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
438     
439      Results will be cached in $this->result_cache.
440       $this->result_cache will be resetted if load_acls is called.
441   */
442   function has_complete_category_acls($dn,$category)
443   {
444     $acl    = "rwcdm";
445     $types  = "rwcdm";
447     if(!is_string($category)){
448       trigger_error("category must be string");   
449       $acl = "";
450     }else{
451       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
452         if (isset($this->ocMapping[$category])){
453           foreach($this->ocMapping[$category] as $oc){
455             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
456             if($oc == "0") continue;
457             $tmp =  $this->get_permissions($dn, $category."/".$oc);
458             for($i = 0 ; $i < strlen($types); $i++) {
459               if(!preg_match("/".$types[$i]."/",$tmp)){ 
460                 $acl = preg_replace("/".$types[$i]."/","",$acl);
461               }
462             }
463           }
464         }else{
465           trigger_error("Invalid type of category ".$category);
466           $acl = "";
467         }
468         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
469       }else{
470         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
471       }
472     }
473     return($acl);
474   }
477 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
478 ?>