Code

Added missing check
[gosa.git] / gosa-core / plugins / admin / groups / class_groupGeneric.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 class group extends plugin
24 {
25   /* Group attributes */
26   var $cn= "";
27   var $description= "";
28   var $gidNumber= "";
29   var $memberUid= array();
30   var $memberUid_used_by_some= array();
32   /* Helpers */
33   var $base= "";
34   var $force_gid= FALSE;
35   var $fon_group= FALSE;
36   var $smbgroup= FALSE;
37   var $groupType= FALSE;
38   var $samba3= FALSE;
39   var $sambaSID= "";
40   var $sambaDomainName= "DEFAULT";
41   var $SID= "";
42   var $ridBase= 0;
43   var $members= array();
44   var $users= array();
45   var $member= array();
46   var $allusers= array();
47   var $saved_gidNumber= "";
48   var $oldgroupType= "";
49   var $orig_dn= "";
50   var $orig_cn= "";
51   var $orig_base= "";
52   var $has_mailAccount= FALSE;
53   var $group_dialog= FALSE;
54   var $nagios_group =FALSE;
55   var $sambaGroupType;
56   var $dialog;
57   var $rfc2307bis= FALSE;
58   var $OnlyShowFirstEntries =200;
59   var $dnMapping= array();
60   var $view_logged = FALSE;
61   var $allowGroupsWithSameNameInOtherSubtrees = true;
63   /* Trustmodel/AccessTo 
64    */
65   var $accessTo= array();
66   var $trustModel= "";
67   var $show_ws_dialog = FALSE;
69   /* attribute list for save action */
70   var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID","accessTo","trustModel");
71   var $objectclasses= array("top", "posixGroup");
73   var $CopyPasteVars  = array("force_gid","fon_group","smbgroup","groupType","sambaSID","sambaDomainName","SID","nagios_group","sambaGroupType");
75   var $multiple_support = TRUE;
77   function group (&$config, $dn= NULL)
78   {
79      /* Set rfc2307bis flag */
80      if (isset($config->current['RFC2307BIS']) && ($config->current['RFC2307BIS']== "true")){
81        $this->rfc2307bis= TRUE;
82        $this->attributes[]= "member";
83        $this->objectclasses[]= "groupOfNames";
84      }
86     plugin::plugin ($config, $dn);
88     /* Load attributes depending on the samba version */
89     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
90     $this->orig_dn= $dn;
91     $this->orig_cn= $this->cn;
93     /* Get member list */
94     if (isset($this->attrs['memberUid'][0])){
95       $tmp= array();
96       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
97         $tmp[$this->attrs['memberUid'][$i]]= $this->attrs['memberUid'][$i];
98       }
99       $this->memberUid= $tmp;
100       ksort ($this->memberUid);
101     }
103     /* Save gidNumber for later use */
104     if (isset($this->attrs['gidNumber'])){
105       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
106     }
108     /* Is a samba group? */
109     if (isset($this->attrs['objectClass'])){
110       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == FALSE ){
111         $this->smbgroup= FALSE;
112       } else {
113         $this->smbgroup= TRUE;
114         if (isset($this->attrs['sambaSID'])){
115           $this->sambaSID= $this->attrs['sambaSID'][0];
116         }
117       }
118       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == FALSE ){
119         $this->fon_group= FALSE;
120       } else {
121         $this->fon_group= TRUE;
122       }
123       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
124         $this->nagios_group= FALSE;
125       } else {
126         $this->nagios_group= TRUE;
127       }
128     }
130     /* Set mail flag */
131     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
132       $this->has_mailAccount= TRUE;
133     }
135     /* Get samba Domain in case of samba 3 */
136     if ($this->samba3 && $this->sambaSID != ""){
137       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
138       $ldap= $this->config->get_ldap_link();
139       $ldap->cd($this->config->current['BASE']);
140       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
141       if ($ldap->count() != 0){
142         $attrs= $ldap->fetch();
143         if(isset($attrs['sambaAlgorithmicRidBase'])){  
144           $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];  
145         } else {  
146           $this->ridBase= $this->config->current['RIDBASE'];  
147         } 
149         /* Get domain name for SID */
150         $this->sambaDomainName= "DEFAULT";
151         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
152           if ($val['SID'] == $this->SID){
153             $this->sambaDomainName= $key;
154             break;
155           }
156         }
157       } else {
158         if (isset($this->config->current['RIDBASE'])){
159           $this->sambaDomainName= "DEFAULT";
160           $this->ridBase= $this->config->current['RIDBASE'];
161           $this->SID= $this->config->current['SID'];
162         } else {
163           msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
164         }
165       }
167       /* Get group type */
168       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
169       if ($this->groupType < 500 || $this->groupType > 553){
170         $this->groupType= 0;
171       }
172       $this->oldgroupType= $this->groupType;
173     }
175     /* Get global filter config */
176     if (!session::is_set("gufilter")){
177       $ui= get_userinfo();
178       $base= get_base_from_people($ui->dn);
179       $gufilter= array( "dselect"       => $base,
180           "regex"           => "*");
181       session::set("gufilter", $gufilter);
182     }
183     $gufilter= session::get('gufilter');
184     $gufilter['SubSearchGroup'] = false;
185     session::set('gufilter',$gufilter);
186   
187     if ($this->dn == "new"){
188       if(session::is_set('CurrentMainBase')){
189         $this->base = session::get('CurrentMainBase');
190       }else{
191         $ui= get_userinfo();
192         $this->base= dn2base($ui->dn);
193       }
194     } else {
196       /* Get object base */
197       $this->base =preg_replace ("/^[^,]+,".normalizePreg(get_groups_ou())."/","",$this->dn);
198     }
199     $this->orig_base = $this->base;
201     /* Is this account a trustAccount? */
202     if (isset($this->attrs['trustModel'])){
203       $this->trustModel= $this->attrs['trustModel'][0];
204       $this->was_trust_account= TRUE;
205     } else {
206       $this->was_trust_account= FALSE;
207       $this->trustModel= "";
208     }
210     $this->accessTo = array();
211     if (isset($this->attrs['accessTo'])){
212       for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
213         $tmp= $this->attrs['accessTo'][$i];
214         $this->accessTo[$tmp]= $tmp;
215       }
216     }
218     /* This is always an account */
219     $this->is_account= TRUE;
220     $this->reload();
221   }
223   function execute()
224   {
225     /* Call parent execute */
226     plugin::execute();
228     /* Log view */
229     if($this->is_account && !$this->view_logged){
230       $this->view_logged = TRUE;
231       new log("view","groups/".get_class($this),$this->dn);
232     }
234     /* Do we represent a valid group? */
235     if (!$this->is_account && $this->parent === NULL){
236       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".msgPool::noValidExtension()."</b>";
237       return ($display);
238     }
240     /* Delete user from group */
241     if (isset($_POST['del_users']) && isset($_POST['members'])){
242       foreach ($_POST['members'] as $value){
243         unset ($this->members["$value"]);
244         $this->removeUser($value);
245       }
246       $this->reload();
247     }
249     /* Add objects? */
250     if (isset($_POST["edit_membership"])){
251       $this->group_dialog= TRUE;
252       $this->dialog= TRUE;
253     }
255     /* Add objects finished? */
256     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
257       $this->group_dialog= FALSE;
258       $this->dialog= FALSE;
259     }
261     /* Add user to group */
262     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
263       foreach ($_POST['users'] as $value){
264         $this->members["$value"]= $this->allusers[$value];
265         asort($this->members);
266         $this->addUser($value);
267       }
268       $this->reload();
269     }
271     /* Base select dialog */
272     $once = true;
273     foreach($_POST as $name => $value){
274       if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
275           
276         $once = false;
277         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
278         $this->dialog->setCurrentBase($this->base);
279       }
280     }
282     /* Dialog handling */
283     if(is_object($this->dialog)){
284       /* Must be called before save_object */
285       $this->dialog->save_object();
287       if($this->dialog->isClosed()){
288         $this->dialog = false;
289       }elseif($this->dialog->isSelected()){
291         /* Check if selected base is valid */
292         $tmp = $this->get_allowed_bases();
293         if(isset($tmp[$this->dialog->isSelected()])){
294           $this->base = $this->dialog->isSelected();
295         }
296         $this->dialog= false;
297       }else{
298         return($this->dialog->execute());
299       }
300     }
303     /* Add user workstation? */
304     if (isset($_POST["add_ws"])){
305       $this->show_ws_dialog= TRUE;
306       $this->dialog= TRUE;
307     }
309     /* Add user workstation? */
310     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
311       foreach($_POST['wslist'] as $ws){
312         $this->accessTo[$ws]= $ws;
313       }
314       ksort($this->accessTo);
315       $this->is_modified= TRUE;
316     }
318     /* Remove user workstations? */
319     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
320       foreach($_POST['workstation_list'] as $name){
321         unset ($this->accessTo[$name]);
322       }
323       $this->is_modified= TRUE;
324     }
326     /* Add user workstation finished? */
327     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
328       $this->show_ws_dialog= FALSE;
329       $this->dialog= FALSE;
330     }
332     $smarty= get_smarty();
334     /* Show ws dialog */
335     if ($this->show_ws_dialog){
337       /* Save data */
338       $sysfilter= session::get("sysfilter");
339       foreach( array("depselect", "regex") as $type){
340         if (isset($_POST[$type])){
341           $sysfilter[$type]= $_POST[$type];
342         }
343       }
344       if (isset($_GET['search'])){
345         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
346         if ($s == "**"){
347           $s= "*";
348         }
349         $sysfilter['regex']= $s;
350       }
351       session::set("sysfilter", $sysfilter);
353       /* Get workstation list */
354       $exclude= "";
355       foreach($this->accessTo as $ws){
356         $exclude.= "(cn=$ws)";
357       }
358       if ($exclude != ""){
359         $exclude= "(!(|$exclude))";
360       }
361       $regex= $sysfilter['regex'];
362       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
364       $deps_a = array(get_ou("serverou"),
365                       get_ou("terminalou"),
366                       get_ou("workstationou"));
368       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'],         array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
369       $wslist= array();
370       foreach ($res as $attrs){
371         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
372       }
373       asort($wslist);
374       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
375       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
376       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
377       $smarty->assign("deplist", $this->config->idepartments);
378       $smarty->assign("alphabet", generate_alphabet());
379       foreach( array("depselect", "regex") as $type){
380         $smarty->assign("$type", $sysfilter[$type]);
381       }
382       $smarty->assign("hint", print_sizelimit_warning());
383       $smarty->assign("wslist", $wslist);
384       $smarty->assign("apply", apply_filter());
385       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
386       return ($display);
387     }
389     /* Assign templating stuff */
390     if ($this->samba3){
391       $smarty->assign("samba3", "true");
392     } else {
393       $smarty->assign("samba3", "");
394     }
396     if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
397       $smarty->assign("nagios",true);
398     }else{
399       $smarty->assign("nagios",false);
400     }
401     
402     if($this->config->search("phoneAccount", "CLASS",array('menu'))){
403       $smarty->assign("pickupGroup",true);
404     }else{
405       $smarty->assign("pickupGroup",false);
406     }
408     /* Manage object add dialog */
409     if ($this->group_dialog){
411       /* Save data */
412       $gufilter= session::get("gufilter");
413       foreach( array("dselect", "regex") as $type){
414         if (isset($_POST[$type])){
415           $gufilter[$type]= $_POST[$type];
416         }
417       }
418       if(isset($_POST['regex'])){
419         if(isset($_POST['SubSearchGroup'])){
420           $gufilter['SubSearchGroup'] = true;
421         }else{
422           $gufilter['SubSearchGroup'] = false;
423         }
424       }
426       if (isset($_GET['search'])){
427         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
428         if ($s == "**"){
429           $s= "*";
430         }
431         $gufilter['regex']= $s;
432       }
433       session::set("gufilter", $gufilter);
434       $this->reload();
436       /* Show dialog */
437       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
438       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
439       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
441       $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
442       $smarty->assign("alphabet", generate_alphabet());
443       foreach( array("dselect", "regex","SubSearchGroup") as $type){
444         $smarty->assign("$type", $gufilter[$type]);
445       }
446       $smarty->assign("hint", print_sizelimit_warning());
447       $smarty->assign("users", $this->displayUsers);
448       $smarty->assign("apply", apply_filter());
449       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
450       return ($display);
451     }
453     $smarty->assign("bases", $this->get_allowed_bases());
454     $smarty->assign("base_select", $this->base);
456     if ($this->samba3){
457       $domains= array();
458       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
459         $domains[$name]= $name;
460       }
461       $smarty->assign("sambaDomains", $domains);
462       $smarty->assign("sambaDomainName", $this->sambaDomainName);
463       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
464           514 => _("Domain guests"));
466       /* Don't loose special groups! If not key'ed above, add it to
467          the combo box... */    
468       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
469         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
470       }
472       $smarty->assign("groupTypes", $groupTypes);
473       $smarty->assign("groupType", $this->groupType);
474     }
476     /* Members and users */
477     $smarty->assign("members", $this->members);
479     /* Work on trust modes */
480     $smarty->assign("trusthide", " disabled ");
481     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
482     if ($this->trustModel == "fullaccess"){
483       $trustmode= 1;
484       // pervent double disable tag in html code, this will disturb our clean w3c html
485       $smarty->assign("trustmode",  $this->getacl("trustModel"));
487     } elseif ($this->trustModel == "byhost"){
488       $trustmode= 2;
489       $smarty->assign("trusthide", "");
490     } else {
491       // pervent double disable tag in html code, this will disturb our clean w3c html
492       $smarty->assign("trustmode",  $this->getacl("trustModel"));
493       $trustmode= 0;
494     }
495     $smarty->assign("trustmode", $trustmode);
496     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
497           2 => _("allow access to these hosts")));
499     if((count($this->accessTo))==0){
500       $smarty->assign("emptyArrAccess",true);
501     } else{
502       $smarty->assign("emptyArrAccess",false);
504     }
506     $smarty->assign("workstations", $this->accessTo);
510     /* Checkboxes */
511     foreach (array("force_gid", "smbgroup") as $val){
512       if ($this->$val == "1"){
513         $smarty->assign("$val", "checked");
514       } else {
515         $smarty->assign("$val", "");
516       }
517     }
518     if ($this->force_gid != "1"){
519       $smarty->assign("forceMode", "disabled");
520     }else{
521       $smarty->assign("forceMode", "");
522     }
523     if ($this->fon_group){
524       $smarty->assign("fon_group", "checked");
525     } else {
526       $smarty->assign("fon_group", "");
527     }
529     if ($this->nagios_group){
530       $smarty->assign("nagios_group", "checked");
531     } else {
532       $smarty->assign("nagios_group", "");
533     }
535     /* Fields */
536     foreach (array("cn", "description", "gidNumber") as $val){
537       $smarty->assign("$val", $this->$val);
538     }
540     $tmp = $this->plInfo();
541     foreach($tmp['plProvidedAcls'] as $name => $translation){
542       $smarty->assign($name."ACL",$this->getacl($name));
543     }
544     
545     if($this->acl_is_writeable("base")){
546       $smarty->assign("baseSelect",true);
547     }else{
548       $smarty->assign("baseSelect",false);
549     }
551     /* Show main page */
552     $smarty->assign("alphabet", generate_alphabet(10));
553     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
554     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
555     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
556     $smarty->assign("deplist", $this->config->idepartments);
558     /* Multiple edit handling */
559     $smarty->assign("multiple_support",$this->multiple_support_active);
561     $smarty->assign("memberUid_All",$this->memberUid);
562     $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
564     foreach($this->attributes as $val){
565       if(in_array($val,$this->multi_boxes)){
566         $smarty->assign("use_".$val,TRUE);
567       }else{
568         $smarty->assign("use_".$val,FALSE);
569       }
570     }
571     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $val){
572       if(in_array($val,$this->multi_boxes)){
573         $smarty->assign("use_".$val,TRUE);
574       }else{
575         $smarty->assign("use_".$val,FALSE);
576       }
577     }
579     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
580   }
582   function addUser($uid)
583   {
584     /* In mutliple edit we have to handle two arrays.
585      *  memberUid               : Containing users used in all groups
586      *  memberUid_used_by_some  : Those which are not used in all groups
587      * So we have to remove the given $uid from the ..used_by_some array first.
588      */
589     if($this->multiple_support_active){
590       if(isset($this->memberUid_used_by_some[$uid])){
591         unset($this->memberUid_used_by_some[$uid]);
592       }
593     }  
595     $this->memberUid[$uid]= $uid;
596   }
599   function removeUser($uid)
600   {
601     $temp= array();
602     if(isset($this->memberUid[$uid])){
603       unset($this->memberUid[$uid]);
604     }
606     /* We have two array contianing group members in multiple edit.
607      *  this->memberUid             : Groups used by all currently edited groups 
608      *  this->memberUid_used_by_some: Used by some 
609      * So we have to remove the specified uid from both arrays.
610      */
611     if($this->multiple_support_active){
612       if(isset($this->memberUid_used_by_some[$uid])){
613         unset($this->memberUid_used_by_some[$uid]);
614       }
615     }
616   }
618   /* Reload data */
619   function reload()
620   {
621     /* Fix regex string */
622     $gufilter = session::get("gufilter");
623     $regex    = normalizeLdap($gufilter['regex']);
624     $MaxUser  = $this->OnlyShowFirstEntries;
626     /* Prepare ldap link */
627     $ldap= $this->config->get_ldap_link();
628     $ldap->cd($gufilter['dselect']);
631     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
632         (Store gathered sn/givenName informations in $this->allusers too, 
633          to be prepared when adding/deleting users)
634      */    
635     $filter = "";
636     foreach ($this->memberUid as $value){
637       if(!isset($this->members[$value])){
638         $filter .= "(uid=".normalizeLdap($value).")";
639       }
640     }
641     if(!empty($filter)){    
642       $ldap->cd($this->config->current['BASE']);
643       $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
644       while($attrs = $ldap->fetch()){
645         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
646         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
647         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
648       } 
649     }
650   
651     /* check if all uids are resolved */
652     foreach ($this->memberUid as $value){
653       if(!isset($this->members[$value])){
654         $this->members[$value] =  _("! unknown id")." [".$value."]"; 
655       }
656     }  
658     /* Create display list of users matching regex & filter 
659      */
660     $this->displayUsers = array();
661     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
663     /* Search in current tree or within subtrees depending on the checkbox from filter section */
664     if($gufilter['SubSearchGroup']){
665       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
666       $base = $gufilter['dselect'];
667     }else{
668       $flag = GL_SIZELIMIT ;
669       $base = get_people_ou().$gufilter['dselect'];
670     }
671     $i = 0;
672   
674     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
676     /* Fetch all users and skip already used users */
677     foreach($res as $attrs){
678       if(in_array($attrs['uid'][0], $this->memberUid)) {
679         continue;
680       }
681       $i ++;
682       if($i > $MaxUser) {
683         break;
684       }
685       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
686       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
687       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
688     }
689   
690     /* If more than max users are found, display a message to warn the user */
691     if($i == $MaxUser){
692       msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries!"), $MaxUser), ERROR_DIALOG);
693     }
694     
695     /* Sort lists */
696     natcasesort($this->members);
697     reset($this->members);
698     natcasesort ($this->displayUsers);
699     reset ($this->displayUsers);
700   }
703   /* Create display name, this was used so often that it is excluded into a seperate function */
704   function createResultName($attrs)
705   {
706     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
707       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
708     } else {
709       $ret= $attrs['uid'][0];
710     }
711     return($ret);
712   }
715   function remove_from_parent()
716   {
717     plugin::remove_from_parent();
719     $ldap= $this->config->get_ldap_link();
720     $ldap->rmdir($this->dn);
721     if (!$ldap->success()){
722       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
723     }
725     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
727     /* Delete references to object groups */
728     $ldap->cd ($this->config->current['BASE']);
729     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
730     while ($ldap->fetch()){
731       $og= new ogroup($this->config, $ldap->getDN());
732       unset($og->member[$this->dn]);
733       $og->save ();
734     }
736     /* Remove ACL dependencies too,
737      */
738     $ldap = $this->config->get_ldap_link();
739     $ldap->cd($this->config->current['BASE']);
740     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
741     while($attrs = $ldap->fetch()){
742       $acl = new acl($this->config,$this->parent,$attrs['dn']);
743       foreach($acl->gosaAclEntry as $id => $entry){
744         foreach($entry['members'] as $m_id => $member){
745           if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
746             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
747             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
748           }
749         }
750       }
751       $acl->save();
752     }
754     /* Remove ACL dependencies too,
755      */
756     $tmp = new acl($this->config,$this->parent,$this->dn);
757     $tmp->remove_acl();
759     /* Send signal to the world that we've done */
760     $this->handle_post_events("remove");
761   }
764   /* Save data to object */
765   function save_object()
766   {
767     /* Save additional values for possible next step */
768     if (isset($_POST['groupedit'])){
770       /* Create a base backup and reset the 
771           base directly after calling plugin::save_object();  
772          Base will be set seperatly a few lines below */
773       $base_tmp = $this->base;
774       plugin::save_object();
775       $this->base = $base_tmp;
777       $this->force_gid= 0;
779       /* Only reset sambagroup flag if we are able to write this flag */
780       if($this->acl_is_writeable("sambaGroupType")){
781         $this->smbgroup = 0;
782       }
784       /* Get base selection */
785       if(isset($_POST['base'])){
786         $tmp = $this->get_allowed_bases();
787         if(isset($tmp[$_POST['base']])){
788           $this->base = $_POST['base'];
789         }
790       }
792       foreach (array(
793             "force_gid"  => "gidNumber", 
794             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
795         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
796           $this->$val= $_POST["$val"];
797         }
798       }
800       /* Save sambaDomain attribute */
801       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
802         $this->sambaDomainName= $_POST['sambaDomainName'];
803         $this->groupType= $_POST['groupType'];
804       }
806       /* Save fon attribute */
807       if ($this->acl_is_writeable("fon_group")){
808         if (isset ($_POST['fon_group'])){
809           $this->fon_group= TRUE;
810         } else {
811           $this->fon_group= FALSE;
812         }
813       }
814       if ($this->acl_is_writeable("nagios_group")){
815         if (isset ($_POST['nagios_group'])){
816           $this->nagios_group= TRUE;
817         } else {
818           $this->nagios_group= FALSE;
819         }
820       }
821     }
823     /* Trust mode - special handling */
824     if($this->acl_is_writeable("trustModel")){
825       if (isset($_POST['trustmode'])){
826         $saved= $this->trustModel;
827         if ($_POST['trustmode'] == "1"){
828           $this->trustModel= "fullaccess";
829         } elseif ($_POST['trustmode'] == "2"){
830           $this->trustModel= "byhost";
831         } else {
832           $this->trustModel= "";
833         }
834         if ($this->trustModel != $saved){
835           $this->is_modified= TRUE;
836         }
837       }
838     }
840   }
843   /* Save to LDAP */
844   function save()
845   {
847     /* ID handling */
848     if ($this->force_gid == 0){
849       if ($this->saved_gidNumber != ""){
850         $this->gidNumber= $this->saved_gidNumber;
851       } else {
852         /* Calculate new, lock uids */
853         $wait= 10;
854         while (get_lock("uidnumber") != ""){
855           sleep (1);
857           /* timed out? */
858           if ($wait-- == 0){
859             break;
860           }
861         }
862         add_lock ("uidnumber", "gosa");
863         $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
864       }
865     }
866   
867     plugin::save(); 
869  /* Trust accounts */
870     $objectclasses= array();
871     foreach ($this->attrs['objectClass'] as $key => $class){
872       if (preg_match('/trustAccount/i', $class)){
873         continue;
874       }
875       $objectclasses[]= $this->attrs['objectClass'][$key];
876     }
877     $this->attrs['objectClass']= $objectclasses;
878     if ($this->trustModel != ""){
879       $this->attrs['objectClass'][]= "trustAccount";
880       $this->attrs['trustModel']= $this->trustModel;
881       $this->attrs['accessTo']= array();
882       if ($this->trustModel == "byhost"){
883         foreach ($this->accessTo as $host){
884           $this->attrs['accessTo'][]= $host;
885         }
886       }
887     } else {
888       if ($this->was_trust_account){
889         $this->attrs['accessTo']= array();
890         $this->attrs['trustModel']= array();
891       }
892     }
896     /* Remove objectClass for samba/phone support */
897     $tmp= array();
898     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
899       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
900           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
901           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
902          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
903         $tmp[]= $this->attrs['objectClass'][$i];
904       }
905     }
906     $this->attrs['objectClass']= $tmp;
907     $ldap= $this->config->get_ldap_link();
909     /* Add samba group functionality */
910     if ($this->samba3 && $this->smbgroup){
911   
912       /* Fixed undefined index ... 
913        */ 
914       $this->SID = $this->ridBase = "";
915       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
916         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
917       }else{
918         msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
919       }
920       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
921         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
922       }else{
923         msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
924       }
926       $this->attrs['objectClass'][]= 'sambaGroupMapping';
927       $this->attrs['sambaGroupType']= "2";
929       /* Check if we need to create a special entry */
930       if ($this->groupType == 0){
932         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
933           $gidNumber= $this->gidNumber;
934           while(TRUE){
935             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
936             $ldap->cd($this->config->current['BASE']);
937             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
938             if ($ldap->count() == 0){
939               break;
940             }
941             $gidNumber++;
942           }
943           $this->attrs['sambaSID']= $sid;
944           $this->sambaSID= $sid;
945         }
947       } else {
948         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
949       }
951       /* User wants me to fake the idMappings? This is useful for
952          making winbind resolve the group names in a reasonable amount
953          of time in combination with larger databases. */
954       if (isset($this->config->current['SAMBAIDMAPPING']) &&
955           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
956         $this->attrs['objectClass'][]= "sambaIdmapEntry";
957       }
959     }
961     /* Add phone functionality */
962     if ($this->fon_group){
963       $this->attrs['objectClass'][]= "goFonPickupGroup";
964     }
966     /* Add nagios functionality */
967     if ($this->nagios_group){
968       $this->attrs['objectClass'][]= "nagiosContactGroup";
969     }
971     /* Take members array */
972     if (count ($this->memberUid)){
973       $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
974     }
976     /* New accounts need proper 'dn', propagate it to remaining objects */
977     if ($this->dn == 'new'){
978       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
979     }
981     /* Add member dn's for RFC2307bis Support */
982     if ($this->rfc2307bis){
983       $this->attrs['member'] = array();
984       if (count($this->memberUid)){
985         foreach($this->attrs['memberUid'] as $uid) {
986           $this->attrs['member'][]= $this->dnMapping[$uid];
987         }
988       } else {
989         $this->attrs['member'][]= $this->dn;
990       }
991     }
993     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
994        new entries. So do a check first... */
995     $ldap->cat ($this->dn, array('dn'));
996     if ($ldap->fetch()){
997       /* Modify needs array() to remove values :-( */
998       if (!count ($this->memberUid)){
999         $this->attrs['memberUid']= array();
1000       }
1001       if ($this->samba3){
1002         if (!$this->smbgroup){
1003           $this->attrs['sambaGroupType']= array();
1004           $this->attrs['sambaSID']= array();
1005         }
1006       }
1007       $mode= "modify";
1008     } else {
1009       $mode= "add";
1010       $ldap->cd($this->config->current['BASE']);
1011       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1012     }
1014     /* Write back to ldap */
1015     $ldap->cd($this->dn);
1016     $this->cleanup();
1017     $ldap->$mode($this->attrs);
1019     /* Remove ACL dependencies too,
1020      */
1021     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1022       $tmp = new acl($this->config,$this->parent,$this->dn);
1023       $tmp->update_acl_membership($this->orig_dn,$this->dn);
1024     }
1026     if($this->initially_was_account){
1027       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1028     }else{
1029       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1030     }
1032     $ret= 0;
1033     if (!$ldap->success()){
1034       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1035       $ret= 1;
1036     }
1038     /* Remove uid lock */
1039     del_lock ("uidnumber");
1041     /* Post that we've done*/
1042     $this->handle_post_events($mode);
1044     return ($ret);
1045   }
1047   function check()
1048   {
1049     /* Call common method to give check the hook */
1050     $message= plugin::check();
1052     /* Permissions for that base? */
1053     if ($this->base != ""){
1054       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
1055     } else {
1056       $new_dn= $this->dn;
1057     }
1059     /* must: cn */
1060     if ($this->cn == "" && $this->acl_is_writeable("cn")){
1061       $message[]= msgPool::required(_("Name"));
1062     }
1064     /* Check for valid input */
1065     if (!tests::is_uid($this->cn)){
1066       if (strict_uid_mode()){
1067         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
1068       } else {
1069         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/i");
1070       }
1071     }
1073     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
1075       /* Check for used 'cn' */
1076       $ldap= $this->config->get_ldap_link();
1077       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
1078         $ldap->cd(get_groups_ou().$this->base);
1079         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
1080         if ($ldap->count() != 0){
1081           $message[]= msgPool::duplicated(_("Name"));
1082         }
1083       }
1085     }else{
1087       /* Check for used 'cn' */
1088       $ldap= $this->config->get_ldap_link();
1089       $ldap->cd($this->config->current['BASE']);
1090       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
1091       if ($ldap->count() != 0){
1093         /* New entry? */
1094         if ($this->dn == 'new'){
1095           $message[]= msgPool::duplicated(_("Name"));
1096         }
1098         /* Moved? */
1099         elseif ($new_dn != $this->orig_dn){
1100           $ldap->fetch();
1101           if ($ldap->getDN() != $this->orig_dn){
1102             $message[]= msgPool::duplicated(_("Name"));
1103           }
1104         }
1105       }
1106     }
1107      
1108     /* Check ID */
1109     if ($this->force_gid == "1"){
1110       if (!tests::is_id($this->gidNumber)){
1111         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1112       } else {
1113         if ($this->gidNumber < $this->config->current['MINID']){
1114           $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1115         }
1117       }
1118     }
1120     /* Check if we are allowed to create or move this object 
1121      */
1122     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1123       $message[] = msgPool::permCreate();
1124     }elseif($this->orig_dn != "new" && $this->orig_base && !$this->acl_is_moveable($this->base)){
1125       $message[] = msgPool::permMove();
1126     }
1128     return ($message);
1129   }
1131   function get_next_id($attrib, $dn)
1132   {
1133     $ids= array();
1134     $ldap= $this->config->get_ldap_link();
1136     $ldap->cd ($this->config->current['BASE']);
1137     if (preg_match('/gidNumber/i', $attrib)){
1138       $oc= "posixGroup";
1139     } else {
1140       $oc= "posixAccount";
1141     }
1142     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1144     /* Get list of ids */
1145     while ($attrs= $ldap->fetch()){
1146       $ids[]= (int)$attrs["$attrib"][0];
1147     }
1149     /* Find out next free id near to UID_BASE */
1150     if (!isset($this->config->current['BASE_HOOK'])){
1151       $base= $this->config->current['UIDBASE'];
1152     } else {
1153       /* Call base hook */
1154       $base= get_base_from_hook($dn, $attrib);
1155     }
1156     for ($id= $base; $id++; $id < pow(2,32)){
1157       if (!in_array($id, $ids)){
1158         return ($id);
1159       }
1160     }
1162     /* Check if id reached maximum */
1163     if ($id >= pow(2,32)){
1164       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1165       exit;
1166     }
1167   }
1169   function getCopyDialog()
1170   {
1171     $vars = array("cn");
1172   
1173     if($this ->force_gid){
1174       $used = " checked ";
1175       $dis  = "";
1176     }else{
1177       $used = "";
1178       $dis  = " disabled ";
1179     }
1181     $smarty = get_smarty();
1182     $smarty->assign("used",$used);
1183     $smarty->assign("dis" ,$dis);
1184     $smarty->assign("cn" ,$this->cn);
1185     $smarty->assign("gidNumber",$this->gidNumber);
1186     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1187     $ret = array();
1188     $ret['string'] = $str;
1189     $ret['status'] = "";
1190     return($ret);
1191   }
1193   function saveCopyDialog()
1194   {
1195     if(isset($_POST['cn'])){
1196       $this->cn = $_POST['cn'];
1197     }
1198     if(isset($_POST['force_gid'])){
1199       $this->force_gid  = 1;
1200       $this->gidNumber= $_POST['gidNumber'];
1201     }else{
1202       $this->force_gid  = 0;
1203       $this->gidNumber  = false;
1204     }
1205   }
1207   
1208   /* Return plugin informations for acl handling  */ 
1209   static function plInfo()
1210   {
1211     return (array(  
1212           "plShortName" => _("Generic"),
1213           "plDescription" => _("Generic group settings"),
1214           "plSelfModify"  => FALSE,
1215           "plDepends"     => array(),
1216           "plPriority"    => 0,
1217           "plSection"     => array("admin"),
1218           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1220           "plProvidedAcls"    => array(
1221             "cn"                => _("Name"),
1222             "base"              => _("Base"),
1223             "description"       => _("Description"),
1225             "fonGroup"          => _("Phone pickup group"),
1226             "nagiosGroup"       => _("Nagios group"),
1228             "gidNumber"         => _("GID"),
1229             "memberUid"         => _("Group member"),
1230             "sambaGroupType"    => _("Samba group type"),
1231             "sambaDomainName"   => _("Samba domain name"),
1232             "sambaSID"          => _("Samba SID"))
1233         ));
1234   }
1237   function multiple_save_object()
1238   {
1239     if(isset($_POST['group_mulitple_edit'])){
1241       /* Create a base backup and reset the
1242          base directly after calling plugin::save_object();
1243          Base will be set seperatly a few lines below */
1244       $base_tmp = $this->base;
1245       plugin::multiple_save_object();
1246       plugin::save_object();
1247       $this->base = $base_tmp;
1249       foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1250         if(isset($_POST['use_'.$attr])){
1251           $this->multi_boxes[] = $attr;
1252         }
1253       }
1255       /* Get base selection */
1256       if(isset($_POST['base'])){
1257         $tmp = $this->get_allowed_bases();
1258         if(isset($tmp[$_POST['base']])){
1259           $this->base = $_POST['base'];
1260         }
1261       }
1263       foreach (array( "smbgroup"   => "sambaGroupType") as $val => $aclname) {
1264         if ($this->acl_is_writeable($aclname)){
1265           if(isset($_POST["$val"])){
1266             $this->$val=  TRUE;
1267           }else{
1268             $this->$val=  FALSE;
1269           }
1270         }
1271       }
1273       /* Save sambaDomain attribute */
1274       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
1275         $this->sambaDomainName= $_POST['sambaDomainName'];
1276         $this->groupType= $_POST['groupType'];
1277       }
1279       /* Save fon attribute */
1280       if ($this->acl_is_writeable("fon_group")){
1281         if (isset ($_POST['fon_group'])){
1282           $this->fon_group= TRUE;
1283         } else {
1284           $this->fon_group= FALSE;
1285         }
1286       }
1287     }
1288   }
1291   function get_multi_edit_values()
1292   {
1293     $ret = plugin::get_multi_edit_values();
1295     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){    
1296       if(in_array($attr,$this->multi_boxes)){
1297         $ret[$attr] = $this->$attr;
1298       }
1299     }
1300     $ret['memberUid'] = $this->memberUid;
1301     $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1302     return($ret);
1303   }
1305   function multiple_execute()
1306   {
1307     return($this->execute());
1308   }
1311   /* Initialize plugin with given atribute arrays
1312    */
1313   function init_multiple_support($attrs,$all)
1314   {
1315     plugin::init_multiple_support($attrs,$all);
1317     $this->memberUid = array();
1318     $this->memberUid_used_by_some = array();
1319     if (isset($attrs['memberUid'])){
1320       for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1321         $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1322       }
1323       ksort($this->memberUid);
1324     }
1326     if (isset($all['memberUid'])){
1327       for ($i= 0; $i<$all['memberUid']['count']; $i++){
1328         if(!in_array($all['memberUid'][$i],$this->memberUid)){
1329           $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1330         }
1331       }
1332       ksort($this->memberUid_used_by_some);
1333     }
1334   }
1337   function PrepareForCopyPaste($source)
1338   {
1339     plugin::PrepareForCopyPaste($source);
1341     $this->memberUid = array();
1342     if(isset($source['memberUid'])){
1343       for($i = 0 ; $i < $source['memberUid']['count']; $i ++){
1344         $this->memberUid[] = $source['memberUid'][$i];
1345       }
1346     }
1347   }
1350   function set_multi_edit_values($attrs)
1351   {
1352     $users = array();
1354     /* Update groupMembership, keep optinal group */
1355     foreach($attrs['memberUid_used_by_some'] as $uid){
1356       if(in_array($uid,$this->memberUid)){
1357         $users[$uid] = $uid;
1358       }
1359     }
1361     /* Update groupMembership, add forced groups */
1362     foreach($attrs['memberUid'] as $uid){
1363       $users[$uid] = $uid;
1364     }
1365     plugin::set_multi_edit_values($attrs);
1366     $this->memberUid = $users;
1367   }
1369 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1370 ?>