Code

Remove multi select window
[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 $objects= array();
46   var $objcache= array();
47   var $memberList= array();
48   var $member= array();
49   var $orig_dn= "";
50   var $orig_cn= "";
51   var $orig_base= "";
52   var $objectSelect= FALSE;
53   var $view_logged = FALSE;
55   var $accessTo= array();
56   var $trustModel= "";
57   var $trustSelect = FALSE;
59   var $was_trust_account= FALSE;
60   var $baseSelector;
62   /* Already assigned Workstations. Will be hidden in selection. 
63    */
64   var $used_workstations = array();
66   /* attribute list for save action */
67   var $attributes= array("cn", "description", "gosaGroupObjects","member","accessTo","trustModel");
68   var $objectclasses= array("top", "gosaGroupOfNames");
70   function ogroup (&$config, $dn= NULL)
71   {
72     plugin::plugin ($config, $dn);
73     $this->orig_dn= $dn;
75     $this->member = array();
77     /* Load member objects */
78     if (isset($this->attrs['member'])){
79       foreach ($this->attrs['member'] as $key => $value){
80         if ("$key" != "count"){
81           $value= @LDAP::convert($value);
82           $this->member["$value"]= "$value";
83         }
84       }
85     }
86     $this->is_account= TRUE;
88     /* Get global filter config */
89     if (!session::is_set("ogfilter")){
90       $ui= get_userinfo();
91       $base= get_base_from_people($ui->dn);
92       $ogfilter= array( "dselect"       => $base,
93           "regex"           => "*");
94       session::set("ogfilter", $ogfilter);
95     }
96     $ogfilter= session::get('ogfilter');
98     /* Adjust flags */
99     foreach( array(   "U" => "accounts",
100           "G" => "groups",
101           "A" => "applications",
102           "D" => "departments",
103           "S" => "servers",
104           "W" => "workstations",
105           "O" => "winstations",
106           "T" => "terminals",
107           "F" => "phones",
108           "_" => "subtrees",
109           "P" => "printers") as $key => $val){
111       if (preg_match("/$key/", $this->gosaGroupObjects)){
112         $ogfilter[$val]= "checked";
113       } else {
114         $ogfilter[$val]= "";
115       }
116     }
117     session::set("ogfilter", $ogfilter);
118   
119     if(session::is_set('CurrentMainBase')){
120      $this->base  = session::get('CurrentMainBase');
121     }
123     /* Set base */
124     if ($this->dn == "new"){
125       $this->base = session::get('CurrentMainBase');
126     } else {
127       $this->base= preg_replace("/^[^,]+,".preg_quote(get_ou("ogroupRDN"), '/')."/i","",$this->dn);
129       /* Is this account a trustAccount? */
130       if ($this->is_account && isset($this->attrs['trustModel'])){
131         $this->trustModel= $this->attrs['trustModel'][0];
132         $this->was_trust_account= TRUE;
133       } else {
134         $this->was_trust_account= FALSE;
135         $this->trustModel= "";
136       }
138       $this->accessTo = array();
139       if ($this->is_account && isset($this->attrs['accessTo'])){
140         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
141           $tmp= $this->attrs['accessTo'][$i];
142           $this->accessTo[$tmp]= $tmp;
143         }
144       }
145     }
147     /* Detect all workstations, which are already assigned to an object group  
148         - Those objects will be hidden in the add object dialog.
149         - Check() will complain if such a system is assigned to this object group.
150      */ 
151     $base = $this->config->current['BASE'];
152     $res    = get_list("(|(objectClass=gotoWorkstation)(objectClass=gotoTerminal))","none" , 
153         $base, array("dn"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
154     $ws_dns = array();
155     foreach($res as $data){
156       $ws_dns[] = $data['dn'];
157     }
158     $res=get_list("(&(member=*)(objectClass=gosaGroupOfNames))","none",
159         $base, array("dn","member", "gosaGroupObjects"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
160     $this->used_workstations = array();
161     foreach($res as $og){
162       if($og['dn'] == $this->dn) continue;
163       $test = array_intersect($ws_dns,$og['member']);
164       if(($og['gosaGroupObjects'] == "[W]" || $og['gosaGroupObjects'] == "[T]") && count($test)){
165         $this->used_workstations = array_merge($this->used_workstations,$test);
166       }
167     }
169     $this->orig_cn = $this->cn;
170     $this->orig_base = $this->base;
172     /* Get global filter config */
173     if (!session::is_set("sysfilter")){
174       $ui= get_userinfo();
175       $base= get_base_from_people($ui->dn);
176       $sysfilter= array( "depselect"       => $base,
177           "regex"           => "*");
178       session::set("sysfilter", $sysfilter);
179     }
181     /* Instanciate base selector */
182     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
183     $this->baseSelector->setSubmitButton(false);
184     $this->baseSelector->setHeight(300);
185     $this->baseSelector->update(true);
187     $this->reload();
188   }
190   function AddDelMembership($NewMember = false){
192     if($NewMember){
194       /* Add member and force reload */
195       $this->member[$NewMember]= $NewMember;
196       $this->reload(); 
198       $this->memberList[$NewMember]= $this->objcache[$NewMember];
199       unset ($this->objects[$NewMember]);
200       uasort ($this->memberList, 'sort_list');
201       reset ($this->memberList);
202     }else{
203       /* Delete objects from group */
204       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
205         foreach ($_POST['members'] as $value){
206           $this->objects["$value"]= $this->memberList[$value];
207           unset ($this->memberList["$value"]);
208           unset ($this->member["$value"]);
209           uasort ($this->objects, 'sort_list');
210           reset ($this->objects);
211         }
212         $this->reload();
213       }
215       /* Add objects to group */
216       if (isset($_POST['objectSelect_save']) && $this->objectSelect instanceOf objectSelect){
217         $objects = $this->objectSelect->save();
218         $skipped = FALSE;
219         foreach($objects as $object){
221           $tmp = "";
222           foreach($this->memberList as $obj){
223             $tmp .= $obj['type'];
224           }
226           $type  = $this->getObjectType($object);
227           $name= $this->getObjectName($object);
228           $dn = $object['dn'];
230           /* Fill array */
231           if (isset($object["description"][0])){
232             $object= array("text" => "$name [".$object["description"][0]."]", "type" => "$type");
233           } elseif (isset($object["uid"][0])) {
234             $object= array("text" => "$name [".$object["uid"][0]."]", "type" => "$type");
235           } else {
236             $object= array("text" => "$name", "type" => "$type");
237           }
239           if(preg_match("/T/",$tmp) && $type == "W"){
240             $skipped =TRUE;
241           }elseif(preg_match("/W/",$tmp) && $type == "T"){
242             $skipped =TRUE;
243           }else{
245             $this->memberList["$dn"]= $object;
246             $this->member["$dn"]= $dn;
247             uasort ($this->memberList, 'sort_list');
248             reset ($this->memberList);
249           }
250         }
251         if($skipped){
252           msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
253         }
254         $this->objectSelect= FALSE;
255         $this->dialog= FALSE;
256       }
257     }
258   }
260   function execute()
261   {
262     /* Call parent execute */
263     plugin::execute();
265     if(!$this->view_logged){
266       $this->view_logged = TRUE;
267       new log("view","ogroups/".get_class($this),$this->dn);
268     }
271     /* Do we represent a valid group? */
272     if (!$this->is_account){
273       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
274         msgPool::noValidExtension("object group")."</b>";
275       return ($display);
276     }
279     /* Load templating engine */
280     $smarty= get_smarty();
281     $smarty->assign("usePrototype", "true");
283     $tmp = $this->plInfo();
284     foreach($tmp['plProvidedAcls'] as $name => $translation){
285       $smarty->assign($name."ACL",$this->getacl($name));
286     }
288     /***********
289      * Trusts 
290      ***********/
292     /* Add user workstation? */
293     if (isset($_POST["add_ws"])){
294       $this->trustSelect= new trustSelect($this->config,get_userinfo());
295       $this->dialog= TRUE;
296     }
298     // Add selected machines to trusted ones.
299     if (isset($_POST["add_ws_finish"]) &&  $this->trustSelect){
300       $trusts = $this->trustSelect->detectPostActions();
301       if(isset($trusts['targets'])){
303         $headpage = $this->trustSelect->getHeadpage();
304         foreach($trusts['targets'] as $id){
305           $attrs = $headpage->getEntry($id);
306           $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
307         }
308         ksort($this->accessTo);
309         $this->is_modified= TRUE;
310       }
311       $this->trustSelect= NULL;
312       $this->dialog= FALSE;
313     }
316     /* Remove user workstations? */
317     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
318       foreach($_POST['workstation_list'] as $name){
319         unset ($this->accessTo[$name]);
320       }
321       $this->is_modified= TRUE;
322     }
324     /* Add user workstation finished? */
325     if (isset($_POST["add_ws_cancel"])){
326       $this->trustSelect= NULL;
327       $this->dialog= FALSE;
328     }
330     /* Show ws dialog */
331     if ($this->trustSelect){
332   
333       // Build up blocklist
334       session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
335       return($this->trustSelect->execute());
336     }
338     /***********
339      * Ende - Trusts 
340      ***********/
343     /* Dialog handling */
344     if(is_object($this->dialog) && $this->acl_is_moveable()){
345       /* Must be called before save_object */
346       $this->dialog->save_object();
348       if($this->dialog->isClosed()){
349         $this->dialog = false;
350       }elseif($this->dialog->isSelected()){
352         /* A new base was selected, check if it is a valid one */
353         $tmp = $this->get_allowed_bases();
354         if(isset($tmp[$this->dialog->isSelected()])){
355           $this->base = $this->dialog->isSelected();
356         }
357         $this->dialog= false;
358       }else{
359         return($this->dialog->execute());
360       }
361     }
363     /* Add objects? */
364     if (isset($_POST["edit_membership"])){
365       $this->objectSelect= new objectSelect($this->config, get_userinfo());
366       $this->dialog= TRUE;
367     }
369     /* Add objects finished? */
370     if (isset($_POST["objectSelect_cancel"])){
371       $this->objectSelect= FALSE;
372       $this->dialog= FALSE;
373     }
375     /* Manage object add dialog */
376     if ($this->objectSelect){
377       session::set('filterBlacklist', array('dn'=> $this->member));
378       return($this->objectSelect->execute());
379     }
381     /* Bases / Departments */
382       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
383         $this->base= $_POST['base'];
384       }
386     /* Assemble combine string */
387     if ($this->gosaGroupObjects == "[]"){
388       $smarty->assign("combinedObjects", _("none"));
389     } elseif (strlen($this->gosaGroupObjects) > 4){
390       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
391     } else {
392       $conv= array(   "U" => _("users"),
393           "G" => _("groups"),
394           "A" => _("applications"),
395           "D" => _("departments"),
396           "S" => _("servers"),
397           "W" => _("workstations"),
398           "O" => _("winstations"),
399           "T" => _("terminals"),
400           "F" => _("phones"),
401           "P" => _("printers"));
403       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
404       $p1= $conv[$type[0]];
405       error_reporting(0);
406       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
407         $p2= $conv[$type[1]];
408         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
409       } else {
410         $smarty->assign("combinedObjects", "$p1");
411       }
412       error_reporting(E_ALL | E_STRICT);
413     }
415     /* Assign variables */
416     $smarty->assign("base", $this->baseSelector->render());
417     $smarty->assign("members", $this->convert_list($this->memberList));
419     /* Objects have to be tuned... */
420     $smarty->assign("objects", $this->convert_list($this->objects));
422     /* Fields */
423     foreach ($this->attributes as $val){
424       $smarty->assign("$val", $this->$val);
425     }
427     /******
428       Trust account
429      ******/
430     $smarty->assign("trusthide", " disabled ");
431     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
432     if ($this->trustModel == "fullaccess"){
433       $trustmode= 1;
434       // pervent double disable tag in html code, this will disturb our clean w3c html
435       $smarty->assign("trustmode",  $this->getacl("trustModel"));
437     } elseif ($this->trustModel == "byhost"){
438       $trustmode= 2;
439       $smarty->assign("trusthide", "");
440     } else {
441       // pervent double disable tag in html code, this will disturb our clean w3c html
442       $smarty->assign("trustmode",  $this->getacl("trustModel"));
443       $trustmode= 0;
444     }
445     $smarty->assign("trustmode", $trustmode);
446     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
447           2 => _("allow access to these hosts")));
449     $smarty->assign("workstations", $this->accessTo);
451     if((count($this->accessTo))==0){
452       $smarty->assign("emptyArrAccess",true);
453     }else{
454       $smarty->assign("emptyArrAccess",false);
455     }
456     /******
457       Ende - Trust account
458      ******/
460     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
461   }
464   /* Save data to object */
465   function save_object()
466   {
467     /* Save additional values for possible next step */
468     if (isset($_POST['ogroupedit'])){
470       /******
471         Trust account 
472        ******/
474       if($this->acl_is_writeable("trustModel")){
475         if (isset($_POST['trustmode'])){
476           $saved= $this->trustModel;
477           if ($_POST['trustmode'] == "1"){
478             $this->trustModel= "fullaccess";
479           } elseif ($_POST['trustmode'] == "2"){
480             $this->trustModel= "byhost";
481           } else {
482             $this->trustModel= "";
483           }
484           if ($this->trustModel != $saved){
485             $this->is_modified= TRUE;
486           }
487         }
488       }
489       /******
490         Ende Trust account
491        ******/
493       /* Create a base backup and reset the
494          base directly after calling plugin::save_object();
495          Base will be set seperatly a few lines below */
496       $base_tmp = $this->base;
497       plugin::save_object();
498       $this->base = $base_tmp;
500       /* Refresh base */
501       if ($this->acl_is_moveable($this->base)){
502         if (!$this->baseSelector->update()) {
503           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
504         }
505         if ($this->base != $this->baseSelector->getBase()) {
506           $this->base= $this->baseSelector->getBase();
507           $this->is_modified= TRUE;
508         }
509       }
511     }
512   }
515   /* (Re-)Load objects */
516   function reload()
517   {
518     /*###########
519       Variable initialisation 
520       ###########*/
522     $this->objects                = array();
523     $this->ui                     = get_userinfo();
524     $filter                       = "";
525     $objectClasses                = array();
526     
527     $ogfilter               = session::get("ogfilter");
528     $regex                  = $ogfilter['regex'];
530     $ldap= $this->config->get_ldap_link();
531     $ldap->cd ($ogfilter['dselect']);
534     /*###########
535       Generate Filter 
536       ###########*/
538     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
539           "DN"=> get_people_ou()           ,"ACL" => "users"), 
540         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
541           "DN"=> get_groups_ou('ogroupRDN') ,"ACL" => "groups"), 
542         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
543           "DN"=> ""                        ,"ACL" => "department"), 
544         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
545           "DN"=> get_ou('serverRDN')        ,"ACL" => "server"),
546         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
547           "DN"=> get_ou('workstationRDN')   ,"ACL" => "workstation"),
548         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
549           "DN"=> get_ou('SAMBAMACHINEACCOUNTRDN')     ,"ACL" => "winstation"),
550         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
551           "DN"=> get_ou('terminalRDN')      ,"ACL" => "terminal"),
552         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
553           "DN"=> get_ou('printerRDN')       ,"ACL" => "printer"),
554         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
555           "DN"=> get_ou('phoneRDN')         ,"ACL" => "phone"));
558     /* Allow searching for applications, if we are not using release managed applications 
559       */
560     if(!$this->IsReleaseManagementActivated()){
561       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
562           "DN"=> get_ou('applicationRDN')   ,"ACL" => "application"); 
563     }
564            
565     /*###########
566       Perform search for selected objectClasses & regex to fill list with objects   
567       ###########*/
569     $Get_list_flags = 0;
570     if($ogfilter['subtrees'] == "checked"){
571       $Get_list_flags |= GL_SUBSEARCH;
572     }    
574     foreach($p_f as $post_name => $data){
576       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
578         if($ogfilter['subtrees']){
579           $base =  $ogfilter['dselect'];
580         }else{
581           $base =  $data['DN'].$ogfilter['dselect'];
582         }
583    
584          
585         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
586         $res    = get_list($filter, $data['ACL']  , $base, 
587                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
589         /* fetch results and append them to the list */
590         foreach($res as $attrs){
592           /* Skip workstations which are already assigned to an object group.
593            */
594           if ($this->gosaGroupObjects == "[W]" || $this->gosaGroupObjects == "[T]"){
595             if(in_array($attrs['dn'],$this->used_workstations)){
596               continue;
597             }
598           }
600           $type= $this->getObjectType($attrs);
601           $name= $this->getObjectName($attrs);
603           /* Fill array */
604           if (isset($attrs["description"][0])){
605             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
606           } elseif (isset($attrs["uid"][0])) {
607             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
608           } else {
609             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
610           }
611         }
612       }
613     }
614     uasort ($this->objects, 'sort_list');
615     reset ($this->objects);
617     
618     /*###########
619       Build member list and try to detect obsolete entries 
620       ###########*/
622     $this->memberList = array();
623   
624     /* Walk through all single member entry */
625     foreach($this->member as $dn){
627       /* The dn for the current member can't be resolved 
628          it seams that this entry was removed 
629        */ 
630       /* Try to resolv the entry again, if it still fails, display error msg */
631       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
633       /* It has failed, add entry with type flag I (Invalid)*/
634       if (!$ldap->success()){
635         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".LDAP::fix($dn),"type" => "I");
637       } else {
639         /* Append this entry to our all object list */
641         /* Fetch object */
642         $attrs= $ldap->fetch();
644         $type= $this->getObjectType($attrs);
645         $name= $this->getObjectName($attrs);
647         if (isset($attrs["description"][0])){
648           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
649         } elseif (isset($attrs["uid"][0])) {
650           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
651         } else {
652           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
653         }
654         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
656         if(isset($attrs['macAddress'][0])){
657           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
658         }else{
659           $this->objcache[$attrs["dn"]]['macAddress']  = "";
660         }
662         if(isset($attrs['uid'])){
663           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
664         }
666         /* Fill array */
667         if (isset($attrs["description"][0])){
668           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
669         } else {
670           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
671         }
673         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
674       }
675     }
676     uasort ($this->memberList, 'sort_list');
677     reset ($this->memberList);
679     /* Assemble types of currently combined objects */
680     $objectTypes= "";
681     foreach ($this->memberList as $dn => $desc){
683       /* Invalid object? */
684       if ($desc['type'] == 'I'){
685         continue;
686       }
688       /* Fine. Add to list. */
689       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
690         $objectTypes.= $desc['type'];
691       }
692     }
693     $this->gosaGroupObjects= "[$objectTypes]";
694   }
697   function convert_list($input)
698   {
699     $temp= "";
700     $conv= array(  
701         "Y" => "plugins/users/images/select_template.png",
702         "U" => "plugins/generic/images/head.png",
703         "G" => "plugins/groups/images/groups.png",
704         "A" => "plugins/ogroups/images/application.png",
705         "D" => "plugins/departments/images/department.png",
706         "S" => "plugins/ogroups/images/server.png",
707         "W" => "plugins/ogroups/images/workstation.png",
708         "O" => "plugins/ogroups/images/winstation.png",
709         "T" => "plugins/ogroups/images/terminal.png",
710         "F" => "plugins/ogroups/images/phone.png",
711         "P" => "plugins/ogroups/images/printer.png",
712         "I" => "images/false.png");
714     foreach ($input as $key => $value){
715       /* Generate output */
716       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
717     }
719     return ($temp);
720   }
723   function getObjectType($attrs)
724   {
725     $type= "I";
727     foreach(array(  
728           "Y" => "gosaUserTemplate",
729           "U" => "gosaAccount",
730           "G" => "posixGroup",
731           "A" => "gosaApplication",
732           "D" => "gosaDepartment",
733           "S" => "goServer",
734           "W" => "gotoWorkstation",
735           "O" => "opsiClient",
736           "T" => "gotoTerminal",
737           "F" => "goFonHardware",
738           "P" => "gotoPrinter") as $index => $class){
739       if (in_array($class, $attrs['objectClass'])){
740         $type= $index;
741         break;
742       }
743     }
745     return ($type);
746   }
749   function getObjectName($attrs)
750   {
751     /* Person? */
752     $name =""; 
753     if (in_array('gosaAccount', $attrs['objectClass'])){
754       if(isset($attrs['sn']) && isset($attrs['givenName'])){
755         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
756       } else {
757         $name= $attrs['uid'][0];
758       }
759     } else {
760       if(isset($attrs["cn"][0])) {
761         $name= $attrs['cn'][0];
762       } else {
763         $name= $attrs['ou'][0];
764       }
765     }
767     return ($name);
768   }
771   function check()
772   {
773     /* Call common method to give check the hook */
774     $message= plugin::check();
776     /* Permissions for that base? */
777     if ($this->base != ""){
778       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
779     } else {
780       $new_dn= $this->dn;
781     }
783     /* Check if we have workstations assigned, that are already assigned to
784         another object group.  */
785     if ($this->gosaGroupObjects == "[W]" || $this->gosaGroupObjects == "[T]" ) {
786       $test =array_intersect($this->used_workstations,$this->member); 
787       if(count($test)){
788         $str = "";
789         foreach($test as $dn){
790           $str .= "<li>".$dn."</li>";
791         }
792         $message[] = sprintf(_("These systems are already configured by other object groups and cannot be added:")."<br><ul>%s</ul>",$str);
793       }
794     }
796     $ldap = $this->config->get_ldap_link();
797     if(LDAP::fix($this->dn) != LDAP::fix($new_dn)){
798       $ldap->cat ($new_dn, array('dn'));
799     }
800     
801     if($ldap->count() !=0){
802       $message[]= msgPool::duplicated(_("Name"));
803     } 
805     /* Set new acl base */
806     if($this->dn == "new") {
807       $this->set_acl_base($this->base);
808     }
810     /* must: cn */
811     if ($this->cn == ""){
812       $message[]= msgPool::required(_("Name"));
813     }
815     /* To many different object types? */
816     if (strlen($this->gosaGroupObjects) > 4){
817       $message[]= _("You can combine two different object types at maximum, only!");
818     }
820     /* Check if we are allowed to create or move this object 
821      */
822     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
823       $message[] = msgPool::permCreate();
824     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
825       $message[] = msgPool::permMove();
826     }
828     return ($message);
829   }
832   /* Save to LDAP */
833   function save()
834   {
835     plugin::save();
837     /* Move members to target array */
838     $this->attrs['member'] =array();
839     foreach ($this->member as $key => $desc){
840       $this->attrs['member'][]= LDAP::fix($key);
841     }
843     $ldap= $this->config->get_ldap_link();
845     /* New accounts need proper 'dn', propagate it to remaining objects */
846     if ($this->dn == 'new'){
847       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
848     }
850     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
851        new entries. So do a check first... */
852     $ldap->cat ($this->dn, array('dn'));
853     if ($ldap->fetch()){
854       /* Modify needs array() to remove values :-( */
855       if (!count ($this->member)){
856         $this->attrs['member']= array();
857       }
858       $mode= "modify";
860     } else {
861       $mode= "add";
862       $ldap->cd($this->config->current['BASE']);
863       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
864     }
866     /******
867       Trust accounts 
868      ******/
869     $objectclasses= array();
870     foreach ($this->attrs['objectClass'] as $key => $class){
871       if (preg_match('/trustAccount/i', $class)){
872         continue;
873       }
874       $objectclasses[]= $this->attrs['objectClass'][$key];
875     }
876     $this->attrs['objectClass']= $objectclasses;
877     if ($this->trustModel != ""){
878       $this->attrs['objectClass'][]= "trustAccount";
879       $this->attrs['trustModel']= $this->trustModel;
880       $this->attrs['accessTo']= array();
881       if ($this->trustModel == "byhost"){
882         foreach ($this->accessTo as $host){
883           $this->attrs['accessTo'][]= $host;
884         }
885       }
886     } else {
887       if ($this->was_trust_account){
888         $this->attrs['accessTo']= array();
889         $this->attrs['trustModel']= array();
890       }
891     }
893     /******
894       Ende - Trust accounts 
895      ******/
897     /* Write back to ldap */
898     $ldap->cd($this->dn);
899     $this->cleanup();
900     $ldap->$mode($this->attrs);
902     if($mode == "add"){
903       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
904     }else{
905       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
906     }
908     /* Trigger post signal */
909     $this->handle_post_events($mode);
911     $ret= 0;
912     if (!$ldap->success()){
913       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
914       $ret= 1;
915     }
917     return ($ret);
918   }
920   function remove_from_parent()
921   {
922     plugin::remove_from_parent();
924     $ldap= $this->config->get_ldap_link();
925     $ldap->rmdir($this->dn);
926     if (!$ldap->success()){
927       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
928     }
930     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
932     /* Trigger remove signal */
933     $this->handle_post_events("remove");
934   }
936   
937   function PrepareForCopyPaste($source)
938   {
939     plugin::PrepareForCopyPaste($source);
941     /* Reload tabs */
942     $this->parent->reload($this->gosaGroupObjects );
943    
944     /* Reload plugins */ 
945     foreach($this->parent->by_object as $name => $class ){
946       if(get_class($this) != $name) {
947         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
948       }
949     }
951     $source_o = new ogroup ($this->config, $source['dn']);
952     foreach(array("accessTo","member","gosaGroupObjects")  as $attr){
953       $this->$attr = $source_o->$attr;
954     }
955   }
958   function getCopyDialog()
959   {
960     $smarty = get_smarty();
961     $smarty->assign("cn",     $this->cn);
962     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
963     $ret = array();
964     $ret['string'] = $str;
965     $ret['status'] = "";
966     return($ret);
967   }
969   function saveCopyDialog()
970   {
971     if(isset($_POST['cn'])){
972       $this->cn = $_POST['cn'];
973     }
974   }
977   function IsReleaseManagementActivated()
978   {
979     /* Check if we should enable the release selection */
980     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
981     if(!empty($tmp)){
982       return(true);
983     }
984     return(false);
985   }
988   static function plInfo()
989   {
990     return (array(
991           "plShortName"   => _("Generic"),
992           "plDescription" => _("Object group generic"),
993           "plSelfModify"  => FALSE,
994           "plDepends"     => array(),
995           "plPriority"    => 1,
996           "plSection"     => array("administration"),
997           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
998                                                       "objectClass"  => "gosaGroupOfNames")),
999           "plProvidedAcls"=> array(
1000             "cn"                => _("Name"),
1001             "base"              => _("Base"),
1002             "description"       => _("Description"),
1003             "trustModel"        => _("Sytem trust"),
1004             "member"            => _("Member"))
1005           ));
1006   }
1009 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1010 ?>