Code

Updated group trustModel ACLs.
[gosa.git] / gosa-core / plugins / admin / ogroups / class_ogroup.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 /* Sort multidimensional arrays for key 'text' */
24 function sort_list($val1, $val2)
25 {
26   $v1= strtolower($val1['text']);
27   $v2= strtolower($val2['text']);
28   if ($v1 > $v2){
29     return 1;
30   }
31   if ($v1 < $v2){
32     return -1;
33   }
34   return 0;
35 }
38 class ogroup extends plugin
39 {
40   /* Variables */
41   var $cn= "";
42   var $description= "";
43   var $base= "";
44   var $gosaGroupObjects= "";
45   var $department= "";
46   var $objects= array();
47   var $objcache= array();
48   var $memberList= array();
49   var $member= array();
50   var $orig_dn= "";
51   var $orig_cn= "";
52   var $orig_base= "";
53   var $group_dialog= FALSE;
54   var $view_logged = FALSE;
56   var $accessTo= array();
57   var $trustModel= "";
58   var $show_ws_dialog = FALSE;
60   var $was_trust_account= FALSE;
62   /* attribute list for save action */
63   var $attributes= array("cn", "description", "gosaGroupObjects","member","accessTo","trustModel");
64   var $objectclasses= array("top", "gosaGroupOfNames");
66   function ogroup (&$config, $dn= NULL)
67   {
68     plugin::plugin ($config, $dn);
69     $this->orig_dn= $dn;
71     $this->member = array();
73     /* Load member objects */
74     if (isset($this->attrs['member'])){
75       foreach ($this->attrs['member'] as $key => $value){
76         if ("$key" != "count"){
77           $value= @LDAP::convert($value);
78           $this->member["$value"]= "$value";
79         }
80       }
81     }
82     $this->is_account= TRUE;
84     /* Get global filter config */
85     if (!session::is_set("ogfilter")){
86       $ui= get_userinfo();
87       $base= get_base_from_people($ui->dn);
88       $ogfilter= array( "dselect"       => $base,
89           "regex"           => "*");
90       session::set("ogfilter", $ogfilter);
91     }
92     $ogfilter= session::get('ogfilter');
94     /* Adjust flags */
95     foreach( array(   "U" => "accounts",
96           "G" => "groups",
97           "A" => "applications",
98           "D" => "departments",
99           "S" => "servers",
100           "W" => "workstations",
101           "O" => "winstations",
102           "T" => "terminals",
103           "F" => "phones",
104           "_" => "subtrees",
105           "P" => "printers") as $key => $val){
107       if (preg_match("/$key/", $this->gosaGroupObjects)){
108         $ogfilter[$val]= "checked";
109       } else {
110         $ogfilter[$val]= "";
111       }
112     }
113     session::set("ogfilter", $ogfilter);
114   
115     if(session::is_set('CurrentMainBase')){
116      $this->base  = session::get('CurrentMainBase');
117     }
119     /* Set base */
120     if ($this->dn == "new"){
121       $this->base = session::get('CurrentMainBase');
122     } else {
123       $this->base= preg_replace("/^[^,]+,".normalizePreg(get_ou("ogroupou"))."/","",$this->dn);
125       /* Is this account a trustAccount? */
126       if ($this->is_account && isset($this->attrs['trustModel'])){
127         $this->trustModel= $this->attrs['trustModel'][0];
128         $this->was_trust_account= TRUE;
129       } else {
130         $this->was_trust_account= FALSE;
131         $this->trustModel= "";
132       }
134       $this->accessTo = array();
135       if ($this->is_account && isset($this->attrs['accessTo'])){
136         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
137           $tmp= $this->attrs['accessTo'][$i];
138           $this->accessTo[$tmp]= $tmp;
139         }
140       }
141     }
143     $this->orig_cn = $this->cn;
144     $this->orig_base = $this->base;
146     /* Get global filter config */
147     if (!session::is_set("sysfilter")){
148       $ui= get_userinfo();
149       $base= get_base_from_people($ui->dn);
150       $sysfilter= array( "depselect"       => $base,
151           "regex"           => "*");
152       session::set("sysfilter", $sysfilter);
153     }
155     /* Load member data */
156     $this->reload();
157   }
159   function AddDelMembership($NewMember = false){
161     if($NewMember){
163       /* Add member and force reload */
164       $this->member[$NewMember]= $NewMember;
165       $this->reload(); 
167       $this->memberList[$NewMember]= $this->objcache[$NewMember];
168       unset ($this->objects[$NewMember]);
169       uasort ($this->memberList, 'sort_list');
170       reset ($this->memberList);
171     }else{
172       /* Delete objects from group */
173       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
174         foreach ($_POST['members'] as $value){
175           $this->objects["$value"]= $this->memberList[$value];
176           unset ($this->memberList["$value"]);
177           unset ($this->member["$value"]);
178           uasort ($this->objects, 'sort_list');
179           reset ($this->objects);
180         }
181         $this->reload();
182       }
184       /* Add objects to group */
185       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
187         $tmp = "";
188         foreach($this->memberList as $obj){
189           $tmp .= $obj['type'];
190         }
191         $skipped = FALSE;
192         foreach ($_POST['objects'] as $value){
193           if(preg_match("/T/",$tmp) && $this->objects[$value]['type'] == "W"){
194             $skipped =TRUE;
195           }elseif(preg_match("/W/",$tmp) && $this->objects[$value]['type'] == "T"){
196             $skipped =TRUE;
197           }else{
198             $this->memberList["$value"]= $this->objects[$value];
199             $this->member["$value"]= $value;
200             unset ($this->objects[$value]);
201             uasort ($this->memberList, 'sort_list');
202             reset ($this->memberList);
203           }
204         }
205         if($skipped){
206           msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
207         }
208         $this->reload();
209       }
210     }
211   }
213   function execute()
214   {
215     /* Call parent execute */
216     plugin::execute();
218     if(!$this->view_logged){
219       $this->view_logged = TRUE;
220       new log("view","ogroups/".get_class($this),$this->dn);
221     }
224     /* Do we represent a valid group? */
225     if (!$this->is_account){
226       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
227         msgPool::noValidExtension("object group")."</b>";
228       return ($display);
229     }
232     /* Load templating engine */
233     $smarty= get_smarty();
235     $tmp = $this->plInfo();
236     foreach($tmp['plProvidedAcls'] as $name => $translation){
237       $smarty->assign($name."ACL",$this->getacl($name));
238     }
240     /* Base select dialog */
241     $once = true;
242     foreach($_POST as $name => $value){
243       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
244         $once = false;
245         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
246         $this->dialog->setCurrentBase($this->base);
247       }
248     }
250     /***********
251      * Trusts 
252      ***********/
254     /* Add user workstation? */
255     if (isset($_POST["add_ws"])){
256       $this->show_ws_dialog= TRUE;
257       $this->dialog= TRUE;
258     }
260     /* Add user workstation? */
261     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
262       foreach($_POST['wslist'] as $ws){
263         $this->accessTo[$ws]= $ws;
264       }
265       ksort($this->accessTo);
266       $this->is_modified= TRUE;
267     }
269     /* Remove user workstations? */
270     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
271       foreach($_POST['workstation_list'] as $name){
272         unset ($this->accessTo[$name]);
273       }
274       $this->is_modified= TRUE;
275     }
277     /* Add user workstation finished? */
278     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
279       $this->show_ws_dialog= FALSE;
280       $this->dialog= FALSE;
281     }
283     /* Show ws dialog */
284     if ($this->show_ws_dialog){
285       /* Save data */
286       $sysfilter= session::get("sysfilter");
287       foreach( array("depselect", "regex") as $type){
288         if (isset($_POST[$type])){
289           $sysfilter[$type]= $_POST[$type];
290         }
291       }
292       if (isset($_GET['search'])){
293         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
294         if ($s == "**"){
295           $s= "*";
296         }
297         $sysfilter['regex']= $s;
298       }
299       session::set("sysfilter", $sysfilter);
301       /* Get workstation list */
302       $exclude= "";
303       foreach($this->accessTo as $ws){
304         $exclude.= "(cn=$ws)";
305       }
306       if ($exclude != ""){
307         $exclude= "(!(|$exclude))";
308       }
309       $regex= $sysfilter['regex'];
310       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
312       $deps_a = array(get_ou("serverou"),
313           get_ou("terminalou"),
314           get_ou("workstationou"));
315       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'],
316           array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
317       $wslist= array();
318       foreach ($res as $attrs){
319         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
320       }
321       asort($wslist);
322       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
323       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
324       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
325       $smarty->assign("deplist", $this->config->idepartments);
326       $smarty->assign("alphabet", generate_alphabet());
327       foreach( array("depselect", "regex") as $type){
328         $smarty->assign("$type", $sysfilter[$type]);
329       }
330       $smarty->assign("hint", print_sizelimit_warning());
331       $smarty->assign("wslist", $wslist);
332       $smarty->assign("apply", apply_filter());
333       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
334       return ($display);
335     }
337     /***********
338      * Ende - Trusts 
339      ***********/
342     /* Dialog handling */
343     if(is_object($this->dialog) && $this->acl_is_moveable()){
344       /* Must be called before save_object */
345       $this->dialog->save_object();
347       if($this->dialog->isClosed()){
348         $this->dialog = false;
349       }elseif($this->dialog->isSelected()){
351         /* A new base was selected, check if it is a valid one */
352         $tmp = $this->get_allowed_bases();
353         if(isset($tmp[$this->dialog->isSelected()])){
354           $this->base = $this->dialog->isSelected();
355         }
356         $this->dialog= false;
357       }else{
358         return($this->dialog->execute());
359       }
360     }
362     /* Add objects? */
363     if (isset($_POST["edit_membership"])){
364       $this->group_dialog= TRUE;
365       $this->dialog= TRUE;
366     }
368     /* Add objects finished? */
369     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
370       $this->group_dialog= FALSE;
371       $this->dialog= FALSE;
372     }
374     /* Manage object add dialog */
375     if ($this->group_dialog){
377       /* Save data */
378       $ogfilter= session::get("ogfilter");
379       foreach( array("dselect", "regex") as $type){
380         if (isset($_POST[$type])){
381           $ogfilter[$type]= $_POST[$type];
382         }
383       }
384       if (isset($_POST['dselect'])){
385         foreach( array("accounts", "groups", "applications", "departments",
386               "servers", "workstations", "winstations", "terminals", "printers","subtrees",
387               "phones") as $type){
389           if (isset($_POST[$type])) {
390             $ogfilter[$type]= "checked";
391           } else {
392             $ogfilter[$type]= "";
393           }
394         }
395       }
396       if (isset($_GET['search'])){
397         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
398         if ($s == "**"){
399           $s= "*";
400         }
401         $ogfilter['regex']= $s;
402       }
403       session::set("ogfilter", $ogfilter);
404       $this->reload();
406       /* Calculate actual groups */
407       $smarty->assign("objects", $this->convert_list($this->objects));
409       /* Show dialog */
410       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
411       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
412       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
413       $smarty->assign("deplist", $this->config->idepartments);
414       $smarty->assign("alphabet", generate_alphabet());
415       foreach( array("dselect", "regex", "subtrees") as $type){
416         $smarty->assign("$type", $ogfilter[$type]);
417       }
418       $smarty->assign("hint", print_sizelimit_warning());
419       $smarty->assign("apply", apply_filter());
421       /* Build up checkboxes 
422        */
423       $ar = array(
424           "departments" => array(
425             "T" => msgPool::selectToView(_("departments")),
426             "C" => (isset($ogfilter['departments']) && ($ogfilter['departments'])),
427             "L" => sprintf(_("Show %s"),_("departments"))),
428           "accounts" => array(
429             "T" => msgPool::selectToView(_("people")),
430             "C" => (isset($ogfilter['accounts']) && ($ogfilter['accounts'])),
431             "L" => sprintf(_("Show %s"),_("people"))),
432           "groups"=> array(
433             "T" => msgPool::selectToView(_("groups")),
434             "C" => (isset($ogfilter['groups']) && ($ogfilter['groups'])),
435             "L" => sprintf(_("Show %s"),_("groups"))),
436           "servers"=> array(
437             "T" => msgPool::selectToView(_("servers")),
438             "C" => (isset($ogfilter['servers']) && ($ogfilter['servers'])),
439             "L" => sprintf(_("Show %s"),_("servers"))),
440           "workstations"=> array(
441             "T" => msgPool::selectToView(_("workstations")),
442             "C" => (isset($ogfilter['workstations']) && ($ogfilter['workstations'])),
443             "L" => sprintf(_("Show %s"),_("workstations"))),
444           "terminals"=> array(
445             "T" => msgPool::selectToView(_("terminals")),
446             "C" => (isset($ogfilter['terminals']) && ($ogfilter['terminals'])),
447             "L" => sprintf(_("Show %s"),_("terminals"))),
448           "printers"=> array(
449             "T" => msgPool::selectToView(_("printer")),
450             "C" => (isset($ogfilter['printers']) && ($ogfilter['printers'])),
451             "L" => sprintf(_("Show %s"),_("printers"))),
452           "phones"=> array(
453             "T" => msgPool::selectToView(_("phones")),
454             "C" => (isset($ogfilter['phones']) && ($ogfilter['phones'])),
455             "L" => sprintf(_("Show %s"),_("phones"))));
456  
457       /* Allow selecting applications if we are having a non 
458           release managed application storage */ 
459       if(!$this->IsReleaseManagementActivated()){
460         $ar["applications"] = array(
461             "T" => msgPool::selectToView(_("applications")),
462             "C" => (isset($ogfilter['applications']) && ($ogfilter['applications'])),
463             "L" => sprintf(_("Show %s"),_("applications")));
464       }
466       $smarty->assign("checkboxes",$ar);
467       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
468       return ($display);
469     }
471     /* Bases / Departments */
472       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
473         $this->base= $_POST['base'];
474       }
476     /* Assemble combine string */
477     if ($this->gosaGroupObjects == "[]"){
478       $smarty->assign("combinedObjects", _("none"));
479     } elseif (strlen($this->gosaGroupObjects) > 4){
480       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
481     } else {
482       $conv= array(   "U" => _("users"),
483           "G" => _("groups"),
484           "A" => _("applications"),
485           "D" => _("departments"),
486           "S" => _("servers"),
487           "W" => _("workstations"),
488           "O" => _("winstations"),
489           "T" => _("terminals"),
490           "F" => _("phones"),
491           "P" => _("printers"));
493       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
494       $p1= $conv[$type[0]];
495       error_reporting(0);
496       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
497         $p2= $conv[$type[1]];
498         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
499       } else {
500         $smarty->assign("combinedObjects", "$p1");
501       }
502       error_reporting(E_ALL | E_STRICT);
503     }
505     /* Assign variables */
506     $smarty->assign("bases", $this->get_allowed_bases());
507     $smarty->assign("base_select", $this->base);
508     $smarty->assign("department", $this->department);
509     $smarty->assign("members", $this->convert_list($this->memberList));
511     /* Objects have to be tuned... */
512     $smarty->assign("objects", $this->convert_list($this->objects));
514     /* Fields */
515     foreach ($this->attributes as $val){
516       $smarty->assign("$val", $this->$val);
517     }
519     /******
520       Trust account
521      ******/
522     $smarty->assign("trusthide", " disabled ");
523     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
524     if ($this->trustModel == "fullaccess"){
525       $trustmode= 1;
526       // pervent double disable tag in html code, this will disturb our clean w3c html
527       $smarty->assign("trustmode",  $this->getacl("trustModel"));
529     } elseif ($this->trustModel == "byhost"){
530       $trustmode= 2;
531       $smarty->assign("trusthide", "");
532     } else {
533       // pervent double disable tag in html code, this will disturb our clean w3c html
534       $smarty->assign("trustmode",  $this->getacl("trustModel"));
535       $trustmode= 0;
536     }
537     $smarty->assign("trustmode", $trustmode);
538     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
539           2 => _("allow access to these hosts")));
541     $smarty->assign("workstations", $this->accessTo);
543     if((count($this->accessTo))==0){
544       $smarty->assign("emptyArrAccess",true);
545     }else{
546       $smarty->assign("emptyArrAccess",false);
547     }
548     /******
549       Ende - Trust account
550      ******/
552     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
553   }
556   /* Save data to object */
557   function save_object()
558   {
559     /* Save additional values for possible next step */
560     if (isset($_POST['ogroupedit'])){
562       /******
563         Trust account 
564        ******/
566       if($this->acl_is_writeable("trustModel")){
567         if (isset($_POST['trustmode'])){
568           $saved= $this->trustModel;
569           if ($_POST['trustmode'] == "1"){
570             $this->trustModel= "fullaccess";
571           } elseif ($_POST['trustmode'] == "2"){
572             $this->trustModel= "byhost";
573           } else {
574             $this->trustModel= "";
575           }
576           if ($this->trustModel != $saved){
577             $this->is_modified= TRUE;
578           }
579         }
580       }
581       /******
582         Ende Trust account
583        ******/
585       /* Create a base backup and reset the
586          base directly after calling plugin::save_object();
587          Base will be set seperatly a few lines below */
588       $base_tmp = $this->base;
589       plugin::save_object();
590       $this->base = $base_tmp;
592       /* Save base, since this is no LDAP attribute */
593       $tmp = $this->get_allowed_bases();
594       if(isset($_POST['base'])){
595         if(isset($tmp[$_POST['base']])){
596           $this->base= $_POST['base'];
597         }
598       }
599     }
600   }
603   /* (Re-)Load objects */
604   function reload()
605   {
606     /*###########
607       Variable initialisation 
608       ###########*/
610     $this->objects                = array();
611     $this->ui                     = get_userinfo();
612     $filter                       = "";
613     $objectClasses                = array();
614     
615     $ogfilter               = session::get("ogfilter");
616     $regex                  = $ogfilter['regex'];
618     $ldap= $this->config->get_ldap_link();
619     $ldap->cd ($ogfilter['dselect']);
622     /*###########
623       Generate Filter 
624       ###########*/
626     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
627           "DN"=> get_people_ou()           ,"ACL" => "users"), 
628         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
629           "DN"=> get_groups_ou('ogroupou') ,"ACL" => "groups"), 
630         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
631           "DN"=> ""                        ,"ACL" => "department"), 
632         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
633           "DN"=> get_ou('serverou')        ,"ACL" => "server"),
634         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
635           "DN"=> get_ou('workstationou')   ,"ACL" => "workstation"),
636         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
637           "DN"=> get_ou('WINSTATIONS')     ,"ACL" => "winstation"),
638         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
639           "DN"=> get_ou('terminalou')      ,"ACL" => "terminal"),
640         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
641           "DN"=> get_ou('printerou')       ,"ACL" => "printer"),
642         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
643           "DN"=> get_ou('phoneou')         ,"ACL" => "phone"));
646     /* Allow searching for applications, if we are not using release managed applications 
647       */
648     if(!$this->IsReleaseManagementActivated()){
649       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
650           "DN"=> get_ou('applicationou')   ,"ACL" => "application"); 
651     }
652            
653     /*###########
654       Perform search for selected objectClasses & regex to fill list with objects   
655       ###########*/
657     $Get_list_flags = 0;
658     if($ogfilter['subtrees'] == "checked"){
659       $Get_list_flags |= GL_SUBSEARCH;
660     }    
662     foreach($p_f as $post_name => $data){
664       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
666         if($ogfilter['subtrees']){
667           $base =  $ogfilter['dselect'];
668         }else{
669           $base =  $data['DN'].$ogfilter['dselect'];
670         }
671    
672          
673         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
674         $res    = get_list($filter, $data['ACL']  , $base, 
675                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
677         /* fetch results and append them to the list */
678         foreach($res as $attrs){
680           $type= $this->getObjectType($attrs);
681           $name= $this->getObjectName($attrs);
683           /* Fill array */
684           if (isset($attrs["description"][0])){
685             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
686           } elseif (isset($attrs["uid"][0])) {
687             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
688           } else {
689             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
690           }
691         }
692       }
693     }
694     uasort ($this->objects, 'sort_list');
695     reset ($this->objects);
697     
698     /*###########
699       Build member list and try to detect obsolete entries 
700       ###########*/
702     $this->memberList = array();
703   
704     /* Walk through all single member entry */
705     foreach($this->member as $dn){
707       /* The dn for the current member can't be resolved 
708          it seams that this entry was removed 
709        */ 
710       /* Try to resolv the entry again, if it still fails, display error msg */
711       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
713       /* It has failed, add entry with type flag I (Invalid)*/
714       if (!$ldap->success()){
715         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
717       } else {
719         /* Append this entry to our all object list */
721         /* Fetch object */
722         $attrs= $ldap->fetch();
724         $type= $this->getObjectType($attrs);
725         $name= $this->getObjectName($attrs);
727         if (isset($attrs["description"][0])){
728           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
729         } elseif (isset($attrs["uid"][0])) {
730           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
731         } else {
732           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
733         }
734         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
736         if(isset($attrs['macAddress'][0])){
737           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
738         }else{
739           $this->objcache[$attrs["dn"]]['macAddress']  = "";
740         }
742         if(isset($attrs['uid'])){
743           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
744         }
746         /* Fill array */
747         if (isset($attrs["description"][0])){
748           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
749         } else {
750           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
751         }
753         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
754       }
755     }
756     uasort ($this->memberList, 'sort_list');
757     reset ($this->memberList);
759     /* Assemble types of currently combined objects */
760     $objectTypes= "";
761     foreach ($this->memberList as $dn => $desc){
763       /* Invalid object? */
764       if ($desc['type'] == 'I'){
765         continue;
766       }
768       /* Fine. Add to list. */
769       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
770         $objectTypes.= $desc['type'];
771       }
772     }
773     $this->gosaGroupObjects= "[$objectTypes]";
774   }
777   function convert_list($input)
778   {
779     $temp= "";
780     $conv= array(  
781         "U" => "plugins/generic/images/head.png",
782         "G" => "plugins/groups/images/groups.png",
783         "A" => "plugins/ogroups/images/application.png",
784         "D" => "plugins/departments/images/department.png",
785         "S" => "plugins/ogroups/images/server.png",
786         "W" => "plugins/ogroups/images/workstation.png",
787         "O" => "plugins/ogroups/images/winstation.png",
788         "T" => "plugins/ogroups/images/terminal.png",
789         "F" => "plugins/ogroups/images/phone.png",
790         "I" => "images/lists/flag.png",
791         "P" => "plugins/ogroups/images/printer.png");
793     foreach ($input as $key => $value){
794       /* Generate output */
795       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
796     }
798     return ($temp);
799   }
802   function getObjectType($attrs)
803   {
804     $type= "I";
806     foreach(array(  "U" => "gosaAccount",
807           "G" => "posixGroup",
808           "A" => "gosaApplication",
809           "D" => "gosaDepartment",
810           "S" => "goServer",
811           "W" => "gotoWorkstation",
812           "O" => "opsiClient",
813           "T" => "gotoTerminal",
814           "F" => "goFonHardware",
815           "P" => "gotoPrinter") as $index => $class){
816       if (in_array($class, $attrs['objectClass'])){
817         $type= $index;
818         break;
819       }
820     }
822     return ($type);
823   }
826   function getObjectName($attrs)
827   {
828     /* Person? */
829     $name =""; 
830     if (in_array('gosaAccount', $attrs['objectClass'])){
831       if(isset($attrs['sn']) && isset($attrs['givenName'])){
832         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
833       } else {
834         $name= $attrs['uid'][0];
835       }
836     } else {
837       if(isset($attrs["cn"][0])) {
838         $name= $attrs['cn'][0];
839       } else {
840         $name= $attrs['ou'][0];
841       }
842     }
844     return ($name);
845   }
848   function check()
849   {
850     /* Call common method to give check the hook */
851     $message= plugin::check();
853     /* Permissions for that base? */
854     if ($this->base != ""){
855       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
856     } else {
857       $new_dn= $this->dn;
858     }
861     $ldap = $this->config->get_ldap_link();
862     if($this->dn != $new_dn){
863       $ldap->cat ($new_dn, array('dn'));
864     }
865     
866     if($ldap->count() !=0){
867       $message[]= msgPool::duplicated(_("Name"));
868     } 
870     /* Set new acl base */
871     if($this->dn == "new") {
872       $this->set_acl_base($this->base);
873     }
875     /* must: cn */
876     if ($this->cn == ""){
877       $message[]= msgPool::required(_("Name"));
878     }
880     /* To many different object types? */
881     if (strlen($this->gosaGroupObjects) > 4){
882       $message[]= _("You can combine two different object types at maximum, only!");
883     }
885     /* Check if we are allowed to create or move this object 
886      */
887     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
888       $message[] = msgPool::permCreate();
889     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
890       $message[] = msgPool::permMove();
891     }
893     return ($message);
894   }
897   /* Save to LDAP */
898   function save()
899   {
900     plugin::save();
902     /* Move members to target array */
903     $this->attrs['member'] =array();
904     foreach ($this->member as $key => $desc){
905       $this->attrs['member'][]= @LDAP::fix($key);
906     }
908     $ldap= $this->config->get_ldap_link();
910     /* New accounts need proper 'dn', propagate it to remaining objects */
911     if ($this->dn == 'new'){
912       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
913     }
915     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
916        new entries. So do a check first... */
917     $ldap->cat ($this->dn, array('dn'));
918     if ($ldap->fetch()){
919       /* Modify needs array() to remove values :-( */
920       if (!count ($this->member)){
921         $this->attrs['member']= array();
922       }
923       $mode= "modify";
925     } else {
926       $mode= "add";
927       $ldap->cd($this->config->current['BASE']);
928       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
929     }
931     /******
932       Trust accounts 
933      ******/
934     $objectclasses= array();
935     foreach ($this->attrs['objectClass'] as $key => $class){
936       if (preg_match('/trustAccount/i', $class)){
937         continue;
938       }
939       $objectclasses[]= $this->attrs['objectClass'][$key];
940     }
941     $this->attrs['objectClass']= $objectclasses;
942     if ($this->trustModel != ""){
943       $this->attrs['objectClass'][]= "trustAccount";
944       $this->attrs['trustModel']= $this->trustModel;
945       $this->attrs['accessTo']= array();
946       if ($this->trustModel == "byhost"){
947         foreach ($this->accessTo as $host){
948           $this->attrs['accessTo'][]= $host;
949         }
950       }
951     } else {
952       if ($this->was_trust_account){
953         $this->attrs['accessTo']= array();
954         $this->attrs['trustModel']= array();
955       }
956     }
958     /******
959       Ende - Trust accounts 
960      ******/
962     /* Write back to ldap */
963     $ldap->cd($this->dn);
964     $this->cleanup();
965     $ldap->$mode($this->attrs);
967     if($mode == "add"){
968       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
969     }else{
970       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
971     }
973     /* Trigger post signal */
974     $this->handle_post_events($mode);
976     $ret= 0;
977     if (!$ldap->success()){
978       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
979       $ret= 1;
980     }
982     return ($ret);
983   }
985   function remove_from_parent()
986   {
987     plugin::remove_from_parent();
989     $ldap= $this->config->get_ldap_link();
990     $ldap->rmdir($this->dn);
991     if (!$ldap->success()){
992       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
993     }
995     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
997     /* Trigger remove signal */
998     $this->handle_post_events("remove");
999   }
1001   
1002   function PrepareForCopyPaste($source)
1003   {
1004     /* Update available object types */
1005     if(isset($source['gosaGroupObjects'][0])){
1006       $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
1007     }
1009     /* Reload tabs */
1010     $this->parent->reload($this->gosaGroupObjects );
1011    
1012     /* Reload plugins */ 
1013     foreach($this->parent->by_object as $name => $class ){
1014       if(get_class($this) != $name) {
1015         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
1016       }
1017     }
1019     /* Load member objects */
1020     if (isset($source['member'])){
1021       foreach ($source['member'] as $key => $value){
1022         if ("$key" != "count"){
1023           $value= @LDAP::convert($value);
1024           $this->member["$value"]= "$value";
1025         }
1026       }
1027     }
1029   }
1032   function getCopyDialog()
1033   {
1034     $smarty = get_smarty();
1035     $smarty->assign("cn",     $this->cn);
1036     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1037     $ret = array();
1038     $ret['string'] = $str;
1039     $ret['status'] = "";
1040     return($ret);
1041   }
1043   function saveCopyDialog()
1044   {
1045     if(isset($_POST['cn'])){
1046       $this->cn = $_POST['cn'];
1047     }
1048   }
1051   function IsReleaseManagementActivated()
1052   {
1053     /* Check if we should enable the release selection */
1054     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1055     if(!empty($tmp)){
1056       return(true);
1057     }
1058     return(false);
1059   }
1062   static function plInfo()
1063   {
1064     return (array(
1065           "plShortName"   => _("Generic"),
1066           "plDescription" => _("Object group generic"),
1067           "plSelfModify"  => FALSE,
1068           "plDepends"     => array(),
1069           "plPriority"    => 1,
1070           "plSection"     => array("administration"),
1071           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
1072                                                       "objectClass"  => "gosaGroupOfNames")),
1073           "plProvidedAcls"=> array(
1074             "cn"                => _("Name"),
1075             "base"              => _("Base"),
1076             "description"       => _("Description"),
1077             "trustModel"        => _("Sytem trust"),
1078             "member"            => _("Member"))
1079           ));
1080   }
1083 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1084 ?>