Code

Udpated fai acls
[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       foreach ($info['plCategory'] as $idx => $data){
237         if (preg_match('/^[0-9]+$/', $idx)){
238           if ($data == $module){
239             $objects[$object]= $object;
240           }
241         } else {
242           if ($idx == $module){
243             $objects[$object]= $object;
244           }
245         }
246       }
247     }
249     /* Get all gosaDepartments */
250     $ldap= $this->config->get_ldap_link();
251     $ldap->cd($this->config->current['BASE']);
252     $ldap->search('objectClass=gosaDepartment', array('dn'));
253     while ($attrs= $ldap->fetch()){
254       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "");
256       /* Build dn array */
257       $path= split(',', $attrs['dn']);
258       $path= array_reverse($path);
260       /* Walk along the path to evaluate the acl */
261       $cpath= "";
262       foreach ($path as $element){
264         /* Clean potential ACLs for each level */
265         $acl= $this->cleanACL($acl);
267         if ($cpath == ""){
268           $cpath= $element;
269         } else {
270           $cpath= $element.','.$cpath;
271         }
272         if (isset($this->ACL[$cpath])){
274           /* Inspect this ACL, place the result into ACL */
275           foreach ($this->ACL[$cpath] as $subacl){
277             /* Reset? Just clean the ACL and turn over to the next one... */
278             if ($subacl['type'] == 'reset'){
279               $acl= $this->cleanACL($acl, TRUE);
280               continue;
281             }
283             /* Per object ACL? */
284             foreach ($objects as $object){
285               if (isset($subacl['acl'][$object])){
286                 foreach($subacl['acl'][$object] as $attribute => $dcl){
287                   if (isset($subacl['acl'][$object][$attribute])){
288                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
289                   }
290                 }
291               }
292             }
294             /* Global ACL? */
295             if (isset($subacl['acl'][0])){
296               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
297               continue;
298             }
299           }
300         }
301       }
303       /* Add department, if we have (some) permissions for the requred module */
304       foreach ($acl as $val){
305         if ($val != ""){
306           $deps[]= $attrs['dn'];
307           break;
308         }
309       }
310     }
312     return ($deps);
313   }
316   function mergeACL($acl, $type, $newACL)
317   {
318     foreach(str_split($newACL) as $char){
320       /* Ignore invalid characters */
321       if (!preg_match('/[rwcdm]/', $char)){
322         continue;
323       }
325       /* Skip permanent and subtree entries */
326       if (preg_match('/[sp]/', $acl[$char])){
327         continue;
328       }
330       switch ($type){
331         case 'psub':
332           $acl[$char]= 'p';
333           break;
335         case 'sub':
336           $acl[$char]= 's';
337           break;
339         case 'one':
340           $acl[$char]= 1;
341           break;
343         case 'base':
344           if ($acl[$char] != 1){
345             $acl[$char]= 0;
346           }
347           break;
348       }
349     }
351     return ($acl);
352   }
355   function cleanACL($acl, $reset= FALSE)
356   {
357     foreach ($acl as $key => $value){
359       /* Reset removes everything but 'p' */
360       if ($reset && $value != 'p'){
361         $acl[$key]= "";
362         continue;
363       }
365       /* Decrease tree level */
366       if (preg_match('/^[0-9]+$/', $value)){
367         if ($value > 0){
368           $acl[$key]= $value - 1;
369         } else {
370           $acl[$key]= "";
371         }
372       }
373     }
375     return ($acl);
376   }
380 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
381 ?>