Code

Updated in
[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     /* Set base */
120     if ($this->dn == "new"){
121       $ui = get_userinfo();
122       $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
123     } else {
124       $this->base= preg_replace("/^[^,]+,".preg_quote(get_ou("ogroupRDN"), '/')."/i","",$this->dn);
126       /* Is this account a trustAccount? */
127       if ($this->is_account && isset($this->attrs['trustModel'])){
128         $this->trustModel= $this->attrs['trustModel'][0];
129         $this->was_trust_account= TRUE;
130       } else {
131         $this->was_trust_account= FALSE;
132         $this->trustModel= "";
133       }
135       $this->accessTo = array();
136       if ($this->is_account && isset($this->attrs['accessTo'])){
137         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
138           $tmp= $this->attrs['accessTo'][$i];
139           $this->accessTo[$tmp]= $tmp;
140         }
141       }
142     }
144     /* Detect all workstations, which are already assigned to an object group  
145         - Those objects will be hidden in the add object dialog.
146         - Check() will complain if such a system is assigned to this object group.
147      */ 
148     $base = $this->config->current['BASE'];
149     $res    = get_list("(|(objectClass=gotoWorkstation)(objectClass=gotoTerminal))","none" , 
150         $base, array("dn"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
151     $ws_dns = array();
152     foreach($res as $data){
153       $ws_dns[] = $data['dn'];
154     }
155     $res=get_list("(&(member=*)(objectClass=gosaGroupOfNames))","none",
156         $base, array("dn","member", "gosaGroupObjects"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
157     $this->used_workstations = array();
158     foreach($res as $og){
159       if($og['dn'] == $this->dn) continue;
160       $test = array_intersect($ws_dns,$og['member']);
161       if(($og['gosaGroupObjects'] == "[W]" || $og['gosaGroupObjects'] == "[T]") && count($test)){
162         $this->used_workstations = array_merge($this->used_workstations,$test);
163       }
164     }
166     $this->orig_cn = $this->cn;
167     $this->orig_base = $this->base;
169     /* Get global filter config */
170     if (!session::is_set("sysfilter")){
171       $ui= get_userinfo();
172       $base= get_base_from_people($ui->dn);
173       $sysfilter= array( "depselect"       => $base,
174           "regex"           => "*");
175       session::set("sysfilter", $sysfilter);
176     }
178     /* Instanciate base selector */
179     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
180     $this->baseSelector->setSubmitButton(false);
181     $this->baseSelector->setHeight(300);
182     $this->baseSelector->update(true);
184     $this->reload();
185   }
187   function AddDelMembership()
188   {
189       /* Delete objects from group */
190       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
191           foreach ($_POST['members'] as $value){
192               $this->objects["$value"]= $this->memberList[$value];
193               unset ($this->memberList["$value"]);
194               unset ($this->member["$value"]);
195               uasort ($this->objects, 'sort_list');
196               reset ($this->objects);
197           }
198           $this->reload();
199       }
201       /* Add objects to group */
202       if (isset($_POST['objectSelect_save']) && $this->objectSelect instanceOf objectSelect){
203           $objects = $this->objectSelect->save();
204           $skipped = FALSE;
205           foreach($objects as $object){
207               $tmp = "";
208               foreach($this->memberList as $obj){
209                   $tmp .= $obj['type'];
210               }
212               $type  = $this->getObjectType($object);
213               $name= $this->getObjectName($object);
214               $dn = $object['dn'];
216               /* Fill array */
217               if (isset($object["description"][0])){
218                   $object= array("text" => "$name [".$object["description"][0]."]", "type" => "$type");
219               } elseif (isset($object["uid"][0])) {
220                   $object= array("text" => "$name [".$object["uid"][0]."]", "type" => "$type");
221               } else {
222                   $object= array("text" => "$name", "type" => "$type");
223               }
225               if(preg_match("/T/",$tmp) && $type == "W"){
226                   $skipped =TRUE;
227               }elseif(preg_match("/W/",$tmp) && $type == "T"){
228                   $skipped =TRUE;
229               }else{
231                   $this->memberList["$dn"]= $object;
232                   $this->member["$dn"]= $dn;
233                   uasort ($this->memberList, 'sort_list');
234                   reset ($this->memberList);
235               }
236           }
237           if($skipped){
238               msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
239           }
240           $this->objectSelect= FALSE;
241           $this->dialog= FALSE;
242           $this->reload();
243       }
244   }
246   function execute()
247   {
248     /* Call parent execute */
249     plugin::execute();
251     if(!$this->view_logged){
252       $this->view_logged = TRUE;
253       new log("view","ogroups/".get_class($this),$this->dn);
254     }
257     /* Do we represent a valid group? */
258     if (!$this->is_account){
259       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
260         msgPool::noValidExtension("object group")."</b>";
261       return ($display);
262     }
265     /* Load templating engine */
266     $smarty= get_smarty();
267     $smarty->assign("usePrototype", "true");
269     $tmp = $this->plInfo();
270     foreach($tmp['plProvidedAcls'] as $name => $translation){
271       $smarty->assign($name."ACL",$this->getacl($name));
272     }
274     /***********
275      * Trusts 
276      ***********/
278     /* Add user workstation? */
279     if (isset($_POST["add_ws"])){
280       $this->trustSelect= new trustSelect($this->config,get_userinfo());
281       $this->dialog= TRUE;
282     }
284     // Add selected machines to trusted ones.
285     if (isset($_POST["add_ws_finish"]) &&  $this->trustSelect){
286       $trusts = $this->trustSelect->detectPostActions();
287       if(isset($trusts['targets'])){
289         $headpage = $this->trustSelect->getHeadpage();
290         foreach($trusts['targets'] as $id){
291           $attrs = $headpage->getEntry($id);
292           $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
293         }
294         ksort($this->accessTo);
295         $this->is_modified= TRUE;
296       }
297       $this->trustSelect= NULL;
298       $this->dialog= FALSE;
299     }
302     /* Remove user workstations? */
303     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
304       foreach($_POST['workstation_list'] as $name){
305         unset ($this->accessTo[$name]);
306       }
307       $this->is_modified= TRUE;
308     }
310     /* Add user workstation finished? */
311     if (isset($_POST["add_ws_cancel"])){
312       $this->trustSelect= NULL;
313       $this->dialog= FALSE;
314     }
316     /* Show ws dialog */
317     if ($this->trustSelect){
318   
319       // Build up blocklist
320       session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
321       return($this->trustSelect->execute());
322     }
324     /***********
325      * Ende - Trusts 
326      ***********/
329     /* Add objects? */
330     if (isset($_POST["edit_membership"])){
331       $this->objectSelect= new objectSelect($this->config, get_userinfo());
332       $this->dialog= TRUE;
333     }
335     /* Add objects finished? */
336     if (isset($_POST["objectSelect_cancel"])){
337       $this->objectSelect= FALSE;
338       $this->dialog= FALSE;
339     }
341     /* Manage object add dialog */
342     if ($this->objectSelect){
343       session::set('filterBlacklist', array('dn'=> $this->member));
344       return($this->objectSelect->execute());
345     }
347     /* Bases / Departments */
348       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
349         $this->base= $_POST['base'];
350       }
352     /* Assemble combine string */
353     if ($this->gosaGroupObjects == "[]"){
354       $smarty->assign("combinedObjects", _("none"));
355     } elseif (strlen($this->gosaGroupObjects) > 4){
356       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
357     } else {
358       $conv= array(   "U" => _("users"),
359           "G" => _("groups"),
360           "A" => _("applications"),
361           "D" => _("departments"),
362           "S" => _("servers"),
363           "W" => _("workstations"),
364           "O" => _("winstations"),
365           "T" => _("terminals"),
366           "F" => _("phones"),
367           "P" => _("printers"));
369       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
370       $p1= $conv[$type[0]];
371       error_reporting(0);
372       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
373         $p2= $conv[$type[1]];
374         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
375       } else {
376         $smarty->assign("combinedObjects", "$p1");
377       }
378       error_reporting(E_ALL | E_STRICT);
379     }
381     /* Assign variables */
382     $smarty->assign("base", $this->baseSelector->render());
383     $smarty->assign("members", $this->convert_list($this->memberList));
385     /* Objects have to be tuned... */
386     $smarty->assign("objects", $this->convert_list($this->objects));
388     /* Fields */
389     foreach ($this->attributes as $val){
390       $smarty->assign("$val", $this->$val);
391     }
393     /******
394       Trust account
395      ******/
396     $smarty->assign("trusthide", " disabled ");
397     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
398     if ($this->trustModel == "fullaccess"){
399       $trustmode= 1;
400       // pervent double disable tag in html code, this will disturb our clean w3c html
401       $smarty->assign("trustmode",  $this->getacl("trustModel"));
403     } elseif ($this->trustModel == "byhost"){
404       $trustmode= 2;
405       $smarty->assign("trusthide", "");
406     } else {
407       // pervent double disable tag in html code, this will disturb our clean w3c html
408       $smarty->assign("trustmode",  $this->getacl("trustModel"));
409       $trustmode= 0;
410     }
411     $smarty->assign("trustmode", $trustmode);
412     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
413           2 => _("allow access to these hosts")));
415     $smarty->assign("workstations", $this->accessTo);
417     if((count($this->accessTo))==0){
418       $smarty->assign("emptyArrAccess",true);
419     }else{
420       $smarty->assign("emptyArrAccess",false);
421     }
422     /******
423       Ende - Trust account
424      ******/
426     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
427   }
430   /* Save data to object */
431   function save_object()
432   {
433     /* Save additional values for possible next step */
434     if (isset($_POST['ogroupedit'])){
436       /******
437         Trust account 
438        ******/
440       if($this->acl_is_writeable("trustModel")){
441         if (isset($_POST['trustmode'])){
442           $saved= $this->trustModel;
443           if ($_POST['trustmode'] == "1"){
444             $this->trustModel= "fullaccess";
445           } elseif ($_POST['trustmode'] == "2"){
446             $this->trustModel= "byhost";
447           } else {
448             $this->trustModel= "";
449           }
450           if ($this->trustModel != $saved){
451             $this->is_modified= TRUE;
452           }
453         }
454       }
455       /******
456         Ende Trust account
457        ******/
459       /* Create a base backup and reset the
460          base directly after calling plugin::save_object();
461          Base will be set seperatly a few lines below */
462       $base_tmp = $this->base;
463       plugin::save_object();
464       $this->base = $base_tmp;
466       /* Refresh base */
467       if ($this->acl_is_moveable($this->base)){
468         if (!$this->baseSelector->update()) {
469           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
470         }
471         if ($this->base != $this->baseSelector->getBase()) {
472           $this->base= $this->baseSelector->getBase();
473           $this->is_modified= TRUE;
474         }
475       }
477     }
478   }
481   /* (Re-)Load objects */
482   function reload()
483   {
484     /*###########
485       Variable initialisation 
486       ###########*/
488     $this->objects                = array();
489     $this->ui                     = get_userinfo();
490     $filter                       = "";
491     $objectClasses                = array();
492     
493     $ogfilter               = session::get("ogfilter");
494     $regex                  = $ogfilter['regex'];
496     $ldap= $this->config->get_ldap_link();
497     $ldap->cd ($ogfilter['dselect']);
500     /*###########
501       Generate Filter 
502       ###########*/
504     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
505           "DN"=> get_people_ou()           ,"ACL" => "users"), 
506         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
507           "DN"=> get_groups_ou('ogroupRDN') ,"ACL" => "groups"), 
508         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
509           "DN"=> ""                        ,"ACL" => "department"), 
510         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
511           "DN"=> get_ou('serverRDN')        ,"ACL" => "server"),
512         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
513           "DN"=> get_ou('workstationRDN')   ,"ACL" => "workstation"),
514         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
515           "DN"=> get_ou('SAMBAMACHINEACCOUNTRDN')     ,"ACL" => "winstation"),
516         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
517           "DN"=> get_ou('terminalRDN')      ,"ACL" => "terminal"),
518         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
519           "DN"=> get_ou('printerRDN')       ,"ACL" => "printer"),
520         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
521           "DN"=> get_ou('phoneRDN')         ,"ACL" => "phone"));
524     /* Allow searching for applications, if we are not using release managed applications 
525       */
526     if(!$this->IsReleaseManagementActivated()){
527       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
528           "DN"=> get_ou('applicationRDN')   ,"ACL" => "application"); 
529     }
530            
531     /*###########
532       Perform search for selected objectClasses & regex to fill list with objects   
533       ###########*/
535     $Get_list_flags = 0;
536     if($ogfilter['subtrees'] == "checked"){
537       $Get_list_flags |= GL_SUBSEARCH;
538     }    
540     foreach($p_f as $post_name => $data){
542       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
544         if($ogfilter['subtrees']){
545           $base =  $ogfilter['dselect'];
546         }else{
547           $base =  $data['DN'].$ogfilter['dselect'];
548         }
549    
550          
551         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
552         $res    = get_list($filter, $data['ACL']  , $base, 
553                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
555         /* fetch results and append them to the list */
556         foreach($res as $attrs){
558           /* Skip workstations which are already assigned to an object group.
559            */
560           if ($this->gosaGroupObjects == "[W]" || $this->gosaGroupObjects == "[T]"){
561             if(in_array_strict($attrs['dn'],$this->used_workstations)){
562               continue;
563             }
564           }
566           $type= $this->getObjectType($attrs);
567           $name= $this->getObjectName($attrs);
569           /* Fill array */
570           if (isset($attrs["description"][0])){
571             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
572           } elseif (isset($attrs["uid"][0])) {
573             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
574           } else {
575             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
576           }
577         }
578       }
579     }
580     uasort ($this->objects, 'sort_list');
581     reset ($this->objects);
583     
584     /*###########
585       Build member list and try to detect obsolete entries 
586       ###########*/
588     $this->memberList = array();
589   
590     /* Walk through all single member entry */
591     foreach($this->member as $dn){
593       /* The dn for the current member can't be resolved 
594          it seams that this entry was removed 
595        */ 
596       /* Try to resolv the entry again, if it still fails, display error msg */
597       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
599       /* It has failed, add entry with type flag I (Invalid)*/
600       if (!$ldap->success()){
601         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".LDAP::fix($dn),"type" => "I");
603       } else {
605         /* Append this entry to our all object list */
607         /* Fetch object */
608         $attrs= $ldap->fetch();
610         $type= $this->getObjectType($attrs);
611         $name= $this->getObjectName($attrs);
613         if (isset($attrs["description"][0])){
614           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
615         } elseif (isset($attrs["uid"][0])) {
616           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
617         } else {
618           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
619         }
620         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
622         if(isset($attrs['macAddress'][0])){
623           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
624         }else{
625           $this->objcache[$attrs["dn"]]['macAddress']  = "";
626         }
628         if(isset($attrs['uid'])){
629           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
630         }
632         /* Fill array */
633         if (isset($attrs["description"][0])){
634           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
635         } else {
636           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
637         }
639         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
640       }
641     }
642     uasort ($this->memberList, 'sort_list');
643     reset ($this->memberList);
645     /* Assemble types of currently combined objects */
646     $objectTypes= "";
647     foreach ($this->memberList as $dn => $desc){
649       /* Invalid object? */
650       if ($desc['type'] == 'I'){
651         continue;
652       }
654       /* Fine. Add to list. */
655       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
656         $objectTypes.= $desc['type'];
657       }
658     }
659     $this->gosaGroupObjects= "[$objectTypes]";
660   }
663   function convert_list($input)
664   {
665     $temp= "";
666     $conv= array(  
667         "Y" => "plugins/users/images/select_template.png",
668         "U" => "plugins/generic/images/head.png",
669         "G" => "plugins/groups/images/groups.png",
670         "A" => "plugins/ogroups/images/application.png",
671         "D" => "plugins/departments/images/department.png",
672         "S" => "plugins/ogroups/images/server.png",
673         "W" => "plugins/ogroups/images/workstation.png",
674         "O" => "plugins/ogroups/images/winstation.png",
675         "T" => "plugins/ogroups/images/terminal.png",
676         "F" => "plugins/ogroups/images/phone.png",
677         "P" => "plugins/ogroups/images/printer.png",
678         "I" => "images/false.png");
680     foreach ($input as $key => $value){
681       /* Generate output */
682       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
683     }
685     return ($temp);
686   }
689   function getObjectType($attrs)
690   {
691     $type= "I";
693     foreach(array(  
694           "Y" => "gosaUserTemplate",
695           "U" => "gosaAccount",
696           "G" => "posixGroup",
697           "A" => "gosaApplication",
698           "D" => "gosaDepartment",
699           "S" => "goServer",
700           "W" => "gotoWorkstation",
701           "O" => "opsiClient",
702           "T" => "gotoTerminal",
703           "F" => "goFonHardware",
704           "P" => "gotoPrinter") as $index => $class){
705       if (in_array_strict($class, $attrs['objectClass'])){
706         $type= $index;
707         break;
708       }
709     }
711     return ($type);
712   }
715   function getObjectName($attrs)
716   {
717     /* Person? */
718     $name =""; 
719     if (in_array_strict('gosaAccount', $attrs['objectClass'])){
720       if(isset($attrs['sn']) && isset($attrs['givenName'])){
721         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
722       } else {
723         $name= $attrs['uid'][0];
724       }
725     } else {
726       if(isset($attrs["cn"][0])) {
727         $name= $attrs['cn'][0];
728       } else {
729         $name= $attrs['ou'][0];
730       }
731     }
733     return ($name);
734   }
737   function check()
738   {
739     /* Call common method to give check the hook */
740     $message= plugin::check();
742     /* Permissions for that base? */
743     if ($this->base != ""){
744       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
745     } else {
746       $new_dn= $this->dn;
747     }
749     /* Check if we have workstations assigned, that are already assigned to
750         another object group.  */
751     if ($this->gosaGroupObjects == "[W]" || $this->gosaGroupObjects == "[T]" ) {
752       $test =array_intersect($this->used_workstations,$this->member); 
753       if(count($test)){
754         $str = "";
755         foreach($test as $dn){
756           $str .= "<li>".$dn."</li>";
757         }
758         $message[] = sprintf(_("These systems are already configured by other object groups and cannot be added:")."<br><ul>%s</ul>",$str);
759       }
760     }
762     $ldap = $this->config->get_ldap_link();
763     if(LDAP::fix($this->dn) != LDAP::fix($new_dn)){
764       $ldap->cat ($new_dn, array('dn'));
765     }
766     
767     if($ldap->count() !=0){
768       $message[]= msgPool::duplicated(_("Name"));
769     } 
771     // Check if a wrong base was supplied
772     if(!$this->baseSelector->checkLastBaseUpdate()){
773       $message[]= msgPool::check_base();;
774     } 
776     /* Set new acl base */
777     if($this->dn == "new") {
778       $this->set_acl_base($this->base);
779     }
781     /* must: cn */
782     if ($this->cn == ""){
783       $message[]= msgPool::required(_("Name"));
784     }
786     if (preg_match('/[=,+<>#;]/', $this->cn)) { 
787       $message[] = msgPool::invalid(_("Name"), $this->cn, "/[^=+,<>#;]/"); 
788     } 
790     /* To many different object types? */
791     if (strlen($this->gosaGroupObjects) > 4){
792       $message[]= _("You can combine two different object types at maximum, only!");
793     }
795     /* Check if we are allowed to create or move this object 
796      */
797     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
798       $message[] = msgPool::permCreate();
799     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
800       $message[] = msgPool::permMove();
801     }
803     return ($message);
804   }
807   /* Save to LDAP */
808   function save()
809   {
810     plugin::save();
812     /* Move members to target array */
813     $this->attrs['member'] =array();
814     foreach ($this->member as $key => $desc){
815       $this->attrs['member'][]= LDAP::fix($key);
816     }
818     $ldap= $this->config->get_ldap_link();
820     /* New accounts need proper 'dn', propagate it to remaining objects */
821     if ($this->dn == 'new'){
822       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
823     }
825     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
826        new entries. So do a check first... */
827     $ldap->cat ($this->dn, array('dn'));
828     if ($ldap->fetch()){
829       /* Modify needs array() to remove values :-( */
830       if (!count ($this->member)){
831         $this->attrs['member']= array();
832       }
833       $mode= "modify";
835     } else {
836       $mode= "add";
837       $ldap->cd($this->config->current['BASE']);
838       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
839     }
841     /******
842       Trust accounts 
843      ******/
844     $objectclasses= array();
845     foreach ($this->attrs['objectClass'] as $key => $class){
846       if (preg_match('/trustAccount/i', $class)){
847         continue;
848       }
849       $objectclasses[]= $this->attrs['objectClass'][$key];
850     }
851     $this->attrs['objectClass']= $objectclasses;
852     if ($this->trustModel != ""){
853       $this->attrs['objectClass'][]= "trustAccount";
854       $this->attrs['trustModel']= $this->trustModel;
855       $this->attrs['accessTo']= array();
856       if ($this->trustModel == "byhost"){
857         foreach ($this->accessTo as $host){
858           $this->attrs['accessTo'][]= $host;
859         }
860       }
861     } else {
862       if ($this->was_trust_account){
863         $this->attrs['accessTo']= array();
864         $this->attrs['trustModel']= array();
865       }
866     }
868     /******
869       Ende - Trust accounts 
870      ******/
872     /* Write back to ldap */
873     $ldap->cd($this->dn);
874     $this->cleanup();
875     $ldap->$mode($this->attrs);
877     if($mode == "add"){
878       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
879     }else{
880       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
881     }
883     /* Trigger post signal */
884     $this->handle_post_events($mode);
886     $ret= 0;
887     if (!$ldap->success()){
888       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
889       $ret= 1;
890     }
892     return ($ret);
893   }
895   function remove_from_parent()
896   {
897     plugin::remove_from_parent();
899     $ldap= $this->config->get_ldap_link();
900     $ldap->rmdir($this->dn);
901     if (!$ldap->success()){
902       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
903     }
905     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
907     /* Trigger remove signal */
908     $this->handle_post_events("remove");
909   }
911   
912   function PrepareForCopyPaste($source)
913   {
914     plugin::PrepareForCopyPaste($source);
916     /* Reload tabs */
917     $this->parent->reload($this->gosaGroupObjects );
918    
919     /* Reload plugins */ 
920     foreach($this->parent->by_object as $name => $class ){
921       if(get_class($this) != $name) {
922         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
923       }
924     }
926     $source_o = new ogroup ($this->config, $source['dn']);
927     foreach(array("accessTo","member","gosaGroupObjects")  as $attr){
928       $this->$attr = $source_o->$attr;
929     }
930   }
933   function getCopyDialog()
934   {
935     $smarty = get_smarty();
936     $smarty->assign("cn",     $this->cn);
937     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
938     $ret = array();
939     $ret['string'] = $str;
940     $ret['status'] = "";
941     return($ret);
942   }
944   function saveCopyDialog()
945   {
946     if(isset($_POST['cn'])){
947       $this->cn = $_POST['cn'];
948     }
949   }
952   function IsReleaseManagementActivated()
953   {
954     /* Check if we should enable the release selection */
955     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
956     if(!empty($tmp)){
957       return(true);
958     }
959     return(false);
960   }
963   static function plInfo()
964   {
965     return (array(
966           "plShortName"   => _("Generic"),
967           "plDescription" => _("Object group generic"),
968           "plSelfModify"  => FALSE,
969           "plDepends"     => array(),
970           "plPriority"    => 1,
971           "plSection"     => array("administration"),
972           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
973                                                       "objectClass"  => "gosaGroupOfNames")),
974           "plProvidedAcls"=> array(
975             "cn"                => _("Name"),
976             "base"              => _("Base"),
977             "description"       => _("Description"),
978             "trustModel"        => _("System trust"),
979             "member"            => _("Member"))
980           ));
981   }
984 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
985 ?>