Code

Removed reset ogroup and set Keep ogroup index to ""
[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     }
175     return ($acl);
176   }
179   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
180   {
181     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
183     /* Build dn array */
184     $path= split(',', $dn);
185     $path= array_reverse($path);
187     /* Walk along the path to evaluate the acl */
188     $cpath= "";
189     foreach ($path as $element){
191       /* Clean potential ACLs for each level */
192       $acl= $this->cleanACL($acl);
194       if ($cpath == ""){
195         $cpath= $element;
196       } else {
197         $cpath= $element.','.$cpath;
198       }
199       if (isset($this->ACL[$cpath])){
201         /* Inspect this ACL, place the result into ACL */
202         foreach ($this->ACL[$cpath] as $subacl){
204           /* Reset? Just clean the ACL and turn over to the next one... */
205           if ($subacl['type'] == 'reset'){
206             $acl= $this->cleanACL($acl, TRUE);
207             continue;
208           }
210           if($subacl['type'] == "role") {
211             echo "role skipped";
212             continue;
213           }
215           /* Per attribute ACL? */
216           if (isset($subacl['acl'][$object][$attribute])){
217             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
218             continue;
219           }
221           /* Per object ACL? */
222           if (isset($subacl['acl'][$object][0])){
223             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
224             continue;
225           }
227           /* Global ACL? */
228           if (isset($subacl['acl']['all'][0])){
229             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
230             continue;
231           }
233           /* If attribute is "", we want to know, if we've *any* permissions here... */
234           if ($attribute == "" && isset($subacl['acl'][$object])){
235             foreach($subacl['acl'][$object] as $attr => $dummy){
236               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
237             }
238             continue;
239           }
241         }
242       }
243     }
245     /* Assemble string */
246     $ret= "";
247     foreach ($acl as $key => $value){
248       if ($value != ""){
249         $ret.= $key;
250       }
251     }
253     /* Remove write if needed */
254     if ($skip_write){
255       $ret= preg_replace('/w/', '', $ret);
256     }
258     return ($ret);
259   }
262   /* Extract all departments that are accessible (direct or 'on the way' to an
263      accessible department) */
264   function get_module_departments($module)
265   {
266     global $plist;
268     $objects= array();
269     $deps= array();
271     /* Extract all relevant objects for this module from plist */
272     foreach ($plist->info as $object => $info){
273       if (!isset($info['plCategory'])){
274         continue;
275       }
276       foreach ($info['plCategory'] as $idx => $data){
277         if (preg_match('/^[0-9]+$/', $idx)){
278           if ($data == $module){
279             $objects[$object]= $object;
280           }
281         } else {
282           if ($idx == $module){
283             $objects[$object]= $object;
284           }
285         }
286       }
287     }
289     /* Load departments here, if we are using php4 */
290     if(is_php4() && !count($this->config->departments)){
291       $this->config->get_departments();
292     }
294     /* For all gosaDepartments */
295     foreach ($this->config->departments as $dn){
296       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
298       /* Build dn array */
299       $path= split(',', $dn);
300       $path= array_reverse($path);
302       /* Walk along the path to evaluate the acl */
303       $cpath= "";
304       foreach ($path as $element){
306         /* Clean potential ACLs for each level */
307         $acl= $this->cleanACL($acl);
309         if ($cpath == ""){
310           $cpath= $element;
311         } else {
312           $cpath= $element.','.$cpath;
313         }
314         if (isset($this->ACL[$cpath])){
316           /* Inspect this ACL, place the result into ACL */
317           foreach ($this->ACL[$cpath] as $subacl){
319             /* Reset? Just clean the ACL and turn over to the next one... */
320             if ($subacl['type'] == 'reset'){
321               $acl= $this->cleanACL($acl, TRUE);
322               continue;
323             }
324     
325             if($subacl['type'] == 'role'){
326               echo "role skipped";
327               continue;
328             }
330             /* Per object ACL? */
331             foreach ($objects as $object){
332               if (isset($subacl['acl']["$module/$object"])){
333                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
334                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
335                 }
336               }
337             }
339             /* Global ACL? */
340             if (isset($subacl['acl']["$module/all"][0])){
341               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
342               continue;
343             }
345             /* Global ACL? */
346             if (isset($subacl['acl']["all"][0])){
347               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
348               continue;
349             }
350           }
351         }
352       }
354       /* Add department, if we have (some) permissions for the required module */
355       foreach ($acl as $val){
356         if ($val != ""){
357           $deps[]= $dn;
358           break;
359         }
360       }
361     }
363     return ($deps);
364   }
367   function mergeACL($acl, $type, $newACL)
368   {
369     if(preg_match("/w/",$newACL) && !preg_match("/r/",$newACL)){
370       $newACL .= "r";
371     }
372     foreach(str_split($newACL) as $char){
374       /* Ignore invalid characters */
375       if (!preg_match('/[rwcdm]/', $char)){
376         continue;
377       }
379       /* Skip permanent and subtree entries */
380       if (preg_match('/[sp]/', $acl[$char])){
381         continue;
382       }
384       switch ($type){
385         case 'psub':
386           $acl[$char]= 'p';
387           break;
389         case 'sub':
390           $acl[$char]= 's';
391           break;
393         case 'one':
394           $acl[$char]= 1;
395           break;
397         case 'base':
398           if ($acl[$char] != 1){
399             $acl[$char]= 0;
400           }
401           break;
402       }
403     }
405     return ($acl);
406   }
409   function cleanACL($acl, $reset= FALSE)
410   {
411     foreach ($acl as $key => $value){
413       /* Reset removes everything but 'p' */
414       if ($reset && $value != 'p'){
415         $acl[$key]= "";
416         continue;
417       }
419       /* Decrease tree level */
420       if (preg_match('/^[0-9]+$/', $value)){
421         if ($value > 0){
422           $acl[$key]= $value - 1;
423         } else {
424           $acl[$key]= "";
425         }
426       }
427     }
429     return ($acl);
430   }
433   /* #FIXME This could be logical wrong or could be optimized in the future
434      Return combined acls for a given category. 
435      All acls will be combined like boolean AND 
436       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
437     
438      Results will be cached in $this->result_cache.
439       $this->result_cache will be resetted if load_acls is called.
440   */
441   function has_complete_category_acls($dn,$category)
442   {
443     $acl    = "rwcdm";
444     $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 ?>