Code

* Fixed undefined index for "All"
[gosa.git] / plugins / admin / groups / class_groupGeneric.inc
1 <?php
3 class group extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Handling of GOsa's base group object";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Group attributes */
11   var $cn= "";
12   var $description= "";
13   var $gidNumber= "";
14   var $memberUid= array();
16   /* Helpers */
17   var $base= "";
18   var $force_gid= FALSE;
19   var $fon_group= FALSE;
20   var $smbgroup= FALSE;
21   var $groupType= FALSE;
22   var $samba3= FALSE;
23   var $sambaSID= "";
24   var $sambaDomainName= "DEFAULT";
25   var $SID= "";
26   var $ridBase= 0;
27   var $members= array();
28   var $users= array();
29   var $member= array();
30   var $allusers= array();
31   var $saved_gidNumber= "";
32   var $oldgroupType= "";
33   var $orig_dn= "";
34   var $orig_cn= "";
35   var $has_mailAccount= FALSE;
36   var $group_dialog= FALSE;
37   var $nagios_group =FALSE;
38   var $sambaGroupType;
39   var $dialog;
40   var $rfc2307bis= FALSE;
41   var $OnlyShowFirstEntries =200;
42   var $dnMapping= array();
44   var $allowGroupsWithSameNameInOtherSubtrees = true;
46   /* attribute list for save action */
47   var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID");
48   var $objectclasses= array("top", "posixGroup");
50   var $CopyPasteVars  = array("force_gid","fon_group","smbgroup","groupType","sambaSID","sambaDomainName","SID","nagios_group","sambaGroupType");
52   function group ($config, $dn= NULL)
53   {
54      /* Set rfc2307bis flag */
55      if (isset($config->current['RFC2307BIS']) && ($config->current['RFC2307BIS']== "true")){
56        $this->rfc2307bis= TRUE;
57        $this->attributes[]= "member";
58        $this->objectclasses[]= "groupOfNames";
59      }
61     plugin::plugin ($config, $dn);
63     /* Load attributes depending on the samba version */
64     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
65     $this->orig_dn= $dn;
66     $this->orig_cn= $this->cn;
68     /* Get member list */
69     if (isset($this->attrs['memberUid'][0])){
70       $tmp= array();
71       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
72         $tmp[]= $this->attrs['memberUid'][$i];
73       }
74       $this->memberUid= $tmp;
75       sort ($this->memberUid);
76     }
78     /* Save gidNumber for later use */
79     if (isset($this->attrs['gidNumber'])){
80       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
81     }
83     /* Is a samba group? */
84     if (isset($this->attrs['objectClass'])){
85       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == NULL ){
86         $this->smbgroup= FALSE;
87       } else {
88         $this->smbgroup= TRUE;
89         if (isset($this->attrs['sambaSID'])){
90           $this->sambaSID= $this->attrs['sambaSID'][0];
91         }
92       }
93       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == NULL ){
94         $this->fon_group= FALSE;
95       } else {
96         $this->fon_group= TRUE;
97       }
98       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == NULL ){
99         $this->nagios_group= FALSE;
100       } else {
101         $this->nagios_group= TRUE;
102       }
103     }
105     /* Set mail flag */
106     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
107       $this->has_mailAccount= TRUE;
108     }
110     /* Get samba Domain in case of samba 3 */
111     if ($this->samba3 && $this->sambaSID != ""){
112       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
113       $ldap= $this->config->get_ldap_link();
114       $ldap->cd($this->config->current['BASE']);
115       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
116       if ($ldap->count() != 0){
117         $attrs= $ldap->fetch();
118         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
120         /* Get domain name for SID */
121         $this->sambaDomainName= "DEFAULT";
122         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
123           if ($val['SID'] == $this->SID){
124             $this->sambaDomainName= $key;
125             break;
126           }
127         }
128       } else {
129         if (isset($this->config->current['RIDBASE'])){
130           $this->sambaDomainName= "DEFAULT";
131           $this->ridBase= $this->config->current['RIDBASE'];
132           $this->SID= $this->config->current['SID'];
133         } else {
134           print_red(_("Can't find this groups SID in LDAP or in your configuration file!"));
135         }
136       }
138       /* Get group type */
139       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
140       if ($this->groupType < 500 || $this->groupType > 553){
141         $this->groupType= 0;
142       }
143       $this->oldgroupType= $this->groupType;
144     }
146     /* Get global filter config */
147     if (!is_global("gufilter")){
148       $ui= get_userinfo();
149       $base= get_base_from_people($ui->dn);
150       $gufilter= array( "dselect"       => $base,
151           "regex"           => "*");
152       register_global("gufilter", $gufilter);
153     }
154     $gufilter= get_global('gufilter');
156     $gufilter['SubSearchGroup'] = false;
157     $gufilter['dselect'] = $_SESSION['gufilter']['dselect'];  
159     register_global('gufilter',$gufilter);
160   
161     if ($this->dn == "new"){
162       if(isset($_SESSION['CurrentMainBase'])){
163         $this->base= $_SESSION['CurrentMainBase'];
164       }else{
165         $ui= get_userinfo();
166         $this->base= dn2base($ui->dn);
167       }
168     } else {
170       /* Get object base */
171       $this->base =preg_replace ("/^[^,]+,".normalizePreg(get_groups_ou())."/","",$this->dn);
172     }
174     /* This is always an account */
175     $this->is_account= TRUE;
176     $this->reload();
177   }
179   function execute()
180   {
181         /* Call parent execute */
182         plugin::execute();
184   /* Do we represent a valid group? */
185     if (!$this->is_account && $this->parent == NULL){
186       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
187         _("This 'dn' is no group.")."</b>";
188       return ($display);
189     }
191     /* Delete user from group */
192     if (isset($_POST['del_users']) && isset($_POST['members'])){
193       foreach ($_POST['members'] as $value){
194         unset ($this->members["$value"]);
195         $this->removeUser($value);
196       }
197       $this->reload();
198     }
200     /* Add objects? */
201     if (isset($_POST["edit_membership"])){
202       $this->group_dialog= TRUE;
203       $this->dialog= TRUE;
204     }
206     /* Add objects finished? */
207     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
208       $this->group_dialog= FALSE;
209       $this->dialog= FALSE;
210     }
212     /* Add user to group */
213     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
214       foreach ($_POST['users'] as $value){
215         $this->members["$value"]= $this->allusers[$value];
216         asort($this->members);
217         $this->addUser($value);
218       }
219       $this->reload();
220     }
222     /* Base select dialog */
223     $once = true;
224     foreach($_POST as $name => $value){
225       if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
226           
227         $once = false;
228         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
229         $this->dialog->setCurrentBase($this->base);
230       }
231     }
233     /* Dialog handling */
234     if(is_object($this->dialog)){
235       /* Must be called before save_object */
236       $this->dialog->save_object();
238       if($this->dialog->isClosed()){
239         $this->dialog = false;
240       }elseif($this->dialog->isSelected()){
242         /* Check if selected base is valid */
243         $tmp = $this->get_allowed_bases();
244         if(isset($tmp[$this->dialog->isSelected()])){
245           $this->base = $this->dialog->isSelected();
246         }
247         $this->dialog= false;
248       }else{
249         return($this->dialog->execute());
250       }
251     }
253    /* Assign templating stuff */
254     $smarty= get_smarty();
255     if ($this->samba3){
256       $smarty->assign("samba3", "true");
257     } else {
258       $smarty->assign("samba3", "");
259     }
261     if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
262       $smarty->assign("nagios",true);
263     }else{
264       $smarty->assign("nagios",false);
265     }
266     
267     if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
268       $smarty->assign("pickupGroup",true);
269     }else{
270       $smarty->assign("pickupGroup",false);
271     }
273     /* Manage object add dialog */
274     if ($this->group_dialog){
276       /* Save data */
277       $gufilter= get_global("gufilter");
278       foreach( array("dselect", "regex") as $type){
279         if (isset($_POST[$type])){
280           $gufilter[$type]= $_POST[$type];
281         }
282       }
283       if(isset($_POST['regex'])){
284         if(isset($_POST['SubSearchGroup'])){
285           $gufilter['SubSearchGroup'] = true;
286         }else{
287           $gufilter['SubSearchGroup'] = false;
288         }
289       }
291       if (isset($_GET['search'])){
292         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
293         if ($s == "**"){
294           $s= "*";
295         }
296         $gufilter['regex']= $s;
297       }
298       register_global("gufilter", $gufilter);
299       $this->reload();
301       /* Show dialog */
302       $smarty->assign("search_image", get_template_path('images/search.png'));
303       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
304       $smarty->assign("tree_image", get_template_path('images/tree.png'));
306       $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
307       $smarty->assign("alphabet", generate_alphabet());
308       foreach( array("dselect", "regex","SubSearchGroup") as $type){
309         $smarty->assign("$type", $gufilter[$type]);
310       }
311       $smarty->assign("hint", print_sizelimit_warning());
312       $smarty->assign("users", $this->displayUsers);
313       $smarty->assign("apply", apply_filter());
314       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
315       return ($display);
316     }
318     $smarty->assign("bases", $this->get_allowed_bases());
319     $smarty->assign("base_select", $this->base);
321     if ($this->samba3){
322       $domains= array();
323       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
324         $domains[$name]= $name;
325       }
326       $smarty->assign("sambaDomains", $domains);
327       $smarty->assign("sambaDomainName", $this->sambaDomainName);
328       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
329           514 => _("Domain guests"));
331       /* Don't loose special groups! If not key'ed above, add it to
332          the combo box... */    
333       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
334         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
335       }
337       $smarty->assign("groupTypes", $groupTypes);
338       $smarty->assign("groupType", $this->groupType);
339     }
341     /* Members and users */
342     $smarty->assign("members", $this->members);
344     /* Checkboxes */
345     foreach (array("force_gid", "smbgroup") as $val){
346       if ($this->$val == "1"){
347         $smarty->assign("$val", "checked");
348       } else {
349         $smarty->assign("$val", "");
350       }
351     }
352     if ($this->force_gid != "1"){
353       $smarty->assign("forceMode", "disabled");
354     }else{
355       $smarty->assign("forceMode", "");
356     }
357     if ($this->fon_group){
358       $smarty->assign("fon_group", "checked");
359     } else {
360       $smarty->assign("fon_group", "");
361     }
363     if ($this->nagios_group){
364       $smarty->assign("nagios_group", "checked");
365     } else {
366       $smarty->assign("nagios_group", "");
367     }
369     /* Fields */
370     foreach (array("cn", "description", "gidNumber") as $val){
371       $smarty->assign("$val", $this->$val);
372     }
374     $tmp = $this->plInfo();
375     foreach($tmp['plProvidedAcls'] as $name => $translation){
376       $smarty->assign($name."ACL",$this->getacl($name));
377     }
378     
379     if($this->acl_is_writeable("base")){
380       $smarty->assign("baseSelect",true);
381     }else{
382       $smarty->assign("baseSelect",false);
383     }
385     /* Show main page */
386     $smarty->assign("alphabet", generate_alphabet(10));
387     $smarty->assign("search_image", get_template_path('images/search.png'));
388     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
389     $smarty->assign("tree_image", get_template_path('images/tree.png'));
390     $smarty->assign("deplist", $this->config->idepartments);
391     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
392   }
394   function addUser($uid)
395   {
396     $this->memberUid[]= $uid;
397     $this->memberUid= array_unique($this->memberUid);
398   }
400   function removeUser($uid)
401   {
402     $temp= array();
403     foreach ($this->memberUid as $value){
404       if ($value != $uid){
405         $temp[]= $value;
406       }
407     }
408     $this->memberUid= $temp;
409   }
412   /* Reload data */
413   function reload()
414   {
415     /* Fix regex string */
416     $gufilter = get_global("gufilter");
417     $regex    = normalizeLdap($gufilter['regex']);
418     $MaxUser  = $this->OnlyShowFirstEntries;
420     /* Prepare ldap link */
421     $ldap= $this->config->get_ldap_link();
422     $ldap->cd($gufilter['dselect']);
425     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
426         (Store gathered sn/givenName informations in $this->allusers too, 
427          to be prepared when adding/deleting users)
428      */    
429     $filter = "";
430     foreach ($this->memberUid as $value){
431       if(!isset($this->members[$value])){
432         $filter .= "(uid=".normalizeLdap($value).")";
433       }
434     }
435     if(!empty($filter)){    
436       $ldap->cd($this->config->current['BASE']);
437       $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
438       while($attrs = $ldap->fetch()){
439         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
440         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
441         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
442       } 
443     }
444   
445     /* check if all uids are resolved */
446     foreach ($this->memberUid as $value){
447       if(!isset($this->members[$value])){
448         $this->members[$value] =  _("! unknown id")." [".$value."]"; 
449       }
450     }  
452     /* Create display list of users matching regex & filter 
453      */
454     $this->displayUsers = array();
455     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
457     /* Search in current tree or within subtrees depending on the checkbox from filter section */
458     if($gufilter['SubSearchGroup']){
459       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
460       $base = $gufilter['dselect'];
461     }else{
462       $flag = GL_SIZELIMIT ;
463       $base = get_people_ou().$gufilter['dselect'];
464     }
465     $i = 0;
466   
468     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
470     /* Fetch all users and skip already used users */
471     foreach($res as $attrs){
472       if(in_array($attrs['uid'][0], $this->memberUid)) {
473         continue;
474       }
475       $i ++;
476       if($i > $MaxUser) {
477         break;
478       }
479       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
480       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
481       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
482     }
483   
484     /* If more than max users are found, display a message to warn the user */
485     if(($i == $MaxUser)){
486       print_red(sprintf(_("Your search method returned more than '%s' users, only '%s' users are shown.") , $MaxUser,$MaxUser));
487     }
488     
489     /* Sort lists */
490     natcasesort($this->members);
491     reset($this->members);
492     natcasesort ($this->displayUsers);
493     reset ($this->displayUsers);
494   }
497   /* Create display name, this was used so often that it is excluded into a seperate function */
498   function createResultName($attrs)
499   {
500     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
501       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
502     } else {
503       $ret= $attrs['uid'][0];
504     }
505     return($ret);
506   }
509   function remove_from_parent()
510   {
511     plugin::remove_from_parent();
513     $ldap= $this->config->get_ldap_link();
514     $ldap->rmdir($this->dn);
515     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn));
517     /* Delete references to object groups */
518     $ldap->cd ($this->config->current['BASE']);
519     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
520     while ($ldap->fetch()){
521       $og= new ogroup($this->config, $ldap->getDN());
522       unset($og->member[$this->dn]);
523       $og->save ();
524     }
526     /* Send signal to the world that we've done */
527     $this->handle_post_events("remove");
528   }
531   /* Save data to object */
532   function save_object()
533   {
534     /* Save additional values for possible next step */
535     if (isset($_POST['groupedit'])){
537       /* Create a base backup and reset the 
538           base directly after calling plugin::save_object();  
539          Base will be set seperatly a few lines below */
540       $base_tmp = $this->base;
541       plugin::save_object();
542       $this->base = $base_tmp;
544       $this->force_gid= 0;
546       /* Only reset sambagroup flag if we are able to write this flag */
547       if($this->acl_is_writeable("sambaGroupType")){
548         $this->smbgroup = 0;
549       }
551       /* Get base selection */
552       if(isset($_POST['base'])){
553         $tmp = $this->get_allowed_bases();
554         if(isset($tmp[$_POST['base']])){
555           $this->base = $_POST['base'];
556         }
557       }
559       foreach (array(
560             "force_gid"  => "gidNumber", 
561             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
562         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
563           $this->$val= $_POST["$val"];
564         }
565       }
567       /* Save sambaDomain attribute */
568       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
569         $this->sambaDomainName= $_POST['sambaDomainName'];
570         $this->groupType= $_POST['groupType'];
571       }
573       /* Save fon attribute */
574       if ($this->acl_is_writeable("fon_group")){
575         if (isset ($_POST['fon_group'])){
576           $this->fon_group= TRUE;
577         } else {
578           $this->fon_group= FALSE;
579         }
580       }
581       if ($this->acl_is_writeable("nagios_group")){
582         if (isset ($_POST['nagios_group'])){
583           $this->nagios_group= TRUE;
584         } else {
585           $this->nagios_group= FALSE;
586         }
587       }
588     }
589   }
592   /* Save to LDAP */
593   function save()
594   {
596     /* ID handling */
597     if ($this->force_gid == 0){
598       if ($this->saved_gidNumber != ""){
599         $this->gidNumber= $this->saved_gidNumber;
600       } else {
601         /* Calculate new, lock uids */
602         $wait= 10;
603         while (get_lock("uidnumber") != ""){
604           sleep (1);
606           /* timed out? */
607           if ($wait-- == 0){
608             break;
609           }
610         }
611         add_lock ("uidnumber", "gosa");
612         $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
613       }
614     }
615   
616     plugin::save(); 
618     /* Remove objectClass for samba/phone support */
619     $tmp= array();
620     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
621       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
622           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
623           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
624          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
625         $tmp[]= $this->attrs['objectClass'][$i];
626       }
627     }
628     $this->attrs['objectClass']= $tmp;
629     $ldap= $this->config->get_ldap_link();
631     /* Add samba group functionality */
632     if ($this->samba3 && $this->smbgroup){
633   
634       /* Fixed undefined index ... 
635        */ 
636       $this->SID = $this->ridBase = "";
637       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
638         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
639       }else{
640         print_red(sprintf(_("No configured SID found for '%s'."),$this->sambaDomainName));
641       }
642       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
643         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
644       }else{
645         print_red(sprintf(_("No configured RIDBASE found for '%s'."),$this->sambaDomainName));
646       }
648       $this->attrs['objectClass'][]= 'sambaGroupMapping';
649       $this->attrs['sambaGroupType']= "2";
651       /* Check if we need to create a special entry */
652       if ($this->groupType == 0){
654         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
655           $gidNumber= $this->gidNumber;
656           while(TRUE){
657             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
658             $ldap->cd($this->config->current['BASE']);
659             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
660             if ($ldap->count() == 0){
661               break;
662             }
663             $gidNumber++;
664           }
665           $this->attrs['sambaSID']= $sid;
666           $this->sambaSID= $sid;
667         }
669       } else {
670         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
671       }
673       /* User wants me to fake the idMappings? This is useful for
674          making winbind resolve the group names in a reasonable amount
675          of time in combination with larger databases. */
676       if (isset($this->config->current['SAMBAIDMAPPING']) &&
677           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
678         $this->attrs['objectClass'][]= "sambaIdmapEntry";
679       }
681     }
683     /* Add phone functionality */
684     if ($this->fon_group){
685       $this->attrs['objectClass'][]= "goFonPickupGroup";
686     }
688     /* Add nagios functionality */
689     if ($this->nagios_group){
690       $this->attrs['objectClass'][]= "nagiosContactGroup";
691     }
693     /* Take members array */
694     if (count ($this->memberUid)){
695       $this->attrs['memberUid']= array_unique($this->memberUid);
696     }
698     /* New accounts need proper 'dn', propagate it to remaining objects */
699     if ($this->dn == 'new'){
700       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
701     }
703     /* Add member dn's for RFC2307bis Support */
704     if ($this->rfc2307bis){
705       if (count($this->memberUid)){
706         $this->attrs['member'] = array();
707         foreach($this->attrs['memberUid'] as $uid) {
708           $this->attrs['member'][]= $this->dnMapping[$uid];
709         }
710       } else {
711         $this->attrs['member'][]= $this->dn;
712       }
713     }
715     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
716        new entries. So do a check first... */
717     $ldap->cat ($this->dn, array('dn'));
718     if ($ldap->fetch()){
719       /* Modify needs array() to remove values :-( */
720       if (!count ($this->memberUid)){
721         $this->attrs['memberUid']= array();
722       }
723       if ($this->samba3){
724         if (!$this->smbgroup){
725           $this->attrs['sambaGroupType']= array();
726           $this->attrs['sambaSID']= array();
727         }
728       }
729       $mode= "modify";
730     } else {
731       $mode= "add";
732       $ldap->cd($this->config->current['BASE']);
733       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
734     }
736     /* Write back to ldap */
737     $ldap->cd($this->dn);
738     $this->cleanup();
739     $ldap->$mode($this->attrs);
741     $ret= 0;
742     if ( show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn))){
743       $ret= 1;
744     }
746     /* Remove uid lock */
747     del_lock ("uidnumber");
749     /* Post that we've done*/
750     $this->handle_post_events($mode);
752     return ($ret);
753   }
755   function check()
756   {
757     /* Call common method to give check the hook */
758     $message= plugin::check();
760     /* Permissions for that base? */
761     if ($this->base != ""){
762       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
763     } else {
764       $new_dn= $this->dn;
765     }
767     /* must: cn */
768     if ($this->cn == "" && $this->acl_is_writeable("cn")){
769       $message[]= "The required field 'Name' is not set.";
770     }
772     /* Check for valid input */
773     if (!is_uid($this->cn)){
774       $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
775     }
777     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
779       /* Check for used 'cn' */
780       $ldap= $this->config->get_ldap_link();
781       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
782         $ldap->cd("ou=groups,".$this->base);
783         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
784         if ($ldap->count() != 0){
785           $message[]= _("Value specified as 'Name' is already used.");
786         }
787       }
789     }else{
791       /* Check for used 'cn' */
792       $ldap= $this->config->get_ldap_link();
793       $ldap->cd($this->config->current['BASE']);
794       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
795       if ($ldap->count() != 0){
797         /* New entry? */
798         if ($this->dn == 'new'){
799           $message[]= _("Value specified as 'Name' is already used.");
800         }
802         /* Moved? */
803         elseif ($new_dn != $this->orig_dn){
804           $ldap->fetch();
805           if ($ldap->getDN() != $this->orig_dn){
806             $message[]= _("Value specified as 'Name' is already used.");
807           }
808         }
809       }
810     }
811      
812     /* Check ID */
813     if ($this->force_gid == "1"){
814       if (!is_id($this->gidNumber)){
815         $message[]= _("Value specified as 'GID' is not valid.");
816       } else {
817         if ($this->gidNumber < $this->config->current['MINID']){
818           $message[]= _("Value specified as 'GID' is too small.");
819         }
821       }
822     }
824     return ($message);
825   }
827   function get_next_id($attrib, $dn)
828   {
829     $ids= array();
830     $ldap= $this->config->get_ldap_link();
832     $ldap->cd ($this->config->current['BASE']);
833     if (preg_match('/gidNumber/i', $attrib)){
834       $oc= "posixGroup";
835     } else {
836       $oc= "posixAccount";
837     }
838     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
840     /* Get list of ids */
841     while ($attrs= $ldap->fetch()){
842       $ids[]= (int)$attrs["$attrib"][0];
843     }
845     /* Find out next free id near to UID_BASE */
846     if (!isset($this->config->current['BASE_HOOK'])){
847       $base= $this->config->current['UIDBASE'];
848     } else {
849       /* Call base hook */
850       $base= get_base_from_hook($dn, $attrib);
851     }
852     for ($id= $base; $id++; $id < pow(2,32)){
853       if (!in_array($id, $ids)){
854         return ($id);
855       }
856     }
858     /* Check if id reached maximum */
859     if ($id >= pow(2,32)){
860       print_red(_("Too many users, can't allocate a free ID!"));
861       exit;
862     }
863   }
865   function getCopyDialog()
866   {
867     $vars = array("cn");
868   
869     if($this ->force_gid){
870       $used = " checked ";
871       $dis  = "";
872     }else{
873       $used = "";
874       $dis  = " disabled ";
875     }
877     $smarty = get_smarty();
878     $smarty->assign("used",$used);
879     $smarty->assign("dis" ,$dis);
880     $smarty->assign("cn" ,$this->cn);
881     $smarty->assign("gidNumber",$this->gidNumber);
882     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
883     $ret = array();
884     $ret['string'] = $str;
885     $ret['status'] = "";
886     return($ret);
887   }
889   function saveCopyDialog()
890   {
891     if(isset($_POST['cn'])){
892       $this->cn = $_POST['cn'];
893     }
894     if(isset($_POST['force_gid'])){
895       $this->force_gid  = 1;
896       $this->gidNumber= $_POST['gidNumber'];
897     }else{
898       $this->force_gid  = 0;
899       $this->gidNumber  = false;
900     }
901   }
903   
904   /* Return plugin informations for acl handling  */ 
905   function plInfo()
906   {
907     return (array(  
908           "plShortName" => _("Generic"),
909           "plDescription" => _("Generic group settings"),
910           "plSelfModify"  => FALSE,
911           "plDepends"     => array(),
912           "plPriority"    => 0,
913           "plSection"     => array("admin"),
914           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
916           "plProvidedAcls"    => array(
917             "cn"                => _("Name"),
918             "base"              => _("Base"),
919             "description"       => _("Description"),
921             "fonGroup"          => _("Phone pickup group"),
922             "nagiosGroup"       => _("Nagios group"),
924             "gidNumber"         => _("GID"),
925             "memberUid"         => _("Group member"),
926             "sambaGroupType"    => _("Samba group type"),
927             "sambaDomainName"   => _("Samba domain name"),
928             "sambaSID"          => _("Samba SID"))
929         ));
930   }
933 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
934 ?>