Code

Starting move
[gosa.git] / gosa-core / 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();
43   var $view_logged = FALSE;
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']) == FALSE ){
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']) == FALSE ){
94         $this->fon_group= FALSE;
95       } else {
96         $this->fon_group= TRUE;
97       }
98       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
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     /* Log view */
185     if($this->is_account && !$this->view_logged){
186       $this->view_logged = TRUE;
187       new log("view","groups/".get_class($this),$this->dn);
188     }
190     /* Do we represent a valid group? */
191     if (!$this->is_account && $this->parent === NULL){
192       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
193         _("This 'dn' is no group.")."</b>";
194       return ($display);
195     }
197     /* Delete user from group */
198     if (isset($_POST['del_users']) && isset($_POST['members'])){
199       foreach ($_POST['members'] as $value){
200         unset ($this->members["$value"]);
201         $this->removeUser($value);
202       }
203       $this->reload();
204     }
206     /* Add objects? */
207     if (isset($_POST["edit_membership"])){
208       $this->group_dialog= TRUE;
209       $this->dialog= TRUE;
210     }
212     /* Add objects finished? */
213     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
214       $this->group_dialog= FALSE;
215       $this->dialog= FALSE;
216     }
218     /* Add user to group */
219     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
220       foreach ($_POST['users'] as $value){
221         $this->members["$value"]= $this->allusers[$value];
222         asort($this->members);
223         $this->addUser($value);
224       }
225       $this->reload();
226     }
228     /* Base select dialog */
229     $once = true;
230     foreach($_POST as $name => $value){
231       if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
232           
233         $once = false;
234         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
235         $this->dialog->setCurrentBase($this->base);
236       }
237     }
239     /* Dialog handling */
240     if(is_object($this->dialog)){
241       /* Must be called before save_object */
242       $this->dialog->save_object();
244       if($this->dialog->isClosed()){
245         $this->dialog = false;
246       }elseif($this->dialog->isSelected()){
248         /* Check if selected base is valid */
249         $tmp = $this->get_allowed_bases();
250         if(isset($tmp[$this->dialog->isSelected()])){
251           $this->base = $this->dialog->isSelected();
252         }
253         $this->dialog= false;
254       }else{
255         return($this->dialog->execute());
256       }
257     }
259    /* Assign templating stuff */
260     $smarty= get_smarty();
261     if ($this->samba3){
262       $smarty->assign("samba3", "true");
263     } else {
264       $smarty->assign("samba3", "");
265     }
267     if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
268       $smarty->assign("nagios",true);
269     }else{
270       $smarty->assign("nagios",false);
271     }
272     
273     if($this->config->search("phoneAccount", "CLASS",array('menu'))){
274       $smarty->assign("pickupGroup",true);
275     }else{
276       $smarty->assign("pickupGroup",false);
277     }
279     /* Manage object add dialog */
280     if ($this->group_dialog){
282       /* Save data */
283       $gufilter= get_global("gufilter");
284       foreach( array("dselect", "regex") as $type){
285         if (isset($_POST[$type])){
286           $gufilter[$type]= $_POST[$type];
287         }
288       }
289       if(isset($_POST['regex'])){
290         if(isset($_POST['SubSearchGroup'])){
291           $gufilter['SubSearchGroup'] = true;
292         }else{
293           $gufilter['SubSearchGroup'] = false;
294         }
295       }
297       if (isset($_GET['search'])){
298         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
299         if ($s == "**"){
300           $s= "*";
301         }
302         $gufilter['regex']= $s;
303       }
304       register_global("gufilter", $gufilter);
305       $this->reload();
307       /* Show dialog */
308       $smarty->assign("search_image", get_template_path('images/search.png'));
309       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
310       $smarty->assign("tree_image", get_template_path('images/tree.png'));
312       $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
313       $smarty->assign("alphabet", generate_alphabet());
314       foreach( array("dselect", "regex","SubSearchGroup") as $type){
315         $smarty->assign("$type", $gufilter[$type]);
316       }
317       $smarty->assign("hint", print_sizelimit_warning());
318       $smarty->assign("users", $this->displayUsers);
319       $smarty->assign("apply", apply_filter());
320       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
321       return ($display);
322     }
324     $smarty->assign("bases", $this->get_allowed_bases());
325     $smarty->assign("base_select", $this->base);
327     if ($this->samba3){
328       $domains= array();
329       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
330         $domains[$name]= $name;
331       }
332       $smarty->assign("sambaDomains", $domains);
333       $smarty->assign("sambaDomainName", $this->sambaDomainName);
334       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
335           514 => _("Domain guests"));
337       /* Don't loose special groups! If not key'ed above, add it to
338          the combo box... */    
339       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
340         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
341       }
343       $smarty->assign("groupTypes", $groupTypes);
344       $smarty->assign("groupType", $this->groupType);
345     }
347     /* Members and users */
348     $smarty->assign("members", $this->members);
350     /* Checkboxes */
351     foreach (array("force_gid", "smbgroup") as $val){
352       if ($this->$val == "1"){
353         $smarty->assign("$val", "checked");
354       } else {
355         $smarty->assign("$val", "");
356       }
357     }
358     if ($this->force_gid != "1"){
359       $smarty->assign("forceMode", "disabled");
360     }else{
361       $smarty->assign("forceMode", "");
362     }
363     if ($this->fon_group){
364       $smarty->assign("fon_group", "checked");
365     } else {
366       $smarty->assign("fon_group", "");
367     }
369     if ($this->nagios_group){
370       $smarty->assign("nagios_group", "checked");
371     } else {
372       $smarty->assign("nagios_group", "");
373     }
375     /* Fields */
376     foreach (array("cn", "description", "gidNumber") as $val){
377       $smarty->assign("$val", $this->$val);
378     }
380     $tmp = $this->plInfo();
381     foreach($tmp['plProvidedAcls'] as $name => $translation){
382       $smarty->assign($name."ACL",$this->getacl($name));
383     }
384     
385     if($this->acl_is_writeable("base")){
386       $smarty->assign("baseSelect",true);
387     }else{
388       $smarty->assign("baseSelect",false);
389     }
391     /* Show main page */
392     $smarty->assign("alphabet", generate_alphabet(10));
393     $smarty->assign("search_image", get_template_path('images/search.png'));
394     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
395     $smarty->assign("tree_image", get_template_path('images/tree.png'));
396     $smarty->assign("deplist", $this->config->idepartments);
397     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
398   }
400   function addUser($uid)
401   {
402     $this->memberUid[]= $uid;
403     $this->memberUid= array_unique($this->memberUid);
404   }
406   function removeUser($uid)
407   {
408     $temp= array();
409     foreach ($this->memberUid as $value){
410       if ($value != $uid){
411         $temp[]= $value;
412       }
413     }
414     $this->memberUid= $temp;
415   }
418   /* Reload data */
419   function reload()
420   {
421     /* Fix regex string */
422     $gufilter = get_global("gufilter");
423     $regex    = normalizeLdap($gufilter['regex']);
424     $MaxUser  = $this->OnlyShowFirstEntries;
426     /* Prepare ldap link */
427     $ldap= $this->config->get_ldap_link();
428     $ldap->cd($gufilter['dselect']);
431     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
432         (Store gathered sn/givenName informations in $this->allusers too, 
433          to be prepared when adding/deleting users)
434      */    
435     $filter = "";
436     foreach ($this->memberUid as $value){
437       if(!isset($this->members[$value])){
438         $filter .= "(uid=".normalizeLdap($value).")";
439       }
440     }
441     if(!empty($filter)){    
442       $ldap->cd($this->config->current['BASE']);
443       $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
444       while($attrs = $ldap->fetch()){
445         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
446         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
447         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
448       } 
449     }
450   
451     /* check if all uids are resolved */
452     foreach ($this->memberUid as $value){
453       if(!isset($this->members[$value])){
454         $this->members[$value] =  _("! unknown id")." [".$value."]"; 
455       }
456     }  
458     /* Create display list of users matching regex & filter 
459      */
460     $this->displayUsers = array();
461     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
463     /* Search in current tree or within subtrees depending on the checkbox from filter section */
464     if($gufilter['SubSearchGroup']){
465       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
466       $base = $gufilter['dselect'];
467     }else{
468       $flag = GL_SIZELIMIT ;
469       $base = get_people_ou().$gufilter['dselect'];
470     }
471     $i = 0;
472   
474     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
476     /* Fetch all users and skip already used users */
477     foreach($res as $attrs){
478       if(in_array($attrs['uid'][0], $this->memberUid)) {
479         continue;
480       }
481       $i ++;
482       if($i > $MaxUser) {
483         break;
484       }
485       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
486       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
487       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
488     }
489   
490     /* If more than max users are found, display a message to warn the user */
491     if(($i == $MaxUser)){
492       print_red(sprintf(_("Your search method returned more than '%s' users, only '%s' users are shown.") , $MaxUser,$MaxUser));
493     }
494     
495     /* Sort lists */
496     natcasesort($this->members);
497     reset($this->members);
498     natcasesort ($this->displayUsers);
499     reset ($this->displayUsers);
500   }
503   /* Create display name, this was used so often that it is excluded into a seperate function */
504   function createResultName($attrs)
505   {
506     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
507       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
508     } else {
509       $ret= $attrs['uid'][0];
510     }
511     return($ret);
512   }
515   function remove_from_parent()
516   {
517     plugin::remove_from_parent();
519     $ldap= $this->config->get_ldap_link();
520     $ldap->rmdir($this->dn);
521     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn));
523     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
525     /* Delete references to object groups */
526     $ldap->cd ($this->config->current['BASE']);
527     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
528     while ($ldap->fetch()){
529       $og= new ogroup($this->config, $ldap->getDN());
530       unset($og->member[$this->dn]);
531       $og->save ();
532     }
534     /* Remove ACL dependencies too,
535      */
536     $ldap = $this->config->get_ldap_link();
537     $ldap->cd($this->config->current['BASE']);
538     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
539     while($attrs = $ldap->fetch()){
540       $acl = new acl($this->config,$this->parent,$attrs['dn']);
541       foreach($acl->gosaAclEntry as $id => $entry){
542         foreach($entry['members'] as $m_id => $member){
543           if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
544             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
545             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
546           }
547         }
548       }
549       $acl -> save();
550     }
552     /* Remove ACL dependencies too,
553      */
554     $tmp = new acl($this->config,$this->parent,$this->dn);
555     $tmp->remove_acl();
557     /* Send signal to the world that we've done */
558     $this->handle_post_events("remove");
559   }
562   /* Save data to object */
563   function save_object()
564   {
565     /* Save additional values for possible next step */
566     if (isset($_POST['groupedit'])){
568       /* Create a base backup and reset the 
569           base directly after calling plugin::save_object();  
570          Base will be set seperatly a few lines below */
571       $base_tmp = $this->base;
572       plugin::save_object();
573       $this->base = $base_tmp;
575       $this->force_gid= 0;
577       /* Only reset sambagroup flag if we are able to write this flag */
578       if($this->acl_is_writeable("sambaGroupType")){
579         $this->smbgroup = 0;
580       }
582       /* Get base selection */
583       if(isset($_POST['base'])){
584         $tmp = $this->get_allowed_bases();
585         if(isset($tmp[$_POST['base']])){
586           $this->base = $_POST['base'];
587         }
588       }
590       foreach (array(
591             "force_gid"  => "gidNumber", 
592             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
593         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
594           $this->$val= $_POST["$val"];
595         }
596       }
598       /* Save sambaDomain attribute */
599       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
600         $this->sambaDomainName= $_POST['sambaDomainName'];
601         $this->groupType= $_POST['groupType'];
602       }
604       /* Save fon attribute */
605       if ($this->acl_is_writeable("fon_group")){
606         if (isset ($_POST['fon_group'])){
607           $this->fon_group= TRUE;
608         } else {
609           $this->fon_group= FALSE;
610         }
611       }
612       if ($this->acl_is_writeable("nagios_group")){
613         if (isset ($_POST['nagios_group'])){
614           $this->nagios_group= TRUE;
615         } else {
616           $this->nagios_group= FALSE;
617         }
618       }
619     }
620   }
623   /* Save to LDAP */
624   function save()
625   {
627     /* ID handling */
628     if ($this->force_gid == 0){
629       if ($this->saved_gidNumber != ""){
630         $this->gidNumber= $this->saved_gidNumber;
631       } else {
632         /* Calculate new, lock uids */
633         $wait= 10;
634         while (get_lock("uidnumber") != ""){
635           sleep (1);
637           /* timed out? */
638           if ($wait-- == 0){
639             break;
640           }
641         }
642         add_lock ("uidnumber", "gosa");
643         $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
644       }
645     }
646   
647     plugin::save(); 
649     /* Remove objectClass for samba/phone support */
650     $tmp= array();
651     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
652       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
653           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
654           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
655          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
656         $tmp[]= $this->attrs['objectClass'][$i];
657       }
658     }
659     $this->attrs['objectClass']= $tmp;
660     $ldap= $this->config->get_ldap_link();
662     /* Add samba group functionality */
663     if ($this->samba3 && $this->smbgroup){
664   
665       /* Fixed undefined index ... 
666        */ 
667       $this->SID = $this->ridBase = "";
668       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
669         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
670       }else{
671         print_red(sprintf(_("No configured SID found for '%s'."),$this->sambaDomainName));
672       }
673       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
674         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
675       }else{
676         print_red(sprintf(_("No configured RIDBASE found for '%s'."),$this->sambaDomainName));
677       }
679       $this->attrs['objectClass'][]= 'sambaGroupMapping';
680       $this->attrs['sambaGroupType']= "2";
682       /* Check if we need to create a special entry */
683       if ($this->groupType == 0){
685         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
686           $gidNumber= $this->gidNumber;
687           while(TRUE){
688             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
689             $ldap->cd($this->config->current['BASE']);
690             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
691             if ($ldap->count() == 0){
692               break;
693             }
694             $gidNumber++;
695           }
696           $this->attrs['sambaSID']= $sid;
697           $this->sambaSID= $sid;
698         }
700       } else {
701         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
702       }
704       /* User wants me to fake the idMappings? This is useful for
705          making winbind resolve the group names in a reasonable amount
706          of time in combination with larger databases. */
707       if (isset($this->config->current['SAMBAIDMAPPING']) &&
708           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
709         $this->attrs['objectClass'][]= "sambaIdmapEntry";
710       }
712     }
714     /* Add phone functionality */
715     if ($this->fon_group){
716       $this->attrs['objectClass'][]= "goFonPickupGroup";
717     }
719     /* Add nagios functionality */
720     if ($this->nagios_group){
721       $this->attrs['objectClass'][]= "nagiosContactGroup";
722     }
724     /* Take members array */
725     if (count ($this->memberUid)){
726       $this->attrs['memberUid']= array_unique($this->memberUid);
727     }
729     /* New accounts need proper 'dn', propagate it to remaining objects */
730     if ($this->dn == 'new'){
731       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
732     }
734     /* Add member dn's for RFC2307bis Support */
735     if ($this->rfc2307bis){
736       if (count($this->memberUid)){
737         $this->attrs['member'] = array();
738         foreach($this->attrs['memberUid'] as $uid) {
739           $this->attrs['member'][]= $this->dnMapping[$uid];
740         }
741       } else {
742         $this->attrs['member'][]= $this->dn;
743       }
744     }
746     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
747        new entries. So do a check first... */
748     $ldap->cat ($this->dn, array('dn'));
749     if ($ldap->fetch()){
750       /* Modify needs array() to remove values :-( */
751       if (!count ($this->memberUid)){
752         $this->attrs['memberUid']= array();
753       }
754       if ($this->samba3){
755         if (!$this->smbgroup){
756           $this->attrs['sambaGroupType']= array();
757           $this->attrs['sambaSID']= array();
758         }
759       }
760       $mode= "modify";
761     } else {
762       $mode= "add";
763       $ldap->cd($this->config->current['BASE']);
764       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
765     }
767     /* Write back to ldap */
768     $ldap->cd($this->dn);
769     $this->cleanup();
770     $ldap->$mode($this->attrs);
772     /* Remove ACL dependencies too,
773      */
774     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
775       $tmp = new acl($this->config,$this->parent,$this->dn);
776       $tmp->update_acl_membership($this->orig_dn,$this->dn);
777     }
779     if($this->initially_was_account){
780       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
781     }else{
782       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
783     }
785     $ret= 0;
786     if ( show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn))){
787       $ret= 1;
788     }
790     /* Remove uid lock */
791     del_lock ("uidnumber");
793     /* Post that we've done*/
794     $this->handle_post_events($mode);
796     return ($ret);
797   }
799   function check()
800   {
801     /* Call common method to give check the hook */
802     $message= plugin::check();
804     /* Permissions for that base? */
805     if ($this->base != ""){
806       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
807     } else {
808       $new_dn= $this->dn;
809     }
811     /* must: cn */
812     if ($this->cn == "" && $this->acl_is_writeable("cn")){
813       $message[]= "The required field 'Name' is not set.";
814     }
816     /* Check for valid input */
817     if (!is_uid($this->cn)){
818       $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
819     }
821     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
823       /* Check for used 'cn' */
824       $ldap= $this->config->get_ldap_link();
825       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
826         $ldap->cd("ou=groups,".$this->base);
827         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
828         if ($ldap->count() != 0){
829           $message[]= _("Value specified as 'Name' is already used.");
830         }
831       }
833     }else{
835       /* Check for used 'cn' */
836       $ldap= $this->config->get_ldap_link();
837       $ldap->cd($this->config->current['BASE']);
838       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
839       if ($ldap->count() != 0){
841         /* New entry? */
842         if ($this->dn == 'new'){
843           $message[]= _("Value specified as 'Name' is already used.");
844         }
846         /* Moved? */
847         elseif ($new_dn != $this->orig_dn){
848           $ldap->fetch();
849           if ($ldap->getDN() != $this->orig_dn){
850             $message[]= _("Value specified as 'Name' is already used.");
851           }
852         }
853       }
854     }
855      
856     /* Check ID */
857     if ($this->force_gid == "1"){
858       if (!is_id($this->gidNumber)){
859         $message[]= _("Value specified as 'GID' is not valid.");
860       } else {
861         if ($this->gidNumber < $this->config->current['MINID']){
862           $message[]= _("Value specified as 'GID' is too small.");
863         }
865       }
866     }
868     return ($message);
869   }
871   function get_next_id($attrib, $dn)
872   {
873     $ids= array();
874     $ldap= $this->config->get_ldap_link();
876     $ldap->cd ($this->config->current['BASE']);
877     if (preg_match('/gidNumber/i', $attrib)){
878       $oc= "posixGroup";
879     } else {
880       $oc= "posixAccount";
881     }
882     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
884     /* Get list of ids */
885     while ($attrs= $ldap->fetch()){
886       $ids[]= (int)$attrs["$attrib"][0];
887     }
889     /* Find out next free id near to UID_BASE */
890     if (!isset($this->config->current['BASE_HOOK'])){
891       $base= $this->config->current['UIDBASE'];
892     } else {
893       /* Call base hook */
894       $base= get_base_from_hook($dn, $attrib);
895     }
896     for ($id= $base; $id++; $id < pow(2,32)){
897       if (!in_array($id, $ids)){
898         return ($id);
899       }
900     }
902     /* Check if id reached maximum */
903     if ($id >= pow(2,32)){
904       print_red(_("Too many users, can't allocate a free ID!"));
905       exit;
906     }
907   }
909   function getCopyDialog()
910   {
911     $vars = array("cn");
912   
913     if($this ->force_gid){
914       $used = " checked ";
915       $dis  = "";
916     }else{
917       $used = "";
918       $dis  = " disabled ";
919     }
921     $smarty = get_smarty();
922     $smarty->assign("used",$used);
923     $smarty->assign("dis" ,$dis);
924     $smarty->assign("cn" ,$this->cn);
925     $smarty->assign("gidNumber",$this->gidNumber);
926     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
927     $ret = array();
928     $ret['string'] = $str;
929     $ret['status'] = "";
930     return($ret);
931   }
933   function saveCopyDialog()
934   {
935     if(isset($_POST['cn'])){
936       $this->cn = $_POST['cn'];
937     }
938     if(isset($_POST['force_gid'])){
939       $this->force_gid  = 1;
940       $this->gidNumber= $_POST['gidNumber'];
941     }else{
942       $this->force_gid  = 0;
943       $this->gidNumber  = false;
944     }
945   }
947   
948   /* Return plugin informations for acl handling  */ 
949   static function plInfo()
950   {
951     return (array(  
952           "plShortName" => _("Generic"),
953           "plDescription" => _("Generic group settings"),
954           "plSelfModify"  => FALSE,
955           "plDepends"     => array(),
956           "plPriority"    => 0,
957           "plSection"     => array("admin"),
958           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
960           "plProvidedAcls"    => array(
961             "cn"                => _("Name"),
962             "base"              => _("Base"),
963             "description"       => _("Description"),
965             "fonGroup"          => _("Phone pickup group"),
966             "nagiosGroup"       => _("Nagios group"),
968             "gidNumber"         => _("GID"),
969             "memberUid"         => _("Group member"),
970             "sambaGroupType"    => _("Samba group type"),
971             "sambaDomainName"   => _("Samba domain name"),
972             "sambaSID"          => _("Samba SID"))
973         ));
974   }
977 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
978 ?>