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();
55 function acl (&$config, $parent, $dn= NULL)
56 {
57 /* Include config object */
58 plugin::plugin($config, $dn);
60 /* Load ACL's */
61 $this->gosaAclEntry= array();
62 if (isset($this->attrs['gosaAclEntry'])){
63 for ($i= 0; $i<$this->attrs['gosaAclEntry']['count']; $i++){
64 $acl= $this->attrs['gosaAclEntry'][$i];
65 $this->gosaAclEntry= array_merge($this->gosaAclEntry, acl::explodeACL($acl));
66 }
67 }
68 ksort($this->gosaAclEntry);
70 /* Save parent - we've to know more about it than other plugins... */
71 $this->parent= &$parent;
73 /* Container? */
74 if (preg_match('/^(o|ou|c|l|dc)=/i', $dn)){
75 $this->isContainer= TRUE;
76 }
78 /* Users */
79 $ui= get_userinfo();
80 $tag= $ui->gosaUnitTag;
81 $ldap= $config->get_ldap_link();
82 $ldap->cd($config->current['BASE']);
83 if ($tag == ""){
84 $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
85 } else {
86 $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
87 }
88 while ($attrs= $ldap->fetch()){
89 $this->users['U:'.$attrs['dn']]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
90 }
91 ksort($this->users);
93 /* Groups */
94 $ldap->cd($config->current['BASE']);
95 if ($tag == ""){
96 $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
97 } else {
98 $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
99 }
100 while ($attrs= $ldap->fetch()){
101 $dsc= "";
102 if (isset($attrs['description'][0])){
103 $dsc= $attrs['description'][0];
104 }
105 $this->groups['G:'.$attrs['dn']]= $attrs['cn'][0].' ['.$dsc.']';
106 }
107 ksort($this->groups);
109 /* Roles */
110 $ldap->cd($config->current['BASE']);
111 # if ($tag == ""){
112 $ldap->search('(objectClass=gosaRole)', array('cn', 'description','gosaAclTemplate','dn'));
113 # } else {
114 # $ldap->search('(&(objectClass=gosaRole)(gosaUnitTag='.$tag.'))', array('cn', 'description','gosaAclTemplate','dn'));
115 # }
116 while ($attrs= $ldap->fetch()){
117 $dsc= "";
118 if (isset($attrs['description'][0])){
119 $dsc= $attrs['description'][0];
120 }
122 $role_id = $attrs['dn'];
124 $this->roles[$role_id]['acls'] =array();
125 for ($i= 0; $i < $attrs['gosaAclTemplate']['count']; $i++){
126 $acl= $attrs['gosaAclTemplate'][$i];
127 $this->roles[$role_id]['acls'] = array_merge($this->roles[$role_id]['acls'],acl::explodeACL($acl));
128 }
129 $this->roles[$role_id]['description'] = $dsc;
130 $this->roles[$role_id]['cn'] = $attrs['cn'][0];
131 }
133 /* Objects */
134 $tmp= session::get('plist');
135 $plist= $tmp->info;
136 $cats = array();
137 if (isset($this->parent) && $this->parent !== NULL){
138 $oc= array();
139 foreach ($this->parent->by_object as $key => $obj){
140 $oc= array_merge($oc, $obj->objectclasses);
141 if(isset($obj->acl_category)){
142 $cats[preg_replace("/\//","",$obj->acl_category)] = preg_replace("/\//","",$obj->acl_category);
143 }
144 }
145 if (in_array_ics('organizationalUnit', $oc)){
146 $this->isContainer= TRUE;
147 }
148 } else {
149 $oc= $this->attrs['objectClass'];
150 }
152 /* Extract available categories from plugin info list */
153 foreach ($plist as $class => $acls){
155 /* Only feed categories */
156 if (isset($acls['plCategory'])){
158 /* Walk through supplied list and feed only translated categories */
159 foreach($acls['plCategory'] as $idx => $data){
161 /* Non numeric index means -> base object containing more informations */
162 if (preg_match('/^[0-9]+$/', $idx)){
163 if (!isset($this->ocMapping[$data])){
164 $this->ocMapping[$data]= array();
165 $this->ocMapping[$data][]= '0';
166 }
168 if(isset($cats[$data])){
169 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
170 }
171 $this->ocMapping[$data][]= $class;
172 } else {
173 if (!isset($this->ocMapping[$idx])){
174 $this->ocMapping[$idx]= array();
175 $this->ocMapping[$idx][]= '0';
176 }
177 $this->ocMapping[$idx][]= $class;
178 $this->aclObjects[$idx]= $data['description'];
180 /* Additionally filter the classes we're interested in in "self edit" mode */
181 if (is_array($data['objectClass'])){
182 foreach($data['objectClass'] as $objectClass){
183 if (in_array_ics($objectClass, $oc)){
184 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
185 break;
186 }
187 }
188 } else {
189 if (in_array_ics($data['objectClass'], $oc)){
190 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
191 }
192 }
193 }
195 }
196 }
197 }
198 $this->aclObjects['all']= '* '._("All categories");
199 $this->ocMapping['all']= array('0' => 'all');
201 /* Sort categories */
202 asort($this->aclObjects);
204 /* Fill acl types */
205 if ($this->isContainer){
206 $this->aclTypes= array("reset" => _("Reset ACLs"),
207 "one" => _("One level"),
208 "base" => _("Current object"),
209 "sub" => _("Complete subtree"),
210 "psub" => _("Complete subtree (permanent)"),
211 "role" => _("Use ACL defined in role"));
212 } else {
213 $this->aclTypes= array("base" => _("Current object"),
214 "role" => _("Use ACL defined in role"));
215 }
216 asort($this->aclTypes);
217 $this->targets= array("user" => _("Users"), "group" => _("Groups"));
218 asort($this->targets);
220 /* Finally - we want to get saved... */
221 $this->is_account= TRUE;
222 }
225 function execute()
226 {
227 /* Call parent execute */
228 plugin::execute();
230 $tmp= session::get('plist');
231 $plist= $tmp->info;
233 /* Handle posts */
234 if (isset($_POST['new_acl'])){
235 $this->dialogState= 'create';
236 $this->dialog= TRUE;
237 $this->currentIndex= count($this->gosaAclEntry);
238 $this->loadAclEntry(TRUE);
239 }
241 $new_acl= array();
242 $aclDialog= FALSE;
243 $firstedit= FALSE;
244 foreach($_POST as $name => $post){
246 /* Actions... */
247 if (preg_match('/^acl_edit_.*_x/', $name)){
248 $this->dialogState= 'create';
249 $firstedit= TRUE;
250 $this->dialog= TRUE;
251 $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
252 $this->loadAclEntry();
253 continue;
254 }
255 if (preg_match('/^acl_del_.*_x/', $name)){
256 unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
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 }
270 if (preg_match('/^cat_del_.*_x/', $name)){
271 $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
272 foreach ($this->ocMapping[$idx] as $key){
273 unset($this->aclContents["$idx/$key"]);
274 }
275 continue;
276 }
278 /* Sorting... */
279 if (preg_match('/^sortup_.*_x/', $name)){
280 $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
281 if ($index > 0){
282 $tmp= $this->gosaAclEntry[$index];
283 $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
284 $this->gosaAclEntry[$index-1]= $tmp;
285 }
286 continue;
287 }
288 if (preg_match('/^sortdown_.*_x/', $name)){
289 $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
290 if ($index < count($this->gosaAclEntry)-1){
291 $tmp= $this->gosaAclEntry[$index];
292 $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
293 $this->gosaAclEntry[$index+1]= $tmp;
294 }
295 continue;
296 }
298 /* ACL saving... */
299 if (preg_match('/^acl_.*_[^xy]$/', $name)){
300 $aclDialog= TRUE;
301 list($dummy, $object, $attribute, $value)= split('_', $name);
303 /* Skip for detection entry */
304 if ($object == 'dummy') {
305 continue;
306 }
308 /* Ordinary ACLs */
309 if (!isset($new_acl[$object])){
310 $new_acl[$object]= array();
311 }
312 if (isset($new_acl[$object][$attribute])){
313 $new_acl[$object][$attribute].= $value;
314 } else {
315 $new_acl[$object][$attribute]= $value;
316 }
317 }
319 if(isset($_POST['selected_role'])){
320 $this->aclContents = "";
321 $this->aclContents = base64_decode($_POST['selected_role']);
322 }
323 }
325 /* Only be interested in new acl's, if we're in the right _POST place */
326 if ($aclDialog && $this->aclObject != "" && is_array($this->ocMapping[$this->aclObject])){
328 foreach ($this->ocMapping[$this->aclObject] as $oc){
330 if(isset($this->aclContents[$oc]) && is_array($this->aclContents)){
331 unset($this->aclContents[$oc]);
332 unset($this->aclContents[$this->aclObject.'/'.$oc]);
333 }else{
334 # trigger_error("Huhm?");
335 }
336 if (isset($new_acl[$oc]) && is_array($new_acl)){
337 $this->aclContents[$oc]= $new_acl[$oc];
338 }
339 if (isset($new_acl[$this->aclObject.'/'.$oc]) && is_array($new_acl)){
340 $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
341 }
342 }
343 }
345 /* Save new acl in case of base edit mode */
346 if ($this->aclType == 'base' && !$firstedit){
347 $this->aclContents= $new_acl;
348 }
350 /* Cancel new acl? */
351 if (isset($_POST['cancel_new_acl'])){
352 $this->dialogState= 'head';
353 $this->dialog= FALSE;
354 if ($this->wasNewEntry){
355 unset ($this->gosaAclEntry[$this->currentIndex]);
356 }
357 }
359 /* Store ACL in main object? */
360 if (isset($_POST['submit_new_acl'])){
361 $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
362 $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
363 $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
364 $this->gosaAclEntry[$this->currentIndex]['filter']= $this->aclFilter;
365 $this->dialogState= 'head';
366 $this->dialog= FALSE;
367 }
369 /* Cancel edit acl? */
370 if (isset($_POST['cancel_edit_acl'])){
371 $this->dialogState= 'create';
372 foreach ($this->ocMapping[$this->aclObject] as $oc){
373 if (isset($this->savedAclContents[$oc])){
374 $this->aclContents[$oc]= $this->savedAclContents[$oc];
375 }
376 }
377 }
379 /* Save edit acl? */
380 if (isset($_POST['submit_edit_acl'])){
381 $this->dialogState= 'create';
382 }
384 /* Add acl? */
385 if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
386 $this->dialogState= 'edit';
387 $this->savedAclContents= array();
388 foreach ($this->ocMapping[$this->aclObject] as $oc){
389 if (isset($this->aclContents[$oc])){
390 $this->savedAclContents[$oc]= $this->aclContents[$oc];
391 }
392 }
393 }
395 /* Add to list? */
396 if (isset($_POST['add']) && isset($_POST['source'])){
397 foreach ($_POST['source'] as $key){
398 if ($this->target == 'user'){
399 $this->recipients[$key]= $this->users[$key];
400 }
401 if ($this->target == 'group'){
402 $this->recipients[$key]= $this->groups[$key];
403 }
404 }
405 ksort($this->recipients);
406 }
408 /* Remove from list? */
409 if (isset($_POST['del']) && isset($_POST['recipient'])){
410 foreach ($_POST['recipient'] as $key){
411 unset($this->recipients[$key]);
412 }
413 }
415 /* Save common values */
416 foreach (array("aclType","aclFilter", "aclObject", "target") as $key){
417 if (isset($_POST[$key])){
418 $this->$key= validate($_POST[$key]);
419 }
420 }
422 /* Create templating instance */
423 $smarty= get_smarty();
425 if ($this->dialogState == 'head'){
426 /* Draw list */
427 $aclList= new divSelectBox("aclList");
428 $aclList->SetHeight(450);
430 /* Fill in entries */
431 foreach ($this->gosaAclEntry as $key => $entry){
432 $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:150px'");
433 $field2= array("string" => $this->assembleAclSummary($entry));
434 $action= "<input type='image' name='sortup_$key' alt='up' title='"._("Up")."' src='images/lists/sort-up.png' align='top'>";
435 $action.= "<input type='image' name='sortdown_$key' alt='down' title='"._("Down")."' src='images/lists/sort-down.png'>";
436 $action.= "<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='acl_edit_$key' title='".msgPool::editButton(_("ACL"))."'>";
437 $action.= "<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='acl_del_$key' title='".msgPool::delButton(_("ACL"))."'>";
439 $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
440 $aclList->AddEntry(array($field1, $field2, $field3));
441 }
443 $smarty->assign("aclList", $aclList->DrawList());
444 }
446 if ($this->dialogState == 'create'){
447 /* Draw list */
448 $aclList= new divSelectBox("aclList");
449 $aclList->SetHeight(150);
451 /* Add settings for all categories to the (permanent) list */
452 foreach ($this->aclObjects as $section => $dsc){
453 $summary= "";
454 foreach($this->ocMapping[$section] as $oc){
455 if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
456 $this->aclContents[$oc][0] != ""){
458 $summary.= "$oc, ";
459 continue;
460 }
461 if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"])){
462 $summary.= "$oc, ";
463 continue;
464 }
465 if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
466 $summary.= "$oc, ";
467 }
468 }
470 /* Set summary... */
471 if ($summary == ""){
472 $summary= '<i>'._("No ACL settings for this category!").'</i>';
473 } else {
474 $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
475 }
477 $field1= array("string" => $dsc, "attach" => "style='width:100px'");
478 $field2= array("string" => $summary);
479 $action= "<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='cat_edit_$section' title='".msgPool::editButton(_("category ACL"))."'>";
480 $action.= "<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='cat_del_$section' title='".msgPool::delButton(_("category ACL"))."'>";
481 $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px'");
482 $aclList->AddEntry(array($field1, $field2, $field3));
483 }
485 $smarty->assign("aclList", $aclList->DrawList());
486 $smarty->assign("aclType", $this->aclType);
487 $smarty->assign("aclFilter", $this->aclFilter);
488 $smarty->assign("aclTypes", $this->aclTypes);
489 $smarty->assign("target", $this->target);
490 $smarty->assign("targets", $this->targets);
492 /* Assign possible target types */
493 $smarty->assign("targets", $this->targets);
494 foreach ($this->attributes as $attr){
495 $smarty->assign($attr, $this->$attr);
496 }
499 /* Generate list */
500 $tmp= array();
501 foreach (array("user" => "users", "group" => "groups") as $field => $arr){
502 if ($this->target == $field){
503 foreach ($this->$arr as $key => $value){
504 if (!isset($this->recipients[$key])){
505 $tmp[$key]= $value;
506 }
507 }
508 }
509 }
510 $smarty->assign('sources', $tmp);
511 $smarty->assign('recipients', $this->recipients);
513 /* Acl selector if scope is base */
514 if ($this->aclType == 'base'){
515 $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
516 }
518 /* Role selector if scope is base */
519 if ($this->aclType == 'role'){
520 $smarty->assign('roleSelector', "Role selector");#, $this->buildRoleSelector($this->myAclObjects));
521 $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
522 }
523 }
525 if ($this->dialogState == 'edit'){
526 $smarty->assign('headline', sprintf(_("Edit ACL for '%s' - scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
528 /* Collect objects for selected category */
529 foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
530 if ($idx == 0){
531 continue;
532 }
533 $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
534 }
535 if ($this->aclObject == 'all'){
536 $aclObjects['all']= _("All objects in current subtree");
537 }
539 /* Role selector if scope is base */
540 if ($this->aclType == 'role'){
541 $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
542 } else {
543 $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
544 }
545 }
547 /* Show main page */
548 $smarty->assign("dialogState", $this->dialogState);
550 return ($smarty->fetch (get_template_path('acl.tpl')));
551 }
554 function sort_by_priority($list)
555 {
556 $tmp= session::get('plist');
557 $plist= $tmp->info;
558 asort($plist);
559 $newSort = array();
561 foreach($list as $name => $translation){
562 $na = preg_replace("/^.*\//","",$name);
563 $prio = 0;
564 if(isset($plist[$na]['plPriority'])){
565 $prio= $plist[$na]['plPriority'] ;
566 }
568 $newSort[$name] = $prio;
569 }
571 asort($newSort);
573 $ret = array();
574 foreach($newSort as $name => $prio){
575 $ret[$name] = $list[$name];
576 }
577 return($ret);
578 }
581 function buildRoleSelector($list)
582 {
583 $D_List =new divSelectBox("Acl_Roles");
585 $selected = $this->aclContents;
586 if(!is_string($this->aclContents) || !isset($list[$this->aclContents])){
587 $selected = key($list);
588 }
590 $str ="";
591 foreach($list as $dn => $values){
593 if($dn == $selected){
594 $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."' checked>";
595 }else{
596 $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."'>";
597 }
599 $field1 = array("string" => $option) ;
600 $field2 = array("string" => $values['cn'], "attach" => "style='width:200px;'") ;
601 $field3 = array("string" => $values['description'],"attach" => "style='border-right:0px;'") ;
603 $D_List->AddEntry(array($field1,$field2,$field3));
604 }
605 return($D_List->DrawList());
606 }
609 function buildAclSelector($list)
610 {
611 $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
612 $cols= 3;
613 $tmp= session::get('plist');
614 $plist= $tmp->info;
615 asort($plist);
617 /* Add select all/none buttons */
618 $style = "style='width:100px;'";
620 $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
621 $display .= "<input ".$style." type='button' name='toggle_all_move' onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
622 $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
623 $display .= "<input ".$style." type='button' name='toggle_all_read' onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
624 $display .= "<input ".$style." type='button' name='toggle_all_write' onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
626 $display .= "<input ".$style." type='button' name='toggle_all_sub_read' onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
627 $display .= "<input ".$style." type='button' name='toggle_all_sub_write' onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
629 $display .= "<br>";
631 $style = "style='width:50px;'";
632 $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
633 $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
634 $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
635 $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
636 $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
637 $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
638 $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
639 $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
640 $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
641 $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
643 $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
644 $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
645 $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
646 $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
648 /* Build general objects */
649 $list =$this->sort_by_priority($list);
650 foreach ($list as $key => $name){
652 /* Create sub acl if it does not exist */
653 if (!isset($this->aclContents[$key])){
654 $this->aclContents[$key]= array();
655 $this->aclContents[$key][0]= '';
656 }
657 $currentAcl= $this->aclContents[$key];
659 /* Get the overall plugin acls
660 */
661 $overall_acl ="";
662 if(isset($currentAcl[0])){
663 $overall_acl = $currentAcl[0];
664 }
666 /* Object header */
667 if(session::get('js')) {
668 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
669 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
670 "\n <tr>".
671 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
672 "\n <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
673 "\n <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/hide advanced settings")."' /></td>".
674 "\n </tr>";
675 } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
676 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
677 "\n <tr>".
678 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
679 "\n <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
680 "\n <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/hide advanced settings")."' /></td>".
681 "\n </tr>";
682 } else {
683 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
684 "\n <tr>".
685 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
686 "\n </tr>";
687 }
688 } else {
689 $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
690 "\n <tr>".
691 "\n <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
692 "\n </tr>";
693 }
695 /* Generate options */
696 $spc= " ";
697 if ($this->isContainer && $this->aclType != 'base'){
698 $options= $this->mkchkbx($key."_0_c", _("Create objects"), preg_match('/c/', $overall_acl)).$spc;
699 $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $overall_acl)).$spc;
700 $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $overall_acl)).$spc;
701 if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
702 $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $overall_acl)).$spc;
703 }
704 } else {
705 $options= $this->mkchkbx($key."_0_m", _("Move object"), preg_match('/m/', $overall_acl)).$spc;
706 $options.= $this->mkchkbx($key."_0_d", _("Remove object"), preg_match('/d/', $overall_acl)).$spc;
707 if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
708 $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $overall_acl)).$spc;
709 }
710 }
712 /* Global options */
713 $more_options= $this->mkchkbx($key."_0_r", _("read"), preg_match('/r/', $overall_acl)).$spc;
714 $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $overall_acl));
716 $display.= "\n <tr>".
717 "\n <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
718 "\n <td style='background-color:#D4D4D4'> "._("Complete object").": $more_options</td>".
719 "\n </tr>";
721 /* Walk through the list of attributes */
722 $cnt= 1;
723 $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
724 asort($splist);
725 if(session::get('js')) {
726 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
727 $display.= "\n <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
728 "\n <td colspan=".$cols.">".
729 "\n <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
730 "\n <table style='width:100%;'>";
731 } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
732 $display.= "\n <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
733 "\n <td colspan=".$cols.">".
734 "\n <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
735 "\n <table style='width:100%;'>";
736 }
737 }
738 foreach($splist as $attr => $dsc){
740 /* Skip pl* attributes, they are internal... */
741 if (preg_match('/^pl[A-Z]+.*$/', $attr)){
742 continue;
743 }
745 /* Open table row */
746 if ($cnt == 1){
747 $display.= "\n <tr>";
748 }
750 /* Close table row */
751 if ($cnt == $cols){
752 $cnt= 1;
753 $rb= "";
754 $end= "\n </tr>";
755 } else {
756 $cnt++;
757 $rb= "border-right:1px solid #A0A0A0;";
758 $end= "";
759 }
761 /* Collect list of attributes */
762 $state= "";
763 if (isset($currentAcl[$attr])){
764 $state= $currentAcl[$attr];
765 }
766 $display.= "\n <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
767 "\n <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
768 }
770 /* Fill missing td's if needed */
771 if (--$cnt != $cols && $cnt != 0){
772 $display.= str_repeat("\n <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'> </td>", $cols-$cnt);
773 }
775 if(session::get('js')) {
776 if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
777 $display.= "\n </table>".
778 "\n </div>".
779 "\n </td>".
780 "\n </tr>";
781 }
782 }
784 $display.= "\n</table><br />\n";
785 }
787 return ($display);
788 }
791 function mkchkbx($name, $text, $state= FALSE)
792 {
793 $state= $state?"checked":"";
794 return "\n <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."' type=checkbox name='acl_$name' $state>".
795 "\n <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."'>$text</label>";
796 }
799 function mkrwbx($name, $state= "")
800 {
801 $rstate= preg_match('/r/', $state)?'checked':'';
802 $wstate= preg_match('/w/', $state)?'checked':'';
803 return ("\n <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r' type=checkbox name='acl_${name}_r' $rstate>".
804 "\n <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r'>"._("read")."</label>".
805 "\n <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w' type=checkbox name='acl_${name}_w' $wstate>".
806 "\n <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w'>"._("write")."</label>");
807 }
810 static function explodeACL($acl)
811 {
813 $list= split(':', $acl);
814 if(count($list) == 5){
815 list($index, $type,$member,$permission,$filter)= $list;
816 $filter = base64_decode($filter);
817 }else{
818 $filter = "";
819 list($index, $type,$member,$permission)= $list;
820 }
822 $a= array( $index => array("type" => $type,
823 "filter"=> $filter,
824 "members" => acl::extractMembers($acl,$type == "role")));
826 /* Handle different types */
827 switch ($type){
829 case 'psub':
830 case 'sub':
831 case 'one':
832 case 'base':
833 $a[$index]['acl']= acl::extractACL($acl);
834 break;
836 case 'role':
837 $a[$index]['acl']= base64_decode(preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl));
838 break;
840 case 'reset':
841 break;
843 default:
844 msg_dialog::display(_("Internal error"), sprintf(_("Unkown ACL type '%s'!"), $type), ERROR_DIALOG);
845 $a= array();
846 }
847 return ($a);
848 }
851 static function extractMembers($acl,$role = FALSE)
852 {
853 global $config;
854 $a= array();
856 /* Rip acl off the string, seperate by ',' and place it in an array */
857 if($role){
858 $ms= preg_replace('/^[^:]+:[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
859 }else{
860 $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
861 }
862 if ($ms == $acl){
863 return $a;
864 }
865 $ma= split(',', $ms);
867 /* Decode dn's, fill with informations from LDAP */
868 $ldap= $config->get_ldap_link();
869 foreach ($ma as $memberdn){
870 $dn= base64_decode($memberdn);
871 $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
873 /* Found entry... */
874 if ($ldap->count()){
875 $attrs= $ldap->fetch();
876 if (in_array_ics('gosaAccount', $attrs['objectClass'])){
877 $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
878 } else {
879 $a['G:'.$dn]= $attrs['cn'][0];
880 if (isset($attrs['description'][0])){
881 $a['G:'.$dn].= " [".$attrs['description'][0]."]";
882 }
883 }
885 /* ... or not */
886 } else {
887 $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
888 }
889 }
891 return ($a);
892 }
895 static function extractACL($acl)
896 {
897 /* Rip acl off the string, seperate by ',' and place it in an array */
898 $as= preg_replace('/^[^:]+:[^:]+:[^:]*:([^:]*).*$/', '\1', $acl);
899 $aa= split(',', $as);
900 $a= array();
902 /* Dis-assemble single ACLs */
903 foreach($aa as $sacl){
905 /* Dis-assemble field ACLs */
906 $ao= split('#', $sacl);
907 $gobject= "";
908 foreach($ao as $idx => $ssacl){
910 /* First is department with global acl */
911 $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
912 $gacl= preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
913 if ($idx == 0){
914 /* Create hash for this object */
915 $gobject= $object;
916 $a[$gobject]= array();
918 /* Append ACL if set */
919 if ($gacl != ""){
920 $a[$gobject]= array($gacl);
921 }
922 } else {
924 /* All other entries get appended... */
925 list($field, $facl)= split(';', $ssacl);
926 $a[$gobject][$field]= $facl;
927 }
929 }
930 }
932 return ($a);
933 }
936 function assembleAclSummary($entry)
937 {
938 $summary= "";
940 /* Summarize ACL */
941 if (isset($entry['acl'])){
942 $acl= "";
944 if($entry['type'] == "role"){
946 if(isset($this->roles[$entry['acl']])){
947 $summary.= sprintf(_("Role: %s"), $this->roles[$entry['acl']]['cn']);
948 }else{
949 $summary.= sprintf(_("Role: %s"), "<i>"._("unknown role")."</i>");
950 }
951 }else{
952 foreach ($entry['acl'] as $name => $object){
953 if (count($object)){
954 $acl.= "$name, ";
955 }
956 }
957 $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
958 }
959 }
961 /* Summarize members */
962 if ($summary != ""){
963 $summary.= ", ";
964 }
965 if (count($entry['members'])){
966 $summary.= _("Members").": ";
967 foreach ($entry['members'] as $cn){
968 $cn= preg_replace('/ \[.*$/', '', $cn);
969 $summary.= $cn.", ";
970 }
971 } else {
972 $summary.= _("ACL takes effect for all users");
973 }
975 return (preg_replace('/, $/', '', $summary));
976 }
979 function loadAclEntry($new= FALSE)
980 {
981 /* New entry gets presets... */
982 if ($new){
983 $this->aclType= 'base';
984 $this->aclFilter= "";
985 $this->recipients= array();
986 $this->aclContents= array();
987 } else {
988 $acl= $this->gosaAclEntry[$this->currentIndex];
989 $this->aclType= $acl['type'];
990 $this->recipients= $acl['members'];
991 $this->aclContents= $acl['acl'];
992 $this->aclFilter= $acl['filter'];
993 }
995 $this->wasNewEntry= $new;
996 }
999 function aclPostHandler()
1000 {
1001 if (isset($_POST['save_acl'])){
1002 $this->save();
1003 return TRUE;
1004 }
1006 return FALSE;
1007 }
1010 function PrepareForCopyPaste($source)
1011 {
1012 plugin::PrepareForCopyPaste($source);
1014 $dn = $source['dn'];
1015 $acl_c = new acl($this->config, $this->parent,$dn);
1016 $this->gosaAclEntry = $acl_c->gosaAclEntry;
1017 }
1020 function save()
1021 {
1022 /* Assemble ACL's */
1023 $tmp_acl= array();
1025 foreach ($this->gosaAclEntry as $prio => $entry){
1026 $final= "";
1027 $members= "";
1028 if (isset($entry['members'])){
1029 foreach ($entry['members'] as $key => $dummy){
1030 $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
1031 }
1032 }
1034 if($entry['type'] != "role"){
1035 $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
1036 }else{
1037 $final= $prio.":".$entry['type'].":".base64_encode($entry['acl']).":".preg_replace('/,$/', '', $members);
1038 }
1040 /* ACL's if needed */
1041 if ($entry['type'] != "reset" && $entry['type'] != "role"){
1042 $acl= ":";
1043 if (isset($entry['acl'])){
1044 foreach ($entry['acl'] as $object => $contents){
1046 /* Only save, if we've some contents in there... */
1047 if (count($contents)){
1048 $acl.= $object.";";
1050 foreach($contents as $attr => $permission){
1052 /* First entry? Its the one for global settings... */
1053 if ($attr == '0'){
1054 $acl.= $permission;
1055 } else {
1056 $acl.= '#'.$attr.';'.$permission;
1057 }
1059 }
1060 $acl.= ',';
1061 }
1063 }
1064 }
1065 $final.= preg_replace('/,$/', '', $acl);
1066 }
1068 /* Append additional filter options
1069 */
1070 if(!empty($entry['filter'])){
1071 $final .= ":".base64_encode($entry['filter']);
1072 }
1074 $tmp_acl[]= $final;
1075 }
1077 /* Call main method */
1078 plugin::save();
1080 /* Finally (re-)assign it... */
1081 $this->attrs['gosaAclEntry']= $tmp_acl;
1083 /* Remove acl from this entry if it is empty... */
1084 if (!count($tmp_acl)){
1085 /* Remove attribute */
1086 if ($this->initially_was_account){
1087 $this->attrs['gosaAclEntry']= array();
1088 } else {
1089 if (isset($this->attrs['gosaAclEntry'])){
1090 unset($this->attrs['gosaAclEntry']);
1091 }
1092 }
1094 /* Remove object class */
1095 $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
1096 }
1098 /* Do LDAP modifications */
1099 $ldap= $this->config->get_ldap_link();
1100 $ldap->cd($this->dn);
1101 $this->cleanup();
1102 $ldap->modify ($this->attrs);
1104 if(count($this->attrs)){
1105 new log("modify","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1106 }
1108 if (!$ldap->success()){
1109 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1110 }
1112 /* Refresh users ACLs */
1113 $ui= get_userinfo();
1114 $ui->loadACL();
1115 session::set('ui',$ui);
1116 }
1119 function remove_from_parent()
1120 {
1121 plugin::remove_from_parent();
1123 /* include global link_info */
1124 $ldap= $this->config->get_ldap_link();
1126 $ldap->cd($this->dn);
1127 $this->cleanup();
1128 $ldap->modify ($this->attrs);
1130 new log("remove","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1132 /* Optionally execute a command after we're done */
1133 $this->handle_post_events("remove",array("uid" => $this->uid));
1134 }
1137 /* Return plugin informations for acl handling */
1138 static function plInfo()
1139 {
1140 return (array(
1141 "plShortName" => _("ACL"),
1142 "plDescription" => _("ACL")._("Access control list").")",
1143 "plSelfModify" => FALSE,
1144 "plDepends" => array(),
1145 "plPriority" => 0,
1146 "plSection" => array("administration"),
1147 "plCategory" => array("acl" => array("description" => _("ACL")." & "._("ACL roles"),
1148 "objectClass" => array("gosaAcl","gosaRole"))),
1149 "plProvidedAcls"=> array(
1150 "cn" => _("Role name"),
1151 "description" => _("Role description"))
1153 ));
1154 }
1157 /* Remove acls defined for $src */
1158 function remove_acl()
1159 {
1160 $this->remove_acl_for_dn($this->dn);
1161 }
1164 /* Remove acls defined for $src */
1165 function remove_acl_for_dn($src = "")
1166 {
1167 if($src == ""){
1168 $src = $this->dn;
1169 }
1170 $ldap = $this->config->get_ldap_link();
1171 $ldap->cd($this->config->current['BASE']);
1172 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1173 while($attrs = $ldap->fetch()){
1174 $acl = new acl($this->config,$this->parent,$attrs['dn']);
1175 foreach($acl->gosaAclEntry as $id => $entry){
1176 foreach($entry['members'] as $m_id => $member){
1177 if($m_id == "U:".$src){
1178 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1179 gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Removed acl for user %s on object %s.",$src,$attrs['dn']));
1180 }
1181 if($m_id == "G:".$src){
1182 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1183 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for group %s on object %s.",$src,$attrs['dn']));
1184 }
1185 }
1186 }
1187 $acl -> save();
1188 }
1189 }
1191 function update_acl_membership($src,$dst)
1192 {
1193 $ldap = $this->config->get_ldap_link();
1194 $ldap->cd($this->config->current['BASE']);
1195 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))",array("gosaAclEntry","dn"));
1196 while($attrs = $ldap->fetch()){
1197 $acl = new acl($this->config,$this->parent,$attrs['dn']);
1198 foreach($acl->gosaAclEntry as $id => $entry){
1199 foreach($entry['members'] as $m_id => $member){
1200 if($m_id == "U:".$src){
1201 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1202 $new = "U:".$dst;
1203 $acl->gosaAclEntry[$id]['members'][$new] = $new;
1204 gosa_log("modify","users/acl",$attrs['dn'],array(),sprintf("Updated acl for user %s on object %s.",$src,$attrs['dn']));
1205 }
1206 if($m_id == "G:".$src){
1207 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
1208 $new = "G:".$dst;
1209 $acl->gosaAclEntry[$id]['members'][$new] = $new;
1210 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Updated acl for group %s on object %s.",$src,$attrs['dn']));
1211 }
1212 }
1213 }
1214 $acl -> save();
1215 }
1216 }
1217 }
1219 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1220 ?>