Code

Updated password change.
[gosa.git] / gosa-core / include / class_userinfo.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class userinfo
24 {
25   var $dn;
26   var $ip;
27   var $username;
28   var $cn;
29   var $uid;
30   var $gidNumber= -1;
31   var $language= "";
32   var $config;
33   var $gosaUnitTag= "";
34   var $subtreeACL= array();
35   var $ACL= array();
36   var $ocMapping= array();
37   var $groups= array();
38   var $result_cache =array();
40   /* get acl's an put them into the userinfo object
41      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
42   function userinfo(&$config, $userdn){
43     $this->config= &$config;
44     $ldap= $this->config->get_ldap_link();
45     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
46     $attrs= $ldap->fetch();
48     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
49       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
50     } else {
51       $this->cn= $attrs['uid'][0];
52     }
53     if (isset($attrs['gidNumber'][0])){
54       $this->gidNumber= $attrs['gidNumber'][0];
55     }
57     /* Assign user language */
58     if (isset($attrs['preferredLanguage'][0])){
59       $this->language= $attrs['preferredLanguage'][0];
60     }
62     if (isset($attrs['gosaUnitTag'][0])){
63       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
64     }
66     $this->dn= $userdn;
67     $this->uid= $attrs['uid'][0];
68     $this->ip= $_SERVER['REMOTE_ADDR'];
70     /* Initialize ACL_CACHE */
71     session::set('ACL_CACHE',array());
72     $this->reset_acl_cache();
73   }
76   public function reset_acl_cache()
77   {
78     /* Initialize ACL_CACHE */
79     session::set('ACL_CACHE',array());
80   }
82   function loadACL()
83   {
84     $this->ACL= array();    
85     $this->groups= array();    
86     $this->result_cache =array();
87     $this->reset_acl_cache();
88     $ldap= $this->config->get_ldap_link();
89     $ldap->cd($this->config->current['BASE']);
91     /* Get member groups... */
92     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
93     while ($attrs= $ldap->fetch()){
94       $this->groups[$attrs['dn']]= $attrs['dn'];
95     }
97     /* Crawl through ACLs and move relevant to the tree */
98     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
99     $aclp= array();
100     $aclc= array();
101     while ($attrs= $ldap->fetch()){
103       /* Insert links in ACL array */
104       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
105       $aclc[$attrs['dn']]= array();
106       $ol= array();
107       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
108         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
109       }
110       $aclc[$attrs['dn']]= $ol;
111     }
113     /* Resolve roles here. 
114      */
115     foreach($aclc as $dn => $data){
116       foreach($data as $prio => $aclc_value)  {
117         if($aclc_value['type'] == "role"){
119           unset($aclc[$dn][$prio]);
121           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
122           $attrs = $ldap->fetch();
124           if(isset($attrs['gosaAclTemplate'])){
125             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
126               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
128               foreach($tmp as $new_acl){
129                 $new_acl['members'] = $aclc_value['members'];
130                 $aclc[$dn][] =$new_acl;
131               }
132             }      
133           }
134         }
135       }
136     }
138     /* ACL's read, sort for tree depth */
139     asort($aclp);
141     /* Sort in tree order */
142     foreach ($aclp as $dn => $acl){
143       /* Check if we need to keep this ACL */
144       foreach($aclc[$dn] as $idx => $type){
145         $interresting= FALSE;
146         
147         /* No members? This is good for all users... */
148         if (!count($type['members'])){
149           $interresting= TRUE;
150         } else {
152           /* Inspect members... */
153           foreach ($type['members'] as $grp => $grpdsc){
154             /* Some group inside the members that is relevant for us? */
155             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
156               $interresting= TRUE;
157             }
159             /* User inside the members? */
160             if (preg_replace('/^U:/', '', $grp) == $this->dn){
161               $interresting= TRUE;
162             }
163           }
164         }
166         if ($interresting){
167           if (!isset($this->ACL[$dn])){
168             $this->ACL[$dn]= array();
169           }
170           $this->ACL[$dn][$idx]= $type;
171         }
172       }
174     }
176   }
179   function get_category_permissions($dn, $category)
180   {
181     /* Get list of objectClasses and get the permissions for it */
182     $acl= "";
183     if (isset($this->ocMapping[$category])){
184       foreach($this->ocMapping[$category] as $oc){
185         $acl.= $this->get_permissions($dn, $category."/".$oc);
186       }
187     }else{
188       trigger_error("ACL request for an invalid category (".$category.").");
189     }
191     return ($acl);
192   }
195   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
196   {
197     /* Push cache answer? */
198     $ACL_CACHE = &session::get('ACL_CACHE');
199     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
201       /* Remove write if needed */
202       if ($skip_write){
203         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
204       }else{
205         $ret = $ACL_CACHE["$dn+$object+$attribute"];
206       } 
207       return($ret);
208     }
210     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
212     /* Build dn array */
213     $path= split(',', $dn);
214     $path= array_reverse($path);
216     /* Walk along the path to evaluate the acl */
217     $cpath= "";
218     foreach ($path as $element){
220       /* Clean potential ACLs for each level */
221       $acl= $this->cleanACL($acl);
223       if ($cpath == ""){
224         $cpath= $element;
225       } else {
226         $cpath= $element.','.$cpath;
227       }
228       if (isset($this->ACL[$cpath])){
230         /* Inspect this ACL, place the result into ACL */
231         foreach ($this->ACL[$cpath] as $subacl){
233           /* Reset? Just clean the ACL and turn over to the next one... */
234           if ($subacl['type'] == 'reset'){
235             $acl= $this->cleanACL($acl, TRUE);
236             continue;
237           }
239           if($subacl['type'] == "role") {
240             echo "role skipped";
241             continue;
242           }
244           /* Per attribute ACL? */
245           if (isset($subacl['acl'][$object][$attribute])){
246             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
247             continue;
248           }
250           /* Per object ACL? */
251           if (isset($subacl['acl'][$object][0])){
252             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
253             continue;
254           }
256           /* Global ACL? */
257           if (isset($subacl['acl']['all'][0])){
258             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
259             continue;
260           }
262           /* If attribute is "", we want to know, if we've *any* permissions here... */
263           if ($attribute == "" && isset($subacl['acl'][$object])){
264             foreach($subacl['acl'][$object] as $attr => $dummy){
265               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
266             }
267             continue;
268           }
270         }
271       }
272     }
274     /* Assemble string */
275     $ret= "";
276     foreach ($acl as $key => $value){
277       if ($value != ""){
278         $ret.= $key;
279       }
280     }
282     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
284     /* Remove write if needed */
285     if ($skip_write){
286       $ret= preg_replace('/w/', '', $ret);
287     }
288     return ($ret);
289   }
292   /* Extract all departments that are accessible (direct or 'on the way' to an
293      accessible department) */
294   function get_module_departments($module)
295   {
296     /* Use cached results if possilbe */
297     $ACL_CACHE = session::get('ACL_CACHE');
298     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
299       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
300     }
302     global $plist;
304     $objects= array();
305     $deps= array();
307     /* Extract all relevant objects for this module from plist */
308     foreach ($plist->info as $object => $info){
309       if (!isset($info['plCategory'])){
310         continue;
311       }
312       foreach ($info['plCategory'] as $idx => $data){
313         if (preg_match('/^[0-9]+$/', $idx)){
314           if ($data == $module){
315             $objects[$object]= $object;
316           }
317         } else {
318           if ($idx == $module){
319             $objects[$object]= $object;
320           }
321         }
322       }
323     }
325     /* For all gosaDepartments */
326     foreach ($this->config->departments as $dn){
327       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
329       /* Build dn array */
330       $path= split(',', $dn);
331       $path= array_reverse($path);
333       /* Walk along the path to evaluate the acl */
334       $cpath= "";
335       foreach ($path as $element){
337         /* Clean potential ACLs for each level */
338         $acl= $this->cleanACL($acl);
340         if ($cpath == ""){
341           $cpath= $element;
342         } else {
343           $cpath= $element.','.$cpath;
344         }
345         if (isset($this->ACL[$cpath])){
347           /* Inspect this ACL, place the result into ACL */
348           foreach ($this->ACL[$cpath] as $subacl){
350             /* Reset? Just clean the ACL and turn over to the next one... */
351             if ($subacl['type'] == 'reset'){
352               $acl= $this->cleanACL($acl, TRUE);
353               continue;
354             }
355     
356             if($subacl['type'] == 'role'){
357               echo "role skipped";
358               continue;
359             }
361             /* Per object ACL? */
362             foreach ($objects as $object){
363               if (isset($subacl['acl']["$module/$object"])){
364                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
365                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
366                 }
367               }
368             }
370             /* Global ACL? */
371             if (isset($subacl['acl']["$module/all"][0])){
372               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
373               continue;
374             }
376             /* Global ACL? */
377             if (isset($subacl['acl']["all"][0])){
378               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
379               continue;
380             }
381           }
382         }
383       }
385       /* Add department, if we have (some) permissions for the required module */
386       foreach ($acl as $val){
387         if ($val != ""){
388           $deps[]= $dn;
389           break;
390         }
391       }
392     }
394     $ACL_CACHE = &session::get('ACL_CACHE');
395     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
396     return ($deps);
397   }
400   function mergeACL($acl, $type, $newACL)
401   {
402     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
403       $newACL .= "r";
404     }
406     foreach(str_split($newACL) as $char){
408       /* Ignore invalid characters */
409       if (!preg_match('/[rwcdm]/', $char)){
410         continue;
411       }
413       /* Skip permanent and subtree entries */
414       if (preg_match('/[sp]/', $acl[$char])){
415         continue;
416       }
418       switch ($type){
419         case 'psub':
420           $acl[$char]= 'p';
421           break;
423         case 'sub':
424           $acl[$char]= 's';
425           break;
427         case 'one':
428           $acl[$char]= 1;
429           break;
431         case 'base':
432           if ($acl[$char] != 1){
433             $acl[$char]= 0;
434           }
435           break;
436       }
437     }
439     return ($acl);
440   }
443   function cleanACL($acl, $reset= FALSE)
444   {
445     foreach ($acl as &$value){
447       /* Reset removes everything but 'p' */
448       if ($reset && $value != 'p'){
449         $value= "";
450         continue;
451       }
453       /* Decrease tree level */
454       if (is_int($value)){
455         if ($value){
456           $value--;
457         } else {
458           $value= "";
459         }
460       }
461     }
463     return ($acl);
464   }
467   /* #FIXME This could be logical wrong or could be optimized in the future
468      Return combined acls for a given category. 
469      All acls will be combined like boolean AND 
470       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
471     
472      Results will be cached in $this->result_cache.
473       $this->result_cache will be resetted if load_acls is called.
474   */
475   function has_complete_category_acls($dn,$category)
476   {
477     $acl    = "rwcdm";
478     $types  = "rwcdm";
480     if(!is_string($category)){
481       trigger_error("category must be string");   
482       $acl = "";
483     }else{
484       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
485         if (isset($this->ocMapping[$category])){
486           foreach($this->ocMapping[$category] as $oc){
488             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
489             if($oc == "0") continue;
490             $tmp =  $this->get_permissions($dn, $category."/".$oc);
491             for($i = 0 ; $i < strlen($types); $i++) {
492               if(!preg_match("/".$types[$i]."/",$tmp)){ 
493                 $acl = preg_replace("/".$types[$i]."/","",$acl);
494               }
495             }
496           }
497         }else{
498           trigger_error("Invalid type of category ".$category);
499           $acl = "";
500         }
501         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
502       }else{
503         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
504       }
505     }
506     return($acl);
507   }
510 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
511 ?>