29d6dc817e95075ddde54d5ae86b134b148a49c8
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->groups= array();
98 $ldap= $this->config->get_ldap_link();
99 $ldap->cd($this->config->current['BASE']);
101 /* Get member groups... */
102 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
103 while ($attrs= $ldap->fetch()){
104 $this->groups[$attrs['dn']]= $attrs['dn'];
105 }
107 /* Crawl through ACLs and move relevant to the tree */
108 $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
109 $aclp= array();
110 $aclc= array();
111 while ($attrs= $ldap->fetch()){
113 /* Insert links in ACL array */
114 $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
115 $aclc[$attrs['dn']]= array();
116 $ol= array();
117 for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
118 $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
119 }
120 $aclc[$attrs['dn']]= $ol;
121 }
123 /* ACL's read, sort for tree depth */
124 asort($aclp);
126 /* Sort in tree order */
127 foreach ($aclp as $dn => $acl){
128 /* Check if we need to keep this ACL */
129 foreach($aclc[$dn] as $idx => $type){
130 $interresting= FALSE;
132 /* No members? This is good for all users... */
133 if (!count($type['members'])){
134 $interresting= TRUE;
135 }
137 /* Inspect members... */
138 foreach ($type['members'] as $grp => $grpdsc){
139 /* Some group inside the members that is relevant for us? */
140 if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
141 $interresting= TRUE;
142 }
144 /* User inside the members? */
145 if (preg_replace('/^U:/', '', $grp) == $this->dn){
146 $interresting= TRUE;
147 }
148 }
150 if ($interresting){
151 if (!isset($this->ACL[$dn])){
152 $this->ACL[$dn]= array();
153 }
154 $this->ACL[$dn][$idx]= $type;
155 }
156 }
158 }
160 }
163 function get_category_permissions($dn, $category)
164 {
165 /* Get list of objectClasses and get the permissions for it */
166 $acl= "";
167 if (isset($this->ocMapping[$category])){
168 foreach($this->ocMapping[$category] as $oc){
169 $acl.= $this->get_permissions($dn, $category."/".$oc, "");
170 }
171 }
173 return ($acl);
174 }
177 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
178 {
179 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
181 /* Build dn array */
182 $path= split(',', $dn);
183 $path= array_reverse($path);
185 /* Walk along the path to evaluate the acl */
186 $cpath= "";
187 foreach ($path as $element){
189 /* Clean potential ACLs for each level */
190 $acl= $this->cleanACL($acl);
192 if ($cpath == ""){
193 $cpath= $element;
194 } else {
195 $cpath= $element.','.$cpath;
196 }
197 if (isset($this->ACL[$cpath])){
199 /* Inspect this ACL, place the result into ACL */
200 foreach ($this->ACL[$cpath] as $subacl){
202 /* Reset? Just clean the ACL and turn over to the next one... */
203 if ($subacl['type'] == 'reset'){
204 $acl= $this->cleanACL($acl, TRUE);
205 continue;
206 }
208 /* Per attribute ACL? */
209 if (isset($subacl['acl'][$object][$attribute])){
210 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
211 continue;
212 }
214 /* Per object ACL? */
215 if (isset($subacl['acl'][$object][0])){
216 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
217 continue;
218 }
220 /* Global ACL? */
221 if (isset($subacl['acl']['all'][0])){
222 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
223 continue;
224 }
226 /* If attribute is "", we want to know, if we've *any* permissions here... */
227 if ($attribute == "" && isset($subacl['acl'][$object])){
228 echo "Global $object<br>";
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 }
410 }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>