Code

Hide not allowed options
[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           "_" => "subtrees",
75           "P" => "printers") as $key => $val){
77       if (preg_match("/$key/", $this->gosaGroupObjects)){
78         $ogfilter[$val]= "checked";
79       } else {
80         $ogfilter[$val]= "";
81       }
82     }
83     register_global("ogfilter", $ogfilter);
84  
85     /* Set base */
86     if ($this->dn == "new"){
87       $this->base= $_SESSION['CurrentMainBase'];
88     } else {
89       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
90     }
92     /* set permissions */
93     $ui= get_userinfo();
94     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
95     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
97     /* Load member data */
98     $this->reload();
99   }
101   function AddDelMembership($NewMember = false)
102   {
103     if($NewMember){
104       $this->importMember($NewMember);
105       $this->memberList[$NewMember]= $this->objcache[$NewMember];
106       $this->member[$NewMember]= $NewMember;
107       unset ($this->objects[$NewMember]);
108       uasort ($this->memberList, 'sort_list');
109       reset ($this->memberList);
110       $this->reload();
111     }else{
112       /* Delete objects from group */
113       if (isset($_POST['delete_membership']) && isset($_POST['members'])  && chkacl($this->acl,"gosaGroupObjects") == ""){
114         foreach ($_POST['members'] as $value){
115           $this->objects["$value"]= $this->memberList[$value];
116           unset ($this->memberList["$value"]);
117           unset ($this->member["$value"]);
118           uasort ($this->objects, 'sort_list');
119           reset ($this->objects);
120         }
121         $this->reload();
122       }
124       /* Add objects to group */
125       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])  && chkacl($this->acl,"gosaGroupObjects") == ""){
126         foreach ($_POST['objects'] as $value){
127           $this->memberList["$value"]= $this->objects[$value];
128           $this->member["$value"]= $value;
129           unset ($this->objects[$value]);
130           uasort ($this->memberList, 'sort_list');
131           reset ($this->memberList);
132         }
133         $this->reload();
134       }
135     }
136   }
138   function execute()
139   {
140         /* Call parent execute */
141         plugin::execute();
143 //    $this->reload();
145     /* Do we represent a valid group? */
146     if (!$this->is_account){
147       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
148         _("This 'dn' is no object group.")."</b>";
149       return ($display);
150     }
152     /* Delete objects from group */
153     if (isset($_POST['delete_membership']) && isset($_POST['members']) && chkacl($this->acl,"gosaGroupObjects") == ""){
154       foreach ($_POST['members'] as $value){
155         if(isset($this->memberList[$value])){
156           $this->objects["$value"]= $this->memberList[$value];
157           unset ($this->memberList["$value"]);
158           unset ($this->member["$value"]);
159           uasort ($this->objects, 'sort_list');
160           reset ($this->objects);
161         }
162       }
163       $this->reload();
164     }
166     /* Add objects to group */
167     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
168       foreach ($_POST['objects'] as $value){
169         if(isset($this->objects[$value])){
170           $this->memberList["$value"]= $this->objects[$value];
171           $this->member["$value"]= $value;
172           unset ($this->objects[$value]);
173           uasort ($this->memberList, 'sort_list');
174           reset ($this->memberList);
175         }
176       }
177       $this->reload();
178     }
180     /* Load templating engine */
181     $smarty= get_smarty();
183     /* Base select dialog */
184     $once = true;
185     foreach($_POST as $name => $value){
186       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
187         $once = false;
188         $this->dialog = new baseSelectDialog($this->config);
189         $this->dialog->setCurrentBase($this->base);
190       }
191     }
193     /* Dialog handling */
194     if(is_object($this->dialog)){
195       /* Must be called before save_object */
196       $this->dialog->save_object();
198       if($this->dialog->isClosed()){
199         $this->dialog = false;
200       }elseif($this->dialog->isSelected()){
201         $this->base = $this->dialog->isSelected();
202         $this->dialog= false;
203       }else{
204         return($this->dialog->execute());
205       }
206     }
208     /* Add objects? */
209     if (isset($_POST["edit_membership"])  && chkacl($this->acl,"gosaGroupObjects") == ""){
210       $this->group_dialog= TRUE;
211       $this->dialog= TRUE;
212     }
214     /* Add objects finished? */
215     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
216       $this->group_dialog= FALSE;
217       $this->dialog= FALSE;
218     }
220     /* Manage object add dialog */
221     if ($this->group_dialog){
223       /* Save data */
224       $ogfilter= get_global("ogfilter");
225       foreach( array("dselect", "regex") as $type){
226         if (isset($_POST[$type])){
227           $ogfilter[$type]= $_POST[$type];
228         }
229       }
230       if (isset($_POST['dselect'])){
231         foreach( array("accounts", "groups", "applications", "departments",
232               "servers", "workstations", "terminals", "printers","subtrees",
233               "phones") as $type){
235           if (isset($_POST[$type])) {
236             $ogfilter[$type]= "checked";
237           } else {
238             $ogfilter[$type]= "";
239           }
240         }
241       }
242       if (isset($_GET['search'])){
243         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
244         if ($s == "**"){
245           $s= "*";
246         }
247         $ogfilter['regex']= $s;
248       }
249       register_global("ogfilter", $ogfilter);
250       $this->reload();
252       /* Calculate actual groups */
253       $smarty->assign("objects", $this->convert_list($this->objects));
255       /* Show dialog */
256       $smarty->assign("search_image", get_template_path('images/search.png'));
257       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
258       $smarty->assign("tree_image", get_template_path('images/tree.png'));
259       $smarty->assign("deplist", $this->config->idepartments);
260       $smarty->assign("alphabet", generate_alphabet());
261       foreach( array("dselect", "regex", "accounts", "groups", "applications",
262             "departments", "servers", "workstations", "terminals","subtrees",
263             "printers", "phones") as $type){
264         $smarty->assign("$type", $ogfilter[$type]);
265       }
266       $smarty->assign("hint", print_sizelimit_warning());
267       $smarty->assign("apply", apply_filter());
269       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
270       return ($display);
271     }
273     /* Bases / Departments */
274     if (isset($_POST['base']) && chkacl($this->acl,"base") == ""){
275       $this->base= $_POST['base'];
276     }
278     /* Assemble combine string */
279     if ($this->gosaGroupObjects == "[]"){
280       $smarty->assign("combinedObjects", _("none"));
281     } elseif (strlen($this->gosaGroupObjects) > 4){
282       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
283     } else {
284       $conv= array(   "U" => _("users"),
285           "G" => _("groups"),
286           "A" => _("applications"),
287           "D" => _("departments"),
288           "S" => _("servers"),
289           "W" => _("workstations"),
290           "T" => _("terminals"),
291           "F" => _("phones"),
292           "P" => _("printers"));
294       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
295       $p1= $conv[$type[0]];
296       error_reporting(0);
297       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
298         $p2= $conv[$type[1]];
299         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
300       } else {
301         $smarty->assign("combinedObjects", "$p1");
302       }
303       error_reporting(E_ALL);
304     }
306     /* Assign variables */
307     $smarty->assign("bases", $this->config->idepartments);
308     $smarty->assign("base_select", $this->base);
309     $smarty->assign("department", $this->department);
310     $smarty->assign("members", $this->convert_list($this->memberList));
312     /* Objects have to be tuned... */
313     $smarty->assign("objects", $this->convert_list($this->objects));
315     /* Fields */
316     foreach ($this->attributes as $val){
317       $smarty->assign("$val", $this->$val);
318       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
319     }
321     /* Assign ACL's */
322     foreach (array("base", "members") as $val){
323       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
324     }
326     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
327   }
330   /* Save data to object */
331   function save_object()
332   {
333     /* Save additional values for possible next step */
334     if (isset($_POST['ogroupedit'])){
335       plugin::save_object();
337       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
338         $this->base= $_POST["base"];
339       }
340     }
341   }
344   /* (Re-)Load objects */
345   function reload()
346   {
347     /*###########
348       Variable initialisation 
349       ###########*/
351     $this->objects                = array();
352     $this->ui                     = get_userinfo();
353     $filter                       = "";
354     $objectClasses                = array();
355     
356     $ogfilter               = get_global("ogfilter");
357     $regex                  = $ogfilter['regex'];
359     /* Get ldap connection */
360     $ldap= $this->config->get_ldap_link();
361     $ldap->cd ($ogfilter['dselect']);
364     /*###########
365       Generate Filter 
366       ###########*/
368     /* Assemble filter */
369     if ($ogfilter['accounts'] == "checked"){
370       $filter.= "(objectClass=gosaAccount)";
371       $objectClasses["gosaAccount"]     = get_people_ou();
372     }
373     if ($ogfilter['groups'] == "checked"){
374       $filter.= "(objectClass=posixGroup)";
375       $objectClasses["posixGroup"]      = get_groups_ou();
376     }
377     if ($ogfilter['applications'] == "checked"){
378       $filter.= "(objectClass=gosaApplication)";
379       $objectClasses["gosaApplication"] = "ou=apps,";
380     }
381     if ($ogfilter['departments'] == "checked"){
382       $filter.= "(objectClass=gosaDepartment)";
383       $objectClasses["gosaDepartment"]  = "";
384     }
385     if ($ogfilter['servers'] == "checked"){
386       $filter.= "(objectClass=goServer)";
387       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
388     }
389     if ($ogfilter['workstations'] == "checked"){
390       $filter.= "(objectClass=gotoWorkstation)";
391       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
392     }
393     if ($ogfilter['terminals'] == "checked"){
394       $filter.= "(objectClass=gotoTerminal)";
395       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
396     }
397     if ($ogfilter['printers'] == "checked"){
398       $filter.= "(objectClass=gotoPrinter)";
400       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
401     }
402     if ($ogfilter['phones'] == "checked"){
403       $filter.= "(objectClass=goFonHardware)";
404       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
405     }
408     /*###########
409       Perform search for selected objectClasses & regex to fill list with objects   
410       ###########*/
412     /* Perform search for selected objectClasses */
413     foreach($objectClasses as $class=> $basedn){
415       if($ogfilter['subtrees'] == "checked"){
416         $ldap->cd($ogfilter['dselect']);
417         $ldap->search("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",
418             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
419       }else{
420         $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
421             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
422       }
423       
424       /* fetch results and append them to the list */
425       while($attrs = $ldap->fetch()){
427         $type= $this->getObjectType($attrs);
428         $name= $this->getObjectName($attrs);
430         /* Fill array */
431         if (isset($attrs["description"][0])){
432           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
433         } elseif (isset($attrs["uid"][0])) {
434           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
435         } else {
436           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
437         }
438       }
439     }
440     uasort ($this->objects, 'sort_list');
441     reset ($this->objects);
443     
444     /*###########
445       Build member list and try to detect obsolete entries 
446       ###########*/
448     $this->memberList = array();
449   
450     /* Walk through all single member entry */
451     foreach($this->member as $dn){
452       $this->importMember($dn);
453     }
454     uasort ($this->memberList, 'sort_list');
455     reset ($this->memberList);
457     /* Assemble types of currently combined objects */
458     $objectTypes= "";
459     foreach ($this->memberList as $dn => $desc){
461       /* Invalid object? */
462       if ($desc['type'] == 'I'){
463         continue;
464       }
466       /* Fine. Add to list. */
467       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
468         $objectTypes.= $desc['type'];
469       }
470     }
471     $this->gosaGroupObjects= "[$objectTypes]";
472   }
475   function importMember($dn)
476   {
477     $ldap= $this->config->get_ldap_link();
479     /* Try to resolv the entry again, if it still fails, display error msg */
480     $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "uid", "description", "objectClass"));
482     /* It has failed, add entry with type flag I (Invalid)*/
483     if ($ldap->error != "Success"){
484       $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
485     } else {
486       /* Append this entry to our all object list */
488       /* Fetch object */
489       $attrs= $ldap->fetch();
491       $type= $this->getObjectType($attrs);
492       $name= $this->getObjectName($attrs);
494       if (isset($attrs["description"][0])){
495         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
496       } elseif (isset($attrs["uid"][0])) {
497         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
498       } else {
499         $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
500       }
501       $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
502       if(isset($attrs['uid'])){
503         $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
504       }
506       /* Fill array */
507       if (isset($attrs["description"][0])){
508         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
509       } else {
510         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
511       }
513       $this->memberList[$dn]= $this->objects[$attrs["dn"]];
514     }
515   }
517   function convert_list($input)
518   {
519     $temp= "";
520     $conv= array(  "U" => "select_user.png",
521         "G" => "select_groups.png",
522         "A" => "select_application.png",
523         "D" => "select_department.png",
524         "S" => "select_server.png",
525         "W" => "select_workstation.png",
526         "T" => "select_terminal.png",
527         "F" => "select_phone.png",
528         "I" => "flag.png",
529         "P" => "select_printer.png");
531     foreach ($input as $key => $value){
532       /* Generate output */
533       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
534     }
536     return ($temp);
537   }
540   function getObjectType($attrs)
541   {
542     $type= "I";
544     foreach(array(  "U" => "gosaAccount",
545           "G" => "posixGroup",
546           "A" => "gosaApplication",
547           "D" => "gosaDepartment",
548           "S" => "goServer",
549           "W" => "gotoWorkstation",
550           "T" => "gotoTerminal",
551           "F" => "goFonHardware",
552           "P" => "gotoPrinter") as $index => $class){
553       if (in_array($class, $attrs['objectClass'])){
554         $type= $index;
555         break;
556       }
557     }
559     return ($type);
560   }
563   function getObjectName($attrs)
564   {
565     /* Person? */
566     $name =""; 
567     if (in_array('gosaAccount', $attrs['objectClass'])){
568       if(isset($attrs['sn']) && isset($attrs['givenName'])){
569         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
570       } else {
571         $name= $attrs['uid'][0];
572       }
573     } else {
574       if(isset($attrs["cn"][0])) {
575         $name= $attrs['cn'][0];
576       } else {
577         $name= $attrs['ou'][0];
578       }
579     }
581     return ($name);
582   }
585   function check()
586   {
587     /* Call common method to give check the hook */
588     $message= plugin::check();
590     /* Permissions for that base? */
591     if ($this->base != ""){
592       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
593     } else {
594       $new_dn= $this->dn;
595     }
598     $ldap = $this->config->get_ldap_link();
599     if($this->dn != $new_dn){
600       $ldap->cat ($new_dn, array('dn'));
601     }
602     
603     if($ldap->count() !=0){
604       $message[]= _("There is already an object with this cn.");
605     } 
607     $ui= get_userinfo();
608     $acl= get_permissions ($new_dn, $ui->subtreeACL);
609     $acl= get_module_permission($acl, "group", $new_dn);
610     if (chkacl($acl, "create") != "" && $this->dn=="new"){
611       $message[]= _("You have no permissions to create a group on this 'Base'.");
612     }
614     /* must: cn */
615     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
616       $message[]= "The required field 'Name' is not set.";
617     }
619     /* To many different object types? */
620     if (strlen($this->gosaGroupObjects) > 4){
621       $message[]= _("You can combine two different object types at maximum only!");
622     }
624     return ($message);
625   }
628   /* Save to LDAP */
629   function save()
630   {
631     plugin::save();
633     /* Move members to target array */
634     $this->attrs['member'] =array();
635     foreach ($this->member as $key => $desc){
636       $this->attrs['member'][]= @LDAP::fix($key);
637     }
639     $ldap= $this->config->get_ldap_link();
641     /* New accounts need proper 'dn', propagate it to remaining objects */
642     if ($this->dn == 'new'){
643       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
644     }
646     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
647        new entries. So do a check first... */
648     $ldap->cat ($this->dn, array('dn'));
649     if ($ldap->fetch()){
650       /* Modify needs array() to remove values :-( */
651       if (!count ($this->member)){
652         $this->attrs['member']= array();
653       }
654       $mode= "modify";
655     } else {
656       $mode= "add";
657       $ldap->cd($this->config->current['BASE']);
658       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
659     }
661     /* Write back to ldap */
662     $ldap->cd($this->dn);
663     $this->cleanup();
664     $ldap->$mode($this->attrs);
666     /* Trigger post signal */
667     $this->handle_post_events($mode);
669     $ret= 0;
670     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
671       $ret= 1;
672     }
674     return ($ret);
675   }
677   function remove_from_parent()
678   {
679     plugin::remove_from_parent();
681     $ldap= $this->config->get_ldap_link();
682     $ldap->rmdir($this->dn);
683     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
685     /* Trigger remove signal */
686     $this->handle_post_events("remove");
687   }
689   function getCopyDialog()
690   {
691     $str = "";
693     $smarty = get_smarty();
694     $smarty->assign("cn",     $this->cn);
695     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
696     $ret = array();
697     $ret['string'] = $str;
698     $ret['status'] = "";
699     return($ret);
700   }
702   function saveCopyDialog()
703   {
704     if(isset($_POST['cn'])){
705       $this->cn = $_POST['cn'];
706     }
707   }
710 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
711 ?>