Code

f082e1f6c6daf883b89a3488484ba818ab8f1d75
[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 $groups= array();
36   /* get acl's an put them into the userinfo object
37      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
38   function userinfo($config, $userdn){
39     $this->config= $config;
40     $ldap= $this->config->get_ldap_link();
41     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
42     $attrs= $ldap->fetch();
44     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
45       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
46     } else {
47       $this->cn= $attrs['uid'][0];
48     }
49     if (isset($attrs['gidNumber'][0])){
50       $this->gidNumber= $attrs['gidNumber'][0];
51     }
53     /* Assign user language */
54     if (isset($attrs['preferredLanguage'][0])){
55       $this->language= $attrs['preferredLanguage'][0];
56     }
58     if (isset($attrs['gosaUnitTag'][0])){
59       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
60     }
62     $this->dn= $userdn;
63     $this->uid= $attrs['uid'][0];
64     $this->ip= $_SERVER['REMOTE_ADDR'];
65   }
68   function loadACL()
69   {
71 #--------------------------------------------------------------------------OLD-BUT-ACTIVE-----------------------------
72     $ldap= $this->config->get_ldap_link();
74     /* Load ACL's from all groups we're in */
75     $this->subtreeACL= array();
76     $ldap->cd($this->config->current['BASE']);
77     if ($this->gidNumber == -1){
78       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
79           "(memberUid=$this->username))");
80     } else {
81       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
82           "(|(memberUid=$this->username)(gidNumber=$this->gidNumber)))");
83     }
85     while($attrs = $ldap->fetch()){
86       $base= preg_replace('/^[^,]+,ou=[^,]+,/i', "",$ldap->getDN());
87       $base= preg_replace("/[ ]*,[ ]*/", ",", $base);
89       for ($i= 0; $i<$attrs["gosaSubtreeACL"]["count"]; $i++){
90         $this->subtreeACL[$base][]= $attrs["gosaSubtreeACL"][$i];
91       }
92     }
93 #echo "NEW ACL LOADING --------------------------------------------------------------------------------------------<br>";
95     $this->ACL= array();    
96     $this->groups= array();    
97     $ldap= $this->config->get_ldap_link();
98     $ldap->cd($this->config->current['BASE']);
100     /* Get member groups... */
101     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
102     while ($attrs= $ldap->fetch()){
103       $this->groups[$attrs['dn']]= $attrs['dn'];
104     }
106     /* Crawl through ACLs and move relevant to the tree */
107     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
108     $aclp= array();
109     $aclc= array();
110     while ($attrs= $ldap->fetch()){
112       /* Insert links in ACL array */
113       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
114       $aclc[$attrs['dn']]= array();
115       $ol= array();
116       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
117         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
118       }
119       $aclc[$attrs['dn']]= $ol;
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         }
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         }
149         if ($interresting){
150           if (!isset($this->ACL[$dn])){
151             $this->ACL[$dn]= array();
152           }
153           $this->ACL[$dn][$idx]= $type;
154         }
155       }
157     }
158   }
161   function get_permissions($dn, $object, $attribute, $skip_write= FALSE)
162   {
163     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
165     /* Build dn array */
166     $path= split(',', $dn);
167     $path= array_reverse($path);
169     /* Walk along the path to evaluate the acl */
170     $cpath= "";
171     foreach ($path as $element){
173       /* Clean potential ACLs for each level */
174       $acl= $this->cleanACL($acl);
176       if ($cpath == ""){
177         $cpath= $element;
178       } else {
179         $cpath= $element.','.$cpath;
180       }
181       if (isset($this->ACL[$cpath])){
183         /* Inspect this ACL, place the result into ACL */
184         foreach ($this->ACL[$cpath] as $subacl){
186           /* Reset? Just clean the ACL and turn over to the next one... */
187           if ($subacl['type'] == 'reset'){
188             $acl= $this->cleanACL($acl, TRUE);
189             continue;
190           }
192           /* Per attribute ACL? */
193           if (isset($subacl['acl'][$object][$attribute])){
194             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
195             continue;
196           }
198           /* Per object ACL? */
199           if (isset($subacl['acl'][$object][0])){
200             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
201             continue;
202           }
204           /* Global ACL? */
205           if (isset($subacl['acl']['all'][0])){
206             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
207             continue;
208           }
210           /* If attribute is "", we want to know, if we've *any* permissions here... */
211           if ($attribute == "" && isset($subacl['acl'][$object])){
212             foreach($subacl['acl'][$object] as $attr => $dummy){
213               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
214             }
215             continue;
216           }
218         }
219       }
220     }
222     /* Assemble string */
223     $ret= "";
224     foreach ($acl as $key => $value){
225       if ($value != ""){
226         $ret.= $key;
227       }
228     }
230     /* Remove write if needed */
231     if ($skip_write){
232       $ret= preg_replace('/w/', '', $ret);
233     }
235     return ($ret);
236   }
239   function get_module_departments($module)
240   {
241     global $plist;
243     $objects= array();
244     $deps= array();
246     /* Extract all relevant objects for this module from plist */
247     foreach ($plist->info as $object => $info){
248       if (!isset($info['plCategory'])){
249         continue;
250       }
251       foreach ($info['plCategory'] as $idx => $data){
252         if (preg_match('/^[0-9]+$/', $idx)){
253           if ($data == $module){
254             $objects[$object]= $object;
255           }
256         } else {
257           if ($idx == $module){
258             $objects[$object]= $object;
259           }
260         }
261       }
262     }
264     /* Get all gosaDepartments */
265     $ldap= $this->config->get_ldap_link();
266     $ldap->cd($this->config->current['BASE']);
267     $ldap->search('objectClass=gosaDepartment', array('dn'));
268     while ($attrs= $ldap->fetch()){
269       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
271       /* Build dn array */
272       $path= split(',', $attrs['dn']);
273       $path= array_reverse($path);
275       /* Walk along the path to evaluate the acl */
276       $cpath= "";
277       foreach ($path as $element){
279         /* Clean potential ACLs for each level */
280         $acl= $this->cleanACL($acl);
282         if ($cpath == ""){
283           $cpath= $element;
284         } else {
285           $cpath= $element.','.$cpath;
286         }
287         if (isset($this->ACL[$cpath])){
289           /* Inspect this ACL, place the result into ACL */
290           foreach ($this->ACL[$cpath] as $subacl){
292             /* Reset? Just clean the ACL and turn over to the next one... */
293             if ($subacl['type'] == 'reset'){
294               $acl= $this->cleanACL($acl, TRUE);
295               continue;
296             }
298             /* Per object ACL? */
299             foreach ($objects as $object){
300               if (isset($subacl['acl'][$object])){
301                 foreach($subacl['acl'][$object] as $attribute => $dcl){
302                   if (isset($subacl['acl'][$object][$attribute])){
303                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
304                   }
305                 }
306               }
307             }
309             /* Global ACL? */
310             if (isset($subacl['acl'][0])){
311               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
312               continue;
313             }
314           }
315         }
316       }
318       /* Add department, if we have (some) permissions for the requred module */
319       foreach ($acl as $val){
320         if ($val != ""){
321           $deps[]= $attrs['dn'];
322           break;
323         }
324       }
325     }
327     return ($deps);
328   }
331   function mergeACL($acl, $type, $newACL)
332   {
333     foreach(str_split($newACL) as $char){
335       /* Ignore invalid characters */
336       if (!preg_match('/[rwcdm]/', $char)){
337         continue;
338       }
340       /* Skip permanent and subtree entries */
341       if (preg_match('/[sp]/', $acl[$char])){
342         continue;
343       }
345       switch ($type){
346         case 'psub':
347           $acl[$char]= 'p';
348           break;
350         case 'sub':
351           $acl[$char]= 's';
352           break;
354         case 'one':
355           $acl[$char]= 1;
356           break;
358         case 'base':
359           if ($acl[$char] != 1){
360             $acl[$char]= 0;
361           }
362           break;
363       }
364     }
366     return ($acl);
367   }
370   function cleanACL($acl, $reset= FALSE)
371   {
372     foreach ($acl as $key => $value){
374       /* Reset removes everything but 'p' */
375       if ($reset && $value != 'p'){
376         $acl[$key]= "";
377         continue;
378       }
380       /* Decrease tree level */
381       if (preg_match('/^[0-9]+$/', $value)){
382         if ($value > 0){
383           $acl[$key]= $value - 1;
384         } else {
385           $acl[$key]= "";
386         }
387       }
388     }
390     return ($acl);
391   }
395 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
396 ?>