Code

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