Code

d7c1fec4b15d5f28ee4f59662e7094d5f40ec070
[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)
162   {
163     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "");
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'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][0]));
201             continue;
202           }
204           /* Global ACL? */
205           if (isset($subacl['acl'][0])){
206             $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
207             continue;
208           }
210         }
212       }
213     }
215     /* Assemble string */
216     $ret= "";
217     foreach ($acl as $key => $value){
218       if ($value != ""){
219         $ret.= $key;
220       }
221     }
223     return ($ret);
224   }
227   function get_module_departments($module)
228   {
229     global $plist;
231     $objects= array();
232     $deps= array();
234     /* Extract all relevant objects for this module from plist */
235     foreach ($plist->info as $object => $info){
236       if (isset($info['plDepends']['objectClass']) && $info['plDepends']['objectClass'] == $module){
237         $objects[$object]= $object;
238       }
239     }
241     /* Get all gosaDepartments */
242     $ldap= $this->config->get_ldap_link();
243     $ldap->cd($this->config->current['BASE']);
244     $ldap->search('objectClass=gosaDepartment', array('dn'));
245     while ($attrs= $ldap->fetch()){
246       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "");
248       /* Build dn array */
249       $path= split(',', $attrs['dn']);
250       $path= array_reverse($path);
252       /* Walk along the path to evaluate the acl */
253       $cpath= "";
254       foreach ($path as $element){
256         /* Clean potential ACLs for each level */
257         $acl= $this->cleanACL($acl);
259         if ($cpath == ""){
260           $cpath= $element;
261         } else {
262           $cpath= $element.','.$cpath;
263         }
264         if (isset($this->ACL[$cpath])){
266           /* Inspect this ACL, place the result into ACL */
267           foreach ($this->ACL[$cpath] as $subacl){
269             /* Reset? Just clean the ACL and turn over to the next one... */
270             if ($subacl['type'] == 'reset'){
271               $acl= $this->cleanACL($acl, TRUE);
272               continue;
273             }
275             /* Per object ACL? */
276             foreach ($objects as $object){
277               if (isset($subacl['acl'][$object])){
278                 foreach($subacl['acl'][$object] as $attribute => $dcl){
279                   if (isset($subacl['acl'][$object][$attribute])){
280                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
281                   }
282                 }
283               }
284             }
286             /* Global ACL? */
287             if (isset($subacl['acl'][0])){
288               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
289               continue;
290             }
291           }
292         }
293       }
295       /* Add department, if we have (some) permissions for the requred module */
296       foreach ($acl as $val){
297         if ($val != ""){
298           $deps[]= $attrs['dn'];
299           break;
300         }
301       }
302     }
304     return ($deps);
305   }
308   function mergeACL($acl, $type, $newACL)
309   {
310     foreach(str_split($newACL) as $char){
312       /* Ignore invalid characters */
313       if (!preg_match('/[rwcdm]/', $char)){
314         continue;
315       }
317       /* Skip permanent and subtree entries */
318       if (preg_match('/[sp]/', $acl[$char])){
319         continue;
320       }
322       switch ($type){
323         case 'psub':
324           $acl[$char]= 'p';
325           break;
327         case 'sub':
328           $acl[$char]= 's';
329           break;
331         case 'one':
332           $acl[$char]= 1;
333           break;
335         case 'base':
336           if ($acl[$char] != 1){
337             $acl[$char]= 0;
338           }
339           break;
340       }
341     }
343     return ($acl);
344   }
347   function cleanACL($acl, $reset= FALSE)
348   {
349     foreach ($acl as $key => $value){
351       /* Reset removes everything but 'p' */
352       if ($reset && $value != 'p'){
353         $acl[$key]= "";
354         continue;
355       }
357       /* Decrease tree level */
358       if (preg_match('/^[0-9]+$/', $value)){
359         if ($value > 0){
360           $acl[$key]= $value - 1;
361         } else {
362           $acl[$key]= "";
363         }
364       }
365     }
367     return ($acl);
368   }
372 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
373 ?>