Code

a88fd660a06c2e67f182f22ba12f78e2bec2daa3
[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      /* set permissions */
87     $ui= get_userinfo();
88     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
89     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
92     /* Load member data */
93     $this->reload();
94   }
96   function AddDelMembership(){
97      /* Delete objects from group */
98     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
99       foreach ($_POST['members'] as $value){
100         $this->objects["$value"]= $this->memberList[$value];
101         unset ($this->memberList["$value"]);
102         unset ($this->member["$value"]);
103         uasort ($this->objects, 'sort_list');
104         reset ($this->objects);
105       }
106       $this->reload();
107     }
109     /* Add objects to group */
110     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
111       foreach ($_POST['objects'] as $value){
112         $this->memberList["$value"]= $this->objects[$value];
113         $this->member["$value"]= $value;
114         unset ($this->objects[$value]);
115         uasort ($this->memberList, 'sort_list');
116         reset ($this->memberList);
117       }
118       $this->reload();
119     }
120   }
122   function execute()
123   {
124         /* Call parent execute */
125         plugin::execute();
127     print $this->acl;
128     
129     $this->reload();
131     /* Do we represent a valid group? */
132     if (!$this->is_account){
133       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
134         _("This 'dn' is no object group.")."</b>";
135       return ($display);
136     }
138     /* Delete objects from group */
139     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
140       foreach ($_POST['members'] as $value){
141         if(isset($this->memberList[$value])){
142           $this->objects["$value"]= $this->memberList[$value];
143           unset ($this->memberList["$value"]);
144           unset ($this->member["$value"]);
145           uasort ($this->objects, 'sort_list');
146           reset ($this->objects);
147         }
148       }
149       $this->reload();
150     }
152     /* Add objects to group */
153     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
154       foreach ($_POST['objects'] as $value){
155         if(isset($this->objects[$value])){
156           $this->memberList["$value"]= $this->objects[$value];
157           $this->member["$value"]= $value;
158           unset ($this->objects[$value]);
159           uasort ($this->memberList, 'sort_list');
160           reset ($this->memberList);
161         }
162       }
163       $this->reload();
164     }
166     /* Load templating engine */
167     $smarty= get_smarty();
169     /* Add objects? */
170     if (isset($_POST["edit_membership"])){
171       $this->group_dialog= TRUE;
172       $this->dialog= TRUE;
173     }
175     /* Add objects finished? */
176     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
177       $this->group_dialog= FALSE;
178       $this->dialog= FALSE;
179     }
181     /* Manage object add dialog */
182     if ($this->group_dialog){
184       /* Save data */
185       $ogfilter= get_global("ogfilter");
186       foreach( array("dselect", "regex") as $type){
187         if (isset($_POST[$type])){
188           $ogfilter[$type]= $_POST[$type];
189         }
190       }
191       if (isset($_POST['dselect'])){
192         foreach( array("accounts", "groups", "applications", "departments",
193               "servers", "workstations", "terminals", "printers",
194               "phones") as $type){
196           if (isset($_POST[$type])) {
197             $ogfilter[$type]= "checked";
198           } else {
199             $ogfilter[$type]= "";
200           }
201         }
202       }
203       if (isset($_GET['search'])){
204         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
205         if ($s == "**"){
206           $s= "*";
207         }
208         $ogfilter['regex']= $s;
209       }
210       register_global("ogfilter", $ogfilter);
211       $this->reload();
213       /* Calculate actual groups */
214       $smarty->assign("objects", $this->convert_list($this->objects));
216       /* Show dialog */
217       $smarty->assign("search_image", get_template_path('images/search.png'));
218       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
219       $smarty->assign("tree_image", get_template_path('images/tree.png'));
220       $smarty->assign("deplist", $this->config->idepartments);
221       $smarty->assign("alphabet", generate_alphabet());
222       foreach( array("dselect", "regex", "accounts", "groups", "applications",
223             "departments", "servers", "workstations", "terminals",
224             "printers", "phones") as $type){
225         $smarty->assign("$type", $ogfilter[$type]);
226       }
227       $smarty->assign("hint", print_sizelimit_warning());
228       $smarty->assign("apply", apply_filter());
230       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
231       return ($display);
232     }
234     /* Bases / Departments */
235    
236       if (isset($_POST['base'])){
237         $this->base= $_POST['base'];
238       }
240     /* Assemble combine string */
241     if ($this->gosaGroupObjects == "[]"){
242       $smarty->assign("combinedObjects", _("none"));
243     } elseif (strlen($this->gosaGroupObjects) > 4){
244       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
245     } else {
246       $conv= array(   "U" => _("users"),
247           "G" => _("groups"),
248           "A" => _("applications"),
249           "D" => _("departments"),
250           "S" => _("servers"),
251           "W" => _("workstations"),
252           "T" => _("terminals"),
253           "F" => _("phones"),
254           "P" => _("printers"));
256       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
257       $p1= $conv[$type[0]];
258       error_reporting(0);
259       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
260         $p2= $conv[$type[1]];
261         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
262       } else {
263         $smarty->assign("combinedObjects", "$p1");
264       }
265       error_reporting(E_ALL);
266     }
268     /* Assign variables */
269     $smarty->assign("bases", $this->config->idepartments);
270     $smarty->assign("base_select", $this->base);
271     $smarty->assign("department", $this->department);
272     $smarty->assign("members", $this->convert_list($this->memberList));
274     /* Objects have to be tuned... */
275     $smarty->assign("objects", $this->convert_list($this->objects));
277     /* Fields */
278     foreach ($this->attributes as $val){
279       $smarty->assign("$val", $this->$val);
280       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
281     }
283     /* Assign ACL's */
284     foreach (array("base", "members") as $val){
285       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
286     }
288     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
289   }
292   /* Save data to object */
293   function save_object()
294   {
295     /* Save additional values for possible next step */
296     if (isset($_POST['ogroupedit'])){
297       plugin::save_object();
299       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
300         $this->base= $_POST["base"];
301       }
303     }
304   }
307   /* (Re-)Load objects */
308   function reload()
309   {
310     /* Generate object list */
311     $this->objects= array();
312     $this->allobjects= array();
313     $ldap= $this->config->get_ldap_link();
315     /* Assemble filter */
316     $ogfilter= get_global("ogfilter");
318     $ldap->cd ($ogfilter['dselect']);
320     $filter= "";
321     if ($ogfilter['accounts'] == "checked"){
322       $filter.= "(objectClass=gosaAccount)";
323     }
324     if ($ogfilter['groups'] == "checked"){
325       $filter.= "(objectClass=posixGroup)";
326     }
327     if ($ogfilter['applications'] == "checked"){
328       $filter.= "(objectClass=gosaApplication)";
329     }
330     if ($ogfilter['departments'] == "checked"){
331       $filter.= "(objectClass=gosaDepartment)";
332     }
333     if ($ogfilter['servers'] == "checked"){
334       $filter.= "(objectClass=goServer)";
335     }
336     if ($ogfilter['workstations'] == "checked"){
337       $filter.= "(objectClass=gotoWorkstation)";
338     }
339     if ($ogfilter['terminals'] == "checked"){
340       $filter.= "(objectClass=gotoTerminal)";
341     }
342     if ($ogfilter['printers'] == "checked"){
343       $filter.= "(objectClass=gotoPrinter)";
344     }
345     if ($ogfilter['phones'] == "checked"){
346       $filter.= "(objectClass=goFonHardware)";
347     }
348     $regex= $ogfilter['regex'];
350     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
351     while ($attrs= $ldap->fetch()){
353       /* Get type */
354       $type= $this->getObjectType($attrs);
355       $name= $this->getObjectName($attrs);
357       /* Fill array */
358       if (isset($attrs["description"][0])){
359         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
360       } elseif (isset($attrs["uid"][0])) {
361         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
362       } else {
363         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
364       }
365     }
366     uasort ($this->objects, 'sort_list');
367     reset ($this->objects);
369     $ldap->cd ($this->config->current['BASE']);
370     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
371     $regex= "*";
373     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
374     while ($attrs= $ldap->fetch()){
376       /* Get type */
377       $type= $this->getObjectType($attrs);
378       $name= $this->getObjectName($attrs);
380       /* Fill array */
381       if (isset($attrs["description"][0])){
382         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
383       } elseif (isset($attrs["uid"][0])) {
384         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
385       } else {
386         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
387       }
388       $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
389       if(isset($attrs['uid'])){
390         $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
391       }
392     }
393     uasort ($this->allobjects, 'sort_list');
394     reset ($this->allobjects);
396     /* Build member list */
397     $this->memberList= array();
398     foreach($this->member as $dn){
400       /* Object in object list? */
401       if (isset($this->allobjects[$dn])){
402         $this->memberList[$dn]= $this->allobjects[$dn];
403         if (isset ($this->objects[$dn])){
404           unset ($this->objects[$dn]);
405         }
407       } else {
409         /* No, try to ge informations from LDAP */
410         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
411         if ($ldap->error != "success"){
412           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
413               "type" => "I");
414         } else {
415           $ldap->cat($dn);
416           $attrs= $ldap->fetch();
417           $type= $this->getObjectType($attrs);
418           $name= $this->getObjectName($attrs);
420           /* Fill array */
421           if (isset($attrs["description"][0])){
422             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
423           } else {
424             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
425           }
427         }
428       }
429     }
430     uasort ($this->memberList, 'sort_list');
431     reset ($this->memberList);
433     /* Assemble types of currently combined objects */
434     $objectTypes= "";
435     foreach ($this->memberList as $dn => $desc){
437       /* Invalid object? */
438       if ($desc['type'] == 'I'){
439         continue;
440       }
442       /* Fine. Add to list. */
443       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
444         $objectTypes.= $desc['type'];
445       }
446     }
447     $this->gosaGroupObjects= "[$objectTypes]";
448   }
451   function convert_list($input)
452   {
453     $temp= "";
454     $conv= array(  "U" => "select_user.png",
455         "G" => "select_groups.png",
456         "A" => "select_application.png",
457         "D" => "select_department.png",
458         "S" => "select_server.png",
459         "W" => "select_workstation.png",
460         "T" => "select_terminal.png",
461         "F" => "select_phone.png",
462         "I" => "flag.png",
463         "P" => "select_printer.png");
465     foreach ($input as $key => $value){
466       /* Generate output */
467       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
468     }
470     return ($temp);
471   }
474   function getObjectType($attrs)
475   {
476     $type= "I";
478     foreach(array(  "U" => "gosaAccount",
479           "G" => "posixGroup",
480           "A" => "gosaApplication",
481           "D" => "gosaDepartment",
482           "S" => "goServer",
483           "W" => "gotoWorkstation",
484           "T" => "gotoTerminal",
485           "F" => "goFonHardware",
486           "P" => "gotoPrinter") as $index => $class){
487       if (in_array($class, $attrs['objectClass'])){
488         $type= $index;
489         break;
490       }
491     }
493     return ($type);
494   }
497   function getObjectName($attrs)
498   {
499     /* Person? */
500     if (in_array('gosaAccount', $attrs['objectClass'])){
501       if(isset($attrs['sn']) && isset($attrs['givenName'])){
502         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
503       } else {
504         $name= $attrs['uid'][0];
505       }
506     } else {
507       if(isset($attrs["cn"][0])) {
508         $name= $attrs['cn'][0];
509       } else {
510         $name= $attrs['ou'][0];
511       }
512     }
514     return ($name);
515   }
518   function check()
519   {
520     $message= array();
522     /* Permissions for that base? */
523     if ($this->base != ""){
524       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
525     } else {
526       $new_dn= $this->dn;
527     }
529     $ui= get_userinfo();
530     $acl= get_permissions ($new_dn, $ui->subtreeACL);
531     $acl= get_module_permission($acl, "group", $new_dn);
532     if (chkacl($acl, "create") != ""){
533       $message[]= _("You have no permissions to create a group on this 'Base'.");
534     }
536     /* must: cn */
537     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
538       $message[]= "The required field 'Name' is not set.";
539     }
541     /* To many different object types? */
542     if (strlen($this->gosaGroupObjects) > 4){
543       $message[]= _("You can combine two different object types at maximum only!");
544     }
546     return ($message);
547   }
550   /* Save to LDAP */
551   function save()
552   {
553     plugin::save();
555     /* Move members to target array */
556     foreach ($this->member as $key => $desc){
557       $this->attrs['member'][]= $key;
558     }
560     $ldap= $this->config->get_ldap_link();
562     /* New accounts need proper 'dn', propagate it to remaining objects */
563     if ($this->dn == 'new'){
564       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
565     }
567     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
568        new entries. So do a check first... */
569     $ldap->cat ($this->dn);
570     if ($ldap->fetch()){
571       /* Modify needs array() to remove values :-( */
572       if (!count ($this->member)){
573         $this->attrs['member']= array();
574       }
575       $mode= "modify";
576     } else {
577       $mode= "add";
578       $ldap->cd($this->config->current['BASE']);
579       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
580     }
582     /* Write back to ldap */
583     $ldap->cd($this->dn);
584     $ldap->$mode($this->attrs);
586     /* Trigger post signal */
587     $this->handle_post_events($mode);
589     $ret= 0;
590     if (show_ldap_error($ldap->get_error())){
591       $ret= 1;
592     }
594     return ($ret);
595   }
597   function remove_from_parent()
598   {
599     plugin::remove_from_parent();
601     $ldap= $this->config->get_ldap_link();
602     $ldap->rmdir($this->dn);
603     show_ldap_error($ldap->get_error());
605     /* Trigger remove signal */
606     $this->handle_post_events("remove");
607   }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>