Code

9c8deabb105061a9dfebc5327a0ef4c408c7fd46
[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->ocMapping= array();
98     $this->groups= array();    
99     $ldap= $this->config->get_ldap_link();
100     $ldap->cd($this->config->current['BASE']);
102     /* Get member groups... */
103     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
104     while ($attrs= $ldap->fetch()){
105       $this->groups[$attrs['dn']]= $attrs['dn'];
106     }
108     /* Crawl through ACLs and move relevant to the tree */
109     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
110     $aclp= array();
111     $aclc= array();
112     while ($attrs= $ldap->fetch()){
114       /* Insert links in ACL array */
115       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
116       $aclc[$attrs['dn']]= array();
117       $ol= array();
118       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
119         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
120       }
121       $aclc[$attrs['dn']]= $ol;
122     }
124     /* ACL's read, sort for tree depth */
125     asort($aclp);
127     /* Sort in tree order */
128     foreach ($aclp as $dn => $acl){
129       /* Check if we need to keep this ACL */
130       foreach($aclc[$dn] as $idx => $type){
131         $interresting= FALSE;
132         
133         /* No members? This is good for all users... */
134         if (!count($type['members'])){
135           $interresting= TRUE;
136         }
138         /* Inspect members... */
139         foreach ($type['members'] as $grp => $grpdsc){
140           /* Some group inside the members that is relevant for us? */
141           if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
142             $interresting= TRUE;
143           }
145           /* User inside the members? */
146           if (preg_replace('/^U:/', '', $grp) == $this->dn){
147             $interresting= TRUE;
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, $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   function get_module_departments($module)
257   {
258     global $plist;
260     $objects= array();
261     $deps= array();
263     /* Extract all relevant objects for this module from plist */
264     foreach ($plist->info as $object => $info){
265       if (!isset($info['plCategory'])){
266         continue;
267       }
268       foreach ($info['plCategory'] as $idx => $data){
269         if (preg_match('/^[0-9]+$/', $idx)){
270           if ($data == $module){
271             $objects[$object]= $object;
272           }
273         } else {
274           if ($idx == $module){
275             $objects[$object]= $object;
276           }
277         }
278       }
279     }
281     /* Get all gosaDepartments */
282     $ldap= $this->config->get_ldap_link();
283     $ldap->cd($this->config->current['BASE']);
284     $ldap->search('objectClass=gosaDepartment', array('dn'));
285     while ($attrs= $ldap->fetch()){
286       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
288       /* Build dn array */
289       $path= split(',', $attrs['dn']);
290       $path= array_reverse($path);
292       /* Walk along the path to evaluate the acl */
293       $cpath= "";
294       foreach ($path as $element){
296         /* Clean potential ACLs for each level */
297         $acl= $this->cleanACL($acl);
299         if ($cpath == ""){
300           $cpath= $element;
301         } else {
302           $cpath= $element.','.$cpath;
303         }
304         if (isset($this->ACL[$cpath])){
306           /* Inspect this ACL, place the result into ACL */
307           foreach ($this->ACL[$cpath] as $subacl){
309             /* Reset? Just clean the ACL and turn over to the next one... */
310             if ($subacl['type'] == 'reset'){
311               $acl= $this->cleanACL($acl, TRUE);
312               continue;
313             }
315             /* Per object ACL? */
316             foreach ($objects as $object){
317               if (isset($subacl['acl'][$object])){
318                 foreach($subacl['acl'][$object] as $attribute => $dcl){
319                   if (isset($subacl['acl'][$object][$attribute])){
320                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
321                   }
322                 }
323               }
324             }
326             /* Global ACL? */
327             if (isset($subacl['acl'][0])){
328               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
329               continue;
330             }
331           }
332         }
333       }
335       /* Add department, if we have (some) permissions for the requred module */
336       foreach ($acl as $val){
337         if ($val != ""){
338           $deps[]= $attrs['dn'];
339           break;
340         }
341       }
342     }
344     return ($deps);
345   }
348   function mergeACL($acl, $type, $newACL)
349   {
350     foreach(str_split($newACL) as $char){
352       /* Ignore invalid characters */
353       if (!preg_match('/[rwcdm]/', $char)){
354         continue;
355       }
357       /* Skip permanent and subtree entries */
358       if (preg_match('/[sp]/', $acl[$char])){
359         continue;
360       }
362       switch ($type){
363         case 'psub':
364           $acl[$char]= 'p';
365           break;
367         case 'sub':
368           $acl[$char]= 's';
369           break;
371         case 'one':
372           $acl[$char]= 1;
373           break;
375         case 'base':
376           if ($acl[$char] != 1){
377             $acl[$char]= 0;
378           }
379           break;
380       }
381     }
383     return ($acl);
384   }
387   function cleanACL($acl, $reset= FALSE)
388   {
389     foreach ($acl as $key => $value){
391       /* Reset removes everything but 'p' */
392       if ($reset && $value != 'p'){
393         $acl[$key]= "";
394         continue;
395       }
397       /* Decrease tree level */
398       if (preg_match('/^[0-9]+$/', $value)){
399         if ($value > 0){
400           $acl[$key]= $value - 1;
401         } else {
402           $acl[$key]= "";
403         }
404       }
405     }
407     return ($acl);
408   }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>