Code

Added acls for printer glpi
[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                 /* Permissions for that base? */
222                 //      $this->dn= "ou=$this->ou,".$this->base;
223                 if ($this->acl_is_createable() && $this->dn == "new"){
224                         $message[]= _("You have no permissions to create a department on this 'Base'.");
225                 }
227                 /* Check for presence of this department */
228                 $ldap= $this->config->get_ldap_link();
229     $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
230     if ($this->orig_dn == "new" && $ldap->count()){
231                         $message[]= _("Department with that 'Name' already exists.");
232                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
233                         $message[]= _("Department with that 'Name' already exists.");
234                 }
236                 /* All required fields are set? */
237                 if ($this->ou == ""){
238                         $message[]= _("Required field 'Name' is not set.");
239                 }
240                 if ($this->description == ""){
241                         $message[]= _("Required field 'Description' is not set.");
242                 }
244     if(is_department_name_reserved($this->ou,$this->base)){
245       $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
246     }
248                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
249                         $message[]= _("The field 'Name' contains invalid characters.");
250                 }
251                 if (!is_phone_nr($this->telephoneNumber)){
252                         $message[]= _("The field 'Phone' contains an invalid phone number.");
253                 }
254                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
255                         $message[]= _("The field 'Fax' contains an invalid phone number.");
256                 }
258                 return $message;
259         }
262         /* Save to LDAP */
263         function save()
264         {
265                 $ldap= $this->config->get_ldap_link();
267                 /* Add tag objects if needed */
268                 if ($this->is_administrational_unit){
270       /* If this wasn't tagged before add oc an reset unit tag */
271       if(!$this->initially_was_tagged){
272                         $this->objectclasses[]= "gosaAdministrativeUnit";
273                           $this->gosaUnitTag= "";
275         /* It seams that this method is called twice, 
276             set this to true. to avoid adding this oc twice */
277         $this->initially_was_tagged = true;
278       }
280                         if ($this->gosaUnitTag == ""){
282                                 /* It's unlikely, but check if already used... */
283                                 $try= 5;
284                                 $ldap->cd($this->config->current['BASE']);
285                                 while ($try--){
287                                         /* Generate microtime stamp as tag */
288                                         list($usec, $sec)= explode(" ", microtime());
289                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
291                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
292                                         if ($ldap->count() == 0){
293                                                 break;
294                                         }
295                                 }
296                                 if($try == 0) {
297                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
298                                         return;
299                                 }
300                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
301                         }
302                 }
304                 plugin::save();
306                 /* Remove tag information if needed */
307                 if (!$this->is_administrational_unit){
308                         $tmp= array();
310                         /* Remove gosaAdministrativeUnit from this plugin */
311       $has_unit_tag= false;
312                         foreach($this->attrs['objectClass'] as $oc){
313                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
314                                         $tmp[]= $oc;
315                                 }
316                                 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
317                                         $has_unit_tag= true;
318                                 }
319                         }
320                         $this->attrs['objectClass']= $tmp;
322                         if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
323                                 $this->attrs['gosaUnitTag']= array();
324                         }
325       $this->gosaUnitTag = "";
326                 }
328                 /* Write back to ldap */
329                 $ldap= $this->config->get_ldap_link();
330                 $ldap->cat($this->dn, array('dn'));
331                 $a= $ldap->fetch();
332                 $ldap->cd($this->dn);
334                 if (count($a)){
335                         $this->cleanup();
336                         $ldap->modify ($this->attrs); 
338                         $this->handle_post_events('modify');
339                 } else {
340                         $ldap->add($this->attrs);
341                         $this->handle_post_events('add');
342                 }
343     show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
345     /* The parameter forces only to set must_be_tagged, and don't touch any objects 
346         This will be done later */
347     $this->tag_objects(true);
349     /* Fix tagging if needed */
350     if (!$this->is_administrational_unit){
351       $this->handle_object_tagging();
352     }
353     
354     /* Optionally execute a command after we're done */
355                 $this->postcreate();
356     return(false);
357         }
360   function ShowMoveFrame()
361   {
362     $smarty = get_smarty();
363     $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
364     $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.");
365     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
366     return($display);
367   }
369   function ShowTagFrame()
370   {
371     $smarty = get_smarty();
372     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
373     $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.");
374     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
375     return($display);
376   }
378         /* Tag objects to have the gosaAdministrativeUnitTag */
379         function tag_objects($OnlySetTagFlag = false)
380         {
381     if(!$OnlySetTagFlag){
382       $smarty= get_smarty();
383       echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
384       echo "<body style='background-image:none;margin:3px;color:black'>";
385       echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
386     }
388                 $add= $this->is_administrational_unit;
389                 $len= strlen($this->dn);
390                 $ldap= $this->config->get_ldap_link();
391                 $ldap->cd($this->dn);
392                 if ($add){
393                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
394                                                                 $this->gosaUnitTag.')))', array('dn'));
395                 } else {
396                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
397                 }
399                 while ($attrs= $ldap->fetch()){
401                         /* Skip self */
402                         if ($attrs['dn'] == $this->dn){
403                                 continue;
404                         }
406                         /* Check for confilicting administrative units */
407                         $fix= true;
408                         foreach ($this->config->adepartments as $key => $tag){
409                                 /* This one is shorter than our dn, its not relevant... */
410                                 if ($len >= strlen($key)){
411                                         continue;
412                                 }
414                                 /* This one matches with the latter part. Break and don't fix this entry */
415                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
416                                         $fix= false;
417                                         break;
418                                 }
419                         }
421       /* Fix entry if needed */
422       if ($fix){
423         if($OnlySetTagFlag){
424           $this->must_be_tagged =true;
425           return;
426         }
427                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
428                         }
429                 }
430     if(!$OnlySetTagFlag){
431       echo '<p class="seperator">&nbsp;</p>';
432       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
433         <br><input type='submit' name='back' value='"._("Continue")."'>
434         </form></div>";
435     }
436         }
439         /* Move/Rename complete trees */
440         function recursive_move($src_dn, $dst_dn,$force = false)
441         {
442     /* If force == false prepare to recursive move this object from src to dst 
443         on the next call. */
444                 if(!$force){
445                         $this->rec_cpy  = true;
446                         $this->rec_src  = $src_dn;
447                         $this->rec_dst  = $dst_dn;
448                 }else{
450       /* If this is called, but not wanted, abort */
451                         if(!$this->rec_cpy){ 
452                                 return;
453                         }
455                         $src_dn = $this->rec_src;
456                         $dst_dn = $this->rec_dst;
458                         /* Print header to have styles included */
459                         $smarty= get_smarty();
460                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
461                         echo "<body style='background-image:none;margin:3px;color:black'>";
463                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
466                         /* Check if the destination entry exists */
467                         $ldap= $this->config->get_ldap_link();
469                         /* Check if destination exists - abort */
470                         $ldap->cat($dst_dn, array('dn'));
471                         if ($ldap->fetch()){
472                                 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
473                                                 E_USER_WARNING);
474                                 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>"; 
475                                 return (FALSE);
476                         }
478                         /* Perform a search for all objects to be moved */
479                         $objects= array();
480                         $ldap->cd($src_dn);
481                         $ldap->search("(objectClass=*)", array("dn"));
482                         while($attrs= $ldap->fetch()){
483                                 $dn= $attrs['dn'];
484                                 $objects[$dn]= strlen($dn);
485                         }
487                         /* Sort objects by indent level */
488                         asort($objects);
489                         reset($objects);
491                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
492                         foreach ($objects as $object => $len){
495                                 $src= str_replace("\\","\\\\",$object);
496                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
497                                 $dst= str_replace($src_dn,$dst_dn,$object);
499                                 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
501                                 if (!$this->copy($src, $dst)){
502                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
503                                         return (FALSE);
504                                 }
506                                 flush();
507                         }
509                         /* Remove src_dn */
510                         $ldap->cd($src_dn);
511                         $ldap->recursive_remove();
512                         $this->rec_src = $this->rec_dst = "";
513                         $this->rec_cpy =false;
515                         echo '<p class="seperator">&nbsp;</p>';
517                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
518                                 <br><input type='submit' name='back' value='"._("Continue")."'>
519                                 </form></div>";
521                         echo "</body></html>";
523                         return (TRUE);
524                 }
525         }
528   /* Return plugin informations for acl handling */ 
529   function plInfo()
530   {
531     return (array("plShortName"   => _("Generic"),
532                   "plDescription" => _("Departments"),
533                   "plSelfModify"  => FALSE,
534                   "plPriority"    => 0,
535                   "plDepends"     => array(),
536                   "plSection"     => array("admin"),
537                   "plCategory"    => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
538             
539                   "plProvidedAcls" => array(
540                     "description"       => _("Description"),
541                     "c"                 => _("Country"),
542                     "base"              => _("Base"),
543                     "l"                 => _("Location"),
544                     "telephoneNumber"   => _("Telephone"),
545                     "ou"                => _("Department name"),
546                     "businessCategory"  => _("Category"),
547                     "st"                => _("State"),
548                     "postalAddress"     => _("Address"),
549                     "gosaUnitTag"       => _("Administrative settings"),
550                     "facsimileTelephoneNumber" => _("Fax"))
551                   ));
552   }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>