Code

Added move / create checks
[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     /* Load member data */
147     $this->reload();
148   }
150   function AddDelMembership($NewMember = false){
152     if($NewMember){
154       /* Add member and force reload */
155       $this->member[$NewMember]= $NewMember;
156       $this->reload(); 
158       $this->memberList[$NewMember]= $this->objcache[$NewMember];
159       unset ($this->objects[$NewMember]);
160       uasort ($this->memberList, 'sort_list');
161       reset ($this->memberList);
162     }else{
163       /* Delete objects from group */
164       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
165         foreach ($_POST['members'] as $value){
166           $this->objects["$value"]= $this->memberList[$value];
167           unset ($this->memberList["$value"]);
168           unset ($this->member["$value"]);
169           uasort ($this->objects, 'sort_list');
170           reset ($this->objects);
171         }
172         $this->reload();
173       }
175       /* Add objects to group */
176       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
178         $tmp = "";
179         foreach($this->memberList as $obj){
180           $tmp .= $obj['type'];
181         }
182         $skipped = FALSE;
183         foreach ($_POST['objects'] as $value){
184           if(preg_match("/T/",$tmp) && $this->objects[$value]['type'] == "W"){
185             $skipped =TRUE;
186           }elseif(preg_match("/W/",$tmp) && $this->objects[$value]['type'] == "T"){
187             $skipped =TRUE;
188           }else{
189             $this->memberList["$value"]= $this->objects[$value];
190             $this->member["$value"]= $value;
191             unset ($this->objects[$value]);
192             uasort ($this->memberList, 'sort_list');
193             reset ($this->memberList);
194           }
195         }
196         if($skipped){
197           msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
198         }
199         $this->reload();
200       }
201     }
202   }
204   function execute()
205   {
206     /* Call parent execute */
207     plugin::execute();
209     if(!$this->view_logged){
210       $this->view_logged = TRUE;
211       new log("view","ogroups/".get_class($this),$this->dn);
212     }
215     /* Do we represent a valid group? */
216     if (!$this->is_account){
217       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
218         msgPool::noValidExtension("object group")."</b>";
219       return ($display);
220     }
223     /* Load templating engine */
224     $smarty= get_smarty();
226     $tmp = $this->plInfo();
227     foreach($tmp['plProvidedAcls'] as $name => $translation){
228       $smarty->assign($name."ACL",$this->getacl($name));
229     }
231     /* Base select dialog */
232     $once = true;
233     foreach($_POST as $name => $value){
234       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
235         $once = false;
236         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
237         $this->dialog->setCurrentBase($this->base);
238       }
239     }
241     /***********
242      * Trusts 
243      ***********/
245     /* Add user workstation? */
246     if (isset($_POST["add_ws"])){
247       $this->show_ws_dialog= TRUE;
248       $this->dialog= TRUE;
249     }
251     /* Add user workstation? */
252     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
253       foreach($_POST['wslist'] as $ws){
254         $this->accessTo[$ws]= $ws;
255       }
256       ksort($this->accessTo);
257       $this->is_modified= TRUE;
258     }
260     /* Remove user workstations? */
261     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
262       foreach($_POST['workstation_list'] as $name){
263         unset ($this->accessTo[$name]);
264       }
265       $this->is_modified= TRUE;
266     }
268     /* Add user workstation finished? */
269     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
270       $this->show_ws_dialog= FALSE;
271       $this->dialog= FALSE;
272     }
274     /* Show ws dialog */
275     if ($this->show_ws_dialog){
276       /* Save data */
277       $sysfilter= session::get("sysfilter");
278       foreach( array("depselect", "regex") as $type){
279         if (isset($_POST[$type])){
280           $sysfilter[$type]= $_POST[$type];
281         }
282       }
283       if (isset($_GET['search'])){
284         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
285         if ($s == "**"){
286           $s= "*";
287         }
288         $sysfilter['regex']= $s;
289       }
290       session::set("sysfilter", $sysfilter);
292       /* Get workstation list */
293       $exclude= "";
294       foreach($this->accessTo as $ws){
295         $exclude.= "(cn=$ws)";
296       }
297       if ($exclude != ""){
298         $exclude= "(!(|$exclude))";
299       }
300       $regex= $sysfilter['regex'];
301       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
303       $deps_a = array(get_ou("serverou"),
304           get_ou("terminalou"),
305           get_ou("workstationou"));
306       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'],
307           array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
308       $wslist= array();
309       foreach ($res as $attrs){
310         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
311       }
312       asort($wslist);
313       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
314       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
315       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
316       $smarty->assign("deplist", $this->config->idepartments);
317       $smarty->assign("alphabet", generate_alphabet());
318       foreach( array("depselect", "regex") as $type){
319         $smarty->assign("$type", $sysfilter[$type]);
320       }
321       $smarty->assign("hint", print_sizelimit_warning());
322       $smarty->assign("wslist", $wslist);
323       $smarty->assign("apply", apply_filter());
324       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
325       return ($display);
326     }
328     /***********
329      * Ende - Trusts 
330      ***********/
333     /* Dialog handling */
334     if(is_object($this->dialog) && $this->acl_is_moveable()){
335       /* Must be called before save_object */
336       $this->dialog->save_object();
338       if($this->dialog->isClosed()){
339         $this->dialog = false;
340       }elseif($this->dialog->isSelected()){
342         /* A new base was selected, check if it is a valid one */
343         $tmp = $this->get_allowed_bases();
344         if(isset($tmp[$this->dialog->isSelected()])){
345           $this->base = $this->dialog->isSelected();
346         }
347         $this->dialog= false;
348       }else{
349         return($this->dialog->execute());
350       }
351     }
353     /* Add objects? */
354     if (isset($_POST["edit_membership"])){
355       $this->group_dialog= TRUE;
356       $this->dialog= TRUE;
357     }
359     /* Add objects finished? */
360     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
361       $this->group_dialog= FALSE;
362       $this->dialog= FALSE;
363     }
365     /* Manage object add dialog */
366     if ($this->group_dialog){
368       /* Save data */
369       $ogfilter= session::get("ogfilter");
370       foreach( array("dselect", "regex") as $type){
371         if (isset($_POST[$type])){
372           $ogfilter[$type]= $_POST[$type];
373         }
374       }
375       if (isset($_POST['dselect'])){
376         foreach( array("accounts", "groups", "applications", "departments",
377               "servers", "workstations", "winstations", "terminals", "printers","subtrees",
378               "phones") as $type){
380           if (isset($_POST[$type])) {
381             $ogfilter[$type]= "checked";
382           } else {
383             $ogfilter[$type]= "";
384           }
385         }
386       }
387       if (isset($_GET['search'])){
388         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
389         if ($s == "**"){
390           $s= "*";
391         }
392         $ogfilter['regex']= $s;
393       }
394       session::set("ogfilter", $ogfilter);
395       $this->reload();
397       /* Calculate actual groups */
398       $smarty->assign("objects", $this->convert_list($this->objects));
400       /* Show dialog */
401       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
402       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
403       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
404       $smarty->assign("deplist", $this->config->idepartments);
405       $smarty->assign("alphabet", generate_alphabet());
406       foreach( array("dselect", "regex", "subtrees") as $type){
407         $smarty->assign("$type", $ogfilter[$type]);
408       }
409       $smarty->assign("hint", print_sizelimit_warning());
410       $smarty->assign("apply", apply_filter());
412       /* Build up checkboxes 
413        */
414       $ar = array(
415           "departments" => array(
416             "T" => msgPool::selectToView(_("departments")),
417             "C" => (isset($ogfilter['departments']) && ($ogfilter['departments'])),
418             "L" => sprintf(_("Show %s"),_("departments"))),
419           "accounts" => array(
420             "T" => msgPool::selectToView(_("people")),
421             "C" => (isset($ogfilter['accounts']) && ($ogfilter['accounts'])),
422             "L" => sprintf(_("Show %s"),_("people"))),
423           "groups"=> array(
424             "T" => msgPool::selectToView(_("groups")),
425             "C" => (isset($ogfilter['groups']) && ($ogfilter['groups'])),
426             "L" => sprintf(_("Show %s"),_("groups"))),
427           "servers"=> array(
428             "T" => msgPool::selectToView(_("servers")),
429             "C" => (isset($ogfilter['servers']) && ($ogfilter['servers'])),
430             "L" => sprintf(_("Show %s"),_("servers"))),
431           "workstations"=> array(
432             "T" => msgPool::selectToView(_("workstations")),
433             "C" => (isset($ogfilter['workstations']) && ($ogfilter['workstations'])),
434             "L" => sprintf(_("Show %s"),_("workstations"))),
435           "terminals"=> array(
436             "T" => msgPool::selectToView(_("terminals")),
437             "C" => (isset($ogfilter['terminals']) && ($ogfilter['terminals'])),
438             "L" => sprintf(_("Show %s"),_("terminals"))),
439           "printers"=> array(
440             "T" => msgPool::selectToView(_("printer")),
441             "C" => (isset($ogfilter['printers']) && ($ogfilter['printers'])),
442             "L" => sprintf(_("Show %s"),_("printers"))),
443           "phones"=> array(
444             "T" => msgPool::selectToView(_("phones")),
445             "C" => (isset($ogfilter['phones']) && ($ogfilter['phones'])),
446             "L" => sprintf(_("Show %s"),_("phones"))));
447  
448       /* Allow selecting applications if we are having a non 
449           release managed application storage */ 
450       if(!$this->IsReleaseManagementActivated()){
451         $ar["applications"] = array(
452             "T" => msgPool::selectToView(_("applications")),
453             "C" => (isset($ogfilter['applications']) && ($ogfilter['applications'])),
454             "L" => sprintf(_("Show %s"),_("applications")));
455       }
457       $smarty->assign("checkboxes",$ar);
458       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
459       return ($display);
460     }
462     /* Bases / Departments */
463       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
464         $this->base= $_POST['base'];
465       }
467     /* Assemble combine string */
468     if ($this->gosaGroupObjects == "[]"){
469       $smarty->assign("combinedObjects", _("none"));
470     } elseif (strlen($this->gosaGroupObjects) > 4){
471       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
472     } else {
473       $conv= array(   "U" => _("users"),
474           "G" => _("groups"),
475           "A" => _("applications"),
476           "D" => _("departments"),
477           "S" => _("servers"),
478           "W" => _("workstations"),
479           "O" => _("winstations"),
480           "T" => _("terminals"),
481           "F" => _("phones"),
482           "P" => _("printers"));
484       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
485       $p1= $conv[$type[0]];
486       error_reporting(0);
487       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
488         $p2= $conv[$type[1]];
489         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
490       } else {
491         $smarty->assign("combinedObjects", "$p1");
492       }
493       error_reporting(E_ALL | E_STRICT);
494     }
496     /* Assign variables */
497     $smarty->assign("bases", $this->get_allowed_bases());
498     $smarty->assign("base_select", $this->base);
499     $smarty->assign("department", $this->department);
500     $smarty->assign("members", $this->convert_list($this->memberList));
502     /* Objects have to be tuned... */
503     $smarty->assign("objects", $this->convert_list($this->objects));
505     /* Fields */
506     foreach ($this->attributes as $val){
507       $smarty->assign("$val", $this->$val);
508     }
510     /******
511       Trust account
512      ******/
513     $smarty->assign("trusthide", " disabled ");
514     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
515     if ($this->trustModel == "fullaccess"){
516       $trustmode= 1;
517       // pervent double disable tag in html code, this will disturb our clean w3c html
518       $smarty->assign("trustmode",  $this->getacl("trustModel"));
520     } elseif ($this->trustModel == "byhost"){
521       $trustmode= 2;
522       $smarty->assign("trusthide", "");
523     } else {
524       // pervent double disable tag in html code, this will disturb our clean w3c html
525       $smarty->assign("trustmode",  $this->getacl("trustModel"));
526       $trustmode= 0;
527     }
528     $smarty->assign("trustmode", $trustmode);
529     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
530           2 => _("allow access to these hosts")));
532     $smarty->assign("workstations", $this->accessTo);
534     if((count($this->accessTo))==0){
535       $smarty->assign("emptyArrAccess",true);
536     }else{
537       $smarty->assign("emptyArrAccess",false);
538     }
539     /******
540       Ende - Trust account
541      ******/
543     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
544   }
547   /* Save data to object */
548   function save_object()
549   {
550     /* Save additional values for possible next step */
551     if (isset($_POST['ogroupedit'])){
553       /******
554         Trust account 
555        ******/
557       if($this->acl_is_writeable("trustModel")){
558         if (isset($_POST['trustmode'])){
559           $saved= $this->trustModel;
560           if ($_POST['trustmode'] == "1"){
561             $this->trustModel= "fullaccess";
562           } elseif ($_POST['trustmode'] == "2"){
563             $this->trustModel= "byhost";
564           } else {
565             $this->trustModel= "";
566           }
567           if ($this->trustModel != $saved){
568             $this->is_modified= TRUE;
569           }
570         }
571       }
572       /******
573         Ende Trust account
574        ******/
576       /* Create a base backup and reset the
577          base directly after calling plugin::save_object();
578          Base will be set seperatly a few lines below */
579       $base_tmp = $this->base;
580       plugin::save_object();
581       $this->base = $base_tmp;
583       /* Save base, since this is no LDAP attribute */
584       $tmp = $this->get_allowed_bases();
585       if(isset($_POST['base'])){
586         if(isset($tmp[$_POST['base']])){
587           $this->base= $_POST['base'];
588         }
589       }
590     }
591   }
594   /* (Re-)Load objects */
595   function reload()
596   {
597     /*###########
598       Variable initialisation 
599       ###########*/
601     $this->objects                = array();
602     $this->ui                     = get_userinfo();
603     $filter                       = "";
604     $objectClasses                = array();
605     
606     $ogfilter               = session::get("ogfilter");
607     $regex                  = $ogfilter['regex'];
609     $ldap= $this->config->get_ldap_link();
610     $ldap->cd ($ogfilter['dselect']);
613     /*###########
614       Generate Filter 
615       ###########*/
617     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
618           "DN"=> get_people_ou()           ,"ACL" => "users"), 
619         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
620           "DN"=> get_groups_ou('ogroupou') ,"ACL" => "groups"), 
621         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
622           "DN"=> ""                        ,"ACL" => "department"), 
623         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
624           "DN"=> get_ou('serverou')        ,"ACL" => "server"),
625         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
626           "DN"=> get_ou('workstationou')   ,"ACL" => "workstation"),
627         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
628           "DN"=> get_ou('WINSTATIONS')     ,"ACL" => "winstation"),
629         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
630           "DN"=> get_ou('terminalou')      ,"ACL" => "terminal"),
631         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
632           "DN"=> get_ou('printerou')       ,"ACL" => "printer"),
633         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
634           "DN"=> get_ou('phoneou')         ,"ACL" => "phone"));
637     /* Allow searching for applications, if we are not using release managed applications 
638       */
639     if(!$this->IsReleaseManagementActivated()){
640       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
641           "DN"=> get_ou('applicationou')   ,"ACL" => "application"); 
642     }
643            
644     /*###########
645       Perform search for selected objectClasses & regex to fill list with objects   
646       ###########*/
648     $Get_list_flags = 0;
649     if($ogfilter['subtrees'] == "checked"){
650       $Get_list_flags |= GL_SUBSEARCH;
651     }    
653     foreach($p_f as $post_name => $data){
655       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
657         if($ogfilter['subtrees']){
658           $base =  $ogfilter['dselect'];
659         }else{
660           $base =  $data['DN'].$ogfilter['dselect'];
661         }
662    
663          
664         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
665         $res    = get_list($filter, $data['ACL']  , $base, 
666                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
668         /* fetch results and append them to the list */
669         foreach($res as $attrs){
671           $type= $this->getObjectType($attrs);
672           $name= $this->getObjectName($attrs);
674           /* Fill array */
675           if (isset($attrs["description"][0])){
676             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
677           } elseif (isset($attrs["uid"][0])) {
678             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
679           } else {
680             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
681           }
682         }
683       }
684     }
685     uasort ($this->objects, 'sort_list');
686     reset ($this->objects);
688     
689     /*###########
690       Build member list and try to detect obsolete entries 
691       ###########*/
693     $this->memberList = array();
694   
695     /* Walk through all single member entry */
696     foreach($this->member as $dn){
698       /* The dn for the current member can't be resolved 
699          it seams that this entry was removed 
700        */ 
701       /* Try to resolv the entry again, if it still fails, display error msg */
702       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
704       /* It has failed, add entry with type flag I (Invalid)*/
705       if (!$ldap->success()){
706         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
708       } else {
710         /* Append this entry to our all object list */
712         /* Fetch object */
713         $attrs= $ldap->fetch();
715         $type= $this->getObjectType($attrs);
716         $name= $this->getObjectName($attrs);
718         if (isset($attrs["description"][0])){
719           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
720         } elseif (isset($attrs["uid"][0])) {
721           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
722         } else {
723           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
724         }
725         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
727         if(isset($attrs['macAddress'][0])){
728           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
729         }else{
730           $this->objcache[$attrs["dn"]]['macAddress']  = "";
731         }
733         if(isset($attrs['uid'])){
734           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
735         }
737         /* Fill array */
738         if (isset($attrs["description"][0])){
739           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
740         } else {
741           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
742         }
744         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
745       }
746     }
747     uasort ($this->memberList, 'sort_list');
748     reset ($this->memberList);
750     /* Assemble types of currently combined objects */
751     $objectTypes= "";
752     foreach ($this->memberList as $dn => $desc){
754       /* Invalid object? */
755       if ($desc['type'] == 'I'){
756         continue;
757       }
759       /* Fine. Add to list. */
760       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
761         $objectTypes.= $desc['type'];
762       }
763     }
764     $this->gosaGroupObjects= "[$objectTypes]";
765   }
768   function convert_list($input)
769   {
770     $temp= "";
771     $conv= array(  "U" => "select_user.png",
772         "G" => "plugins/groups/images/groups.png",
773         "A" => "plugins/ogroups/images/application.png",
774         "D" => "plugins/departments/images/department.png",
775         "S" => "plugins/ogroups/images/server.png",
776         "W" => "plugins/ogroups/images/workstation.png",
777         "O" => "plugins/ogroups/images/winstation.png",
778         "T" => "plugins/ogroups/images/terminal.png",
779         "F" => "plugins/ogroups/images/phone.png",
780         "I" => "images/lists/flag.png",
781         "P" => "plugins/ogroups/images/printer.png");
783     foreach ($input as $key => $value){
784       /* Generate output */
785       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
786     }
788     return ($temp);
789   }
792   function getObjectType($attrs)
793   {
794     $type= "I";
796     foreach(array(  "U" => "gosaAccount",
797           "G" => "posixGroup",
798           "A" => "gosaApplication",
799           "D" => "gosaDepartment",
800           "S" => "goServer",
801           "W" => "gotoWorkstation",
802           "O" => "opsiClient",
803           "T" => "gotoTerminal",
804           "F" => "goFonHardware",
805           "P" => "gotoPrinter") as $index => $class){
806       if (in_array($class, $attrs['objectClass'])){
807         $type= $index;
808         break;
809       }
810     }
812     return ($type);
813   }
816   function getObjectName($attrs)
817   {
818     /* Person? */
819     $name =""; 
820     if (in_array('gosaAccount', $attrs['objectClass'])){
821       if(isset($attrs['sn']) && isset($attrs['givenName'])){
822         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
823       } else {
824         $name= $attrs['uid'][0];
825       }
826     } else {
827       if(isset($attrs["cn"][0])) {
828         $name= $attrs['cn'][0];
829       } else {
830         $name= $attrs['ou'][0];
831       }
832     }
834     return ($name);
835   }
838   function check()
839   {
840     /* Call common method to give check the hook */
841     $message= plugin::check();
843     /* Permissions for that base? */
844     if ($this->base != ""){
845       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
846     } else {
847       $new_dn= $this->dn;
848     }
851     $ldap = $this->config->get_ldap_link();
852     if($this->dn != $new_dn){
853       $ldap->cat ($new_dn, array('dn'));
854     }
855     
856     if($ldap->count() !=0){
857       $message[]= msgPool::duplicated(_("Name"));
858     } 
860     /* Set new acl base */
861     if($this->dn == "new") {
862       $this->set_acl_base($this->base);
863     }
865     /* must: cn */
866     if ($this->cn == ""){
867       $message[]= msgPool::required(_("Name"));
868     }
870     /* To many different object types? */
871     if (strlen($this->gosaGroupObjects) > 4){
872       $message[]= _("You can combine two different object types at maximum, only!");
873     }
875     /* Check if we are allowed to create or move this object 
876      */
877     if($this->dn == "new" && !$this->acl_is_createable($this->base)){
878       $message[] = msgPool::permCreate();
879     }elseif($this->cn != $this->orig_cn || $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
880       $message[] = msgPool::permMove();
881     }
883     return ($message);
884   }
887   /* Save to LDAP */
888   function save()
889   {
890     plugin::save();
892     /* Move members to target array */
893     $this->attrs['member'] =array();
894     foreach ($this->member as $key => $desc){
895       $this->attrs['member'][]= @LDAP::fix($key);
896     }
898     $ldap= $this->config->get_ldap_link();
900     /* New accounts need proper 'dn', propagate it to remaining objects */
901     if ($this->dn == 'new'){
902       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
903     }
905     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
906        new entries. So do a check first... */
907     $ldap->cat ($this->dn, array('dn'));
908     if ($ldap->fetch()){
909       /* Modify needs array() to remove values :-( */
910       if (!count ($this->member)){
911         $this->attrs['member']= array();
912       }
913       $mode= "modify";
915     } else {
916       $mode= "add";
917       $ldap->cd($this->config->current['BASE']);
918       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
919     }
921     /******
922       Trust accounts 
923      ******/
924     $objectclasses= array();
925     foreach ($this->attrs['objectClass'] as $key => $class){
926       if (preg_match('/trustAccount/i', $class)){
927         continue;
928       }
929       $objectclasses[]= $this->attrs['objectClass'][$key];
930     }
931     $this->attrs['objectClass']= $objectclasses;
932     if ($this->trustModel != ""){
933       $this->attrs['objectClass'][]= "trustAccount";
934       $this->attrs['trustModel']= $this->trustModel;
935       $this->attrs['accessTo']= array();
936       if ($this->trustModel == "byhost"){
937         foreach ($this->accessTo as $host){
938           $this->attrs['accessTo'][]= $host;
939         }
940       }
941     } else {
942       if ($this->was_trust_account){
943         $this->attrs['accessTo']= array();
944         $this->attrs['trustModel']= array();
945       }
946     }
948     /******
949       Ende - Trust accounts 
950      ******/
952     /* Write back to ldap */
953     $ldap->cd($this->dn);
954     $this->cleanup();
955     $ldap->$mode($this->attrs);
957     if($mode == "add"){
958       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
959     }else{
960       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
961     }
963     /* Trigger post signal */
964     $this->handle_post_events($mode);
966     $ret= 0;
967     if (!$ldap->success()){
968       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
969       $ret= 1;
970     }
972     return ($ret);
973   }
975   function remove_from_parent()
976   {
977     plugin::remove_from_parent();
979     $ldap= $this->config->get_ldap_link();
980     $ldap->rmdir($this->dn);
981     if (!$ldap->success()){
982       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
983     }
985     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
987     /* Trigger remove signal */
988     $this->handle_post_events("remove");
989   }
991   
992   function PrepareForCopyPaste($source)
993   {
994     /* Update available object types */
995     if(isset($source['gosaGroupObjects'][0])){
996       $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
997     }
999     /* Reload tabs */
1000     $this->parent->reload($this->gosaGroupObjects );
1001    
1002     /* Reload plugins */ 
1003     foreach($this->parent->by_object as $name => $class ){
1004       if(get_class($this) != $name) {
1005         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
1006       }
1007     }
1009     /* Load member objects */
1010     if (isset($source['member'])){
1011       foreach ($source['member'] as $key => $value){
1012         if ("$key" != "count"){
1013           $value= @LDAP::convert($value);
1014           $this->member["$value"]= "$value";
1015         }
1016       }
1017     }
1019   }
1022   function getCopyDialog()
1023   {
1024     $smarty = get_smarty();
1025     $smarty->assign("cn",     $this->cn);
1026     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1027     $ret = array();
1028     $ret['string'] = $str;
1029     $ret['status'] = "";
1030     return($ret);
1031   }
1033   function saveCopyDialog()
1034   {
1035     if(isset($_POST['cn'])){
1036       $this->cn = $_POST['cn'];
1037     }
1038   }
1041   function IsReleaseManagementActivated()
1042   {
1043     /* Check if we should enable the release selection */
1044     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1045     if(!empty($tmp)){
1046       return(true);
1047     }
1048     return(false);
1049   }
1052   static function plInfo()
1053   {
1054     return (array(
1055           "plShortName"   => _("Generic"),
1056           "plDescription" => _("Object group generic"),
1057           "plSelfModify"  => FALSE,
1058           "plDepends"     => array(),
1059           "plPriority"    => 1,
1060           "plSection"     => array("administration"),
1061           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
1062                                                       "objectClass"  => "gosaGroupOfNames")),
1063           "plProvidedAcls"=> array(
1064             "cn"                => _("Name"),
1065             "base"              => _("Base"),
1066             "description"       => _("Description"),
1067             "member"            => _("Member"))
1068           ));
1069   }
1072 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1073 ?>