Code

Only display allowed bases
[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();
37   /* get acl's an put them into the userinfo object
38      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
39   function userinfo($config, $userdn){
40     $this->config= $config;
41     $ldap= $this->config->get_ldap_link();
42     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
43     $attrs= $ldap->fetch();
45     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
46       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
47     } else {
48       $this->cn= $attrs['uid'][0];
49     }
50     if (isset($attrs['gidNumber'][0])){
51       $this->gidNumber= $attrs['gidNumber'][0];
52     }
54     /* Assign user language */
55     if (isset($attrs['preferredLanguage'][0])){
56       $this->language= $attrs['preferredLanguage'][0];
57     }
59     if (isset($attrs['gosaUnitTag'][0])){
60       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
61     }
63     $this->dn= $userdn;
64     $this->uid= $attrs['uid'][0];
65     $this->ip= $_SERVER['REMOTE_ADDR'];
66   }
69   function loadACL()
70   {
72 #--------------------------------------------------------------------------OLD-BUT-ACTIVE-----------------------------
73     $ldap= $this->config->get_ldap_link();
75     /* Load ACL's from all groups we're in */
76     $this->subtreeACL= array();
77     $ldap->cd($this->config->current['BASE']);
78     if ($this->gidNumber == -1){
79       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
80           "(memberUid=$this->username))");
81     } else {
82       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
83           "(|(memberUid=$this->username)(gidNumber=$this->gidNumber)))");
84     }
86     while($attrs = $ldap->fetch()){
87       $base= preg_replace('/^[^,]+,ou=[^,]+,/i', "",$ldap->getDN());
88       $base= preg_replace("/[ ]*,[ ]*/", ",", $base);
90       for ($i= 0; $i<$attrs["gosaSubtreeACL"]["count"]; $i++){
91         $this->subtreeACL[$base][]= $attrs["gosaSubtreeACL"][$i];
92       }
93     }
94 #echo "NEW ACL LOADING --------------------------------------------------------------------------------------------<br>";
96     $this->ACL= array();    
97     $this->groups= array();    
98     $ldap= $this->config->get_ldap_link();
99     $ldap->cd($this->config->current['BASE']);
101     /* Get member groups... */
102     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
103     while ($attrs= $ldap->fetch()){
104       $this->groups[$attrs['dn']]= $attrs['dn'];
105     }
107     /* Crawl through ACLs and move relevant to the tree */
108     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
109     $aclp= array();
110     $aclc= array();
111     while ($attrs= $ldap->fetch()){
113       /* Insert links in ACL array */
114       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
115       $aclc[$attrs['dn']]= array();
116       $ol= array();
117       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
118         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
119       }
120       $aclc[$attrs['dn']]= $ol;
121     }
123     /* ACL's read, sort for tree depth */
124     asort($aclp);
126     /* Sort in tree order */
127     foreach ($aclp as $dn => $acl){
128       /* Check if we need to keep this ACL */
129       foreach($aclc[$dn] as $idx => $type){
130         $interresting= FALSE;
131         
132         /* No members? This is good for all users... */
133         if (!count($type['members'])){
134           $interresting= TRUE;
135         } else {
137           /* Inspect members... */
138           foreach ($type['members'] as $grp => $grpdsc){
139             /* Some group inside the members that is relevant for us? */
140             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
141               $interresting= TRUE;
142             }
144             /* User inside the members? */
145             if (preg_replace('/^U:/', '', $grp) == $this->dn){
146               $interresting= TRUE;
147             }
148           }
149         }
151         if ($interresting){
152           if (!isset($this->ACL[$dn])){
153             $this->ACL[$dn]= array();
154           }
155           $this->ACL[$dn][$idx]= $type;
156         }
157       }
159     }
161   }
164   function get_category_permissions($dn, $category)
165   {
166     /* Get list of objectClasses and get the permissions for it */
167     $acl= "";
168     if (isset($this->ocMapping[$category])){
169       foreach($this->ocMapping[$category] as $oc){
170         $acl.= $this->get_permissions($dn, $category."/".$oc);
171       }
172     }
174     return ($acl);
175   }
178   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
179   {
180     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
182     /* Build dn array */
183     $path= split(',', $dn);
184     $path= array_reverse($path);
186     /* Walk along the path to evaluate the acl */
187     $cpath= "";
188     foreach ($path as $element){
190       /* Clean potential ACLs for each level */
191       $acl= $this->cleanACL($acl);
193       if ($cpath == ""){
194         $cpath= $element;
195       } else {
196         $cpath= $element.','.$cpath;
197       }
198       if (isset($this->ACL[$cpath])){
200         /* Inspect this ACL, place the result into ACL */
201         foreach ($this->ACL[$cpath] as $subacl){
203           /* Reset? Just clean the ACL and turn over to the next one... */
204           if ($subacl['type'] == 'reset'){
205             $acl= $this->cleanACL($acl, TRUE);
206             continue;
207           }
209           /* Per attribute ACL? */
210           if (isset($subacl['acl'][$object][$attribute])){
211             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
212             continue;
213           }
215           /* Per object ACL? */
216           if (isset($subacl['acl'][$object][0])){
217             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
218             continue;
219           }
221           /* Global ACL? */
222           if (isset($subacl['acl']['all'][0])){
223             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
224             continue;
225           }
227           /* If attribute is "", we want to know, if we've *any* permissions here... */
228           if ($attribute == "" && isset($subacl['acl'][$object])){
229             foreach($subacl['acl'][$object] as $attr => $dummy){
230               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
231             }
232             continue;
233           }
235         }
236       }
237     }
239     /* Assemble string */
240     $ret= "";
241     foreach ($acl as $key => $value){
242       if ($value != ""){
243         $ret.= $key;
244       }
245     }
247     /* Remove write if needed */
248     if ($skip_write){
249       $ret= preg_replace('/w/', '', $ret);
250     }
252     return ($ret);
253   }
256   /* Extract all departments that are accessible (direct or 'on the way' to an
257      accessible department) */
258   function get_module_departments($module)
259   {
260     global $plist;
262     $objects= array();
263     $deps= array();
265     /* Extract all relevant objects for this module from plist */
266     foreach ($plist->info as $object => $info){
267       if (!isset($info['plCategory'])){
268         continue;
269       }
270       foreach ($info['plCategory'] as $idx => $data){
271         if (preg_match('/^[0-9]+$/', $idx)){
272           if ($data == $module){
273             $objects[$object]= $object;
274           }
275         } else {
276           if ($idx == $module){
277             $objects[$object]= $object;
278           }
279         }
280       }
281     }
283     /* For all gosaDepartments */
284     foreach ($this->config->departments as $dn){
285       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
287       /* Build dn array */
288       $path= split(',', $dn);
289       $path= array_reverse($path);
291       /* Walk along the path to evaluate the acl */
292       $cpath= "";
293       foreach ($path as $element){
295         /* Clean potential ACLs for each level */
296         $acl= $this->cleanACL($acl);
298         if ($cpath == ""){
299           $cpath= $element;
300         } else {
301           $cpath= $element.','.$cpath;
302         }
303         if (isset($this->ACL[$cpath])){
305           /* Inspect this ACL, place the result into ACL */
306           foreach ($this->ACL[$cpath] as $subacl){
308             /* Reset? Just clean the ACL and turn over to the next one... */
309             if ($subacl['type'] == 'reset'){
310               $acl= $this->cleanACL($acl, TRUE);
311               continue;
312             }
314             /* Per object ACL? */
315             foreach ($objects as $object){
316               if (isset($subacl['acl']["$module/$object"])){
317                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
318                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
319                 }
320               }
321             }
323             /* Global ACL? */
324             if (isset($subacl['acl']["$module/all"][0])){
325               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
326               continue;
327             }
329             /* Global ACL? */
330             if (isset($subacl['acl']["all"][0])){
331               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
332               continue;
333             }
334           }
335         }
336       }
338       /* Add department, if we have (some) permissions for the required module */
339       foreach ($acl as $val){
340         if ($val != ""){
341           $deps[]= $dn;
342           break;
343         }
344       }
345     }
347     return ($deps);
348   }
351   function mergeACL($acl, $type, $newACL)
352   {
353     foreach(str_split($newACL) as $char){
355       /* Ignore invalid characters */
356       if (!preg_match('/[rwcdm]/', $char)){
357         continue;
358       }
360       /* Skip permanent and subtree entries */
361       if (preg_match('/[sp]/', $acl[$char])){
362         continue;
363       }
365       switch ($type){
366         case 'psub':
367           $acl[$char]= 'p';
368           break;
370         case 'sub':
371           $acl[$char]= 's';
372           break;
374         case 'one':
375           $acl[$char]= 1;
376           break;
378         case 'base':
379           if ($acl[$char] != 1){
380             $acl[$char]= 0;
381           }
382           break;
383       }
384     }
386     return ($acl);
387   }
390   function cleanACL($acl, $reset= FALSE)
391   {
392     foreach ($acl as $key => $value){
394       /* Reset removes everything but 'p' */
395       if ($reset && $value != 'p'){
396         $acl[$key]= "";
397         continue;
398       }
400       /* Decrease tree level */
401       if (preg_match('/^[0-9]+$/', $value)){
402         if ($value > 0){
403           $acl[$key]= $value - 1;
404         } else {
405           $acl[$key]= "";
406         }
407       }
408     }
410     return ($acl);
411   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>