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 acl extends plugin
24 {
25 /* Definitions */
26 var $plHeadline= "Access control";
27 var $plDescription= "Manage access control lists";
29 /* attribute list for save action */
30 var $attributes= array('gosaAclEntry');
31 var $objectclasses= array('gosaAcl');
33 /* Helpers */
34 var $dialogState= "head";
35 var $gosaAclEntry= array();
36 var $aclType= "";
37 var $aclObject= "";
38 var $aclContents= array();
39 var $target= "group";
40 var $aclTypes= array();
41 var $aclObjects= array();
42 var $aclFilter= "";
43 var $aclMyObjects= array();
44 var $users= array();
45 var $roles= array();
46 var $groups= array();
47 var $recipients= array();
48 var $isContainer= FALSE;
49 var $currentIndex= 0;
50 var $wasNewEntry= FALSE;
51 var $ocMapping= array();
52 var $savedAclContents= array();
53 var $myAclObjects = array();
54 var $acl_category = "acl/";
56 function acl (&$config, $parent, $dn= NULL)
57 {
58 /* Include config object */
59 plugin::plugin($config, $dn);
61 /* Load ACL's */
62 $this->gosaAclEntry= array();
63 if (isset($this->attrs['gosaAclEntry'])){
64 for ($i= 0; $i<$this->attrs['gosaAclEntry']['count']; $i++){
65 $acl= $this->attrs['gosaAclEntry'][$i];
66 $this->gosaAclEntry= array_merge($this->gosaAclEntry, acl::explodeACL($acl));
67 }
68 }
69 ksort($this->gosaAclEntry);
71 /* Save parent - we've to know more about it than other plugins... */
72 $this->parent= &$parent;
74 /* Container? */
75 if (preg_match('/^(o|ou|c|l|dc)=/i', $dn)){
76 $this->isContainer= TRUE;
77 }
79 /* Users */
80 $ui= get_userinfo();
81 $tag= $ui->gosaUnitTag;
82 $ldap= $config->get_ldap_link();
83 $ldap->cd($config->current['BASE']);
84 if ($tag == ""){
85 $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
86 } else {
87 $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
88 }
89 while ($attrs= $ldap->fetch()){
90 $this->users['U:'.$attrs['dn']]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
91 }
92 ksort($this->users);
94 /* Groups */
95 $ldap->cd($config->current['BASE']);
96 # if ($tag == ""){
97 $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
98 # } else {
99 # $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
100 # }
101 while ($attrs= $ldap->fetch()){
102 $dsc= "";
103 if (isset($attrs['description'][0])){
104 $dsc= $attrs['description'][0];
105 }
106 $this->groups['G:'.$attrs['dn']]= $attrs['cn'][0].' ['.$dsc.']';
107 }
108 ksort($this->groups);
110 /* Roles */
111 $ldap->cd($config->current['BASE']);
112 # if ($tag == ""){
113 $ldap->search('(objectClass=gosaRole)', array('cn', 'description','gosaAclTemplate','dn'));
114 # } else {
115 # $ldap->search('(&(objectClass=gosaRole)(gosaUnitTag='.$tag.'))', array('cn', 'description','gosaAclTemplate','dn'));
116 # }
117 while ($attrs= $ldap->fetch()){
118 $dsc= "";
119 if (isset($attrs['description'][0])){
120 $dsc= $attrs['description'][0];
121 }
123 $role_id = $attrs['dn'];
125 $this->roles[$role_id]['acls'] =array();
126 for ($i= 0; $i < $attrs['gosaAclTemplate']['count']; $i++){
127 $acl= $attrs['gosaAclTemplate'][$i];
128 $this->roles[$role_id]['acls'] = array_merge($this->roles[$role_id]['acls'],acl::explodeACL($acl));
129 }
130 $this->roles[$role_id]['description'] = $dsc;
131 $this->roles[$role_id]['cn'] = $attrs['cn'][0];
132 }
134 /* Objects */
135 $tmp= session::global_get('plist');
136 $plist= $tmp->info;
137 $cats = array();
138 if (isset($this->parent) && $this->parent !== NULL){
139 $oc= array();
140 foreach ($this->parent->by_object as $key => $obj){
141 $oc= array_merge($oc, $obj->objectclasses);
142 if(isset($obj->acl_category)){
143 $tmp= str_replace("/","",$obj->acl_category);
144 $cats[$tmp] = $tmp;
145 }
146 }
147 if (in_array_ics('organizationalUnit', $oc)){
148 $this->isContainer= TRUE;
149 }
150 } else {
151 $oc= $this->attrs['objectClass'];
152 }
154 /* Extract available categories from plugin info list */
155 foreach ($plist as $class => $acls){
157 /* Only feed categories */
158 if (isset($acls['plCategory'])){
160 /* Walk through supplied list and feed only translated categories */
161 foreach($acls['plCategory'] as $idx => $data){
163 /* Non numeric index means -> base object containing more informations */
164 if (preg_match('/^[0-9]+$/', $idx)){
166 if (!isset($this->ocMapping[$data])){
167 $this->ocMapping[$data]= array();
168 $this->ocMapping[$data][]= '0';
169 }
171 if(isset($cats[$data])){
172 $this->myAclObjects[$data.'/'.$class]= $acls['plDescription'];
173 }
174 $this->ocMapping[$data][]= $class;
175 } else {
176 if (!isset($this->ocMapping[$idx])){
177 $this->ocMapping[$idx]= array();
178 $this->ocMapping[$idx][]= '0';
179 }
180 $this->ocMapping[$idx][]= $class;
181 $this->aclObjects[$idx]= $data['description'];
183 /* Additionally filter the classes we're interested in in "self edit" mode */
184 if (is_array($data['objectClass'])){
185 foreach($data['objectClass'] as $objectClass){
186 if (in_array_ics($objectClass, $oc)){
187 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
188 break;
189 }
190 }
191 } else {
192 if (in_array_ics($data['objectClass'], $oc)){
193 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
194 }
195 }
196 }
198 }
199 }
200 }
201 $this->aclObjects['all']= '* '._("All categories");
202 $this->ocMapping['all']= array('0' => 'all');
204 /* Sort categories */
205 asort($this->aclObjects);
207 /* Fill acl types */
208 if ($this->isContainer){
209 $this->aclTypes= array("reset" => _("Reset ACLs"),
210 "one" => _("One level"),
211 "base" => _("Current object"),
212 "sub" => _("Complete subtree"),
213 "psub" => _("Complete subtree (permanent)"),
214 "role" => _("Use ACL defined in role"));
215 } else {
216 $this->aclTypes= array("base" => _("Current object"),
217 "role" => _("Use ACL defined in role"));
218 }
219 asort($this->aclTypes);
220 $this->targets= array("user" => _("Users"), "group" => _("Groups"));
221 asort($this->targets);
223 /* Finally - we want to get saved... */
224 $this->is_account= TRUE;
225 }
228 function execute()
229 {
230 /* Call parent execute */
231 plugin::execute();
233 $tmp= session::global_get('plist');
234 $plist= $tmp->info;
236 /* Handle posts */
237 if (isset($_POST['new_acl'])){
238 $this->dialogState= 'create';
239 $this->dialog= TRUE;
240 $this->currentIndex= count($this->gosaAclEntry);
241 $this->loadAclEntry(TRUE);
242 }
244 $new_acl= array();
245 $aclDialog= FALSE;
246 $firstedit= FALSE;
248 foreach($_POST as $name => $post){
250 /* Actions... */
251 if (preg_match('/^acl_edit_.*_x/', $name)){
252 $this->dialogState= 'create';
253 $firstedit= TRUE;
254 $this->dialog= TRUE;
255 $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
256 $this->loadAclEntry();
257 continue;
258 }
260 if (preg_match('/^cat_edit_.*_x/', $name)){
261 $this->aclObject= preg_replace('/^cat_edit_([^_]+)_.*$/', '\1', $name);
262 $this->dialogState= 'edit';
263 foreach ($this->ocMapping[$this->aclObject] as $oc){
264 if (isset($this->aclContents[$oc])){
265 $this->savedAclContents[$oc]= $this->aclContents[$oc];
266 }
267 }
268 continue;
269 }
271 /* Only handle posts, if we allowed to modify ACLs */
272 if(!$this->acl_is_writeable("")){
273 continue;
274 }
276 if (preg_match('/^acl_del_.*_x/', $name)){
277 unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
278 continue;
279 }
281 if (preg_match('/^cat_del_.*_x/', $name)){
282 $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
283 foreach ($this->ocMapping[$idx] as $key){
284 unset($this->aclContents["$idx/$key"]);
285 }
286 continue;
287 }
289 /* Sorting... */
290 if (preg_match('/^sortup_.*_x/', $name)){
291 $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
292 if ($index > 0){
293 $tmp= $this->gosaAclEntry[$index];
294 $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
295 $this->gosaAclEntry[$index-1]= $tmp;
296 }
297 continue;
298 }
299 if (preg_match('/^sortdown_.*_x/', $name)){
300 $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
301 if ($index < count($this->gosaAclEntry)-1){
302 $tmp= $this->gosaAclEntry[$index];
303 $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
304 $this->gosaAclEntry[$index+1]= $tmp;
305 }
306 continue;
307 }
309 /* ACL saving... */
310 if (preg_match('/^acl_.*_[^xy]$/', $name)){
311 list($dummy, $object, $attribute, $value)= split('_', $name);
313 /* Skip for detection entry */
314 if ($object == 'dummy') {
315 continue;
316 }
318 /* Ordinary ACLs */
319 if (!isset($new_acl[$object])){
320 $new_acl[$object]= array();
321 }
322 if (isset($new_acl[$object][$attribute])){
323 $new_acl[$object][$attribute].= $value;
324 } else {
325 $new_acl[$object][$attribute]= $value;
326 }
327 }
329 if(isset($_POST['selected_role'])){
330 $this->aclContents = "";
331 $this->aclContents = base64_decode($_POST['selected_role']);
332 }
333 }
335 if(isset($_POST['acl_dummy_0_0_0'])){
336 $aclDialog= TRUE;
337 }
339 if($this->acl_is_writeable("")){
341 /* Only be interested in new acl's, if we're in the right _POST place */
342 if ($aclDialog && $this->aclObject != "" && is_array($this->ocMapping[$this->aclObject])){
344 foreach ($this->ocMapping[$this->aclObject] as $oc){
346 if(isset($this->aclContents[$oc]) && is_array($this->aclContents)){
347 unset($this->aclContents[$oc]);
348 }elseif(isset($this->aclContents[$this->aclObject.'/'.$oc]) && is_array($this->aclContents)){
349 unset($this->aclContents[$this->aclObject.'/'.$oc]);
350 }else{
351 # trigger_error("Huhm?");
352 }
353 if (isset($new_acl[$oc]) && is_array($new_acl)){
354 $this->aclContents[$oc]= $new_acl[$oc];
355 }
356 if (isset($new_acl[$this->aclObject.'/'.$oc]) && is_array($new_acl)){
357 $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
358 }
359 }
360 }
362 /* Save new acl in case of base edit mode */
363 if ($this->aclType == 'base' && !$firstedit){
364 $this->aclContents= $new_acl;
365 }
366 }
368 /* Cancel new acl? */
369 if (isset($_POST['cancel_new_acl'])){
370 $this->dialogState= 'head';
371 $this->dialog= FALSE;
372 if ($this->wasNewEntry){
373 unset ($this->gosaAclEntry[$this->currentIndex]);
374 }
375 }
377 /* Save common values */
378 if($this->acl_is_writeable("")){
379 foreach (array("aclType","aclFilter", "aclObject", "target") as $key){
380 if (isset($_POST[$key])){
381 $this->$key= validate($_POST[$key]);
382 }
383 }
384 }
386 /* Store ACL in main object? */
387 if (isset($_POST['submit_new_acl'])){
388 $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
389 $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
390 $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
391 $this->gosaAclEntry[$this->currentIndex]['filter']= $this->aclFilter;
392 $this->dialogState= 'head';
393 $this->dialog= FALSE;
394 }
396 /* Cancel edit acl? */
397 if (isset($_POST['cancel_edit_acl'])){
398 $this->dialogState= 'create';
399 foreach ($this->ocMapping[$this->aclObject] as $oc){
400 if (isset($this->savedAclContents[$oc])){
401 $this->aclContents[$oc]= $this->savedAclContents[$oc];
402 }
403 }
404 }
406 /* Save edit acl? */
407 if (isset($_POST['submit_edit_acl'])){
408 $this->dialogState= 'create';
409 }
411 /* Add acl? */
412 if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
413 $this->dialogState= 'edit';
414 $this->savedAclContents= array();
415 foreach ($this->ocMapping[$this->aclObject] as $oc){
416 if (isset($this->aclContents[$oc])){
417 $this->savedAclContents[$oc]= $this->aclContents[$oc];
418 }
419 }
420 }
422 /* Add to list? */
423 if (isset($_POST['add']) && isset($_POST['source'])){
424 foreach ($_POST['source'] as $key){
425 if ($this->target == 'user'){
426 $this->recipients[$key]= $this->users[$key];
427 }
428 if ($this->target == 'group'){
429 $this->recipients[$key]= $this->groups[$key];
430 }
431 }
432 ksort($this->recipients);
433 }
435 /* Remove from list? */
436 if (isset($_POST['del']) && isset($_POST['recipient'])){
437 foreach ($_POST['recipient'] as $key){
438 unset($this->recipients[$key]);
439 }
440 }
442 /* Create templating instance */
443 $smarty= get_smarty();
444 $smarty->assign("acl_readable",$this->acl_is_readable(""));
445 if(!$this->acl_is_readable("")){
446 return ($smarty->fetch (get_template_path('acl.tpl')));
447 }
449 if ($this->dialogState == 'head'){
450 /* Draw list */
451 $aclList= new divSelectBox("aclList");
452 $aclList->SetHeight(450);
454 /* Fill in entries */
455 foreach ($this->gosaAclEntry as $key => $entry){
456 if(!$this->acl_is_readable("")) continue;
458 $action ="";
460 $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:150px'");
461 $field2= array("string" => $this->assembleAclSummary($entry));
463 if($this->acl_is_writeable("")){
464 $action.= "<input type='image' name='sortup_$key' alt='up'
465 title='"._("Up")."' src='images/lists/sort-up.png' align='top'>";
466 $action.= "<input type='image' name='sortdown_$key' alt='down'
467 title='"._("Down")."' src='images/lists/sort-down.png'>";
468 }
470 if($this->acl_is_readable("")){
471 $action.= "<input class='center' type='image' src='images/lists/edit.png'
472 alt='"._("Edit")."' name='acl_edit_$key' title='".msgPool::editButton(_("ACL"))."'>";
473 }
474 if($this->acl_is_removeable("")){
475 $action.= "<input class='center' type='image' src='images/lists/trash.png'
476 alt='"._("Delete")."' name='acl_del_$key' title='".msgPool::delButton(_("ACL"))."'>";
477 }
479 $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
480 $aclList->AddEntry(array($field1, $field2, $field3));
481 }
483 $smarty->assign("aclList", $aclList->DrawList());
484 }
486 if ($this->dialogState == 'create'){
487 /* Draw list */
488 $aclList= new divSelectBox("aclList");
489 $aclList->SetHeight(150);
491 /* Add settings for all categories to the (permanent) list */
492 foreach ($this->aclObjects as $section => $dsc){
493 $summary= "";
494 foreach($this->ocMapping[$section] as $oc){
495 if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
496 $this->aclContents[$oc][0] != ""){
498 $summary.= "$oc, ";
499 continue;
500 }
501 if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"])){
502 $summary.= "$oc, ";
503 continue;
504 }
505 if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
506 $summary.= "$oc, ";
507 }
508 }
510 /* Set summary... */
511 if ($summary == ""){
512 $summary= '<i>'._("No ACL settings for this category!").'</i>';
513 } else {
514 $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
515 }
517 $actions ="";
518 if($this->acl_is_readable("")){
519 $actions= "<input class='center' type='image' src='images/lists/edit.png'
520 alt='"._("Edit")."' name='cat_edit_$section' title='".msgPool::editButton(_("category ACL"))."'>";
521 }
522 if($this->acl_is_removeable()){
523 $actions.= "<input class='center' type='image' src='images/lists/trash.png'
524 alt='"._("Delete")."' name='cat_del_$section' title='".msgPool::delButton(_("category ACL"))."'>";
525 }
527 $field1= array("string" => $dsc, "attach" => "style='width:100px'");
528 $field2= array("string" => $summary);
529 $field3= array("string" => $actions, "attach" => "style='border-right:0px;width:50px'");
530 $aclList->AddEntry(array($field1, $field2, $field3));
531 }
533 $smarty->assign("aclList", $aclList->DrawList());
534 $smarty->assign("aclType", $this->aclType);
535 $smarty->assign("aclFilter", $this->aclFilter);
536 $smarty->assign("aclTypes", $this->aclTypes);
537 $smarty->assign("target", $this->target);
538 $smarty->assign("targets", $this->targets);
540 /* Assign possible target types */
541 $smarty->assign("targets", $this->targets);
542 foreach ($this->attributes as $attr){
543 $smarty->assign($attr, $this->$attr);
544 }
547 /* Generate list */
548 $tmp= array();
549 foreach (array("user" => "users", "group" => "groups") as $field => $arr){
550 if ($this->target == $field){
551 foreach ($this->$arr as $key => $value){
552 if (!isset($this->recipients[$key])){
553 $tmp[$key]= $value;
554 }
555 }
556 }
557 }
558 $smarty->assign('sources', $tmp);
559 $smarty->assign('recipients', $this->recipients);
561 /* Acl selector if scope is base */
562 if ($this->aclType == 'base'){
563 $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
564 }
566 /* Role selector if scope is base */
567 if ($this->aclType == 'role'){
568 $smarty->assign('roleSelector', "Role selector");#, $this->buildRoleSelector($this->myAclObjects));
569 $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
570 }
571 }
573 if ($this->dialogState == 'edit'){
574 $smarty->assign('headline', sprintf(_("Edit ACL for '%s' - scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
576 /* Collect objects for selected category */
577 foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
578 if ($idx == 0){
579 continue;
580 }
581 $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
582 }
583 if ($this->aclObject == 'all'){
584 $aclObjects['all']= _("All objects in current subtree");
585 }
587 /* Role selector if scope is base */
588 if ($this->aclType == 'role'){
589 $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
590 } else {
591 $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
592 }
593 }
595 /* Show main page */
596 $smarty->assign("dialogState", $this->dialogState);
598 /* Assign acls */
599 $smarty->assign("acl_createable",$this->acl_is_createable());
600 $smarty->assign("acl_writeable" ,$this->acl_is_writeable(""));
601 $smarty->assign("acl_readable" ,$this->acl_is_readable(""));
602 $smarty->assign("acl_removeable",$this->acl_is_removeable());
604 return ($smarty->fetch (get_template_path('acl.tpl')));
605 }
608 function sort_by_priority($list)
609 {
610 $tmp= session::global_get('plist');
611 $plist= $tmp->info;
612 asort($plist);
613 $newSort = array();
615 foreach($list as $name => $translation){
616 $na = preg_replace("/^.*\//","",$name);
617 $prio = 0;
618 if(isset($plist[$na]['plPriority'])){
619 $prio= $plist[$na]['plPriority'] ;
620 }
622 $newSort[$name] = $prio;
623 }
625 asort($newSort);
627 $ret = array();
628 foreach($newSort as $name => $prio){
629 $ret[$name] = $list[$name];
630 }
631 return($ret);
632 }
635 function buildRoleSelector($list)
636 {
637 $D_List =new divSelectBox("Acl_Roles");
639 $selected = $this->aclContents;
640 if(!is_string($this->aclContents) || !isset($list[$this->aclContents])){
641 $selected = key($list);
642 }
644 $str ="";
645 foreach($list as $dn => $values){
647 if($dn == $selected){
648 $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."' checked>";
649 }else{
650 $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."'>";
651 }
653 $field1 = array("string" => $option) ;
654 $field2 = array("string" => $values['cn'], "attach" => "style='width:200px;'") ;
655 $field3 = array("string" => $values['description'],"attach" => "style='border-right:0px;'") ;
657 $D_List->AddEntry(array($field1,$field2,$field3));
658 }
659 return($D_List->DrawList());
660 }
663 function buildAclSelector($list)
664 {
665 $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
666 $cols= 3;
667 $tmp= session::global_get('plist');
668 $plist= $tmp->info;
669 asort($plist);
671 /* Add select all/none buttons */
672 $style = "style='width:100px;'";
674 if($this->acl_is_writeable("")){
675 $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
676 $display .= "<input ".$style." type='button' name='toggle_all_move' onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
677 $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
678 $display .= "<input ".$style." type='button' name='toggle_all_read' onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
679 $display .= "<input ".$style." type='button' name='toggle_all_write' onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
681 $display .= "<input ".$style." type='button' name='toggle_all_sub_read' onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
682 $display .= "<input ".$style." type='button' name='toggle_all_sub_write' onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
684 $display .= "<br>";
686 $style = "style='width:50px;'";
687 $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
688 $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
689 $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
690 $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
691 $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
692 $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
693 $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
694 $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
695 $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
696 $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
698 $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
699 $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
700 $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
701 $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
702 }
704 /* Build general objects */
705 $list =$this->sort_by_priority($list);
706 foreach ($list as $key => $name){
708 /* Create sub acl if it does not exist */
709 if (!isset($this->aclContents[$key])){
710 $this->aclContents[$key]= array();
711 $this->aclContents[$key][0]= '';
712 }
713 $currentAcl= $this->aclContents[$key];
715 /* Get the overall plugin acls
716 */
717 $overall_acl ="";
718 if(isset($currentAcl[0])){
719 $overall_acl = $currentAcl[0];
720 }
722 /* Object header */
723 $tname= preg_replace("/[^a-z0-9]/i","_",$name);
724 if(session::global_get('js')) {
725 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
726 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
727 "\n <tr>".
728 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
729 "\n <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
730 "\n <input type='button' onclick='divtoggle(\"$tname\");' value='"._("Show/hide advanced settings")."' /></td>".
731 "\n </tr>";
732 } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
733 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
734 "\n <tr>".
735 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
736 "\n <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
737 "\n <input type='button' onclick='divtoggle(\"$tname\");' value='"._("Show/hide advanced settings")."' /></td>".
738 "\n </tr>";
739 } else {
740 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
741 "\n <tr>".
742 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
743 "\n </tr>";
744 }
745 } else {
746 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
747 "\n <tr>".
748 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
749 "\n </tr>";
750 }
752 /* Generate options */
753 $spc= " ";
754 $options= $this->mkchkbx($key."_0_c", _("Create objects"), preg_match('/c/', $overall_acl)).$spc;
755 $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $overall_acl)).$spc;
756 $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $overall_acl)).$spc;
757 if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
758 $options.= $this->mkchkbx($key."_0_s", _("Grant permission to owner"), preg_match('/s/', $overall_acl)).$spc;
759 }
761 /* Global options */
762 $more_options= $this->mkchkbx($key."_0_r", _("read"), preg_match('/r/', $overall_acl)).$spc;
763 $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $overall_acl));
765 $display.= "\n <tr>".
766 "\n <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
767 "\n <td style='background-color:#D4D4D4'> "._("Complete object").": $more_options</td>".
768 "\n </tr>";
770 /* Walk through the list of attributes */
771 $cnt= 1;
772 $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
773 # asort($splist);
774 if(session::global_get('js')) {
775 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
776 $display.= "\n <tr id='tr_$tname' style='vertical-align:top;height:0px;'>".
777 "\n <td colspan=".$cols.">".
778 "\n <div id='$tname' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
779 "\n <table style='width:100%;'>";
780 } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
781 $display.= "\n <tr id='tr_$tname' style='vertical-align:top;height:0px;'>".
782 "\n <td colspan=".$cols.">".
783 "\n <div id='$tname' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
784 "\n <table style='width:100%;'>";
785 }
786 }
787 foreach($splist as $attr => $dsc){
789 /* Skip pl* attributes, they are internal... */
790 if (preg_match('/^pl[A-Z]+.*$/', $attr)){
791 continue;
792 }
794 /* Open table row */
795 if ($cnt == 1){
796 $display.= "\n <tr>";
797 }
799 /* Close table row */
800 if ($cnt == $cols){
801 $cnt= 1;
802 $rb= "";
803 $end= "\n </tr>";
804 } else {
805 $cnt++;
806 $rb= "border-right:1px solid #A0A0A0;";
807 $end= "";
808 }
810 /* Collect list of attributes */
811 $state= "";
812 if (isset($currentAcl[$attr])){
813 $state= $currentAcl[$attr];
814 }
815 $display.= "\n <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
816 "\n <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
817 }
819 /* Fill missing td's if needed */
820 if (--$cnt != $cols && $cnt != 0){
821 $display.= str_repeat("\n <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'> </td>", $cols-$cnt);
822 }
824 if(session::global_get('js')) {
825 if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
826 $display.= "\n </table>".
827 "\n </div>".
828 "\n </td>".
829 "\n </tr>";
830 }
831 }
833 $display.= "\n</table><br />\n";
834 }
836 return ($display);
837 }
840 function mkchkbx($name, $text, $state= FALSE)
841 {
842 $state= $state?"checked":"";
843 if($this->acl_is_writeable("")){
844 $tname= preg_replace("/[^a-z0-9]/i","_",$name);
845 return "\n <input id='acl_$tname' type=checkbox name='acl_$name' $state>".
846 "\n <label for='acl_$tname'>$text</label>";
847 }else{
848 return "\n <input type='checkbox' disabled name='dummy_".microtime(1)."' $state>$text";
849 }
850 }
853 function mkrwbx($name, $state= "")
854 {
855 $rstate= preg_match('/r/', $state)?'checked':'';
856 $wstate= preg_match('/w/', $state)?'checked':'';
857 $tname= preg_replace("/[^a-z0-9]/i","_",$name);
859 if($this->acl_is_writeable("")){
860 return ("\n <input id='acl_".$tname."_r' type=checkbox name='acl_${name}_r' $rstate>".
861 "\n <label for='acl_".$tname."_r'>"._("read")."</label>".
862 "\n <input id='acl_".$tname."_w' type=checkbox name='acl_${name}_w' $wstate>".
863 "\n <label for='acl_".$tname."_w'>"._("write")."</label>");
864 }else{
865 return ("\n <input disabled type=checkbox name='dummy_".microtime(1)."' $rstate>"._("read").
866 "\n <input disabled type=checkbox name='dummy_".microtime(1)."' $wstate>"._("write"));
867 }
868 }
871 static function explodeACL($acl)
872 {
874 $list= split(':', $acl);
875 if(count($list) == 5){
876 list($index, $type,$member,$permission,$filter)= $list;
877 $filter = base64_decode($filter);
878 }else{
879 $filter = "";
880 list($index, $type,$member,$permission)= $list;
881 }
883 $a= array( $index => array("type" => $type,
884 "filter"=> $filter,
885 "members" => acl::extractMembers($acl,$type == "role")));
887 /* Handle different types */
888 switch ($type){
890 case 'psub':
891 case 'sub':
892 case 'one':
893 case 'base':
894 $a[$index]['acl']= acl::extractACL($acl);
895 break;
897 case 'role':
898 $a[$index]['acl']= base64_decode(preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl));
899 break;
901 case 'reset':
902 break;
904 default:
905 msg_dialog::display(_("Internal error"), sprintf(_("Unkown ACL type '%s'!"), $type), ERROR_DIALOG);
906 $a= array();
907 }
908 return ($a);
909 }
912 static function extractMembers($acl,$role = FALSE)
913 {
914 global $config;
915 $a= array();
917 /* Rip acl off the string, seperate by ',' and place it in an array */
918 if($role){
919 $ms= preg_replace('/^[^:]+:[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
920 }else{
921 $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
922 }
923 if ($ms == $acl){
924 return $a;
925 }
926 $ma= split(',', $ms);
928 /* Decode dn's, fill with informations from LDAP */
929 $ldap= $config->get_ldap_link();
930 foreach ($ma as $memberdn){
931 $dn= base64_decode($memberdn);
932 $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
934 /* Found entry... */
935 if ($ldap->count()){
936 $attrs= $ldap->fetch();
937 if (in_array_ics('gosaAccount', $attrs['objectClass'])){
938 $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
939 } else {
940 $a['G:'.$dn]= $attrs['cn'][0];
941 if (isset($attrs['description'][0])){
942 $a['G:'.$dn].= " [".$attrs['description'][0]."]";
943 }
944 }
946 /* ... or not */
947 } else {
948 $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
949 }
950 }
952 return ($a);
953 }
956 static function extractACL($acl)
957 {
958 /* Rip acl off the string, seperate by ',' and place it in an array */
959 $as= preg_replace('/^[^:]+:[^:]+:[^:]*:([^:]*).*$/', '\1', $acl);
960 $aa= split(',', $as);
961 $a= array();
963 /* Dis-assemble single ACLs */
964 foreach($aa as $sacl){
966 /* Dis-assemble field ACLs */
967 $ao= split('#', $sacl);
968 $gobject= "";
969 foreach($ao as $idx => $ssacl){
971 /* First is department with global acl */
972 $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
973 $gacl= preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
974 if ($idx == 0){
975 /* Create hash for this object */
976 $gobject= $object;
977 $a[$gobject]= array();
979 /* Append ACL if set */
980 if ($gacl != ""){
981 $a[$gobject]= array($gacl);
982 }
983 } else {
985 /* All other entries get appended... */
986 list($field, $facl)= split(';', $ssacl);
987 $a[$gobject][$field]= $facl;
988 }
990 }
991 }
993 return ($a);
994 }
997 function assembleAclSummary($entry)
998 {
999 $summary= "";
1001 /* Summarize ACL */
1002 if (isset($entry['acl'])){
1003 $acl= "";
1005 if($entry['type'] == "role"){
1007 if(isset($this->roles[$entry['acl']])){
1008 $summary.= sprintf(_("Role: %s"), $this->roles[$entry['acl']]['cn']);
1009 }else{
1010 $summary.= sprintf(_("Role: %s"), "<i>"._("unknown role")."</i>");
1011 }
1012 }else{
1013 foreach ($entry['acl'] as $name => $object){
1014 if (count($object)){
1015 $acl.= "$name, ";
1016 }
1017 }
1018 $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
1019 }
1020 }
1022 /* Summarize members */
1023 if ($summary != ""){
1024 $summary.= ", ";
1025 }
1026 if (count($entry['members'])){
1027 $summary.= _("Members").": ";
1028 foreach ($entry['members'] as $cn){
1029 $cn= preg_replace('/ \[.*$/', '', $cn);
1030 $summary.= $cn.", ";
1031 }
1032 } else {
1033 $summary.= _("ACL takes effect for all users");
1034 }
1036 return (preg_replace('/, $/', '', $summary));
1037 }
1040 function loadAclEntry($new= FALSE)
1041 {
1042 /* New entry gets presets... */
1043 if ($new){
1044 $this->aclType= 'base';
1045 $this->aclFilter= "";
1046 $this->recipients= array();
1047 $this->aclContents= array();
1048 } else {
1049 $acl= $this->gosaAclEntry[$this->currentIndex];
1050 $this->aclType= $acl['type'];
1051 $this->recipients= $acl['members'];
1052 $this->aclContents= $acl['acl'];
1053 $this->aclFilter= $acl['filter'];
1054 }
1056 $this->wasNewEntry= $new;
1057 }
1060 function aclPostHandler()
1061 {
1062 if (isset($_POST['save_acl'])){
1063 $this->save();
1064 return TRUE;
1065 }
1067 return FALSE;
1068 }
1071 function PrepareForCopyPaste($source)
1072 {
1073 plugin::PrepareForCopyPaste($source);
1075 $dn = $source['dn'];
1076 $acl_c = new acl($this->config, $this->parent,$dn);
1077 $this->gosaAclEntry = $acl_c->gosaAclEntry;
1078 }
1081 function save()
1082 {
1083 /* Assemble ACL's */
1084 $tmp_acl= array();
1086 foreach ($this->gosaAclEntry as $prio => $entry){
1087 $final= "";
1088 $members= "";
1089 if (isset($entry['members'])){
1090 foreach ($entry['members'] as $key => $dummy){
1091 $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
1092 }
1093 }
1095 if($entry['type'] != "role"){
1096 $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
1097 }else{
1098 $final= $prio.":".$entry['type'].":".base64_encode($entry['acl']).":".preg_replace('/,$/', '', $members);
1099 }
1101 /* ACL's if needed */
1102 if ($entry['type'] != "reset" && $entry['type'] != "role"){
1103 $acl= ":";
1104 if (isset($entry['acl'])){
1105 foreach ($entry['acl'] as $object => $contents){
1107 /* Only save, if we've some contents in there... */
1108 if (count($contents)){
1109 $acl.= $object.";";
1111 foreach($contents as $attr => $permission){
1113 /* First entry? Its the one for global settings... */
1114 if ($attr == '0'){
1115 $acl.= $permission;
1116 } else {
1117 $acl.= '#'.$attr.';'.$permission;
1118 }
1120 }
1121 $acl.= ',';
1122 }
1124 }
1125 }
1126 $final.= preg_replace('/,$/', '', $acl);
1127 }
1129 /* Append additional filter options
1130 */
1131 if(!empty($entry['filter'])){
1132 $final .= ":".base64_encode($entry['filter']);
1133 }
1135 $tmp_acl[]= $final;
1136 }
1138 /* Call main method */
1139 plugin::save();
1141 /* Finally (re-)assign it... */
1142 $this->attrs['gosaAclEntry']= $tmp_acl;
1144 /* Remove acl from this entry if it is empty... */
1145 if (!count($tmp_acl)){
1146 /* Remove attribute */
1147 if ($this->initially_was_account){
1148 $this->attrs['gosaAclEntry']= array();
1149 } else {
1150 if (isset($this->attrs['gosaAclEntry'])){
1151 unset($this->attrs['gosaAclEntry']);
1152 }
1153 }
1155 /* Remove object class */
1156 $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
1157 }
1159 /* Do LDAP modifications */
1160 $ldap= $this->config->get_ldap_link();
1161 $ldap->cd($this->dn);
1162 $this->cleanup();
1163 $ldap->modify ($this->attrs);
1165 if(count($this->attrs)){
1166 new log("modify","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1167 }
1169 if (!$ldap->success()){
1170 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1171 }
1173 /* Refresh users ACLs */
1174 $ui= get_userinfo();
1175 $ui->loadACL();
1176 session::global_set('ui',$ui);
1177 }
1180 function remove_from_parent()
1181 {
1182 plugin::remove_from_parent();
1184 /* include global link_info */
1185 $ldap= $this->config->get_ldap_link();
1187 $ldap->cd($this->dn);
1188 $this->cleanup();
1189 $ldap->modify ($this->attrs);
1191 new log("remove","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1193 /* Optionally execute a command after we're done */
1194 $this->handle_post_events("remove",array("uid" => $this->uid));
1195 }
1198 /* Return plugin informations for acl handling */
1199 static function plInfo()
1200 {
1201 return (array(
1202 "plShortName" => _("ACL"),
1203 "plDescription" => _("ACL")." ("._("Access control list").")",
1204 "plSelfModify" => FALSE,
1205 "plDepends" => array(),
1206 "plPriority" => 0,
1207 "plSection" => array("administration"),
1208 "plCategory" => array("acl" => array("description" => _("ACL")." & "._("ACL roles"),
1209 "objectClass" => array("gosaAcl","gosaRole"))),
1210 "plProvidedAcls"=> array(
1211 // "cn" => _("Role name"),
1212 // "description" => _("Role description")
1213 )
1215 ));
1216 }
1219 /* Remove acls defined for $src */
1220 function remove_acl()
1221 {
1222 $this->remove_acl_for_dn($this->dn);
1223 }
1226 /* Remove acls defined for $src */
1227 function remove_acl_for_dn($src = "")
1228 {
1229 if($src == ""){
1230 $src = $this->dn;
1231 }
1232 $ldap = $this->config->get_ldap_link();
1233 $ldap->cd($this->config->current['BASE']);
1234 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1235 while($attrs = $ldap->fetch()){
1236 $acl = new acl($this->config,$this->parent,$attrs['dn']);
1237 foreach($acl->gosaAclEntry as $id => $entry){
1238 foreach($entry['members'] as $m_id => $member){
1239 if($m_id == "U:".$src){
1240 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1241 gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Removed acl for user %s on object %s.",$src,$attrs['dn']));
1242 }
1243 if($m_id == "G:".$src){
1244 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1245 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for group %s on object %s.",$src,$attrs['dn']));
1246 }
1247 }
1248 }
1249 $acl -> save();
1250 }
1251 }
1253 function update_acl_membership($src,$dst)
1254 {
1255 $ldap = $this->config->get_ldap_link();
1256 $ldap->cd($this->config->current['BASE']);
1257 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1258 while($attrs = $ldap->fetch()){
1259 $acl = new acl($this->config,$this->parent,$attrs['dn']);
1260 foreach($acl->gosaAclEntry as $id => $entry){
1261 foreach($entry['members'] as $m_id => $member){
1262 if($m_id == "U:".$src){
1263 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1264 $new = "U:".$dst;
1265 $acl->gosaAclEntry[$id]['members'][$new] = $new;
1266 gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Updated acl for user %s on object %s.",$src,$attrs['dn']));
1267 }
1268 if($m_id == "G:".$src){
1269 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1270 $new = "G:".$dst;
1271 $acl->gosaAclEntry[$id]['members'][$new] = $new;
1272 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Updated acl for group %s on object %s.",$src,$attrs['dn']));
1273 }
1274 }
1275 }
1276 $acl -> save();
1277 }
1278 }
1280 }
1282 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1283 ?>