Code

Fixed locking for acl roles.
[gosa.git] / gosa-core / plugins / admin / acl / class_aclManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class aclManagement extends plugin
24 {
25   /* Plugin definitions */
26   var $plHeadline     = "ACL";
27   var $plDescription  = "Manage access control lists";
29   /* Dialog attributes */
30   var $acltabs              = NULL;
31   var $ui                   = NULL;
32   var $acl                  = "";
33   var $DivListACL           = NULL;
35   var $CopyPasteHandler;
36   var $start_pasting_copied_objects = FALSE;
37   var $dns = array();
39   function aclManagement(&$config, &$ui)
40   {
41     /* Save configuration for internal use */
42     $this->config = &$config;
43     $this->ui     = &$ui;
45     /* Copy & Paste enabled ?*/
46     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
47       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48     }
50     /* Creat dialog object */
51     $this->DivListACL = new divListACL($this->config,$this);
52   }
55   function execute()
56   {
57     /* Call parent execute */
58     plugin::execute();
60     session::set('LOCK_VARS_TO_USE',array("/^item_selected/","/^menu_action/","/^list/","/^id_/","/^list_acl_role_del/","/^list_acl_del/","/^menu_action/"));
62     $smarty       = get_smarty();                 // Smarty instance
63     $s_action     = "";                           // Contains the action to be taken
64     $s_entry      = "";                           // The value for s_action
66     /* Edit entry button pressed? */
67     if( isset($_GET['act']) && $_GET['act'] == "list_edit_entry" ){
68       $s_action= "edit";
69       $s_entry= validate($_GET['id']);
70     }
72     /* Edit entry button pressed? */
73     if( isset($_GET['act']) && $_GET['act'] == "list_edit_role" ){
74       $s_action= "edit_role";
75       $s_entry= validate($_GET['id']);
76     }
78     $types = array( 
79                     "del"       =>"^list_acl_del",
80                     "edit"      =>"^list_acl_edit",
81                     "del_role"  =>"^list_acl_role_del",
82                     "edit_role" =>"^list_acl_role_edit",
83                     "copy"      =>"^copy",
84                     "cut"       =>"^cut",
85                     "copy_multiple"=> "^multiple_copy_objects",
86                     "cut_multiple" => "^multiple_cut_objects",
87                     "editPaste" =>"^editPaste",
88                     "addrole"   =>"^new_acl_role");
90     /* Test relevant POST values */  
91     foreach($_POST as $key => $val){
93       /* Get every possible POST combination and set s_action/s_entry accordingly */
94       foreach($types as $act => $name){
96         if (preg_match("/".$name.".*/", $key)){
97           $s_action= $act;
98           $s_entry= preg_replace("/".$name."_/i", "", $key);
99         }
100       }
101     } 
103     /* Remove coordinate prefix from POST, required by some browsers */
104     $s_entry= preg_replace("/_.$/", "", $s_entry);
106     /* Seperate possibly encoded tab and entry, default to tab "acl" */
107     if(preg_match("/.*-.*/", $s_entry)){
108       $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
109       $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
110     }else{
111       $s_tab= "generic";
112     }
113    
115     /* handle C&P from layers menu */
116     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
117       $s_action = "copy_multiple";
118     }
119     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
120       $s_action = "cut_multiple";
121     }
122     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
123       $s_action = "editPaste";
124     }
126     /* Create options */
127     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "new_acl_role"){
128       $s_action = "new";
129     }
131     /* handle remove from layers menu */
132     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
133       $s_action = "del_multiple";
134     }
136  
137     /********************
138       Copy & Paste Handling  ...
139      ********************/
141     /* Display the copy & paste dialog, if it is currently open */
142     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
143     if($ret){
144       return($ret);
145     }
148     /********************
149      * Add new Role entry 
150      ********************/
152     if(($s_action == "new") && (!isset($this->acltabs->config))){
154       /* Get 'dn' from posted acl, must be unique */
155       $this->dn= "new";
157       /* Check permissions */
158       if(preg_match("/c/",$this->ui->get_permissions($this->DivListACL->selectedBase,"acl/acl"))){
160         /* Register acltabs to trigger edit dialog */
161         $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
162         $this->acltabs->set_acl_base($this->DivListACL->selectedBase);
163       }else{
164         msg_dialog::display(_("Permission error"), msgPool::permCreate(), ERROR_DIALOG);
165       }
166     }
168     /********************
169       Edit existing entry 
170      ********************/
172     if (($s_action=="edit" || $s_action=="edit_role") && (!isset($this->acltabs->config))){
174       /* Get 'dn' from posted acl, must be unique */
175       $this->dn= $this->list[trim($s_entry)]['dn'];
177       /* Check permissions */
178       if(preg_match("/r/",$this->ui->get_permissions($this->dn,"acl/acl"))){
179       
180         /* Check locking, save current plugin in 'back_plugin', so
181            the dialog knows where to return. */
182         if (($acl= get_lock($this->dn)) != ""){
183           return(gen_locked_message ($acl, $this->dn));
184         }
186         /* Lock the current entry, so everyone will get the above dialog */
187         add_lock ($this->dn, $this->ui->dn);
189         /* Register acltabs to trigger edit dialog */ 
190         if($s_action=="edit_role"){
191           $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
192           $this->acltabs-> set_acl_base($this->dn);
193         }else{
194           $this->acltabs= new acltab($this->config, NULL,$this->dn);
195           $this->acltabs-> set_acl_base($this->dn);
196         }
198         /* Set ACL and move DN to the headline */
199         session::set('objectinfo',$this->dn);
200       }else{
201         msg_dialog::display(_("Permission error"), msgPool::permModify(), ERROR_DIALOG);
202       }
203     }
206     /********************
207       Edit canceled 
208      ********************/
209     if(isset($_POST['delete_lock'])){
210       $this->remove_lock();
211       session::un_set('objectinfo');
212     }
214     /* Reset all relevant data, if we get a _cancel request */
215     if (isset($_POST['edit_cancel'])){
216       $this->remove_lock();
217       $this->acltabs= NULL;
218       session::un_set('objectinfo');
219     }
222     /********************
223       Delete entry requested, display confirm dialog
224      ********************/
226     /* Remove acl was requested */
227     if ($s_action=="del_role"){
229       /* Get 'dn' from posted acl */
230       $this->dn= $this->list[trim($s_entry)]['dn'];
232       /* Check permissions */
233       if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
235         /* Check locking, save current plugin in 'back_plugin', so
236            the dialog knows where to return. */
237         if (($acl= get_lock($this->dn)) != ""){
238           return(gen_locked_message ($acl, $this->dn));
239         }
241         /* Lock the current entry, so nobody will edit it during deletion */
242         add_lock ($this->dn, $this->ui->dn);
243         $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn)));
244         $smarty->assign("is_role",true);
245   
246         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
247       } else {
249         /* Obviously the acl isn't allowed to delete. Show message and
250            clean session. */
251         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
252       }
253     }
256     /********************
257       Delete entry confirmed 
258      ********************/
260     /* Confirmation for deletion has been passed. Acl should be deleted. */
261     if (isset($_POST['delete_acl_role_confirmed'])){
263       /* Check permissions */
264       if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
266         /* Delete request is permitted, perform LDAP action */
267         $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
268         $this->acltabs->delete();
269         unset ($this->acltabs);
270         $this->acltabs= NULL;
271       } else {
273         /* Normally this shouldn't be reached, send some extra
274            logs to notify the administrator */
275         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
277         if(isset($this->ui->uid)){
278           new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role deletion.");
279         }
280       }
282       /* Remove lock file after successfull deletion */
283       $this->remove_lock();
284     }
286     /********************
287       Delete entry requested, display confirm dialog
288      ********************/
290     /* Remove acl was requested */
291     if ($s_action=="del" || $s_action == "del_multiple"){
293       /* Get 'dn' from posted acl */
294       if($s_action == "del"){
295         $this->dns = array( $this->list[trim($s_entry)]['dn']); 
296       }else{
297         $this->dns = array();
298         foreach($this->list_get_selected_items() as $id){
299           $this->dns[] = $this->list[$id]['dn'];
300         }
301       }
303       /* Display lock messages */
304       if ($user= get_multiple_locks($this->dns)){
305         return(gen_locked_message($user,$this->dns));
306       }
308       $dns_names = array();
309       foreach($this->dns as $key => $dn){
311         /* Check permissions */
312         if(!preg_match("/d/",$this->ui->get_permissions($dn,"acl/acl"))){
314           /* Obviously the acl isn't allowed to delete. Show message and
315              clean session. */
316           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
317           unset($this->dns[$key]);
318           continue;
319         }
321         $dns_names[] = LDAP::fix($dn);
322       }
324       if(count($this->dns)){
325         add_lock($this->dns,$this->ui->dn);  
326         $smarty->assign("info", msgPool::deleteInfo($dns_names));
327         $smarty->assign("is_role",false);
328         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
329       }
330     }
333     /********************
334       Delete entry confirmed 
335      ********************/
337     /* Confirmation for deletion has been passed. Acl should be deleted. */
338     if (isset($_POST['delete_acl_confirm'])){
340       /* Check permissions */
341       if(preg_match("/d/",$this->ui->get_permissions($this->dn,"acl/acl"))){
343         /* Delete request is permitted, perform LDAP action */
344         $this->acltabs= new acl($this->config, NULL,$this->dn);
345         $this->acltabs->remove_from_parent();
346         unset ($this->acltabs);
347         $this->acltabs= NULL;
348       } else {
350         /* Normally this shouldn't be reached, send some extra
351            logs to notify the administrator */
352         msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
354         if(isset($this->ui->uid)){
355           new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role      deletion.");
356           
357         }
358       }
360       /* Remove lock file after successfull deletion */
361       $this->remove_lock();
362     }
364     
365     /********************
366       Delete entry Canceled 
367      ********************/
369     /* Delete acl canceled? */
370     if (isset($_POST['delete_acl_cancel'])){
371       $this->remove_lock();
372     }
375     /********************
376       Edit entry finished (Save) 
377      ********************/
379     /* Finish acl edit is triggered by the tabulator dialog, so
380        the acl wants to save edited data. Check and save at this
381        point. */
382     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->acltabs->config))){
384       /* Check tabs, will feed message array */
385       $this->acltabs->save_object();
386       $message= $this->acltabs->check();
388       /* Save, or display error message? */
389       if (count($message) == 0){
391         /* Save acl data to ldap */
392         if($this->acltabs->save() == 1){
393           return;
394         }
396         if (!isset($_POST['edit_apply'])){
398           /* ACl has been saved successfully, remove lock from LDAP. */
399           if ($this->dn != "new"){
400             $this->remove_lock();
401           }
403           unset ($this->acltabs);
404           $this->acltabs= NULL;
405           session::un_set('objectinfo');
406         }
407       } else {
408         /* Ok. There seem to be errors regarding to the tab data,
409            show message and continue as usual. */
410         msg_dialog::displayChecks($message);
411       }
412     }
415     /********************
416       Display subdialog 
417      ********************/
420     /* Show tab dialog if object is present */
421     if(isset($this->acltabs->config)){
422       
423       /* Save object */
424       $this->acltabs->save_object();
425       $display= $this->acltabs->execute();
427       /* Don't show buttons if tab dialog requests this */
428       if(isset($this->acltabs)){
430         /* Skip displaying save/cancel if there is a sub dialog open */
431         if (!isset($this->acltabs->dialog) || !$this->acltabs->dialog){
432           $display.= "<p style=\"text-align:right\">\n";
433 //          $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
434           $display.= "&nbsp;\n";
436           /* Skip Apply if it is a new entry */
437           #if ($this->dn != "new"){
438           #  $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
439           #  $display.= "&nbsp;\n";
440           #}
442   //        $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
443           $display.= "</p>";
444         }
445       }
446       return ($display);
447     }
448     
449     /* Check if there is a snapshot dialog open */
450     $base = $this->DivListACL->selectedBase;
451     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
452       return($str);
453     }
454   
455     /* Return rendered main page */
456         /* Display dialog with system list */
457     $this->DivListACL->parent = $this;
458     $this->DivListACL->execute();
460     /* Add departments if subsearch is disabled */
461     if(!$this->DivListACL->SubSearch){
462       $this->DivListACL->AddDepartments($this->DivListACL->selectedBase,3,1);
463     }
464     $this->reload();
465     $this->DivListACL->setEntries($this->list);
466     return($this->DivListACL->Draw());
467   }
470   function reload()
471   {
472     /* Get divlist informations from filter part */
473     $Regex      = $this->DivListACL -> Regex;
474     $SubSearch  = $this->DivListACL -> SubSearch;
475     $base       = $this->DivListACL -> selectedBase; 
476     $Attrs      = array("ou","cn","description","gosaAclEntry","objectClass");
477     $res        = array();
478     $tmp        = array();                                    // Will contain temporary results 
479     $ldap       = $this->config->get_ldap_link();
480     $Filter     = "(&(objectClass=gosaACL)(gosaAclEntry=*)(|(cn=".$Regex.")(ou=".$Regex.")))";
481     $FilterRoles= "(&(objectClass=gosaRole)(|(cn=".$Regex.")(ou=".$Regex.")))";
483     /* Fetch following structures, this will be used if !$SubSearch */
484     $fetch_this = array(
485       "ME"      => array("TYPE" => "cat"    , "FLAGS" => GL_SIZELIMIT                ,"BASE"=>""),
486       "SYSTEMS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('systemsou')),
487       "APPS"    => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('applicationou')),
488       "PEOPLE"  => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_people_ou()),
489       "GROUPS"  => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_groups_ou()));
491     /* Subsearch ? */
492     if($SubSearch){
494       /* Get all object in this base */
495       $Flags        = GL_SIZELIMIT | GL_SUBSEARCH;
496       $fetch_base   = $base;
497       $tmp  = get_list($Filter,   "acl", $fetch_base, $Attrs, $Flags);
498       $tmp2 = get_list($FilterRoles,  "acl", $fetch_base, $Attrs, $Flags);
499       foreach($tmp as $entry){
500         $res[] = $entry;
501       }
502       foreach($tmp2 as $entry){
503         $res[] = $entry;
504       }
506     }else{
508       $tmp_roles = get_list($FilterRoles,  "acl", get_ou('aclroleou').$base, $Attrs,GL_SIZELIMIT);
510       foreach($tmp_roles as $entry){
511         $res[] = $entry;
512       }
514       /* Walk through all possible bases */
515       foreach($fetch_this as $type => $data){
517         /* Get requried attributes */
518         $Flags        = $data['FLAGS'];
519         $fetch_base   = $data['BASE'].$base;
520         $Type         = $data['TYPE'];
522         /* Check if method is cat or search */
523         if($Type == "search"){
524           $tmp = get_list($Filter, "acl", $fetch_base, $Attrs, $Flags);
525           foreach($tmp as $entry){
526             $res[$entry['dn']] = $entry;
527           }
528         }else{
529           $ldap->cat($fetch_base,$Attrs);
530           $attrs = $ldap->fetch();
531           if($attrs && isset($attrs['gosaAclEntry'])){
532             $re2 = preg_replace("/\*/",".*",$Regex);
534             if(!isset($attrs['ou']) && !isset($attrs['cn'])){
535               $namingAttr= preg_replace("/^[^=]*+=([^,]*),.*$/","\\1",$fetch_base);
536               if(preg_match("/".$re2."/i",$namingAttr)){
537                 $res[$attrs['dn']] = $attrs;
538               }
539             }elseif( (isset($attrs['cn'][0]) && preg_match("/".$re2."/i",$attrs['cn'][0]))
540                ||(isset($attrs['ou'][0]) && preg_match("/".$re2."/i",$attrs['ou'][0]))){
541               $res[$attrs['dn']] = $attrs;
542             }
543           }
544         }
545       }
546     }
547     $this->list = array_values($res);
548   }
551   function copyPasteHandling_from_queue($s_action,$s_entry)
552   {
553     /* Check if Copy & Paste is disabled */
554     if(!is_object($this->CopyPasteHandler)){
555       return("");
556     }
558     /* Add a single entry to queue */
559     if($s_action == "cut" || $s_action == "copy"){
561       /* Cleanup object queue */
562       $this->CopyPasteHandler->cleanup_queue();
563       $dn = $this->list[$s_entry]['dn'];
564   
565       if(isset($this->list[$s_entry]['objectClass']) && in_array("gosaRole",$this->list[$s_entry]['objectClass'])){
566         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"aclroletab","ACLROLETAB","aclroles");
567       }
568     }
570     /* Add entries to queue */
571     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
573       /* Cleanup object queue */
574       $this->CopyPasteHandler->cleanup_queue();
576       /* Add new entries to CP queue */
577       foreach($this->list_get_selected_items() as $id){
578         $dn = $this->list[$id]['dn'];
580         if(isset($this->list[$id]['objectClass']) && in_array("gosaRole",$this->list[$id]['objectClass'])){
582           if($s_action == "copy_multiple"){
583             $this->CopyPasteHandler->add_to_queue($dn,"copy","aclroletab","ACLROLETAB","aclroles");
584           }
585           if($s_action == "cut_multiple"){
586             $this->CopyPasteHandler->add_to_queue($dn,"cut","aclroletab","ACLROLETAB","aclroles");
587           }
588         }
589       }
590     }
592     /* Start pasting entries */
593     if($s_action == "editPaste"){
594       $this->start_pasting_copied_objects = TRUE;
595     }
596     /* Return C&P dialog */
597     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
599       /* Get dialog */
600       $this->CopyPasteHandler->SetVar("base",$this->DivListACL->selectedBase);
601       $data = $this->CopyPasteHandler->execute();
603       /* Return dialog data */
604       if(!empty($data)){
605         return($data);
606       }
607     }
609     /* Automatically disable status for pasting */
610     if(!$this->CopyPasteHandler->entries_queued()){
611       $this->start_pasting_copied_objects = FALSE;
612     }
613     return("");
614   }
617   function list_get_selected_items()
618   {
619     $ids = array();
620     foreach($_POST as $name => $value){
621       if(preg_match("/^item_selected_[0-9]*$/",$name)){
622         $id   = preg_replace("/^item_selected_/","",$name);
623         $ids[$id] = $id;
624       }
625     }
626     return($ids);
627   }
630   function remove_lock()
631   {
632     /* Remove acl lock if a DN is marked as "currently edited" */
633     if (isset($this->acltabs->dn)){
634       del_lock ($this->acltabs->dn);
635     }
636     del_lock ($this->dn);
637     del_lock ($this->dns);
638   }
641   function save_object()
642   {
643     /* Handle divlist filter && department selection*/
644     if(!is_object($this->acltabs)){
645       $this->DivListACL->save_object();
646     }
647     if(is_object($this->CopyPasteHandler)){
648       $this->CopyPasteHandler->save_object();
649     }
650   }
652   /* A set of disabled and therefore overloaded functions. They are
653      not needed in this class. */
654   function remove_from_parent() { } 
655   function check() { } 
657   function save() {
658     echo "SAVE ACL";
659   } 
661   function adapt_from_template($dn, $skip= array()) { } 
662   function password_change_needed() { } 
665   /* Return departments, that will be included within snapshot detection */
666   function get_used_snapshot_bases()
667   {
668     return(array());
669   }  
671
672 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
673 ?>