Code

Some acl fixes
[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     if(isset($_SESSION['CurrentMainBase'])){
86      $this->base = $_SESSION['CurrentMainBase'];
87     }
89     /* Set base */
90     if ($this->dn == "new"){
91       $this->base= $_SESSION['CurrentMainBase'];
92     } else {
93       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
94     }
96     /* Load member data */
97     $this->reload();
98   }
100   function AddDelMembership($NewMember = false){
102     if($NewMember){
103       $this->memberList[$NewMember]= $this->objcache[$NewMember];
104       $this->member[$NewMember]= $NewMember;
105       unset ($this->objects[$NewMember]);
106       uasort ($this->memberList, 'sort_list');
107       reset ($this->memberList);
108       $this->reload();
109     }else{
110       /* Delete objects from group */
111       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
112         foreach ($_POST['members'] as $value){
113           $this->objects["$value"]= $this->memberList[$value];
114           unset ($this->memberList["$value"]);
115           unset ($this->member["$value"]);
116           uasort ($this->objects, 'sort_list');
117           reset ($this->objects);
118         }
119         $this->reload();
120       }
122       /* Add objects to group */
123       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
124         foreach ($_POST['objects'] as $value){
125           $this->memberList["$value"]= $this->objects[$value];
126           $this->member["$value"]= $value;
127           unset ($this->objects[$value]);
128           uasort ($this->memberList, 'sort_list');
129           reset ($this->memberList);
130         }
131         $this->reload();
132       }
133     }
134   }
136   function execute()
137   {
138         /* Call parent execute */
139         plugin::execute();
141 //    $this->reload();
143     /* Do we represent a valid group? */
144     if (!$this->is_account){
145       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
146         _("This 'dn' is no object group.")."</b>";
147       return ($display);
148     }
150     /* Delete objects from group */
151     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
152       foreach ($_POST['members'] as $value){
153         if(isset($this->memberList[$value])){
154           $this->objects["$value"]= $this->memberList[$value];
155           unset ($this->memberList["$value"]);
156           unset ($this->member["$value"]);
157           uasort ($this->objects, 'sort_list');
158           reset ($this->objects);
159         }
160       }
161       $this->reload();
162     }
164     /* Add objects to group */
165     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
166       foreach ($_POST['objects'] as $value){
167         if(isset($this->objects[$value])){
168           $this->memberList["$value"]= $this->objects[$value];
169           $this->member["$value"]= $value;
170           unset ($this->objects[$value]);
171           uasort ($this->memberList, 'sort_list');
172           reset ($this->memberList);
173         }
174       }
175       $this->reload();
176     }
178     /* Load templating engine */
179     $smarty= get_smarty();
181     $tmp = $this->plInfo();
182     foreach($tmp['plProvidedAcls'] as $name => $translation){
183       $smarty->assign($name."ACL",$this->getacl($name));
184     }
186     /* Create base acls */
187     $baseACL = $this->getacl("base",(!is_object($this->parent) && !isset($_SESSION['edit'])));
188     if(!$this->acl_is_moveable()) {
189       $baseACL = preg_replace("/w/","",$baseACL);
190     }
191     $smarty->assign("baseACL",          $baseACL);
193     /* Base select dialog */
194     $once = true;
195     foreach($_POST as $name => $value){
196       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
197         $once = false;
198         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
199         $this->dialog->setCurrentBase($this->base);
200       }
201     }
203     /* Dialog handling */
204     if(is_object($this->dialog) && $this->acl_is_moveable()){
205       /* Must be called before save_object */
206       $this->dialog->save_object();
208       if($this->dialog->isClosed()){
209         $this->dialog = false;
210       }elseif($this->dialog->isSelected()){
211         $this->base = $this->dialog->isSelected();
212         $this->dialog= false;
213       }else{
214         return($this->dialog->execute());
215       }
216     }
218     /* Add objects? */
219     if (isset($_POST["edit_membership"])){
220       $this->group_dialog= TRUE;
221       $this->dialog= TRUE;
222     }
224     /* Add objects finished? */
225     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
226       $this->group_dialog= FALSE;
227       $this->dialog= FALSE;
228     }
230     /* Manage object add dialog */
231     if ($this->group_dialog){
233       /* Save data */
234       $ogfilter= get_global("ogfilter");
235       foreach( array("dselect", "regex") as $type){
236         if (isset($_POST[$type])){
237           $ogfilter[$type]= $_POST[$type];
238         }
239       }
240       if (isset($_POST['dselect'])){
241         foreach( array("accounts", "groups", "applications", "departments",
242               "servers", "workstations", "terminals", "printers","subtrees",
243               "phones") as $type){
245           if (isset($_POST[$type])) {
246             $ogfilter[$type]= "checked";
247           } else {
248             $ogfilter[$type]= "";
249           }
250         }
251       }
252       if (isset($_GET['search'])){
253         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
254         if ($s == "**"){
255           $s= "*";
256         }
257         $ogfilter['regex']= $s;
258       }
259       register_global("ogfilter", $ogfilter);
260       $this->reload();
262       /* Calculate actual groups */
263       $smarty->assign("objects", $this->convert_list($this->objects));
265       /* Show dialog */
266       $smarty->assign("search_image", get_template_path('images/search.png'));
267       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
268       $smarty->assign("tree_image", get_template_path('images/tree.png'));
269       $smarty->assign("deplist", $this->config->idepartments);
270       $smarty->assign("alphabet", generate_alphabet());
271       foreach( array("dselect", "regex", "accounts", "groups", "applications",
272             "departments", "servers", "workstations", "terminals","subtrees",
273             "printers", "phones") as $type){
274         $smarty->assign("$type", $ogfilter[$type]);
275       }
276       $smarty->assign("hint", print_sizelimit_warning());
277       $smarty->assign("apply", apply_filter());
279       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
280       return ($display);
281     }
283     /* Bases / Departments */
284       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
285         $this->base= $_POST['base'];
286       }
288     /* Assemble combine string */
289     if ($this->gosaGroupObjects == "[]"){
290       $smarty->assign("combinedObjects", _("none"));
291     } elseif (strlen($this->gosaGroupObjects) > 4){
292       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
293     } else {
294       $conv= array(   "U" => _("users"),
295           "G" => _("groups"),
296           "A" => _("applications"),
297           "D" => _("departments"),
298           "S" => _("servers"),
299           "W" => _("workstations"),
300           "T" => _("terminals"),
301           "F" => _("phones"),
302           "P" => _("printers"));
304       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
305       $p1= $conv[$type[0]];
306       error_reporting(0);
307       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
308         $p2= $conv[$type[1]];
309         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
310       } else {
311         $smarty->assign("combinedObjects", "$p1");
312       }
313       error_reporting(E_ALL);
314     }
316     /* Assign variables */
317     $smarty->assign("bases", $this->get_allowed_bases());
318     $smarty->assign("base_select", $this->base);
319     $smarty->assign("department", $this->department);
320     $smarty->assign("members", $this->convert_list($this->memberList));
322     /* Objects have to be tuned... */
323     $smarty->assign("objects", $this->convert_list($this->objects));
325     /* Fields */
326     foreach ($this->attributes as $val){
327       $smarty->assign("$val", $this->$val);
328     }
330     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
331   }
334   /* Save data to object */
335   function save_object()
336   {
337     /* Save additional values for possible next step */
338     if (isset($_POST['ogroupedit'])){
339       plugin::save_object();
340   
341     }
342   }
345   /* (Re-)Load objects */
346   function reload()
347   {
348     /*###########
349       Variable initialisation 
350       ###########*/
352     $this->objects                = array();
353     $this->ui                     = get_userinfo();
354     $filter                       = "";
355     $objectClasses                = array();
356     
357     $ogfilter               = get_global("ogfilter");
358     $regex                  = $ogfilter['regex'];
360     $ldap= $this->config->get_ldap_link();
361     $ldap->cd ($ogfilter['dselect']);
364     /*###########
365       Generate Filter 
366       ###########*/
368     $p_f= array("accounts"        => array("CLASS"=>"gosaAccount"    ,"DN"=> get_people_ou()                  ,"ACL" => "users"), 
369                 "groups"          => array("CLASS"=>"posixGroup"     ,"DN"=> get_groups_ou()                  ,"ACL" => "groups"), 
370                 "applications"    => array("CLASS"=>"gosaApplication","DN"=> "ou=apps,"                       ,"ACL" => "application"), 
371                 "departments"     => array("CLASS"=>"gosaDepartment" ,"DN"=> ""                               ,"ACL" => "department"), 
372                 "servers"         => array("CLASS"=>"goServer"       ,"DN"=> "ou=servers,ou=systems,"         ,"ACL" => "server"),
373                 "workstations"    => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems,"    ,"ACL" => "workstation"),
374                 "terminals"       => array("CLASS"=>"gotoTerminal"   ,"DN"=> "ou=terminals,ou=systems,"       ,"ACL" => "terminal"),
375                 "printers"        => array("CLASS"=>"gotoPrinter"    ,"DN"=> "ou=printers,ou=systems,"        ,"ACL" => "printer"),
376                 "phones"          => array("CLASS"=>"goFonHardware"  ,"DN"=> "ou=phones,ou=systems,"          ,"ACL" => "phone"));
379     /*###########
380       Perform search for selected objectClasses & regex to fill list with objects   
381       ###########*/
383     $Get_list_flags = 0;
384     if($ogfilter['subtrees'] == "checked"){
385       $Get_list_flags |= GL_SUBSEARCH;
386     }    
388     foreach($p_f as $post_name => $data){
390       if($ogfilter[$post_name] == "checked"){
391         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
392         $res    = get_list($filter, $data['ACL']  , $data['DN'].$ogfilter['dselect'], 
393                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
395         /* fetch results and append them to the list */
396         foreach($res as $attrs){
398           $type= $this->getObjectType($attrs);
399           $name= $this->getObjectName($attrs);
401           /* Fill array */
402           if (isset($attrs["description"][0])){
403             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
404           } elseif (isset($attrs["uid"][0])) {
405             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
406           } else {
407             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
408           }
409         }
410       }
411     }
412     uasort ($this->objects, 'sort_list');
413     reset ($this->objects);
415     
416     /*###########
417       Build member list and try to detect obsolete entries 
418       ###########*/
420     $this->memberList = array();
421   
422     /* Walk through all single member entry */
423     foreach($this->member as $dn){
425       /* The dn for the current member can't be resolved 
426          it seams that this entry was removed 
427        */ 
428       /* Try to resolv the entry again, if it still fails, display error msg */
429       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
431       /* It has failed, add entry with type flag I (Invalid)*/
432       if ($ldap->error != "Success"){
433         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
435       } else {
437         /* Append this entry to our all object list */
439         /* Fetch object */
440         $attrs= $ldap->fetch();
442         $type= $this->getObjectType($attrs);
443         $name= $this->getObjectName($attrs);
445         if (isset($attrs["description"][0])){
446           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
447         } elseif (isset($attrs["uid"][0])) {
448           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
449         } else {
450           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
451         }
452         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
453         if(isset($attrs['uid'])){
454           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
455         }
457         /* Fill array */
458         if (isset($attrs["description"][0])){
459           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
460         } else {
461           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
462         }
464         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
465       }
466     }
467     uasort ($this->memberList, 'sort_list');
468     reset ($this->memberList);
470     /* Assemble types of currently combined objects */
471     $objectTypes= "";
472     foreach ($this->memberList as $dn => $desc){
474       /* Invalid object? */
475       if ($desc['type'] == 'I'){
476         continue;
477       }
479       /* Fine. Add to list. */
480       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
481         $objectTypes.= $desc['type'];
482       }
483     }
484     $this->gosaGroupObjects= "[$objectTypes]";
485   }
488   function convert_list($input)
489   {
490     $temp= "";
491     $conv= array(  "U" => "select_user.png",
492         "G" => "select_groups.png",
493         "A" => "select_application.png",
494         "D" => "select_department.png",
495         "S" => "select_server.png",
496         "W" => "select_workstation.png",
497         "T" => "select_terminal.png",
498         "F" => "select_phone.png",
499         "I" => "flag.png",
500         "P" => "select_printer.png");
502     foreach ($input as $key => $value){
503       /* Generate output */
504       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
505     }
507     return ($temp);
508   }
511   function getObjectType($attrs)
512   {
513     $type= "I";
515     foreach(array(  "U" => "gosaAccount",
516           "G" => "posixGroup",
517           "A" => "gosaApplication",
518           "D" => "gosaDepartment",
519           "S" => "goServer",
520           "W" => "gotoWorkstation",
521           "T" => "gotoTerminal",
522           "F" => "goFonHardware",
523           "P" => "gotoPrinter") as $index => $class){
524       if (in_array($class, $attrs['objectClass'])){
525         $type= $index;
526         break;
527       }
528     }
530     return ($type);
531   }
534   function getObjectName($attrs)
535   {
536     /* Person? */
537     $name =""; 
538     if (in_array('gosaAccount', $attrs['objectClass'])){
539       if(isset($attrs['sn']) && isset($attrs['givenName'])){
540         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
541       } else {
542         $name= $attrs['uid'][0];
543       }
544     } else {
545       if(isset($attrs["cn"][0])) {
546         $name= $attrs['cn'][0];
547       } else {
548         $name= $attrs['ou'][0];
549       }
550     }
552     return ($name);
553   }
556   function check()
557   {
558     /* Call common method to give check the hook */
559     $message= plugin::check();
561     /* Permissions for that base? */
562     if ($this->base != ""){
563       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
564     } else {
565       $new_dn= $this->dn;
566     }
569     $ldap = $this->config->get_ldap_link();
570     if($this->dn != $new_dn){
571       $ldap->cat ($new_dn, array('dn'));
572     }
573     
574     if($ldap->count() !=0){
575       $message[]= _("There is already an object with this cn.");
576     } 
578     /* Set new acl base */
579     if($this->dn == "new") {
580       $this->set_acl_base($this->base);
581     }
583     if ($this->orig_dn == "new" && !$this->acl_is_createable()){
584       $message[]= _("You have no permissions to create a group on this 'Base'.");
585     }
587     /* must: cn */
588     if ($this->cn == "" && $this->acl_is_writeable("cn")){
589       $message[]= "The required field 'Name' is not set.";
590     }
592     /* To many different object types? */
593     if (strlen($this->gosaGroupObjects) > 4){
594       $message[]= _("You can combine two different object types at maximum only!");
595     }
597     return ($message);
598   }
601   /* Save to LDAP */
602   function save()
603   {
604     plugin::save();
606     /* Move members to target array */
607     $this->attrs['member'] =array();
608     foreach ($this->member as $key => $desc){
609       $this->attrs['member'][]= @LDAP::fix($key);
610     }
612     $ldap= $this->config->get_ldap_link();
614     /* New accounts need proper 'dn', propagate it to remaining objects */
615     if ($this->dn == 'new'){
616       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
617     }
619     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
620        new entries. So do a check first... */
621     $ldap->cat ($this->dn, array('dn'));
622     if ($ldap->fetch()){
623       /* Modify needs array() to remove values :-( */
624       if (!count ($this->member)){
625         $this->attrs['member']= array();
626       }
627       $mode= "modify";
628     } else {
629       $mode= "add";
630       $ldap->cd($this->config->current['BASE']);
631       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
632     }
634     /* Write back to ldap */
635     $ldap->cd($this->dn);
636     $this->cleanup();
637     $ldap->$mode($this->attrs);
639     /* Trigger post signal */
640     $this->handle_post_events($mode);
642     $ret= 0;
643     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
644       $ret= 1;
645     }
647     return ($ret);
648   }
650   function remove_from_parent()
651   {
652     plugin::remove_from_parent();
654     $ldap= $this->config->get_ldap_link();
655     $ldap->rmdir($this->dn);
656     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
658     /* Trigger remove signal */
659     $this->handle_post_events("remove");
660   }
663   function getCopyDialog()
664   {
665     $smarty = get_smarty();
666     $smarty->assign("cn",     $this->cn);
667     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
668     $ret = array();
669     $ret['string'] = $str;
670     $ret['status'] = "";
671     return($ret);
672   }
674   function saveCopyDialog()
675   {
676     if(isset($_POST['cn'])){
677       $this->cn = $_POST['cn'];
678     }
679   }
682   function plInfo()
683   {
684     return (array(
685           "plShortName"   => _("Generic"),
686           "plDescription" => _("Object group generic"),
687           "plSelfModify"  => FALSE,
688           "plDepends"     => array(),
689           "plPriority"    => 1,
690           "plSection"     => array("administration"),
691           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
692                                                       "objectClass"  => "gosaGroupOfNames")),
693           "plProvidedAcls"=> array(
694             "cn"                => _("Name"),
695             "base"              => _("Base"),
696             "description"       => _("Description"),
697             "member"            => _("Member"))
698           ));
699   }
702 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
703 ?>