Code

Fixed objectgroup handling
[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 $allobjects= 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");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     /* Load member objects */
44     if (isset($this->attrs['member'])){
45       foreach ($this->attrs['member'] as $key => $value){
46         if ("$key" != "count"){
47           $this->member["$value"]= "$value";
48         }
49       }
50     }
51     $this->is_account= TRUE;
53     /* Get global filter config */
54     if (!is_global("ogfilter")){
55       $ui= get_userinfo();
56       $base= get_base_from_people($ui->dn);
57       $ogfilter= array( "dselect"       => $base,
58           "regex"           => "*");
59       register_global("ogfilter", $ogfilter);
60     }
61     $ogfilter= get_global('ogfilter');
63     /* Adjust flags */
64     foreach( array(   "U" => "accounts",
65           "G" => "groups",
66           "A" => "applications",
67           "D" => "departments",
68           "S" => "servers",
69           "W" => "workstations",
70           "T" => "terminals",
71           "F" => "phones",
72           "P" => "printers") as $key => $val){
74       if (preg_match("/$key/", $this->gosaGroupObjects)){
75         $ogfilter[$val]= "checked";
76       } else {
77         $ogfilter[$val]= "";
78       }
79     }
80     register_global("ogfilter", $ogfilter);
81   
82     if(isset($_SESSION['ogroupfilter']['depselect'])){
83      $this->base = $_SESSION['ogroupfilter']['depselect'];
84     }
86     /* Load member data */
87     $this->reload();
88   }
90   function AddDelMembership(){
91      /* Delete objects from group */
92     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
93       foreach ($_POST['members'] as $value){
94         $this->objects["$value"]= $this->memberList[$value];
95         unset ($this->memberList["$value"]);
96         unset ($this->member["$value"]);
97         uasort ($this->objects, 'sort_list');
98         reset ($this->objects);
99       }
100       $this->reload();
101     }
103     /* Add objects to group */
104     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
105       foreach ($_POST['objects'] as $value){
106         $this->memberList["$value"]= $this->objects[$value];
107         $this->member["$value"]= $value;
108         unset ($this->objects[$value]);
109         uasort ($this->memberList, 'sort_list');
110         reset ($this->memberList);
111       }
112       $this->reload();
113     }
114   }
116   function execute()
117   {
118     
119     $this->reload();
121     /* Do we represent a valid group? */
122     if (!$this->is_account){
123       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
124         _("This 'dn' is no object group.")."</b>";
125       return ($display);
126     }
128     /* Delete objects from group */
129     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
130       foreach ($_POST['members'] as $value){
131         if(isset($this->memberList[$value])){
132           $this->objects["$value"]= $this->memberList[$value];
133           unset ($this->memberList["$value"]);
134           unset ($this->member["$value"]);
135           uasort ($this->objects, 'sort_list');
136           reset ($this->objects);
137         }
138       }
139       $this->reload();
140     }
142     /* Add objects to group */
143     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
144       foreach ($_POST['objects'] as $value){
145         if(isset($this->objects[$value])){
146           $this->memberList["$value"]= $this->objects[$value];
147           $this->member["$value"]= $value;
148           unset ($this->objects[$value]);
149           uasort ($this->memberList, 'sort_list');
150           reset ($this->memberList);
151         }
152       }
153       $this->reload();
154     }
156     /* Load templating engine */
157     $smarty= get_smarty();
159     /* Add objects? */
160     if (isset($_POST["edit_membership"])){
161       $this->group_dialog= TRUE;
162       $this->dialog= TRUE;
163     }
165     /* Add objects finished? */
166     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
167       $this->group_dialog= FALSE;
168       $this->dialog= FALSE;
169     }
171     /* Manage object add dialog */
172     if ($this->group_dialog){
174       /* Save data */
175       $ogfilter= get_global("ogfilter");
176       foreach( array("dselect", "regex") as $type){
177         if (isset($_POST[$type])){
178           $ogfilter[$type]= $_POST[$type];
179         }
180       }
181       if (isset($_POST['dselect'])){
182         foreach( array("accounts", "groups", "applications", "departments",
183               "servers", "workstations", "terminals", "printers",
184               "phones") as $type){
186           if (isset($_POST[$type])) {
187             $ogfilter[$type]= "checked";
188           } else {
189             $ogfilter[$type]= "";
190           }
191         }
192       }
193       if (isset($_GET['search'])){
194         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
195         if ($s == "**"){
196           $s= "*";
197         }
198         $ogfilter['regex']= $s;
199       }
200       register_global("ogfilter", $ogfilter);
201       $this->reload();
203       /* Calculate actual groups */
204       $smarty->assign("objects", $this->convert_list($this->objects));
206       /* Show dialog */
207       $smarty->assign("search_image", get_template_path('images/search.png'));
208       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
209       $smarty->assign("tree_image", get_template_path('images/tree.png'));
210       $smarty->assign("deplist", $this->config->idepartments);
211       $smarty->assign("alphabet", generate_alphabet());
212       foreach( array("dselect", "regex", "accounts", "groups", "applications",
213             "departments", "servers", "workstations", "terminals",
214             "printers", "phones") as $type){
215         $smarty->assign("$type", $ogfilter[$type]);
216       }
217       $smarty->assign("hint", print_sizelimit_warning());
218       $smarty->assign("apply", apply_filter());
220       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
221       return ($display);
222     }
224     /* Bases / Departments */
225    
226       if (isset($_POST['base'])){
227         $this->base= $_POST['base'];
228       }
230     /* Assemble combine string */
231     if ($this->gosaGroupObjects == "[]"){
232       $smarty->assign("combinedObjects", _("none"));
233     } elseif (strlen($this->gosaGroupObjects) > 4){
234       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
235     } else {
236       $conv= array(   "U" => _("users"),
237           "G" => _("groups"),
238           "A" => _("applications"),
239           "D" => _("departments"),
240           "S" => _("servers"),
241           "W" => _("workstations"),
242           "T" => _("terminals"),
243           "F" => _("phones"),
244           "P" => _("printers"));
246       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
247       $p1= $conv[$type[0]];
248       error_reporting(0);
249       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
250         $p2= $conv[$type[1]];
251         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
252       } else {
253         $smarty->assign("combinedObjects", "$p1");
254       }
255       error_reporting(E_ALL);
256     }
258     /* Assign variables */
259     $smarty->assign("bases", $this->config->idepartments);
260     $smarty->assign("base_select", $this->base);
261     $smarty->assign("department", $this->department);
262     $smarty->assign("members", $this->convert_list($this->memberList));
264     /* Objects have to be tuned... */
265     $smarty->assign("objects", $this->convert_list($this->objects));
267     /* Fields */
268     foreach ($this->attributes as $val){
269       $smarty->assign("$val", $this->$val);
270       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
271     }
273     /* Assign ACL's */
274     foreach (array("base", "members") as $val){
275       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
276     }
278     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
279   }
282   /* Save data to object */
283   function save_object()
284   {
285     /* Save additional values for possible next step */
286     if (isset($_POST['ogroupedit'])){
287       plugin::save_object();
289       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
290         $this->base= $_POST["base"];
291       }
293     }
294   }
297   /* (Re-)Load objects */
298   function reload()
299   {
300     /* Generate object list */
301     $this->objects= array();
302     $this->allobjects= array();
303     $ldap= $this->config->get_ldap_link();
305     /* Assemble filter */
306     $ogfilter= get_global("ogfilter");
308     $ldap->cd ($ogfilter['dselect']);
310     $filter= "";
311     if ($ogfilter['accounts'] == "checked"){
312       $filter.= "(objectClass=gosaAccount)";
313     }
314     if ($ogfilter['groups'] == "checked"){
315       $filter.= "(objectClass=posixGroup)";
316     }
317     if ($ogfilter['applications'] == "checked"){
318       $filter.= "(objectClass=gosaApplication)";
319     }
320     if ($ogfilter['departments'] == "checked"){
321       $filter.= "(objectClass=gosaDepartment)";
322     }
323     if ($ogfilter['servers'] == "checked"){
324       $filter.= "(objectClass=goServer)";
325     }
326     if ($ogfilter['workstations'] == "checked"){
327       $filter.= "(objectClass=gotoWorkstation)";
328     }
329     if ($ogfilter['terminals'] == "checked"){
330       $filter.= "(objectClass=gotoTerminal)";
331     }
332     if ($ogfilter['printers'] == "checked"){
333       $filter.= "(objectClass=gotoPrinter)";
334     }
335     if ($ogfilter['phones'] == "checked"){
336       $filter.= "(objectClass=goFonHardware)";
337     }
338     $regex= $ogfilter['regex'];
340     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
341     while ($attrs= $ldap->fetch()){
343       /* Get type */
344       $type= $this->getObjectType($attrs);
345       $name= $this->getObjectName($attrs);
347       /* Fill array */
348       if (isset($attrs["description"][0])){
349         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
350       } elseif (isset($attrs["uid"][0])) {
351         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
352       } else {
353         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
354       }
355     }
356     uasort ($this->objects, 'sort_list');
357     reset ($this->objects);
359     $ldap->cd ($this->config->current['BASE']);
360     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
361     $regex= "*";
363     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
364     while ($attrs= $ldap->fetch()){
366       /* Get type */
367       $type= $this->getObjectType($attrs);
368       $name= $this->getObjectName($attrs);
370       /* Fill array */
371       if (isset($attrs["description"][0])){
372         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
373       } elseif (isset($attrs["uid"][0])) {
374         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
375       } else {
376         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
377       }
378       $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
379       if(isset($attrs['uid'])){
380         $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
381       }
382     }
383     uasort ($this->allobjects, 'sort_list');
384     reset ($this->allobjects);
386     /* Build member list */
387     $this->memberList= array();
388     foreach($this->member as $dn){
390       /* Object in object list? */
391       if (isset($this->allobjects[$dn])){
392         $this->memberList[$dn]= $this->allobjects[$dn];
393         if (isset ($this->objects[$dn])){
394           unset ($this->objects[$dn]);
395         }
397       } else {
399         /* No, try to ge informations from LDAP */
400         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
401         if ($ldap->error != "success"){
402           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
403               "type" => "I");
404         } else {
405           $ldap->cat($dn);
406           $attrs= $ldap->fetch();
407           $type= $this->getObjectType($attrs);
408           $name= $this->getObjectName($attrs);
410           /* Fill array */
411           if (isset($attrs["description"][0])){
412             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
413           } else {
414             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
415           }
417         }
418       }
419     }
420     uasort ($this->memberList, 'sort_list');
421     reset ($this->memberList);
423     /* Assemble types of currently combined objects */
424     $objectTypes= "";
425     foreach ($this->memberList as $dn => $desc){
427       /* Invalid object? */
428       if ($desc['type'] == 'I'){
429         continue;
430       }
432       /* Fine. Add to list. */
433       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
434         $objectTypes.= $desc['type'];
435       }
436     }
437     $this->gosaGroupObjects= "[$objectTypes]";
438   }
441   function convert_list($input)
442   {
443     $temp= "";
444     $conv= array(  "U" => "select_user.png",
445         "G" => "select_groups.png",
446         "A" => "select_application.png",
447         "D" => "select_department.png",
448         "S" => "select_server.png",
449         "W" => "select_workstation.png",
450         "T" => "select_terminal.png",
451         "F" => "select_phone.png",
452         "I" => "flag.png",
453         "P" => "select_printer.png");
455     foreach ($input as $key => $value){
456       /* Generate output */
457       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
458     }
460     return ($temp);
461   }
464   function getObjectType($attrs)
465   {
466     $type= "I";
468     foreach(array(  "U" => "gosaAccount",
469           "G" => "posixGroup",
470           "A" => "gosaApplication",
471           "D" => "gosaDepartment",
472           "S" => "goServer",
473           "W" => "gotoWorkstation",
474           "T" => "gotoTerminal",
475           "F" => "goFonHardware",
476           "P" => "gotoPrinter") as $index => $class){
477       if (in_array($class, $attrs['objectClass'])){
478         $type= $index;
479         break;
480       }
481     }
483     return ($type);
484   }
487   function getObjectName($attrs)
488   {
489     /* Person? */
490     if (in_array('gosaAccount', $attrs['objectClass'])){
491       if(isset($attrs['sn']) && isset($attrs['givenName'])){
492         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
493       } else {
494         $name= $attrs['uid'][0];
495       }
496     } else {
497       if(isset($attrs["cn"][0])) {
498         $name= $attrs['cn'][0];
499       } else {
500         $name= $attrs['ou'][0];
501       }
502     }
504     return ($name);
505   }
508   function check()
509   {
510     $message= array();
512     /* Permissions for that base? */
513     if ($this->base != ""){
514       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
515     } else {
516       $new_dn= $this->dn;
517     }
519     $ui= get_userinfo();
520     $acl= get_permissions ($new_dn, $ui->subtreeACL);
521     $acl= get_module_permission($acl, "group", $new_dn);
522     if (chkacl($acl, "create") != ""){
523       $message[]= _("You have no permissions to create a group on this 'Base'.");
524     }
526     /* must: cn */
527     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
528       $message[]= "The required field 'Name' is not set.";
529     }
531     /* To many different object types? */
532     if (strlen($this->gosaGroupObjects) > 4){
533       $message[]= _("You can combine two different object types at maximum only!");
534     }
536     return ($message);
537   }
540   /* Save to LDAP */
541   function save()
542   {
543     plugin::save();
545     /* Move members to target array */
546     foreach ($this->member as $key => $desc){
547       $this->attrs['member'][]= $key;
548     }
550     $ldap= $this->config->get_ldap_link();
552     /* New accounts need proper 'dn', propagate it to remaining objects */
553     if ($this->dn == 'new'){
554       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
555     }
557     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
558        new entries. So do a check first... */
559     $ldap->cat ($this->dn);
560     if ($ldap->fetch()){
561       /* Modify needs array() to remove values :-( */
562       if (!count ($this->member)){
563         $this->attrs['member']= array();
564       }
565       $mode= "modify";
566     } else {
567       $mode= "add";
568       $ldap->cd($this->config->current['BASE']);
569       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
570     }
572     /* Write back to ldap */
573     $ldap->cd($this->dn);
574     $ldap->$mode($this->attrs);
576     /* Trigger post signal */
577     $this->handle_post_events($mode);
579     $ret= 0;
580     if (show_ldap_error($ldap->get_error())){
581       $ret= 1;
582     }
584     return ($ret);
585   }
587   function remove_from_parent()
588   {
589     plugin::remove_from_parent();
591     $ldap= $this->config->get_ldap_link();
592     $ldap->rmdir($this->dn);
593     show_ldap_error($ldap->get_error());
595     /* Trigger remove signal */
596     $this->handle_post_events("remove");
597   }
601 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
602 ?>