Code

9715ac161c635f8d0b15d3358d7e0443d387230e
[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           $this->member["$value"]= "$value";
50         }
51       }
52     }
53     $this->is_account= TRUE;
55     /* Get global filter config */
56     if (!is_global("ogfilter")){
57       $ui= get_userinfo();
58       $base= get_base_from_people($ui->dn);
59       $ogfilter= array( "dselect"       => $base,
60           "regex"           => "*");
61       register_global("ogfilter", $ogfilter);
62     }
63     $ogfilter= get_global('ogfilter');
65     /* Adjust flags */
66     foreach( array(   "U" => "accounts",
67           "G" => "groups",
68           "A" => "applications",
69           "D" => "departments",
70           "S" => "servers",
71           "W" => "workstations",
72           "T" => "terminals",
73           "F" => "phones",
74           "P" => "printers") as $key => $val){
76       if (preg_match("/$key/", $this->gosaGroupObjects)){
77         $ogfilter[$val]= "checked";
78       } else {
79         $ogfilter[$val]= "";
80       }
81     }
82     register_global("ogfilter", $ogfilter);
83   
84     if(isset($_SESSION['CurrentMainBase'])){
85      $this->base = $_SESSION['CurrentMainBase'];
86     }
88      /* set permissions */
89     $ui= get_userinfo();
90     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
91     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
94     /* Load member data */
95     $this->reload();
96   }
98   function AddDelMembership($NewMember = false){
100     if($NewMember){
101       $this->memberList[$NewMember]= $this->objcache[$NewMember];
102       $this->member[$NewMember]= $NewMember;
103       unset ($this->objects[$NewMember]);
104       uasort ($this->memberList, 'sort_list');
105       reset ($this->memberList);
106       $this->reload();
107     }else{
108       /* Delete objects from group */
109       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
110         foreach ($_POST['members'] as $value){
111           $this->objects["$value"]= $this->memberList[$value];
112           unset ($this->memberList["$value"]);
113           unset ($this->member["$value"]);
114           uasort ($this->objects, 'sort_list');
115           reset ($this->objects);
116         }
117         $this->reload();
118       }
120       /* Add objects to group */
121       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
122         foreach ($_POST['objects'] as $value){
123           $this->memberList["$value"]= $this->objects[$value];
124           $this->member["$value"]= $value;
125           unset ($this->objects[$value]);
126           uasort ($this->memberList, 'sort_list');
127           reset ($this->memberList);
128         }
129         $this->reload();
130       }
131     }
132   }
134   function execute()
135   {
136         /* Call parent execute */
137         plugin::execute();
139 //    $this->reload();
141     /* Do we represent a valid group? */
142     if (!$this->is_account){
143       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
144         _("This 'dn' is no object group.")."</b>";
145       return ($display);
146     }
148     /* Delete objects from group */
149     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
150       foreach ($_POST['members'] as $value){
151         if(isset($this->memberList[$value])){
152           $this->objects["$value"]= $this->memberList[$value];
153           unset ($this->memberList["$value"]);
154           unset ($this->member["$value"]);
155           uasort ($this->objects, 'sort_list');
156           reset ($this->objects);
157         }
158       }
159       $this->reload();
160     }
162     /* Add objects to group */
163     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
164       foreach ($_POST['objects'] as $value){
165         if(isset($this->objects[$value])){
166           $this->memberList["$value"]= $this->objects[$value];
167           $this->member["$value"]= $value;
168           unset ($this->objects[$value]);
169           uasort ($this->memberList, 'sort_list');
170           reset ($this->memberList);
171         }
172       }
173       $this->reload();
174     }
176     /* Load templating engine */
177     $smarty= get_smarty();
179     /* Base select dialog */
180     $once = true;
181     foreach($_POST as $name => $value){
182       if(preg_match("/^chooseBase/",$name) && $once){
183         $once = false;
184         $this->dialog = new baseSelectDialog($this->config);
185         $this->dialog->setCurrentBase($this->base);
186       }
187     }
189     /* Dialog handling */
190     if(is_object($this->dialog)){
191       /* Must be called before save_object */
192       $this->dialog->save_object();
194       if($this->dialog->isClosed()){
195         $this->dialog = false;
196       }elseif($this->dialog->isSelected()){
197         $this->base = $this->dialog->isSelected();
198         $this->dialog= false;
199       }else{
200         return($this->dialog->execute());
201       }
202     }
204     /* Add objects? */
205     if (isset($_POST["edit_membership"])){
206       $this->group_dialog= TRUE;
207       $this->dialog= TRUE;
208     }
210     /* Add objects finished? */
211     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
212       $this->group_dialog= FALSE;
213       $this->dialog= FALSE;
214     }
216     /* Manage object add dialog */
217     if ($this->group_dialog){
219       /* Save data */
220       $ogfilter= get_global("ogfilter");
221       foreach( array("dselect", "regex") as $type){
222         if (isset($_POST[$type])){
223           $ogfilter[$type]= $_POST[$type];
224         }
225       }
226       if (isset($_POST['dselect'])){
227         foreach( array("accounts", "groups", "applications", "departments",
228               "servers", "workstations", "terminals", "printers",
229               "phones") as $type){
231           if (isset($_POST[$type])) {
232             $ogfilter[$type]= "checked";
233           } else {
234             $ogfilter[$type]= "";
235           }
236         }
237       }
238       if (isset($_GET['search'])){
239         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
240         if ($s == "**"){
241           $s= "*";
242         }
243         $ogfilter['regex']= $s;
244       }
245       register_global("ogfilter", $ogfilter);
246       $this->reload();
248       /* Calculate actual groups */
249       $smarty->assign("objects", $this->convert_list($this->objects));
251       /* Show dialog */
252       $smarty->assign("search_image", get_template_path('images/search.png'));
253       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
254       $smarty->assign("tree_image", get_template_path('images/tree.png'));
255       $smarty->assign("deplist", $this->config->idepartments);
256       $smarty->assign("alphabet", generate_alphabet());
257       foreach( array("dselect", "regex", "accounts", "groups", "applications",
258             "departments", "servers", "workstations", "terminals",
259             "printers", "phones") as $type){
260         $smarty->assign("$type", $ogfilter[$type]);
261       }
262       $smarty->assign("hint", print_sizelimit_warning());
263       $smarty->assign("apply", apply_filter());
265       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
266       return ($display);
267     }
269     /* Bases / Departments */
270    
271       if (isset($_POST['base'])){
272         $this->base= $_POST['base'];
273       }
275     /* Assemble combine string */
276     if ($this->gosaGroupObjects == "[]"){
277       $smarty->assign("combinedObjects", _("none"));
278     } elseif (strlen($this->gosaGroupObjects) > 4){
279       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
280     } else {
281       $conv= array(   "U" => _("users"),
282           "G" => _("groups"),
283           "A" => _("applications"),
284           "D" => _("departments"),
285           "S" => _("servers"),
286           "W" => _("workstations"),
287           "T" => _("terminals"),
288           "F" => _("phones"),
289           "P" => _("printers"));
291       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
292       $p1= $conv[$type[0]];
293       error_reporting(0);
294       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
295         $p2= $conv[$type[1]];
296         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
297       } else {
298         $smarty->assign("combinedObjects", "$p1");
299       }
300       error_reporting(E_ALL);
301     }
303     /* Assign variables */
304     $smarty->assign("bases", $this->config->idepartments);
305     $smarty->assign("base_select", $this->base);
306     $smarty->assign("department", $this->department);
307     $smarty->assign("members", $this->convert_list($this->memberList));
309     /* Objects have to be tuned... */
310     $smarty->assign("objects", $this->convert_list($this->objects));
312     /* Fields */
313     foreach ($this->attributes as $val){
314       $smarty->assign("$val", $this->$val);
315       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
316     }
318     /* Assign ACL's */
319     foreach (array("base", "members") as $val){
320       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
321     }
323     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
324   }
327   /* Save data to object */
328   function save_object()
329   {
330     /* Save additional values for possible next step */
331     if (isset($_POST['ogroupedit'])){
332       plugin::save_object();
334       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
335         $this->base= $_POST["base"];
336       }
338     }
339   }
342   /* (Re-)Load objects */
343   function reload()
344   {
345     /*###########
346       Variable initialisation 
347       ###########*/
349     $this->objects                = array();
350     $this->ui                     = get_userinfo();
351     $filter                       = "";
352     $objectClasses                = array();
353     
354     $ogfilter               = get_global("ogfilter");
355     $regex                  = $ogfilter['regex'];
357     /* Get ldap connection */
358     $ldap= $this->config->get_ldap_link();
359     $ldap->cd ($ogfilter['dselect']);
362     /*###########
363       Generate Filter 
364       ###########*/
366     /* Assemble filter */
367     if ($ogfilter['accounts'] == "checked"){
368       $filter.= "(objectClass=gosaAccount)";
369       $objectClasses["gosaAccount"]     = get_people_ou();
370     }
371     if ($ogfilter['groups'] == "checked"){
372       $filter.= "(objectClass=posixGroup)";
373       $objectClasses["posixGroup"]      = get_groups_ou();
374     }
375     if ($ogfilter['applications'] == "checked"){
376       $filter.= "(objectClass=gosaApplication)";
377       $objectClasses["gosaApplication"] = "ou=apps,";
378     }
379     if ($ogfilter['departments'] == "checked"){
380       $filter.= "(objectClass=gosaDepartment)";
381       $objectClasses["gosaDepartment"]  = "";
382     }
383     if ($ogfilter['servers'] == "checked"){
384       $filter.= "(objectClass=goServer)";
385       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
386     }
387     if ($ogfilter['workstations'] == "checked"){
388       $filter.= "(objectClass=gotoWorkstation)";
389       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
390     }
391     if ($ogfilter['terminals'] == "checked"){
392       $filter.= "(objectClass=gotoTerminal)";
393       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
394     }
395     if ($ogfilter['printers'] == "checked"){
396       $filter.= "(objectClass=gotoPrinter)";
398       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
399     }
400     if ($ogfilter['phones'] == "checked"){
401       $filter.= "(objectClass=goFonHardware)";
402       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
403     }
406     /*###########
407       Perform search for selected objectClasses & regex to fill list with objects   
408       ###########*/
410     /* Perform search for selected objectClasses */
411     foreach($objectClasses as $class=> $basedn){
412       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
413           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
414       
415       /* fetch results and append them to the list */
416       while($attrs = $ldap->fetch()){
418         $type= $this->getObjectType($attrs);
419         $name= $this->getObjectName($attrs);
421         /* Fill array */
422         if (isset($attrs["description"][0])){
423           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
424         } elseif (isset($attrs["uid"][0])) {
425           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
426         } else {
427           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
428         }
429       }
430     }
431     uasort ($this->objects, 'sort_list');
432     reset ($this->objects);
434     
435     /*###########
436       Build member list and try to detect obsolete entries 
437       ###########*/
439     $this->memberList = array();
440   
441     /* Walk through all single member entry */
442     foreach($this->member as $dn){
444       /* The dn for the current member can't be resolved 
445          it seams that this entry was removed 
446        */ 
447       /* Try to resolv the entry again, if it still fails, display error msg */
448       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
450       /* It has failed, add entry with type flag I (Invalid)*/
451       if ($ldap->error != "Success"){
452         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
454       } else {
456         /* Append this entry to our all object list */
458         /* Fetch object */
459         $attrs= $ldap->fetch();
461         $type= $this->getObjectType($attrs);
462         $name= $this->getObjectName($attrs);
464         if (isset($attrs["description"][0])){
465           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
466         } elseif (isset($attrs["uid"][0])) {
467           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
468         } else {
469           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
470         }
471         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
472         if(isset($attrs['uid'])){
473           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
474         }
476         /* Fill array */
477         if (isset($attrs["description"][0])){
478           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
479         } else {
480           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
481         }
483         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
484       }
485     }
486     uasort ($this->memberList, 'sort_list');
487     reset ($this->memberList);
489     /* Assemble types of currently combined objects */
490     $objectTypes= "";
491     foreach ($this->memberList as $dn => $desc){
493       /* Invalid object? */
494       if ($desc['type'] == 'I'){
495         continue;
496       }
498       /* Fine. Add to list. */
499       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
500         $objectTypes.= $desc['type'];
501       }
502     }
503     $this->gosaGroupObjects= "[$objectTypes]";
504   }
507   function convert_list($input)
508   {
509     $temp= "";
510     $conv= array(  "U" => "select_user.png",
511         "G" => "select_groups.png",
512         "A" => "select_application.png",
513         "D" => "select_department.png",
514         "S" => "select_server.png",
515         "W" => "select_workstation.png",
516         "T" => "select_terminal.png",
517         "F" => "select_phone.png",
518         "I" => "flag.png",
519         "P" => "select_printer.png");
521     foreach ($input as $key => $value){
522       /* Generate output */
523       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
524     }
526     return ($temp);
527   }
530   function getObjectType($attrs)
531   {
532     $type= "I";
534     foreach(array(  "U" => "gosaAccount",
535           "G" => "posixGroup",
536           "A" => "gosaApplication",
537           "D" => "gosaDepartment",
538           "S" => "goServer",
539           "W" => "gotoWorkstation",
540           "T" => "gotoTerminal",
541           "F" => "goFonHardware",
542           "P" => "gotoPrinter") as $index => $class){
543       if (in_array($class, $attrs['objectClass'])){
544         $type= $index;
545         break;
546       }
547     }
549     return ($type);
550   }
553   function getObjectName($attrs)
554   {
555     /* Person? */
556     $name =""; 
557     if (in_array('gosaAccount', $attrs['objectClass'])){
558       if(isset($attrs['sn']) && isset($attrs['givenName'])){
559         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
560       } else {
561         $name= $attrs['uid'][0];
562       }
563     } else {
564       if(isset($attrs["cn"][0])) {
565         $name= $attrs['cn'][0];
566       } else {
567         $name= $attrs['ou'][0];
568       }
569     }
571     return ($name);
572   }
575   function check()
576   {
577     /* Call common method to give check the hook */
578     $message= plugin::check();
580     /* Permissions for that base? */
581     if ($this->base != ""){
582       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
583     } else {
584       $new_dn= $this->dn;
585     }
588     $ldap = $this->config->get_ldap_link();
589     if($this->dn != $new_dn){
590       $ldap->cat ($new_dn, array('dn'));
591     }
592     
593     if($ldap->count() !=0){
594       $message[]= _("There is already an object with this cn.");
595     } 
597     $ui= get_userinfo();
598     $acl= get_permissions ($new_dn, $ui->subtreeACL);
599     $acl= get_module_permission($acl, "group", $new_dn);
600     if (chkacl($acl, "create") != ""){
601       $message[]= _("You have no permissions to create a group on this 'Base'.");
602     }
604     /* must: cn */
605     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
606       $message[]= "The required field 'Name' is not set.";
607     }
609     /* To many different object types? */
610     if (strlen($this->gosaGroupObjects) > 4){
611       $message[]= _("You can combine two different object types at maximum only!");
612     }
614     return ($message);
615   }
618   /* Save to LDAP */
619   function save()
620   {
621     plugin::save();
623     /* Move members to target array */
624     $this->attrs['member'] =array();
625     foreach ($this->member as $key => $desc){
626       $this->attrs['member'][]= @LDAP::fix($key);
627     }
629     $ldap= $this->config->get_ldap_link();
631     /* New accounts need proper 'dn', propagate it to remaining objects */
632     if ($this->dn == 'new'){
633       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
634     }
636     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
637        new entries. So do a check first... */
638     $ldap->cat ($this->dn, array('dn'));
639     if ($ldap->fetch()){
640       /* Modify needs array() to remove values :-( */
641       if (!count ($this->member)){
642         $this->attrs['member']= array();
643       }
644       $mode= "modify";
645     } else {
646       $mode= "add";
647       $ldap->cd($this->config->current['BASE']);
648       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
649     }
651     /* Write back to ldap */
652     $ldap->cd($this->dn);
653     $this->cleanup();
654     $ldap->$mode($this->attrs);
656     /* Trigger post signal */
657     $this->handle_post_events($mode);
659     $ret= 0;
660     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
661       $ret= 1;
662     }
664     return ($ret);
665   }
667   function remove_from_parent()
668   {
669     plugin::remove_from_parent();
671     $ldap= $this->config->get_ldap_link();
672     $ldap->rmdir($this->dn);
673     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
675     /* Trigger remove signal */
676     $this->handle_post_events("remove");
677   }
679   function getCopyDialog()
680   {
681     $str  = "";
682     $str .= _("Group name");
683     $str .= "&nbsp;<input type='text' name='cn' value='".$this->cn."'>";
684     return($str);
685   }
687   function saveCopyDialog()
688   {
689     if(isset($_POST['cn'])){
690       $this->cn = $_POST['cn'];
691     }
692   }
695 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
696 ?>