Code

Updated ACL handling.
[gosa.git] / gosa-core / include / class_acl.inc
1 <?php
3 class acl extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Access control";
7   var $plDescription= "This does something";
9   /* attribute list for save action */
10   var $attributes= array('gosaAclEntry');
11   var $objectclasses= array('gosaAcl');
13   /* Helpers */
14   var $dialogState= "head";
15   var $gosaAclEntry= array();
16   var $aclType= "";
17   var $aclObject= "";
18   var $aclContents= array();
19   var $target= "group";
20   var $aclTypes= array();
21   var $aclObjects= array();
22   var $aclMyObjects= array();
23   var $users= array();
24   var $roles= array();
25   var $groups= array();
26   var $recipients= array();
27   var $isContainer= FALSE;
28   var $currentIndex= 0;
29   var $wasNewEntry= FALSE;
30   var $ocMapping= array();
31   var $savedAclContents= array();
32   var $myAclObjects = array();
34   function acl (&$config, $parent, $dn= NULL)
35   {
36     /* Include config object */
37     plugin::plugin($config, $dn);
39     /* Load ACL's */
40     $this->gosaAclEntry= array();
41     if (isset($this->attrs['gosaAclEntry'])){
42       for ($i= 0; $i<$this->attrs['gosaAclEntry']['count']; $i++){
43         $acl= $this->attrs['gosaAclEntry'][$i];
44         $this->gosaAclEntry= array_merge($this->gosaAclEntry, acl::explodeACL($acl));
45       }
46     }
47     ksort($this->gosaAclEntry);
49     /* Save parent - we've to know more about it than other plugins... */
50     $this->parent= &$parent;
52     /* Container? */
53     if (preg_match('/^(o|ou|c|l|dc)=/i', $dn)){
54       $this->isContainer= TRUE;
55     }
57     /* Users */
58     $ui= get_userinfo();
59     $tag= $ui->gosaUnitTag;
60     $ldap= $config->get_ldap_link();
61     $ldap->cd($config->current['BASE']);
62     if ($tag == ""){
63       $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
64     } else {
65       $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
66     }
67     while ($attrs= $ldap->fetch()){
68       $this->users['U:'.$attrs['dn']]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
69     }
70     ksort($this->users);
72     /* Groups */
73     $ldap->cd($config->current['BASE']);
74     if ($tag == ""){
75       $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
76     } else {
77       $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
78     }
79     while ($attrs= $ldap->fetch()){
80       $dsc= "";
81       if (isset($attrs['description'][0])){
82         $dsc= $attrs['description'][0];
83       }
84       $this->groups['G:'.$attrs['dn']]= $attrs['cn'][0].' ['.$dsc.']';
85     }
86     ksort($this->groups);
88     /* Roles */
89     $ldap->cd($config->current['BASE']);
90 #    if ($tag == ""){
91       $ldap->search('(objectClass=gosaRole)', array('cn', 'description','gosaAclTemplate','dn'));
92 #    } else {
93 #     $ldap->search('(&(objectClass=gosaRole)(gosaUnitTag='.$tag.'))', array('cn', 'description','gosaAclTemplate','dn'));
94 #    }
95     while ($attrs= $ldap->fetch()){
96       $dsc= "";
97       if (isset($attrs['description'][0])){
98         $dsc= $attrs['description'][0];
99       }
101       $role_id = $attrs['dn'];
103       $this->roles[$role_id]['acls'] =array();
104       for ($i= 0; $i < $attrs['gosaAclTemplate']['count']; $i++){
105         $acl= $attrs['gosaAclTemplate'][$i];
106         $this->roles[$role_id]['acls'] = array_merge($this->roles[$role_id]['acls'],acl::explodeACL($acl));
107       }
108       $this->roles[$role_id]['description'] = $dsc;
109       $this->roles[$role_id]['cn'] = $attrs['cn'][0];
110     }
112     /* Objects */
113     $tmp= session::get('plist');
114     $plist= $tmp->info;
115     $cats = array();
116     if (isset($this->parent) && $this->parent !== NULL){
117       $oc= array();
118       foreach ($this->parent->by_object as $key => $obj){
119         $oc= array_merge($oc, $obj->objectclasses);
120         if(isset($obj->acl_category)){
121           $cats[preg_replace("/\//","",$obj->acl_category)] = preg_replace("/\//","",$obj->acl_category);
122         }
123       }
124       if (in_array_ics('organizationalUnit', $oc)){
125         $this->isContainer= TRUE;
126       }
127     } else {
128       $oc=  $this->attrs['objectClass'];
129     }
131     /* Extract available categories from plugin info list */
132     foreach ($plist as $class => $acls){
134       /* Only feed categories */
135       if (isset($acls['plCategory'])){
137         /* Walk through supplied list and feed only translated categories */
138         foreach($acls['plCategory'] as $idx => $data){
140           /* Non numeric index means -> base object containing more informations */
141           if (preg_match('/^[0-9]+$/', $idx)){
142             if (!isset($this->ocMapping[$data])){
143               $this->ocMapping[$data]= array();
144               $this->ocMapping[$data][]= '0';
145             }
147             if(isset($cats[$data])){
148               $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
149             }
150             $this->ocMapping[$data][]= $class;
151           } else {
152             if (!isset($this->ocMapping[$idx])){
153               $this->ocMapping[$idx]= array();
154               $this->ocMapping[$idx][]= '0';
155             }
156             $this->ocMapping[$idx][]= $class;
157             $this->aclObjects[$idx]= $data['description'];
159             /* Additionally filter the classes we're interested in in "self edit" mode */
160             if (is_array($data['objectClass'])){
161               foreach($data['objectClass'] as $objectClass){
162                 if (in_array_ics($objectClass, $oc)){
163                   $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
164                   break;
165                 }
166               }
167             } else {
168               if (in_array_ics($data['objectClass'], $oc)){
169                 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
170               }
171             }
172           }
174         }
175       }
176     }
177     $this->aclObjects['all']= '*&nbsp;'._("All categories");
178     $this->ocMapping['all']= array('0' => 'all');
180     /* Sort categories */
181     asort($this->aclObjects);
183     /* Fill acl types */
184     if ($this->isContainer){
185       $this->aclTypes= array("reset" => _("Reset ACLs"),
186                              "one" => _("One level"),
187                              "base" => _("Current object"),
188                              "sub" => _("Complete subtree"),
189                              "psub" => _("Complete subtree (permanent)"),
190                              "role" => _("Use ACL defined in role"));
191     } else {
192       $this->aclTypes= array("base" => _("Current object"),
193           "role" => _("Use ACL defined in role"));
194     }
195     asort($this->aclTypes);
196     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
197     asort($this->targets);
199     /* Finally - we want to get saved... */
200     $this->is_account= TRUE;
201   }
204   function execute()
205   {
206     /* Call parent execute */
207     plugin::execute();
208   
209     $tmp= session::get('plist');
210     $plist= $tmp->info;
212     /* Handle posts */
213     if (isset($_POST['new_acl'])){
214       $this->dialogState= 'create';
215       $this->dialog= TRUE;
216       $this->currentIndex= count($this->gosaAclEntry);
217       $this->loadAclEntry(TRUE);
218     }
220     $new_acl= array();
221     $aclDialog= FALSE;
222     $firstedit= FALSE;
223     foreach($_POST as $name => $post){
225       /* Actions... */
226       if (preg_match('/^acl_edit_.*_x/', $name)){
227         $this->dialogState= 'create';
228         $firstedit= TRUE;
229         $this->dialog= TRUE;
230         $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
231         $this->loadAclEntry();
232         continue;
233       }
234       if (preg_match('/^acl_del_.*_x/', $name)){
235         unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
236         continue;
237       }
239       if (preg_match('/^cat_edit_.*_x/', $name)){
240         $this->aclObject= preg_replace('/^cat_edit_([^_]+)_.*$/', '\1', $name);
241         $this->dialogState= 'edit';
242         foreach ($this->ocMapping[$this->aclObject] as $oc){
243           if (isset($this->aclContents[$oc])){
244             $this->savedAclContents[$oc]= $this->aclContents[$oc];
245           }
246         }
247         continue;
248       }
249       if (preg_match('/^cat_del_.*_x/', $name)){
250         $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
251         foreach ($this->ocMapping[$idx] as $key){
252           unset($this->aclContents["$idx/$key"]);
253         }
254         continue;
255       }
257       /* Sorting... */
258       if (preg_match('/^sortup_.*_x/', $name)){
259         $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
260         if ($index > 0){
261           $tmp= $this->gosaAclEntry[$index];
262           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
263           $this->gosaAclEntry[$index-1]= $tmp;
264         }
265         continue;
266       }
267       if (preg_match('/^sortdown_.*_x/', $name)){
268         $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
269         if ($index < count($this->gosaAclEntry)-1){
270           $tmp= $this->gosaAclEntry[$index];
271           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
272           $this->gosaAclEntry[$index+1]= $tmp;
273         }
274         continue;
275       }
277       /* ACL saving... */
278       if (preg_match('/^acl_.*_[^xy]$/', $name)){
279         $aclDialog= TRUE;
280         list($dummy, $object, $attribute, $value)= split('_', $name);
282         /* Skip for detection entry */
283         if ($object == 'dummy') {
284           continue;
285         }
287         /* Ordinary ACLs */
288         if (!isset($new_acl[$object])){
289           $new_acl[$object]= array();
290         }
291         if (isset($new_acl[$object][$attribute])){
292           $new_acl[$object][$attribute].= $value;
293         } else {
294           $new_acl[$object][$attribute]= $value;
295         }
296       }
298       if(isset($_POST['selected_role'])){
299         $this->aclContents = "";
300         $this->aclContents = base64_decode($_POST['selected_role']);
301       }
302     }
303     
304     /* Only be interested in new acl's, if we're in the right _POST place */
305     if ($aclDialog && $this->aclObject != "" && is_array($this->ocMapping[$this->aclObject])){
307       foreach ($this->ocMapping[$this->aclObject] as $oc){
308         unset($this->aclContents[$oc]);
309         unset($this->aclContents[$this->aclObject.'/'.$oc]);
310         if (isset($new_acl[$oc])){
311           $this->aclContents[$oc]= $new_acl[$oc];
312         }
313         if (isset($new_acl[$this->aclObject.'/'.$oc])){
314           $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
315         }
316       }
317     }
319     /* Save new acl in case of base edit mode */
320     if ($this->aclType == 'base' && !$firstedit){
321       $this->aclContents= $new_acl;
322     }
324     /* Cancel new acl? */
325     if (isset($_POST['cancel_new_acl'])){
326       $this->dialogState= 'head';
327       $this->dialog= FALSE;
328       if ($this->wasNewEntry){
329         unset ($this->gosaAclEntry[$this->currentIndex]);
330       }
331     }
333     /* Store ACL in main object? */
334     if (isset($_POST['submit_new_acl'])){
335       $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
336       $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
337       $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
338       $this->dialogState= 'head';
339       $this->dialog= FALSE;
340     }
342     /* Cancel edit acl? */
343     if (isset($_POST['cancel_edit_acl'])){
344       $this->dialogState= 'create';
345       foreach ($this->ocMapping[$this->aclObject] as $oc){
346         if (isset($this->savedAclContents[$oc])){
347           $this->aclContents[$oc]= $this->savedAclContents[$oc];
348         }
349       }
350     }
352     /* Save edit acl? */
353     if (isset($_POST['submit_edit_acl'])){
354       $this->dialogState= 'create';
355     }
357     /* Add acl? */
358     if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
359       $this->dialogState= 'edit';
360       $this->savedAclContents= array();
361       foreach ($this->ocMapping[$this->aclObject] as $oc){
362         if (isset($this->aclContents[$oc])){
363           $this->savedAclContents[$oc]= $this->aclContents[$oc];
364         }
365       }
366     }
368     /* Add to list? */
369     if (isset($_POST['add']) && isset($_POST['source'])){
370       foreach ($_POST['source'] as $key){
371         if ($this->target == 'user'){
372           $this->recipients[$key]= $this->users[$key];
373         }
374         if ($this->target == 'group'){
375           $this->recipients[$key]= $this->groups[$key];
376         }
377       }
378       ksort($this->recipients);
379     }
381     /* Remove from list? */
382     if (isset($_POST['del']) && isset($_POST['recipient'])){
383       foreach ($_POST['recipient'] as $key){
384           unset($this->recipients[$key]);
385       }
386     }
388     /* Save common values */
389     foreach (array("aclType", "aclObject", "target") as $key){
390       if (isset($_POST[$key])){
391         $this->$key= validate($_POST[$key]);
392       }
393     }
395     /* Create templating instance */
396     $smarty= get_smarty();
398     if ($this->dialogState == 'head'){
399       /* Draw list */
400       $aclList= new divSelectBox("aclList");
401       $aclList->SetHeight(450);
402       
403       /* Fill in entries */
404       foreach ($this->gosaAclEntry as $key => $entry){
405         $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:150px'");
406         $field2= array("string" => $this->assembleAclSummary($entry));
407         $action= "<input type='image' name='sortup_$key' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top'>";
408         $action.= "<input type='image' name='sortdown_$key' alt='down' title='"._("Down")."' src='images/sort_down.png'>";
409         $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='acl_edit_$key' title='"._("Edit ACL")."'>";
410         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='acl_del_$key' title='"._("Delete ACL")."'>";
412         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
413         $aclList->AddEntry(array($field1, $field2, $field3));
414       }
416       $smarty->assign("aclList", $aclList->DrawList());
417     }
419     if ($this->dialogState == 'create'){
420       /* Draw list */
421       $aclList= new divSelectBox("aclList");
422       $aclList->SetHeight(150);
424       /* Add settings for all categories to the (permanent) list */
425       foreach ($this->aclObjects as $section => $dsc){
426         $summary= "";
427         foreach($this->ocMapping[$section] as $oc){
428           if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
429               $this->aclContents[$oc][0] != ""){
431             $summary.= "$oc, ";
432             continue;
433           }
434           if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"])){
435             $summary.= "$oc, ";
436             continue;
437           }
438           if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
439             $summary.= "$oc, ";
440           }
441         }
443         /* Set summary... */
444         if ($summary == ""){
445           $summary= '<i>'._("No ACL settings for this category").'</i>';
446         } else {
447           $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
448         }
450         $field1= array("string" => $dsc, "attach" => "style='width:100px'");
451         $field2= array("string" => $summary);
452         $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='cat_edit_$section' title='"._("Edit categories ACLs")."'>";
453         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='cat_del_$section' title='"._("Clear categories ACLs")."'>";
454         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px'");
455         $aclList->AddEntry(array($field1, $field2, $field3));
456       }
458       $smarty->assign("aclList", $aclList->DrawList());
459       $smarty->assign("aclType", $this->aclType);
460       $smarty->assign("aclTypes", $this->aclTypes);
461       $smarty->assign("target", $this->target);
462       $smarty->assign("targets", $this->targets);
464       /* Assign possible target types */
465       $smarty->assign("targets", $this->targets);
466       foreach ($this->attributes as $attr){
467         $smarty->assign($attr, $this->$attr);
468       }
471       /* Generate list */
472       $tmp= array();
473       foreach (array("user" => "users", "group" => "groups") as $field => $arr){
474         if ($this->target == $field){
475           foreach ($this->$arr as $key => $value){
476             if (!isset($this->recipients[$key])){
477               $tmp[$key]= $value;
478             }
479           }
480         }
481       }
482       $smarty->assign('sources', $tmp);
483       $smarty->assign('recipients', $this->recipients);
485       /* Acl selector if scope is base */
486       if ($this->aclType == 'base'){
487         $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
488       }
490       /* Role selector if scope is base */
491       if ($this->aclType == 'role'){
492         $smarty->assign('roleSelector', "Role selector");#, $this->buildRoleSelector($this->myAclObjects));
493         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
494       }
495     }
497     if ($this->dialogState == 'edit'){
498       $smarty->assign('headline', sprintf(_("Edit ACL for '%s', scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
500       /* Collect objects for selected category */
501       foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
502         if ($idx == 0){
503           continue;
504         }
505         $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
506       }
507       if ($this->aclObject == 'all'){
508         $aclObjects['all']= _("All objects in current subtree");
509       }
511       /* Role selector if scope is base */
512       if ($this->aclType == 'role'){
513         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
514       } else {
515         $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
516       }
517     }
519     /* Show main page */
520     $smarty->assign("dialogState", $this->dialogState);
522     return ($smarty->fetch (get_template_path('acl.tpl')));
523   }
526   function sort_by_priority($list)
527   {
528     $tmp= session::get('plist');
529     $plist= $tmp->info;
530     asort($plist);
531     $newSort = array();
533     foreach($list as $name => $translation){
534       $na  =  preg_replace("/^.*\//","",$name);
535       $prio = 0;
536       if(isset($plist[$na]['plPriority'])){
537         $prio=  $plist[$na]['plPriority'] ;
538       }
540       $newSort[$name] = $prio;
541     }
543     asort($newSort);
545     $ret = array();
546     foreach($newSort as $name => $prio){
547       $ret[$name] = $list[$name];
548     }
549     return($ret);
550   }
553   function buildRoleSelector($list)
554   {
555     $D_List =new divSelectBox("Acl_Roles");
556  
557     $selected = $this->aclContents;
558     if(!is_string($this->aclContents) || !isset($list[$this->aclContents])){
559       $selected = key($list);
560     }
562     $str ="";
563     foreach($list as $dn => $values){
565       if($dn == $selected){    
566         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."' checked>";
567       }else{
568         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."'>";
569       }
570  
571       $field1 = array("string" => $option) ;
572       $field2 = array("string" => $values['cn'], "attach" => "style='width:200px;'") ;
573       $field3 = array("string" => $values['description'],"attach" => "style='border-right:0px;'") ;
575       $D_List->AddEntry(array($field1,$field2,$field3));
576     }
577     return($D_List->DrawList());
578   } 
581   function buildAclSelector($list)
582   {
583     $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
584     $cols= 3;
585     $tmp= session::get('plist');
586     $plist= $tmp->info;
587     asort($plist);
589     /* Add select all/none buttons */
590     $style = "style='width:100px;'";
592     $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
593     $display .= "<input ".$style." type='button' name='toggle_all_move'   onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
594     $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
595     $display .= "<input ".$style." type='button' name='toggle_all_read'   onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
596     $display .= "<input ".$style." type='button' name='toggle_all_write'  onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
597     
598     $display .= "<input ".$style." type='button' name='toggle_all_sub_read'  onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
599     $display .= "<input ".$style." type='button' name='toggle_all_sub_write'  onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
600   
601     $display .= "<br>";
602   
603     $style = "style='width:50px;'";
604     $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
605     $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
606     $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
607     $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
608     $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
609     $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
610     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
611     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
612     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
613     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
615     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
616     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
617     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
618     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
620     /* Build general objects */
621     $list =$this->sort_by_priority($list);
622     foreach ($list as $key => $name){
624       /* Create sub acl if it does not exist */
625       if (!isset($this->aclContents[$key])){
626         $this->aclContents[$key]= array();
627         $this->aclContents[$key][0]= '';
628       }
629       $currentAcl= $this->aclContents[$key];
631       /* Get the overall plugin acls 
632        */
633       $overall_acl ="";
634       if(isset($currentAcl[0])){
635         $overall_acl = $currentAcl[0];
636       }
638       /* Object header */
639       if(session::get('js')) {
640         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
641           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
642                      "\n  <tr>".
643                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
644                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
645                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
646                      "\n  </tr>";
647         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
648           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
649                      "\n  <tr>".
650                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
651                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
652                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
653                      "\n  </tr>";
654         } else {
655           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
656                      "\n  <tr>".
657                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
658                      "\n  </tr>";
659         }
660       } else {
661           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
662                      "\n  <tr>".
663                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
664                      "\n  </tr>";
665       }
667       /* Generate options */
668       $spc= "&nbsp;&nbsp;";
669       if ($this->isContainer && $this->aclType != 'base'){
670         $options= $this->mkchkbx($key."_0_c",  _("Create objects"), preg_match('/c/', $overall_acl)).$spc;
671         $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $overall_acl)).$spc;
672         $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $overall_acl)).$spc;
673         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
674           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $overall_acl)).$spc;
675         }
676       } else {
677         $options= $this->mkchkbx($key."_0_m", _("Move object"), preg_match('/m/', $overall_acl)).$spc;
678         $options.= $this->mkchkbx($key."_0_d", _("Remove object"), preg_match('/d/', $overall_acl)).$spc;
679         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
680           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $overall_acl)).$spc;
681         }
682       }
684       /* Global options */
685       $more_options= $this->mkchkbx($key."_0_r",  _("read"), preg_match('/r/', $overall_acl)).$spc;
686       $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $overall_acl));
688       $display.= "\n  <tr>".
689                  "\n    <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
690                  "\n    <td style='background-color:#D4D4D4'>&nbsp;"._("Complete object").": $more_options</td>".
691                  "\n  </tr>";
693       /* Walk through the list of attributes */
694       $cnt= 1;
695       $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
696       asort($splist);
697       if(session::get('js')) {
698         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
699           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
700                      "\n    <td colspan=".$cols.">".
701                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
702                      "\n        <table style='width:100%;'>";
703         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
704           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
705                      "\n    <td colspan=".$cols.">".
706                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
707                      "\n        <table style='width:100%;'>";
708         }
709       }
710       foreach($splist as $attr => $dsc){
712         /* Skip pl* attributes, they are internal... */
713         if (preg_match('/^pl[A-Z]+.*$/', $attr)){
714           continue;
715         }
717         /* Open table row */
718         if ($cnt == 1){
719           $display.= "\n  <tr>";
720         }
722         /* Close table row */
723         if ($cnt == $cols){
724           $cnt= 1;
725           $rb= "";
726           $end= "\n  </tr>";
727         } else {
728           $cnt++;
729           $rb= "border-right:1px solid #A0A0A0;";
730           $end= "";
731         }
733         /* Collect list of attributes */
734         $state= "";
735         if (isset($currentAcl[$attr])){
736           $state= $currentAcl[$attr];
737         }
738         $display.= "\n    <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
739                    "\n      <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
740       }
741       
742       /* Fill missing td's if needed */
743       if (--$cnt != $cols && $cnt != 0){
744        $display.= str_repeat("\n    <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'>&nbsp;</td>", $cols-$cnt); 
745       }
747       if(session::get('js')) {
748         if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
749           $display.= "\n        </table>".
750                      "\n      </div>".
751                      "\n    </td>".
752                      "\n  </tr>";
753         }
754       }
756       $display.= "\n</table><br />\n";
757     }
759     return ($display);
760   }
763   function mkchkbx($name, $text, $state= FALSE)
764   {
765     $state= $state?"checked":"";
766     return "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."' type=checkbox name='acl_$name' $state>".
767            "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."'>$text</label>";
768   }
771   function mkrwbx($name, $state= "")
772   {
773     $rstate= preg_match('/r/', $state)?'checked':'';
774     $wstate= preg_match('/w/', $state)?'checked':'';
775     return ("\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r' type=checkbox name='acl_${name}_r' $rstate>".
776             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r'>"._("read")."</label>".
777             "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w' type=checkbox name='acl_${name}_w' $wstate>".
778             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w'>"._("write")."</label>");
779   }
782   static function explodeACL($acl)
783   {
784     list($index, $type)= split(':', $acl);
785     $a= array( $index => array("type" => $type,
786                                "members" => acl::extractMembers($acl,$type == "role")));
787    
788     /* Handle different types */
789     switch ($type){
791       case 'psub':
792       case 'sub':
793       case 'one':
794       case 'base':
795         $a[$index]['acl']= acl::extractACL($acl);
796         break;
797       
798       case 'role':
799         $a[$index]['acl']= base64_decode(preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl));
800         break;
802       case 'reset':
803         break;
804       
805       default:
806         msg_dialog::display(_("Internal error"), sprintf(_("Unkown ACL type '%s'. Don't know how to handle it."), $type), ERROR_DIALOG);
807         $a= array();
808     }
809     return ($a);
810   }
813   static function extractMembers($acl,$role = FALSE)
814   {
815     global $config;
816     $a= array();
818     /* Rip acl off the string, seperate by ',' and place it in an array */
819     if($role){
820       $ms= preg_replace('/^[^:]+:[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
821     }else{
822       $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
823     }
824     if ($ms == $acl){
825       return $a;
826     }
827     $ma= split(',', $ms);
829     /* Decode dn's, fill with informations from LDAP */
830     $ldap= $config->get_ldap_link();
831     foreach ($ma as $memberdn){
832       $dn= base64_decode($memberdn);
833       $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
835       /* Found entry... */
836       if ($ldap->count()){
837         $attrs= $ldap->fetch();
838         if (in_array_ics('gosaAccount', $attrs['objectClass'])){
839           $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
840         } else {
841           $a['G:'.$dn]= $attrs['cn'][0];
842           if (isset($attrs['description'][0])){
843             $a['G:'.$dn].= " [".$attrs['description'][0]."]";
844           }
845         }
847       /* ... or not */
848       } else {
849         $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
850       }
851     }
853     return ($a);
854   }
857   static function extractACL($acl)
858   {
859     /* Rip acl off the string, seperate by ',' and place it in an array */
860     $as= preg_replace('/^[^:]+:[^:]+:[^:]*:(.*)$/', '\1', $acl);
861     $aa= split(',', $as);
862     $a= array();
864     /* Dis-assemble single ACLs */
865     foreach($aa as $sacl){
866       
867       /* Dis-assemble field ACLs */
868       $ao= split('#', $sacl);
869       $gobject= "";
870       foreach($ao as $idx => $ssacl){
872         /* First is department with global acl */
873         $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
874         $gacl=   preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
875         if ($idx == 0){
876           /* Create hash for this object */
877           $gobject= $object;
878           $a[$gobject]= array();
880           /* Append ACL if set */
881           if ($gacl != ""){
882             $a[$gobject]= array($gacl);
883           }
884         } else {
886           /* All other entries get appended... */
887           list($field, $facl)= split(';', $ssacl);
888           $a[$gobject][$field]= $facl;
889         }
891       }
892     }
894     return ($a);
895   }
897   
898   function assembleAclSummary($entry)
899   {
900     $summary= "";
902     /* Summarize ACL */
903     if (isset($entry['acl'])){
904       $acl= "";
906       if($entry['type'] == "role"){
908         if(isset($this->roles[$entry['acl']])){  
909           $summary.= sprintf(_("Role: %s"), $this->roles[$entry['acl']]['cn']);
910         }else{
911           $summary.= sprintf(_("Role: %s"), "<i>"._("Unknown role, possibly removed")."</i>");
912         }
913       }else{
914         foreach ($entry['acl'] as $name => $object){
915           if (count($object)){
916             $acl.= "$name, ";
917           }
918         }
919         $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
920       }
921     }
923     /* Summarize members */
924     if ($summary != ""){
925       $summary.= ", ";
926     }
927     if (count($entry['members'])){
928       $summary.= _("Members:")." ";
929       foreach ($entry['members'] as $cn){
930         $cn= preg_replace('/ \[.*$/', '', $cn);
931         $summary.= $cn.", ";
932       }
933     } else {
934       $summary.= _("ACL is valid for all users");
935     }
937     return (preg_replace('/, $/', '', $summary));
938   }
941   function loadAclEntry($new= FALSE)
942   {
943     /* New entry gets presets... */
944     if ($new){
945       $this->aclType= 'base';
946       $this->recipients= array();
947       $this->aclContents= array();
948     } else {
949       $acl= $this->gosaAclEntry[$this->currentIndex];
950       $this->aclType= $acl['type'];
951       $this->recipients= $acl['members'];
952       $this->aclContents= $acl['acl'];
953     }
955     $this->wasNewEntry= $new;
956   }
959   function aclPostHandler()
960   {
961     if (isset($_POST['save_acl'])){
962       $this->save();
963       return TRUE;
964     }
966     return FALSE;
967   }
969   
970   function PrepareForCopyPaste($source)
971   {
972     plugin::PrepareForCopyPaste($source);
973     
974     $dn = $source['dn'];
975     $acl_c = new acl($this->config, $this->parent,$dn);
976     $this->gosaAclEntry = $acl_c->gosaAclEntry;
977   }
980   function save()
981   {
982     /* Assemble ACL's */
983     $tmp_acl= array();
984     foreach ($this->gosaAclEntry as $prio => $entry){
985       $final= "";
986       $members= "";
987       if (isset($entry['members'])){
988         foreach ($entry['members'] as $key => $dummy){
989           $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
990         }
991       }
993       if($entry['type'] != "role"){
994         $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
995       }else{
996         $final= $prio.":".$entry['type'].":".base64_encode($entry['acl']).":".preg_replace('/,$/', '', $members);
997       }
999       /* ACL's if needed */
1000       if ($entry['type'] != "reset" && $entry['type'] != "role"){
1001         $acl= ":";
1002         if (isset($entry['acl'])){
1003           foreach ($entry['acl'] as $object => $contents){
1005             /* Only save, if we've some contents in there... */
1006             if (count($contents)){
1007               $acl.= $object.";";
1009               foreach($contents as $attr => $permission){
1011                 /* First entry? Its the one for global settings... */
1012                 if ($attr == '0'){
1013                   $acl.= $permission;
1014                 } else {
1015                   $acl.= '#'.$attr.';'.$permission;
1016                 }
1018               }
1019               $acl.= ',';
1020             }
1021             
1022           }
1023         }
1024         $final.= preg_replace('/,$/', '', $acl);
1025       }
1027       $tmp_acl[]= $final;
1028     } 
1030     /* Call main method */
1031     plugin::save();
1033     /* Finally (re-)assign it... */
1034     $this->attrs['gosaAclEntry']= $tmp_acl;
1036     /* Remove acl from this entry if it is empty... */
1037     if (!count($tmp_acl)){
1038       /* Remove attribute */
1039       if ($this->initially_was_account){
1040         $this->attrs['gosaAclEntry']= array();
1041       } else {
1042         if (isset($this->attrs['gosaAclEntry'])){
1043           unset($this->attrs['gosaAclEntry']);
1044         }
1045       }
1047       /* Remove object class */
1048       $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
1049     }    
1051     /* Do LDAP modifications */
1052     $ldap= $this->config->get_ldap_link();
1053     $ldap->cd($this->dn);
1054     $this->cleanup();
1055     $ldap->modify ($this->attrs);
1057     if(count($this->attrs)){
1058       new log("modify","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1059     }
1061     show_ldap_error($ldap->get_error(), sprintf(_("Saving ACLs with dn '%s' failed."),$this->dn));
1063     /* Refresh users ACLs */
1064     $ui= get_userinfo();
1065     $ui->loadACL();
1066     session::set('ui',$ui);
1067   }
1070   function remove_from_parent()
1071   {
1072     plugin::remove_from_parent();
1074     /* include global link_info */
1075     $ldap= $this->config->get_ldap_link();
1077     $ldap->cd($this->dn);
1078     $this->cleanup();
1079     $ldap->modify ($this->attrs);
1081     new log("remove","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1083     /* Optionally execute a command after we're done */
1084     $this->handle_post_events("remove",array("uid" => $this->uid));
1085   }
1087   
1088   /* Return plugin informations for acl handling */
1089   static function plInfo()
1090   {
1091     return (array(
1092           "plShortName"   => _("ACL"),
1093           "plDescription" => _("ACL")._("Access control list").")",
1094           "plSelfModify"  => FALSE,
1095           "plDepends"     => array(),
1096           "plPriority"    => 0,
1097           "plSection"     => array("administration"),
1098           "plCategory"    => array("acl" => array("description"  => _("ACL")."&nbsp;&amp;&nbsp;"._("ACL roles"),
1099                                                           "objectClass"  => array("gosaAcl","gosaRole"))),
1100           "plProvidedAcls"=> array(
1101             "cn"          => _("Role name"),
1102             "description" => _("Role description"))
1104           ));
1105   }
1108   /* Remove acls defined for $src */
1109   function remove_acl()
1110   {
1111     $this->remove_acl_for_dn($this->dn);
1112   }
1115   /* Remove acls defined for $src */
1116   function remove_acl_for_dn($src = "")
1117   {
1118     if($src == ""){
1119       $src = $this->dn;
1120     }
1121     $ldap = $this->config->get_ldap_link();
1122     $ldap->cd($this->config->current['BASE']);
1123     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1124     while($attrs = $ldap->fetch()){
1125       $acl = new acl($this->config,$this->parent,$attrs['dn']);
1126       foreach($acl->gosaAclEntry as $id => $entry){
1127         foreach($entry['members'] as $m_id => $member){
1128           if($m_id == "U:".$src){
1129             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1130             gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Removed acl for user %s on object %s.",$src,$attrs['dn']));
1131           }
1132           if($m_id == "G:".$src){
1133             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1134             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for group %s on object %s.",$src,$attrs['dn']));
1135           }
1136         }
1137       }
1138       $acl -> save();
1139     }
1140   }
1142   function update_acl_membership($src,$dst)
1143   {
1144     $ldap = $this->config->get_ldap_link();
1145     $ldap->cd($this->config->current['BASE']);
1146     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1147     while($attrs = $ldap->fetch()){
1148       $acl = new acl($this->config,$this->parent,$attrs['dn']);
1149       foreach($acl->gosaAclEntry as $id => $entry){
1150         foreach($entry['members'] as $m_id => $member){
1151           if($m_id == "U:".$src){
1152             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1153             $new = "U:".$dst;
1154             $acl->gosaAclEntry[$id]['members'][$new] = $new;
1155             gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Updated acl for user %s on object %s.",$src,$attrs['dn']));
1156           }
1157           if($m_id == "G:".$src){
1158             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1159             $new = "G:".$dst;
1160             $acl->gosaAclEntry[$id]['members'][$new] = $new;
1161             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Updated acl for group %s on object %s.",$src,$attrs['dn']));
1162           }
1163         }
1164       }
1165       $acl -> save();
1166     }
1167   }
1170 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1171 ?>