Code

066b57e031a96d92dc660b0574273c620de8818d
[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_category_permissions($dn, $category)
162   {
163     return ("cdmrw");
164   }
167   function get_permissions($dn, $object, $attribute, $skip_write= FALSE)
168   {
169     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
171     /* Build dn array */
172     $path= split(',', $dn);
173     $path= array_reverse($path);
175     /* Walk along the path to evaluate the acl */
176     $cpath= "";
177     foreach ($path as $element){
179       /* Clean potential ACLs for each level */
180       $acl= $this->cleanACL($acl);
182       if ($cpath == ""){
183         $cpath= $element;
184       } else {
185         $cpath= $element.','.$cpath;
186       }
187       if (isset($this->ACL[$cpath])){
189         /* Inspect this ACL, place the result into ACL */
190         foreach ($this->ACL[$cpath] as $subacl){
192           /* Reset? Just clean the ACL and turn over to the next one... */
193           if ($subacl['type'] == 'reset'){
194             $acl= $this->cleanACL($acl, TRUE);
195             continue;
196           }
198           /* Per attribute ACL? */
199           if (isset($subacl['acl'][$object][$attribute])){
200             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
201             continue;
202           }
204           /* Per object ACL? */
205           if (isset($subacl['acl'][$object][0])){
206             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
207             continue;
208           }
210           /* Global ACL? */
211           if (isset($subacl['acl']['all'][0])){
212             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
213             continue;
214           }
216         }
217       }
218     }
220     /* Assemble string */
221     $ret= "";
222     foreach ($acl as $key => $value){
223       if ($value != ""){
224         $ret.= $key;
225       }
226     }
228     /* Remove write if needed */
229     if ($skip_write){
230       $ret= preg_replace('/w/', '', $ret);
231     }
233     return ($ret);
234   }
237   function get_module_departments($module)
238   {
239     global $plist;
241     $objects= array();
242     $deps= array();
244     /* Extract all relevant objects for this module from plist */
245     foreach ($plist->info as $object => $info){
246       if (!isset($info['plCategory'])){
247         continue;
248       }
249       foreach ($info['plCategory'] as $idx => $data){
250         if (preg_match('/^[0-9]+$/', $idx)){
251           if ($data == $module){
252             $objects[$object]= $object;
253           }
254         } else {
255           if ($idx == $module){
256             $objects[$object]= $object;
257           }
258         }
259       }
260     }
262     /* Get all gosaDepartments */
263     $ldap= $this->config->get_ldap_link();
264     $ldap->cd($this->config->current['BASE']);
265     $ldap->search('objectClass=gosaDepartment', array('dn'));
266     while ($attrs= $ldap->fetch()){
267       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
269       /* Build dn array */
270       $path= split(',', $attrs['dn']);
271       $path= array_reverse($path);
273       /* Walk along the path to evaluate the acl */
274       $cpath= "";
275       foreach ($path as $element){
277         /* Clean potential ACLs for each level */
278         $acl= $this->cleanACL($acl);
280         if ($cpath == ""){
281           $cpath= $element;
282         } else {
283           $cpath= $element.','.$cpath;
284         }
285         if (isset($this->ACL[$cpath])){
287           /* Inspect this ACL, place the result into ACL */
288           foreach ($this->ACL[$cpath] as $subacl){
290             /* Reset? Just clean the ACL and turn over to the next one... */
291             if ($subacl['type'] == 'reset'){
292               $acl= $this->cleanACL($acl, TRUE);
293               continue;
294             }
296             /* Per object ACL? */
297             foreach ($objects as $object){
298               if (isset($subacl['acl'][$object])){
299                 foreach($subacl['acl'][$object] as $attribute => $dcl){
300                   if (isset($subacl['acl'][$object][$attribute])){
301                     $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][$object][$attribute]));
302                   }
303                 }
304               }
305             }
307             /* Global ACL? */
308             if (isset($subacl['acl'][0])){
309               $acl= $this->mergeACL($acl, $subacl['type'], preg_replace('/[cdm]/', '', $subacl['acl'][0]));
310               continue;
311             }
312           }
313         }
314       }
316       /* Add department, if we have (some) permissions for the requred module */
317       foreach ($acl as $val){
318         if ($val != ""){
319           $deps[]= $attrs['dn'];
320           break;
321         }
322       }
323     }
325     return ($deps);
326   }
329   function mergeACL($acl, $type, $newACL)
330   {
331     foreach(str_split($newACL) as $char){
333       /* Ignore invalid characters */
334       if (!preg_match('/[rwcdm]/', $char)){
335         continue;
336       }
338       /* Skip permanent and subtree entries */
339       if (preg_match('/[sp]/', $acl[$char])){
340         continue;
341       }
343       switch ($type){
344         case 'psub':
345           $acl[$char]= 'p';
346           break;
348         case 'sub':
349           $acl[$char]= 's';
350           break;
352         case 'one':
353           $acl[$char]= 1;
354           break;
356         case 'base':
357           if ($acl[$char] != 1){
358             $acl[$char]= 0;
359           }
360           break;
361       }
362     }
364     return ($acl);
365   }
368   function cleanACL($acl, $reset= FALSE)
369   {
370     foreach ($acl as $key => $value){
372       /* Reset removes everything but 'p' */
373       if ($reset && $value != 'p'){
374         $acl[$key]= "";
375         continue;
376       }
378       /* Decrease tree level */
379       if (preg_match('/^[0-9]+$/', $value)){
380         if ($value > 0){
381           $acl[$key]= $value - 1;
382         } else {
383           $acl[$key]= "";
384         }
385       }
386     }
388     return ($acl);
389   }
393 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
394 ?>