Code

Added set_acl_base() to all check() functions, to ensure that the checks will be...
[gosa.git] / plugins / admin / departments / class_departmentGeneric.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2003  Cajus Pollmeier
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class department extends plugin
22 {
23         /* department attributes */
24         var $ou= "";
25         var $description= "";
26         var $base= "";
27         var $st= "";
28         var $l= "";
29         var $postalAddress= "";
30         var $businessCategory= "";
31         var $telephoneNumber= "";
32         var $facsimileTelephoneNumber= "";
33         var $orig_dn= "";
34         var $is_administrational_unit= false;
35         var $gosaUnitTag= "";
37         var $rec_dst=false;     // Destination for recursive move
38         var $rec_src=false;     // Source for recursive move 
39         var $rec_cpy=false;     // Is recursive move requested ? 
41         /* Headpage attributes */
42         var $last_dep_sorting= "invalid";
43         var $departments= array();
45   var $must_be_tagged = false;
47         /* attribute list for save action */
48         var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
49                         "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
50         var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
51   var $initially_was_tagged = false;
53         function department ($config, $dn)
54         {
56                 plugin::plugin($config, $dn);
57                 $this->is_account= TRUE;
58                 $this->ui= get_userinfo();
59                 $this->dn= $dn;
60                 $this->orig_dn= $dn;
61                 $this->config= $config;
63                 /* Set base */
64                 if ($this->dn == "new"){
65                         $ui= get_userinfo();
66                         if(isset($_SESSION['CurrentMainBase'])){
67                                 $this->base= $_SESSION['CurrentMainBase'];
68                         }else{
69                                 $this->base= dn2base($ui->dn);
70                         }
71                 } else {
72                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
73                 }
75                 /* Is administrational Unit? */
76                 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
77                         $this->is_administrational_unit= true;
78       $this->initially_was_tagged = true;
79                 }
80         }
82         function execute()
83         {
84                 /* Call parent execute */
85                 plugin::execute();
87                 /* Reload departments */
88                 $this->config->get_departments($this->dn);
89                 $this->config->make_idepartments();
90                 $smarty= get_smarty();
92     /* Assign base ACL */
93     $baseACL = $this->getacl("base");
94     if(!$this->acl_is_moveable()) {
95       $baseACL = preg_replace("/w/","",$baseACL);
96     }
97     $smarty->assign("baseACL",          $baseACL);
99     $tmp = $this->plInfo();
100     foreach($tmp['plProvidedAcls'] as $name => $translation){
101       $smarty->assign($name."ACL",$this->getacl($name));
102     }
104                 /* Base select dialog */
105                 $once = true;
106                 foreach($_POST as $name => $value){
107                         if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
108                                 $once = false;
109                                 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
110                                 $this->dialog->setCurrentBase($this->base);
111                         }
112                 }
114                 /* Dialog handling */
115                 if(is_object($this->dialog)){
116                         /* Must be called before save_object */
117                         $this->dialog->save_object();
119                         if($this->dialog->isClosed()){
120                                 $this->dialog = false;
121                         }elseif($this->dialog->isSelected()){
122                                 $this->base = $this->dialog->isSelected();
123                                 $this->dialog= false;
124                         }else{
125                                 return($this->dialog->execute());
126                         }
127                 }
129                 /* Hide all departments, that are subtrees of this department */
130     $bases = $this->get_allowed_bases();
131                 if(($this->dn == "new")||($this->dn == "")){
132                         $tmp = $bases;
133                 }else{
134                         $tmp    = array();      
135                         foreach($bases as $dn=>$base){
136                                 $fixed = str_replace("/","\\",$this->dn);
137                                 /* Only attach departments which are not a subtree of this one */
138                                 if(!preg_match("/".$fixed."/",$dn)){
139                                         $tmp[$dn]=$base;
140                                 }
141                         }
142                 }
143                 $smarty->assign("bases", $tmp);
145                 foreach ($this->attributes as $val){
146                         $smarty->assign("$val", $this->$val);
147                 }
148                 $smarty->assign("base_select", $this->base);
150                 /* Set admin unit flag */
151                 if ($this->is_administrational_unit) {
152                         $smarty->assign("unitTag", "checked");
153                 } else {
154                         $smarty->assign("unitTag", "");
155                 }
157                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
158         }
160         function clear_fields()
161         {
162                 $this->dn   = "";
163                 $this->base = "";
165                 foreach ($this->attributes as $val){
166                         $this->$val= "";
167                 }
168         }
170         function remove_from_parent()
171         {
172                 $ldap= $this->config->get_ldap_link();
173                 $ldap->cd ($this->dn);
174                 $ldap->recursive_remove();
175     show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
177                 /* Optionally execute a command after we're done */
178                 $this->handle_post_events('remove');
179         }
181         function must_be_tagged()
182         {
183                 return $this->must_be_tagged;
184         }
186         function am_i_moved()
187         {
188                 return $this->rec_cpy;
189         }
192         /* Save data to object */
193         function save_object()
194         {
195                 if (isset($_POST['dep_generic_posted'])){
196                         plugin::save_object();
198                         /* Save base, since this is no LDAP attribute */
199       if($this->acl_is_moveable() && isset($_POST['base'])){
200                                 $this->base= $_POST['base'];
201                         }
203                         /* Save tagging flag */
204                         if ($this->acl_is_writeable("unitTag")){
205                                 if (isset($_POST['unitTag'])){
206                                         $this->is_administrational_unit= true;
207                                 } else {
208                                         $this->is_administrational_unit= false;
209                                 }
210                         }
211                 }
212         }
215         /* Check values */
216         function check()
217         {
218                 /* Call common method to give check the hook */
219                 $message= plugin::check();
221     
222     /* Set new acl base */
223     if($this->dn == "new") {
224       $this->set_acl_base($this->base);
225     }
227                 /* Permissions for that base? */
228                 //      $this->dn= "ou=$this->ou,".$this->base;
229                 if (!$this->acl_is_createable() && $this->dn == "new"){
230                         $message[]= _("You have no permissions to create a department on this 'Base'.");
231                 }
233                 /* Check for presence of this department */
234                 $ldap= $this->config->get_ldap_link();
235     $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
236     if ($this->orig_dn == "new" && $ldap->count()){
237                         $message[]= _("Department with that 'Name' already exists.");
238                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
239                         $message[]= _("Department with that 'Name' already exists.");
240                 }
242                 /* All required fields are set? */
243                 if ($this->ou == ""){
244                         $message[]= _("Required field 'Name' is not set.");
245                 }
246                 if ($this->description == ""){
247                         $message[]= _("Required field 'Description' is not set.");
248                 }
250     if(is_department_name_reserved($this->ou,$this->base)){
251       $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
252     }
254                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
255                         $message[]= _("The field 'Name' contains invalid characters.");
256                 }
257                 if (!is_phone_nr($this->telephoneNumber)){
258                         $message[]= _("The field 'Phone' contains an invalid phone number.");
259                 }
260                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
261                         $message[]= _("The field 'Fax' contains an invalid phone number.");
262                 }
264                 return $message;
265         }
268         /* Save to LDAP */
269         function save()
270         {
271                 $ldap= $this->config->get_ldap_link();
273                 /* Add tag objects if needed */
274                 if ($this->is_administrational_unit){
276       /* If this wasn't tagged before add oc an reset unit tag */
277       if(!$this->initially_was_tagged){
278                         $this->objectclasses[]= "gosaAdministrativeUnit";
279                           $this->gosaUnitTag= "";
281         /* It seams that this method is called twice, 
282             set this to true. to avoid adding this oc twice */
283         $this->initially_was_tagged = true;
284       }
286                         if ($this->gosaUnitTag == ""){
288                                 /* It's unlikely, but check if already used... */
289                                 $try= 5;
290                                 $ldap->cd($this->config->current['BASE']);
291                                 while ($try--){
293                                         /* Generate microtime stamp as tag */
294                                         list($usec, $sec)= explode(" ", microtime());
295                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
297                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
298                                         if ($ldap->count() == 0){
299                                                 break;
300                                         }
301                                 }
302                                 if($try == 0) {
303                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
304                                         return;
305                                 }
306                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
307                         }
308                 }
310                 plugin::save();
312                 /* Remove tag information if needed */
313                 if (!$this->is_administrational_unit){
314                         $tmp= array();
316                         /* Remove gosaAdministrativeUnit from this plugin */
317       $has_unit_tag= false;
318                         foreach($this->attrs['objectClass'] as $oc){
319                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
320                                         $tmp[]= $oc;
321                                 }
322                                 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
323                                         $has_unit_tag= true;
324                                 }
325                         }
326                         $this->attrs['objectClass']= $tmp;
328                         if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
329                                 $this->attrs['gosaUnitTag']= array();
330                         }
331       $this->gosaUnitTag = "";
332                 }
334                 /* Write back to ldap */
335                 $ldap= $this->config->get_ldap_link();
336                 $ldap->cat($this->dn, array('dn'));
337                 $a= $ldap->fetch();
338                 $ldap->cd($this->dn);
340                 if (count($a)){
341                         $this->cleanup();
342                         $ldap->modify ($this->attrs); 
344                         $this->handle_post_events('modify');
345                 } else {
346                         $ldap->add($this->attrs);
347                         $this->handle_post_events('add');
348                 }
349     show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
351     /* The parameter forces only to set must_be_tagged, and don't touch any objects 
352         This will be done later */
353     $this->tag_objects(true);
355     /* Fix tagging if needed */
356     if (!$this->is_administrational_unit){
357       $this->handle_object_tagging();
358     }
359     
360     /* Optionally execute a command after we're done */
361                 $this->postcreate();
362     return(false);
363         }
366   function ShowMoveFrame()
367   {
368     $smarty = get_smarty();
369     $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
370     $smarty->assign("message","As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
371     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
372     return($display);
373   }
375   function ShowTagFrame()
376   {
377     $smarty = get_smarty();
378     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
379     $smarty->assign("message","As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
380     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
381     return($display);
382   }
384         /* Tag objects to have the gosaAdministrativeUnitTag */
385         function tag_objects($OnlySetTagFlag = false)
386         {
387     if(!$OnlySetTagFlag){
388       $smarty= get_smarty();
389       echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
390       echo "<body style='background-image:none;margin:3px;color:black'>";
391       echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
392     }
394                 $add= $this->is_administrational_unit;
395                 $len= strlen($this->dn);
396                 $ldap= $this->config->get_ldap_link();
397                 $ldap->cd($this->dn);
398                 if ($add){
399                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
400                                                                 $this->gosaUnitTag.')))', array('dn'));
401                 } else {
402                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
403                 }
405                 while ($attrs= $ldap->fetch()){
407                         /* Skip self */
408                         if ($attrs['dn'] == $this->dn){
409                                 continue;
410                         }
412                         /* Check for confilicting administrative units */
413                         $fix= true;
414                         foreach ($this->config->adepartments as $key => $tag){
415                                 /* This one is shorter than our dn, its not relevant... */
416                                 if ($len >= strlen($key)){
417                                         continue;
418                                 }
420                                 /* This one matches with the latter part. Break and don't fix this entry */
421                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
422                                         $fix= false;
423                                         break;
424                                 }
425                         }
427       /* Fix entry if needed */
428       if ($fix){
429         if($OnlySetTagFlag){
430           $this->must_be_tagged =true;
431           return;
432         }
433                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
434                         }
435                 }
436     if(!$OnlySetTagFlag){
437       echo '<p class="seperator">&nbsp;</p>';
438       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
439         <br><input type='submit' name='back' value='"._("Continue")."'>
440         </form></div>";
441     }
442         }
445         /* Move/Rename complete trees */
446         function recursive_move($src_dn, $dst_dn,$force = false)
447         {
448     /* If force == false prepare to recursive move this object from src to dst 
449         on the next call. */
450                 if(!$force){
451                         $this->rec_cpy  = true;
452                         $this->rec_src  = $src_dn;
453                         $this->rec_dst  = $dst_dn;
454                 }else{
456       /* If this is called, but not wanted, abort */
457                         if(!$this->rec_cpy){ 
458                                 return;
459                         }
461                         $src_dn = $this->rec_src;
462                         $dst_dn = $this->rec_dst;
464                         /* Print header to have styles included */
465                         $smarty= get_smarty();
466                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
467                         echo "<body style='background-image:none;margin:3px;color:black'>";
469                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
472                         /* Check if the destination entry exists */
473                         $ldap= $this->config->get_ldap_link();
475                         /* Check if destination exists - abort */
476                         $ldap->cat($dst_dn, array('dn'));
477                         if ($ldap->fetch()){
478                                 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
479                                                 E_USER_WARNING);
480                                 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>"; 
481                                 return (FALSE);
482                         }
484                         /* Perform a search for all objects to be moved */
485                         $objects= array();
486                         $ldap->cd($src_dn);
487                         $ldap->search("(objectClass=*)", array("dn"));
488                         while($attrs= $ldap->fetch()){
489                                 $dn= $attrs['dn'];
490                                 $objects[$dn]= strlen($dn);
491                         }
493                         /* Sort objects by indent level */
494                         asort($objects);
495                         reset($objects);
497                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
498                         foreach ($objects as $object => $len){
501                                 $src= str_replace("\\","\\\\",$object);
502                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
503                                 $dst= str_replace($src_dn,$dst_dn,$object);
505                                 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
507                                 if (!$this->copy($src, $dst)){
508                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
509                                         return (FALSE);
510                                 }
512                                 flush();
513                         }
515                         /* Remove src_dn */
516                         $ldap->cd($src_dn);
517                         $ldap->recursive_remove();
518                         $this->rec_src = $this->rec_dst = "";
519                         $this->rec_cpy =false;
521                         echo '<p class="seperator">&nbsp;</p>';
523                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
524                                 <br><input type='submit' name='back' value='"._("Continue")."'>
525                                 </form></div>";
527                         echo "</body></html>";
529                         return (TRUE);
530                 }
531         }
534   /* Return plugin informations for acl handling */ 
535   function plInfo()
536   {
537     return (array("plShortName"   => _("Generic"),
538                   "plDescription" => _("Departments"),
539                   "plSelfModify"  => FALSE,
540                   "plPriority"    => 0,
541                   "plDepends"     => array(),
542                   "plSection"     => array("admin"),
543                   "plCategory"    => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
544             
545                   "plProvidedAcls" => array(
546                     "description"       => _("Description"),
547                     "c"                 => _("Country"),
548                     "base"              => _("Base"),
549                     "l"                 => _("Location"),
550                     "telephoneNumber"   => _("Telephone"),
551                     "ou"                => _("Department name"),
552                     "businessCategory"  => _("Category"),
553                     "st"                => _("State"),
554                     "postalAddress"     => _("Address"),
555                     "gosaUnitTag"       => _("Administrative settings"),
556                     "facsimileTelephoneNumber" => _("Fax"))
557                   ));
558   }
560 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
561 ?>