Code

Updated with skip_write flag
[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" => "");
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     /* Remove write if needed */
224     if ($skip_write){
225       $ret= preg_replace('/w/', '', $ret);
226     }
228     return ($ret);
229   }
232   function get_module_departments($module)
233   {
234     global $plist;
236     $objects= array();
237     $deps= array();
239     /* Extract all relevant objects for this module from plist */
240     foreach ($plist->info as $object => $info){
241       foreach ($info['plCategory'] as $idx => $data){
242         if (preg_match('/^[0-9]+$/', $idx)){
243           if ($data == $module){
244             $objects[$object]= $object;
245           }
246         } else {
247           if ($idx == $module){
248             $objects[$object]= $object;
249           }
250         }
251       }
252     }
254     /* Get all gosaDepartments */
255     $ldap= $this->config->get_ldap_link();
256     $ldap->cd($this->config->current['BASE']);
257     $ldap->search('objectClass=gosaDepartment', array('dn'));
258     while ($attrs= $ldap->fetch()){
259       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "");
261       /* Build dn array */
262       $path= split(',', $attrs['dn']);
263       $path= array_reverse($path);
265       /* Walk along the path to evaluate the acl */
266       $cpath= "";
267       foreach ($path as $element){
269         /* Clean potential ACLs for each level */
270         $acl= $this->cleanACL($acl);
272         if ($cpath == ""){
273           $cpath= $element;
274         } else {
275           $cpath= $element.','.$cpath;
276         }
277         if (isset($this->ACL[$cpath])){
279           /* Inspect this ACL, place the result into ACL */
280           foreach ($this->ACL[$cpath] as $subacl){
282             /* Reset? Just clean the ACL and turn over to the next one... */
283             if ($subacl['type'] == 'reset'){
284               $acl= $this->cleanACL($acl, TRUE);
285               continue;
286             }
288             /* Per object ACL? */
289             foreach ($objects as $object){
290               if (isset($subacl['acl'][$object])){
291                 foreach($subacl['acl'][$object] as $attribute => $dcl){
292                   if (isset($subacl['acl'][$object][$attribute])){
293                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
294                   }
295                 }
296               }
297             }
299             /* Global ACL? */
300             if (isset($subacl['acl'][0])){
301               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
302               continue;
303             }
304           }
305         }
306       }
308       /* Add department, if we have (some) permissions for the requred module */
309       foreach ($acl as $val){
310         if ($val != ""){
311           $deps[]= $attrs['dn'];
312           break;
313         }
314       }
315     }
317     return ($deps);
318   }
321   function mergeACL($acl, $type, $newACL)
322   {
323     foreach(str_split($newACL) as $char){
325       /* Ignore invalid characters */
326       if (!preg_match('/[rwcdm]/', $char)){
327         continue;
328       }
330       /* Skip permanent and subtree entries */
331       if (preg_match('/[sp]/', $acl[$char])){
332         continue;
333       }
335       switch ($type){
336         case 'psub':
337           $acl[$char]= 'p';
338           break;
340         case 'sub':
341           $acl[$char]= 's';
342           break;
344         case 'one':
345           $acl[$char]= 1;
346           break;
348         case 'base':
349           if ($acl[$char] != 1){
350             $acl[$char]= 0;
351           }
352           break;
353       }
354     }
356     return ($acl);
357   }
360   function cleanACL($acl, $reset= FALSE)
361   {
362     foreach ($acl as $key => $value){
364       /* Reset removes everything but 'p' */
365       if ($reset && $value != 'p'){
366         $acl[$key]= "";
367         continue;
368       }
370       /* Decrease tree level */
371       if (preg_match('/^[0-9]+$/', $value)){
372         if ($value > 0){
373           $acl[$key]= $value - 1;
374         } else {
375           $acl[$key]= "";
376         }
377       }
378     }
380     return ($acl);
381   }
385 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
386 ?>