Code

Added multiple remove to group list
[gosa.git] / plugins / admin / ogroups / class_ogroup.inc
1 <?php
4 /* Sort multidimensional arrays for key 'text' */
5 function sort_list($val1, $val2)
6 {
7   $v1= strtolower($val1['text']);
8   $v2= strtolower($val2['text']);
9   if ($v1 > $v2){
10     return 1;
11   }
12   if ($v1 < $v2){
13     return -1;
14   }
15   return 0;
16 }
19 class ogroup extends plugin
20 {
21   /* Variables */
22   var $cn= "";
23   var $description= "";
24   var $base= "";
25   var $gosaGroupObjects= "";
26   var $department= "";
27   var $objects= array();
28   var $objcache= array();
29   var $memberList= array();
30   var $member= array();
31   var $orig_dn= "";
32   var $group_dialog= FALSE;
34   /* attribute list for save action */
35   var $attributes= array("cn", "description", "gosaGroupObjects","member");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     $this->member = array();
45     /* Load member objects */
46     if (isset($this->attrs['member'])){
47       foreach ($this->attrs['member'] as $key => $value){
48         if ("$key" != "count"){
49           $value= @LDAP::convert($value);
50           $this->member["$value"]= "$value";
51         }
52       }
53     }
54     $this->is_account= TRUE;
56     /* Get global filter config */
57     if (!is_global("ogfilter")){
58       $ui= get_userinfo();
59       $base= get_base_from_people($ui->dn);
60       $ogfilter= array( "dselect"       => $base,
61           "regex"           => "*");
62       register_global("ogfilter", $ogfilter);
63     }
64     $ogfilter= get_global('ogfilter');
66     /* Adjust flags */
67     foreach( array(   "U" => "accounts",
68           "G" => "groups",
69           "A" => "applications",
70           "D" => "departments",
71           "S" => "servers",
72           "W" => "workstations",
73           "T" => "terminals",
74           "F" => "phones",
75           "_" => "subtrees",
76           "P" => "printers") as $key => $val){
78       if (preg_match("/$key/", $this->gosaGroupObjects)){
79         $ogfilter[$val]= "checked";
80       } else {
81         $ogfilter[$val]= "";
82       }
83     }
84     register_global("ogfilter", $ogfilter);
85   
86     if(isset($_SESSION['CurrentMainBase'])){
87      $this->base = $_SESSION['CurrentMainBase'];
88     }
90     /* Set base */
91     if ($this->dn == "new"){
92       $this->base= $_SESSION['CurrentMainBase'];
93     } else {
94       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
95     }
97     /* Load member data */
98     $this->reload();
99   }
101   function AddDelMembership($NewMember = false){
103     if($NewMember){
105       /* Add member and force reload */
106       $this->member[$NewMember]= $NewMember;
107       $this->reload(); 
109       $this->memberList[$NewMember]= $this->objcache[$NewMember];
110       unset ($this->objects[$NewMember]);
111       uasort ($this->memberList, 'sort_list');
112       reset ($this->memberList);
113     }else{
114       /* Delete objects from group */
115       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
116         foreach ($_POST['members'] as $value){
117           $this->objects["$value"]= $this->memberList[$value];
118           unset ($this->memberList["$value"]);
119           unset ($this->member["$value"]);
120           uasort ($this->objects, 'sort_list');
121           reset ($this->objects);
122         }
123         $this->reload();
124       }
126       /* Add objects to group */
127       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
128         foreach ($_POST['objects'] as $value){
129           $this->memberList["$value"]= $this->objects[$value];
130           $this->member["$value"]= $value;
131           unset ($this->objects[$value]);
132           uasort ($this->memberList, 'sort_list');
133           reset ($this->memberList);
134         }
135         $this->reload();
136       }
137     }
138   }
140   function execute()
141   {
142         /* Call parent execute */
143         plugin::execute();
145 //    $this->reload();
147     /* Do we represent a valid group? */
148     if (!$this->is_account){
149       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
150         _("This 'dn' is no object group.")."</b>";
151       return ($display);
152     }
154     /* Delete objects from group */
155     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
156       foreach ($_POST['members'] as $value){
157         if(isset($this->memberList[$value])){
158           $this->objects["$value"]= $this->memberList[$value];
159           unset ($this->memberList["$value"]);
160           unset ($this->member["$value"]);
161           uasort ($this->objects, 'sort_list');
162           reset ($this->objects);
163         }
164       }
165       $this->reload();
166     }
168     /* Add objects to group */
169     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
170       foreach ($_POST['objects'] as $value){
171         if(isset($this->objects[$value])){
172           $this->memberList["$value"]= $this->objects[$value];
173           $this->member["$value"]= $value;
174           unset ($this->objects[$value]);
175           uasort ($this->memberList, 'sort_list');
176           reset ($this->memberList);
177         }
178       }
179       $this->reload();
180     }
182     /* Load templating engine */
183     $smarty= get_smarty();
185     $tmp = $this->plInfo();
186     foreach($tmp['plProvidedAcls'] as $name => $translation){
187       $smarty->assign($name."ACL",$this->getacl($name));
188     }
190     /* Base select dialog */
191     $once = true;
192     foreach($_POST as $name => $value){
193       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
194         $once = false;
195         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
196         $this->dialog->setCurrentBase($this->base);
197       }
198     }
200     /* Dialog handling */
201     if(is_object($this->dialog) && $this->acl_is_moveable()){
202       /* Must be called before save_object */
203       $this->dialog->save_object();
205       if($this->dialog->isClosed()){
206         $this->dialog = false;
207       }elseif($this->dialog->isSelected()){
209         /* A new base was selected, check if it is a valid one */
210         $tmp = $this->get_allowed_bases();
211         if(isset($tmp[$this->dialog->isSelected()])){
212           $this->base = $this->dialog->isSelected();
213         }
214         $this->dialog= false;
215       }else{
216         return($this->dialog->execute());
217       }
218     }
220     /* Add objects? */
221     if (isset($_POST["edit_membership"])){
222       $this->group_dialog= TRUE;
223       $this->dialog= TRUE;
224     }
226     /* Add objects finished? */
227     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
228       $this->group_dialog= FALSE;
229       $this->dialog= FALSE;
230     }
232     /* Manage object add dialog */
233     if ($this->group_dialog){
235       /* Save data */
236       $ogfilter= get_global("ogfilter");
237       foreach( array("dselect", "regex") as $type){
238         if (isset($_POST[$type])){
239           $ogfilter[$type]= $_POST[$type];
240         }
241       }
242       if (isset($_POST['dselect'])){
243         foreach( array("accounts", "groups", "applications", "departments",
244               "servers", "workstations", "terminals", "printers","subtrees",
245               "phones") as $type){
247           if (isset($_POST[$type])) {
248             $ogfilter[$type]= "checked";
249           } else {
250             $ogfilter[$type]= "";
251           }
252         }
253       }
254       if (isset($_GET['search'])){
255         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
256         if ($s == "**"){
257           $s= "*";
258         }
259         $ogfilter['regex']= $s;
260       }
261       register_global("ogfilter", $ogfilter);
262       $this->reload();
264       /* Calculate actual groups */
265       $smarty->assign("objects", $this->convert_list($this->objects));
267       /* Show dialog */
268       $smarty->assign("search_image", get_template_path('images/search.png'));
269       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
270       $smarty->assign("tree_image", get_template_path('images/tree.png'));
271       $smarty->assign("deplist", $this->config->idepartments);
272       $smarty->assign("alphabet", generate_alphabet());
273       foreach( array("dselect", "regex", "accounts", "groups", "applications",
274             "departments", "servers", "workstations", "terminals","subtrees",
275             "printers", "phones") as $type){
276         $smarty->assign("$type", $ogfilter[$type]);
277       }
278       $smarty->assign("hint", print_sizelimit_warning());
279       $smarty->assign("apply", apply_filter());
281       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
282       return ($display);
283     }
285     /* Bases / Departments */
286       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
287         $this->base= $_POST['base'];
288       }
290     /* Assemble combine string */
291     if ($this->gosaGroupObjects == "[]"){
292       $smarty->assign("combinedObjects", _("none"));
293     } elseif (strlen($this->gosaGroupObjects) > 4){
294       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
295     } else {
296       $conv= array(   "U" => _("users"),
297           "G" => _("groups"),
298           "A" => _("applications"),
299           "D" => _("departments"),
300           "S" => _("servers"),
301           "W" => _("workstations"),
302           "T" => _("terminals"),
303           "F" => _("phones"),
304           "P" => _("printers"));
306       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
307       $p1= $conv[$type[0]];
308       error_reporting(0);
309       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
310         $p2= $conv[$type[1]];
311         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
312       } else {
313         $smarty->assign("combinedObjects", "$p1");
314       }
315       error_reporting(E_ALL);
316     }
318     /* Assign variables */
319     $smarty->assign("bases", $this->get_allowed_bases());
320     $smarty->assign("base_select", $this->base);
321     $smarty->assign("department", $this->department);
322     $smarty->assign("members", $this->convert_list($this->memberList));
324     /* Objects have to be tuned... */
325     $smarty->assign("objects", $this->convert_list($this->objects));
327     /* Fields */
328     foreach ($this->attributes as $val){
329       $smarty->assign("$val", $this->$val);
330     }
332     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
333   }
336   /* Save data to object */
337   function save_object()
338   {
339     /* Save additional values for possible next step */
340     if (isset($_POST['ogroupedit'])){
342       /* Create a base backup and reset the
343          base directly after calling plugin::save_object();
344          Base will be set seperatly a few lines below */
345       $base_tmp = $this->base;
346       plugin::save_object();
347       $this->base = $base_tmp;
349       /* Save base, since this is no LDAP attribute */
350       $tmp = $this->get_allowed_bases();
351       if(isset($_POST['base'])){
352         if(isset($tmp[$_POST['base']])){
353           $this->base= $_POST['base'];
354         }
355       }
356     }
357   }
360   /* (Re-)Load objects */
361   function reload()
362   {
363     /*###########
364       Variable initialisation 
365       ###########*/
367     $this->objects                = array();
368     $this->ui                     = get_userinfo();
369     $filter                       = "";
370     $objectClasses                = array();
371     
372     $ogfilter               = get_global("ogfilter");
373     $regex                  = $ogfilter['regex'];
375     $ldap= $this->config->get_ldap_link();
376     $ldap->cd ($ogfilter['dselect']);
379     /*###########
380       Generate Filter 
381       ###########*/
383     $p_f= array("accounts"        => array("CLASS"=>"gosaAccount"    ,"DN"=> get_people_ou()                  ,"ACL" => "users"), 
384                 "groups"          => array("CLASS"=>"posixGroup"     ,"DN"=> get_groups_ou()                  ,"ACL" => "groups"), 
385                 "applications"    => array("CLASS"=>"gosaApplication","DN"=> "ou=apps,"                       ,"ACL" => "application"), 
386                 "departments"     => array("CLASS"=>"gosaDepartment" ,"DN"=> ""                               ,"ACL" => "department"), 
387                 "servers"         => array("CLASS"=>"goServer"       ,"DN"=> "ou=servers,ou=systems,"         ,"ACL" => "server"),
388                 "workstations"    => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems,"    ,"ACL" => "workstation"),
389                 "terminals"       => array("CLASS"=>"gotoTerminal"   ,"DN"=> "ou=terminals,ou=systems,"       ,"ACL" => "terminal"),
390                 "printers"        => array("CLASS"=>"gotoPrinter"    ,"DN"=> "ou=printers,ou=systems,"        ,"ACL" => "printer"),
391                 "phones"          => array("CLASS"=>"goFonHardware"  ,"DN"=> "ou=phones,ou=systems,"          ,"ACL" => "phone"));
394     /*###########
395       Perform search for selected objectClasses & regex to fill list with objects   
396       ###########*/
398     $Get_list_flags = 0;
399     if($ogfilter['subtrees'] == "checked"){
400       $Get_list_flags |= GL_SUBSEARCH;
401     }    
403     foreach($p_f as $post_name => $data){
405       if($ogfilter[$post_name] == "checked"){
406         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
407         $res    = get_list($filter, $data['ACL']  , $data['DN'].$ogfilter['dselect'], 
408                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
410         /* fetch results and append them to the list */
411         foreach($res as $attrs){
413           $type= $this->getObjectType($attrs);
414           $name= $this->getObjectName($attrs);
416           /* Fill array */
417           if (isset($attrs["description"][0])){
418             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
419           } elseif (isset($attrs["uid"][0])) {
420             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
421           } else {
422             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
423           }
424         }
425       }
426     }
427     uasort ($this->objects, 'sort_list');
428     reset ($this->objects);
430     
431     /*###########
432       Build member list and try to detect obsolete entries 
433       ###########*/
435     $this->memberList = array();
436   
437     /* Walk through all single member entry */
438     foreach($this->member as $dn){
440       /* The dn for the current member can't be resolved 
441          it seams that this entry was removed 
442        */ 
443       /* Try to resolv the entry again, if it still fails, display error msg */
444       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
446       /* It has failed, add entry with type flag I (Invalid)*/
447       if ($ldap->error != "Success"){
448         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
450       } else {
452         /* Append this entry to our all object list */
454         /* Fetch object */
455         $attrs= $ldap->fetch();
457         $type= $this->getObjectType($attrs);
458         $name= $this->getObjectName($attrs);
460         if (isset($attrs["description"][0])){
461           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
462         } elseif (isset($attrs["uid"][0])) {
463           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
464         } else {
465           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
466         }
467         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
468         if(isset($attrs['uid'])){
469           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
470         }
472         /* Fill array */
473         if (isset($attrs["description"][0])){
474           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
475         } else {
476           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
477         }
479         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
480       }
481     }
482     uasort ($this->memberList, 'sort_list');
483     reset ($this->memberList);
485     /* Assemble types of currently combined objects */
486     $objectTypes= "";
487     foreach ($this->memberList as $dn => $desc){
489       /* Invalid object? */
490       if ($desc['type'] == 'I'){
491         continue;
492       }
494       /* Fine. Add to list. */
495       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
496         $objectTypes.= $desc['type'];
497       }
498     }
499     $this->gosaGroupObjects= "[$objectTypes]";
500   }
503   function convert_list($input)
504   {
505     $temp= "";
506     $conv= array(  "U" => "select_user.png",
507         "G" => "select_groups.png",
508         "A" => "select_application.png",
509         "D" => "select_department.png",
510         "S" => "select_server.png",
511         "W" => "select_workstation.png",
512         "T" => "select_terminal.png",
513         "F" => "select_phone.png",
514         "I" => "flag.png",
515         "P" => "select_printer.png");
517     foreach ($input as $key => $value){
518       /* Generate output */
519       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
520     }
522     return ($temp);
523   }
526   function getObjectType($attrs)
527   {
528     $type= "I";
530     foreach(array(  "U" => "gosaAccount",
531           "G" => "posixGroup",
532           "A" => "gosaApplication",
533           "D" => "gosaDepartment",
534           "S" => "goServer",
535           "W" => "gotoWorkstation",
536           "T" => "gotoTerminal",
537           "F" => "goFonHardware",
538           "P" => "gotoPrinter") as $index => $class){
539       if (in_array($class, $attrs['objectClass'])){
540         $type= $index;
541         break;
542       }
543     }
545     return ($type);
546   }
549   function getObjectName($attrs)
550   {
551     /* Person? */
552     $name =""; 
553     if (in_array('gosaAccount', $attrs['objectClass'])){
554       if(isset($attrs['sn']) && isset($attrs['givenName'])){
555         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
556       } else {
557         $name= $attrs['uid'][0];
558       }
559     } else {
560       if(isset($attrs["cn"][0])) {
561         $name= $attrs['cn'][0];
562       } else {
563         $name= $attrs['ou'][0];
564       }
565     }
567     return ($name);
568   }
571   function check()
572   {
573     /* Call common method to give check the hook */
574     $message= plugin::check();
576     /* Permissions for that base? */
577     if ($this->base != ""){
578       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
579     } else {
580       $new_dn= $this->dn;
581     }
584     $ldap = $this->config->get_ldap_link();
585     if($this->dn != $new_dn){
586       $ldap->cat ($new_dn, array('dn'));
587     }
588     
589     if($ldap->count() !=0){
590       $message[]= _("There is already an object with this cn.");
591     } 
593     /* Set new acl base */
594     if($this->dn == "new") {
595       $this->set_acl_base($this->base);
596     }
598     /* must: cn */
599     if ($this->cn == ""){
600       $message[]= "The required field 'Name' is not set.";
601     }
603     /* To many different object types? */
604     if (strlen($this->gosaGroupObjects) > 4){
605       $message[]= _("You can combine two different object types at maximum only!");
606     }
608     return ($message);
609   }
612   /* Save to LDAP */
613   function save()
614   {
615     plugin::save();
617     /* Move members to target array */
618     $this->attrs['member'] =array();
619     foreach ($this->member as $key => $desc){
620       $this->attrs['member'][]= @LDAP::fix($key);
621     }
623     $ldap= $this->config->get_ldap_link();
625     /* New accounts need proper 'dn', propagate it to remaining objects */
626     if ($this->dn == 'new'){
627       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
628     }
630     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
631        new entries. So do a check first... */
632     $ldap->cat ($this->dn, array('dn'));
633     if ($ldap->fetch()){
634       /* Modify needs array() to remove values :-( */
635       if (!count ($this->member)){
636         $this->attrs['member']= array();
637       }
638       $mode= "modify";
639     } else {
640       $mode= "add";
641       $ldap->cd($this->config->current['BASE']);
642       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
643     }
645     /* Write back to ldap */
646     $ldap->cd($this->dn);
647     $this->cleanup();
648     $ldap->$mode($this->attrs);
650     /* Trigger post signal */
651     $this->handle_post_events($mode);
653     $ret= 0;
654     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
655       $ret= 1;
656     }
658     return ($ret);
659   }
661   function remove_from_parent()
662   {
663     plugin::remove_from_parent();
665     $ldap= $this->config->get_ldap_link();
666     $ldap->rmdir($this->dn);
667     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
669     /* Trigger remove signal */
670     $this->handle_post_events("remove");
671   }
674   function getCopyDialog()
675   {
676     $smarty = get_smarty();
677     $smarty->assign("cn",     $this->cn);
678     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
679     $ret = array();
680     $ret['string'] = $str;
681     $ret['status'] = "";
682     return($ret);
683   }
685   function saveCopyDialog()
686   {
687     if(isset($_POST['cn'])){
688       $this->cn = $_POST['cn'];
689     }
690   }
693   function plInfo()
694   {
695     return (array(
696           "plShortName"   => _("Generic"),
697           "plDescription" => _("Object group generic"),
698           "plSelfModify"  => FALSE,
699           "plDepends"     => array(),
700           "plPriority"    => 1,
701           "plSection"     => array("administration"),
702           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
703                                                       "objectClass"  => "gosaGroupOfNames")),
704           "plProvidedAcls"=> array(
705             "cn"                => _("Name"),
706             "base"              => _("Base"),
707             "description"       => _("Description"),
708             "member"            => _("Member"))
709           ));
710   }
713 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
714 ?>