Code

Update Copy & Paste.-
[gosa.git] / gosa-core / plugins / admin / groups / class_groupGeneric.inc
1 <?php
3 class group extends plugin
4 {
5   /* Group attributes */
6   var $cn= "";
7   var $description= "";
8   var $gidNumber= "";
9   var $memberUid= array();
10   var $memberUid_used_by_some= array();
12   /* Helpers */
13   var $base= "";
14   var $force_gid= FALSE;
15   var $fon_group= FALSE;
16   var $smbgroup= FALSE;
17   var $groupType= FALSE;
18   var $samba3= FALSE;
19   var $sambaSID= "";
20   var $sambaDomainName= "DEFAULT";
21   var $SID= "";
22   var $ridBase= 0;
23   var $members= array();
24   var $users= array();
25   var $member= array();
26   var $allusers= array();
27   var $saved_gidNumber= "";
28   var $oldgroupType= "";
29   var $orig_dn= "";
30   var $orig_cn= "";
31   var $has_mailAccount= FALSE;
32   var $group_dialog= FALSE;
33   var $nagios_group =FALSE;
34   var $sambaGroupType;
35   var $dialog;
36   var $rfc2307bis= FALSE;
37   var $OnlyShowFirstEntries =200;
38   var $dnMapping= array();
39   var $view_logged = FALSE;
40   var $allowGroupsWithSameNameInOtherSubtrees = true;
42   /* attribute list for save action */
43   var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID");
44   var $objectclasses= array("top", "posixGroup");
46   var $CopyPasteVars  = array("force_gid","fon_group","smbgroup","groupType","sambaSID","sambaDomainName","SID","nagios_group","sambaGroupType");
48   var $multiple_support = TRUE;
50   function group (&$config, $dn= NULL)
51   {
52      /* Set rfc2307bis flag */
53      if (isset($config->current['RFC2307BIS']) && ($config->current['RFC2307BIS']== "true")){
54        $this->rfc2307bis= TRUE;
55        $this->attributes[]= "member";
56        $this->objectclasses[]= "groupOfNames";
57      }
59     plugin::plugin ($config, $dn);
61     /* Load attributes depending on the samba version */
62     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
63     $this->orig_dn= $dn;
64     $this->orig_cn= $this->cn;
66     /* Get member list */
67     if (isset($this->attrs['memberUid'][0])){
68       $tmp= array();
69       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
70         $tmp[$this->attrs['memberUid'][$i]]= $this->attrs['memberUid'][$i];
71       }
72       $this->memberUid= $tmp;
73       ksort ($this->memberUid);
74     }
76     /* Save gidNumber for later use */
77     if (isset($this->attrs['gidNumber'])){
78       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
79     }
81     /* Is a samba group? */
82     if (isset($this->attrs['objectClass'])){
83       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == FALSE ){
84         $this->smbgroup= FALSE;
85       } else {
86         $this->smbgroup= TRUE;
87         if (isset($this->attrs['sambaSID'])){
88           $this->sambaSID= $this->attrs['sambaSID'][0];
89         }
90       }
91       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == FALSE ){
92         $this->fon_group= FALSE;
93       } else {
94         $this->fon_group= TRUE;
95       }
96       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
97         $this->nagios_group= FALSE;
98       } else {
99         $this->nagios_group= TRUE;
100       }
101     }
103     /* Set mail flag */
104     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
105       $this->has_mailAccount= TRUE;
106     }
108     /* Get samba Domain in case of samba 3 */
109     if ($this->samba3 && $this->sambaSID != ""){
110       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
111       $ldap= $this->config->get_ldap_link();
112       $ldap->cd($this->config->current['BASE']);
113       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
114       if ($ldap->count() != 0){
115         $attrs= $ldap->fetch();
116         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
118         /* Get domain name for SID */
119         $this->sambaDomainName= "DEFAULT";
120         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
121           if ($val['SID'] == $this->SID){
122             $this->sambaDomainName= $key;
123             break;
124           }
125         }
126       } else {
127         if (isset($this->config->current['RIDBASE'])){
128           $this->sambaDomainName= "DEFAULT";
129           $this->ridBase= $this->config->current['RIDBASE'];
130           $this->SID= $this->config->current['SID'];
131         } else {
132           msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
133         }
134       }
136       /* Get group type */
137       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
138       if ($this->groupType < 500 || $this->groupType > 553){
139         $this->groupType= 0;
140       }
141       $this->oldgroupType= $this->groupType;
142     }
144     /* Get global filter config */
145     if (!session::is_set("gufilter")){
146       $ui= get_userinfo();
147       $base= get_base_from_people($ui->dn);
148       $gufilter= array( "dselect"       => $base,
149           "regex"           => "*");
150       session::set("gufilter", $gufilter);
151     }
152     $gufilter= session::get('gufilter');
153     $gufilter['SubSearchGroup'] = false;
154     session::set('gufilter',$gufilter);
155   
156     if ($this->dn == "new"){
157       if(session::is_set('CurrentMainBase')){
158         $this->base = session::get('CurrentMainBase');
159       }else{
160         $ui= get_userinfo();
161         $this->base= dn2base($ui->dn);
162       }
163     } else {
165       /* Get object base */
166       $this->base =preg_replace ("/^[^,]+,".normalizePreg(get_groups_ou())."/","",$this->dn);
167     }
169     /* This is always an account */
170     $this->is_account= TRUE;
171     $this->reload();
172   }
174   function execute()
175   {
176     /* Call parent execute */
177     plugin::execute();
179     /* Log view */
180     if($this->is_account && !$this->view_logged){
181       $this->view_logged = TRUE;
182       new log("view","groups/".get_class($this),$this->dn);
183     }
185     /* Do we represent a valid group? */
186     if (!$this->is_account && $this->parent === NULL){
187       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
188         _("This 'dn' is no group.")."</b>";
189       return ($display);
190     }
192     /* Delete user from group */
193     if (isset($_POST['del_users']) && isset($_POST['members'])){
194       foreach ($_POST['members'] as $value){
195         unset ($this->members["$value"]);
196         $this->removeUser($value);
197       }
198       $this->reload();
199     }
201     /* Add objects? */
202     if (isset($_POST["edit_membership"])){
203       $this->group_dialog= TRUE;
204       $this->dialog= TRUE;
205     }
207     /* Add objects finished? */
208     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
209       $this->group_dialog= FALSE;
210       $this->dialog= FALSE;
211     }
213     /* Add user to group */
214     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
215       foreach ($_POST['users'] as $value){
216         $this->members["$value"]= $this->allusers[$value];
217         asort($this->members);
218         $this->addUser($value);
219       }
220       $this->reload();
221     }
223     /* Base select dialog */
224     $once = true;
225     foreach($_POST as $name => $value){
226       if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
227           
228         $once = false;
229         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
230         $this->dialog->setCurrentBase($this->base);
231       }
232     }
234     /* Dialog handling */
235     if(is_object($this->dialog)){
236       /* Must be called before save_object */
237       $this->dialog->save_object();
239       if($this->dialog->isClosed()){
240         $this->dialog = false;
241       }elseif($this->dialog->isSelected()){
243         /* Check if selected base is valid */
244         $tmp = $this->get_allowed_bases();
245         if(isset($tmp[$this->dialog->isSelected()])){
246           $this->base = $this->dialog->isSelected();
247         }
248         $this->dialog= false;
249       }else{
250         return($this->dialog->execute());
251       }
252     }
254    /* Assign templating stuff */
255     $smarty= get_smarty();
256     if ($this->samba3){
257       $smarty->assign("samba3", "true");
258     } else {
259       $smarty->assign("samba3", "");
260     }
262     if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
263       $smarty->assign("nagios",true);
264     }else{
265       $smarty->assign("nagios",false);
266     }
267     
268     if($this->config->search("phoneAccount", "CLASS",array('menu'))){
269       $smarty->assign("pickupGroup",true);
270     }else{
271       $smarty->assign("pickupGroup",false);
272     }
274     /* Manage object add dialog */
275     if ($this->group_dialog){
277       /* Save data */
278       $gufilter= session::get("gufilter");
279       foreach( array("dselect", "regex") as $type){
280         if (isset($_POST[$type])){
281           $gufilter[$type]= $_POST[$type];
282         }
283       }
284       if(isset($_POST['regex'])){
285         if(isset($_POST['SubSearchGroup'])){
286           $gufilter['SubSearchGroup'] = true;
287         }else{
288           $gufilter['SubSearchGroup'] = false;
289         }
290       }
292       if (isset($_GET['search'])){
293         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
294         if ($s == "**"){
295           $s= "*";
296         }
297         $gufilter['regex']= $s;
298       }
299       session::set("gufilter", $gufilter);
300       $this->reload();
302       /* Show dialog */
303       $smarty->assign("search_image", get_template_path('images/search.png'));
304       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
305       $smarty->assign("tree_image", get_template_path('images/tree.png'));
307       $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
308       $smarty->assign("alphabet", generate_alphabet());
309       foreach( array("dselect", "regex","SubSearchGroup") as $type){
310         $smarty->assign("$type", $gufilter[$type]);
311       }
312       $smarty->assign("hint", print_sizelimit_warning());
313       $smarty->assign("users", $this->displayUsers);
314       $smarty->assign("apply", apply_filter());
315       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
316       return ($display);
317     }
319     $smarty->assign("bases", $this->get_allowed_bases());
320     $smarty->assign("base_select", $this->base);
322     if ($this->samba3){
323       $domains= array();
324       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
325         $domains[$name]= $name;
326       }
327       $smarty->assign("sambaDomains", $domains);
328       $smarty->assign("sambaDomainName", $this->sambaDomainName);
329       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
330           514 => _("Domain guests"));
332       /* Don't loose special groups! If not key'ed above, add it to
333          the combo box... */    
334       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
335         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
336       }
338       $smarty->assign("groupTypes", $groupTypes);
339       $smarty->assign("groupType", $this->groupType);
340     }
342     /* Members and users */
343     $smarty->assign("members", $this->members);
345     /* Checkboxes */
346     foreach (array("force_gid", "smbgroup") as $val){
347       if ($this->$val == "1"){
348         $smarty->assign("$val", "checked");
349       } else {
350         $smarty->assign("$val", "");
351       }
352     }
353     if ($this->force_gid != "1"){
354       $smarty->assign("forceMode", "disabled");
355     }else{
356       $smarty->assign("forceMode", "");
357     }
358     if ($this->fon_group){
359       $smarty->assign("fon_group", "checked");
360     } else {
361       $smarty->assign("fon_group", "");
362     }
364     if ($this->nagios_group){
365       $smarty->assign("nagios_group", "checked");
366     } else {
367       $smarty->assign("nagios_group", "");
368     }
370     /* Fields */
371     foreach (array("cn", "description", "gidNumber") as $val){
372       $smarty->assign("$val", $this->$val);
373     }
375     $tmp = $this->plInfo();
376     foreach($tmp['plProvidedAcls'] as $name => $translation){
377       $smarty->assign($name."ACL",$this->getacl($name));
378     }
379     
380     if($this->acl_is_writeable("base")){
381       $smarty->assign("baseSelect",true);
382     }else{
383       $smarty->assign("baseSelect",false);
384     }
386     /* Show main page */
387     $smarty->assign("alphabet", generate_alphabet(10));
388     $smarty->assign("search_image", get_template_path('images/search.png'));
389     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
390     $smarty->assign("tree_image", get_template_path('images/tree.png'));
391     $smarty->assign("deplist", $this->config->idepartments);
393     /* Multiple edit handling */
394     $smarty->assign("multiple_support",$this->multiple_support_active);
396     $smarty->assign("memberUid_All",$this->memberUid);
397     $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
399     foreach($this->attributes as $val){
400       if(in_array($val,$this->multi_boxes)){
401         $smarty->assign("use_".$val,TRUE);
402       }else{
403         $smarty->assign("use_".$val,FALSE);
404       }
405     }
406     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $val){
407       if(in_array($val,$this->multi_boxes)){
408         $smarty->assign("use_".$val,TRUE);
409       }else{
410         $smarty->assign("use_".$val,FALSE);
411       }
412     }
414     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
415   }
417   function addUser($uid)
418   {
419     /* In mutliple edit we have to handle two arrays.
420      *  memberUid               : Containing users used in all groups
421      *  memberUid_used_by_some  : Those which are not used in all groups
422      * So we have to remove the given $uid from the ..used_by_some array first.
423      */
424     if($this->multiple_support_active){
425       if(isset($this->memberUid_used_by_some[$uid])){
426         unset($this->memberUid_used_by_some[$uid]);
427       }
428     }  
430     $this->memberUid[$uid]= $uid;
431   }
434   function removeUser($uid)
435   {
436     $temp= array();
437     if(isset($this->memberUid[$uid])){
438       unset($this->memberUid[$uid]);
439     }
441     /* We have two array contianing group members in multiple edit.
442      *  this->memberUid             : Groups used by all currently edited groups 
443      *  this->memberUid_used_by_some: Used by some 
444      * So we have to remove the specified uid from both arrays.
445      */
446     if($this->multiple_support_active){
447       if(isset($this->memberUid_used_by_some[$uid])){
448         unset($this->memberUid_used_by_some[$uid]);
449       }
450     }
451   }
453   /* Reload data */
454   function reload()
455   {
456     /* Fix regex string */
457     $gufilter = session::get("gufilter");
458     $regex    = normalizeLdap($gufilter['regex']);
459     $MaxUser  = $this->OnlyShowFirstEntries;
461     /* Prepare ldap link */
462     $ldap= $this->config->get_ldap_link();
463     $ldap->cd($gufilter['dselect']);
466     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
467         (Store gathered sn/givenName informations in $this->allusers too, 
468          to be prepared when adding/deleting users)
469      */    
470     $filter = "";
471     foreach ($this->memberUid as $value){
472       if(!isset($this->members[$value])){
473         $filter .= "(uid=".normalizeLdap($value).")";
474       }
475     }
476     if(!empty($filter)){    
477       $ldap->cd($this->config->current['BASE']);
478       $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
479       while($attrs = $ldap->fetch()){
480         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
481         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
482         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
483       } 
484     }
485   
486     /* check if all uids are resolved */
487     foreach ($this->memberUid as $value){
488       if(!isset($this->members[$value])){
489         $this->members[$value] =  _("! unknown id")." [".$value."]"; 
490       }
491     }  
493     /* Create display list of users matching regex & filter 
494      */
495     $this->displayUsers = array();
496     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
498     /* Search in current tree or within subtrees depending on the checkbox from filter section */
499     if($gufilter['SubSearchGroup']){
500       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
501       $base = $gufilter['dselect'];
502     }else{
503       $flag = GL_SIZELIMIT ;
504       $base = get_people_ou().$gufilter['dselect'];
505     }
506     $i = 0;
507   
509     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
511     /* Fetch all users and skip already used users */
512     foreach($res as $attrs){
513       if(in_array($attrs['uid'][0], $this->memberUid)) {
514         continue;
515       }
516       $i ++;
517       if($i > $MaxUser) {
518         break;
519       }
520       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
521       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
522       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
523     }
524   
525     /* If more than max users are found, display a message to warn the user */
526     if($i == $MaxUser){
527       msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries."), $MaxUser), ERROR_DIALOG);
528     }
529     
530     /* Sort lists */
531     natcasesort($this->members);
532     reset($this->members);
533     natcasesort ($this->displayUsers);
534     reset ($this->displayUsers);
535   }
538   /* Create display name, this was used so often that it is excluded into a seperate function */
539   function createResultName($attrs)
540   {
541     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
542       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
543     } else {
544       $ret= $attrs['uid'][0];
545     }
546     return($ret);
547   }
550   function remove_from_parent()
551   {
552     plugin::remove_from_parent();
554     $ldap= $this->config->get_ldap_link();
555     $ldap->rmdir($this->dn);
556     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn));
558     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
560     /* Delete references to object groups */
561     $ldap->cd ($this->config->current['BASE']);
562     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
563     while ($ldap->fetch()){
564       $og= new ogroup($this->config, $ldap->getDN());
565       unset($og->member[$this->dn]);
566       $og->save ();
567     }
569     /* Remove ACL dependencies too,
570      */
571     $ldap = $this->config->get_ldap_link();
572     $ldap->cd($this->config->current['BASE']);
573     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
574     while($attrs = $ldap->fetch()){
575       $acl = new acl($this->config,$this->parent,$attrs['dn']);
576       foreach($acl->gosaAclEntry as $id => $entry){
577         foreach($entry['members'] as $m_id => $member){
578           if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
579             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
580             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
581           }
582         }
583       }
584       $acl -> save();
585     }
587     /* Remove ACL dependencies too,
588      */
589     $tmp = new acl($this->config,$this->parent,$this->dn);
590     $tmp->remove_acl();
592     /* Send signal to the world that we've done */
593     $this->handle_post_events("remove");
594   }
597   /* Save data to object */
598   function save_object()
599   {
600     /* Save additional values for possible next step */
601     if (isset($_POST['groupedit'])){
603       /* Create a base backup and reset the 
604           base directly after calling plugin::save_object();  
605          Base will be set seperatly a few lines below */
606       $base_tmp = $this->base;
607       plugin::save_object();
608       $this->base = $base_tmp;
610       $this->force_gid= 0;
612       /* Only reset sambagroup flag if we are able to write this flag */
613       if($this->acl_is_writeable("sambaGroupType")){
614         $this->smbgroup = 0;
615       }
617       /* Get base selection */
618       if(isset($_POST['base'])){
619         $tmp = $this->get_allowed_bases();
620         if(isset($tmp[$_POST['base']])){
621           $this->base = $_POST['base'];
622         }
623       }
625       foreach (array(
626             "force_gid"  => "gidNumber", 
627             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
628         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
629           $this->$val= $_POST["$val"];
630         }
631       }
633       /* Save sambaDomain attribute */
634       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
635         $this->sambaDomainName= $_POST['sambaDomainName'];
636         $this->groupType= $_POST['groupType'];
637       }
639       /* Save fon attribute */
640       if ($this->acl_is_writeable("fon_group")){
641         if (isset ($_POST['fon_group'])){
642           $this->fon_group= TRUE;
643         } else {
644           $this->fon_group= FALSE;
645         }
646       }
647       if ($this->acl_is_writeable("nagios_group")){
648         if (isset ($_POST['nagios_group'])){
649           $this->nagios_group= TRUE;
650         } else {
651           $this->nagios_group= FALSE;
652         }
653       }
654     }
655   }
658   /* Save to LDAP */
659   function save()
660   {
662     /* ID handling */
663     if ($this->force_gid == 0){
664       if ($this->saved_gidNumber != ""){
665         $this->gidNumber= $this->saved_gidNumber;
666       } else {
667         /* Calculate new, lock uids */
668         $wait= 10;
669         while (get_lock("uidnumber") != ""){
670           sleep (1);
672           /* timed out? */
673           if ($wait-- == 0){
674             break;
675           }
676         }
677         add_lock ("uidnumber", "gosa");
678         $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
679       }
680     }
681   
682     plugin::save(); 
684     /* Remove objectClass for samba/phone support */
685     $tmp= array();
686     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
687       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
688           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
689           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
690          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
691         $tmp[]= $this->attrs['objectClass'][$i];
692       }
693     }
694     $this->attrs['objectClass']= $tmp;
695     $ldap= $this->config->get_ldap_link();
697     /* Add samba group functionality */
698     if ($this->samba3 && $this->smbgroup){
699   
700       /* Fixed undefined index ... 
701        */ 
702       $this->SID = $this->ridBase = "";
703       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
704         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
705       }else{
706         msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
707       }
708       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
709         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
710       }else{
711         msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
712       }
714       $this->attrs['objectClass'][]= 'sambaGroupMapping';
715       $this->attrs['sambaGroupType']= "2";
717       /* Check if we need to create a special entry */
718       if ($this->groupType == 0){
720         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
721           $gidNumber= $this->gidNumber;
722           while(TRUE){
723             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
724             $ldap->cd($this->config->current['BASE']);
725             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
726             if ($ldap->count() == 0){
727               break;
728             }
729             $gidNumber++;
730           }
731           $this->attrs['sambaSID']= $sid;
732           $this->sambaSID= $sid;
733         }
735       } else {
736         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
737       }
739       /* User wants me to fake the idMappings? This is useful for
740          making winbind resolve the group names in a reasonable amount
741          of time in combination with larger databases. */
742       if (isset($this->config->current['SAMBAIDMAPPING']) &&
743           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
744         $this->attrs['objectClass'][]= "sambaIdmapEntry";
745       }
747     }
749     /* Add phone functionality */
750     if ($this->fon_group){
751       $this->attrs['objectClass'][]= "goFonPickupGroup";
752     }
754     /* Add nagios functionality */
755     if ($this->nagios_group){
756       $this->attrs['objectClass'][]= "nagiosContactGroup";
757     }
759     /* Take members array */
760     if (count ($this->memberUid)){
761       $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
762     }
764     /* New accounts need proper 'dn', propagate it to remaining objects */
765     if ($this->dn == 'new'){
766       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
767     }
769     /* Add member dn's for RFC2307bis Support */
770     if ($this->rfc2307bis){
771       if (count($this->memberUid)){
772         $this->attrs['member'] = array();
773         foreach($this->attrs['memberUid'] as $uid) {
774           $this->attrs['member'][]= $this->dnMapping[$uid];
775         }
776       } else {
777         $this->attrs['member'][]= $this->dn;
778       }
779     }
781     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
782        new entries. So do a check first... */
783     $ldap->cat ($this->dn, array('dn'));
784     if ($ldap->fetch()){
785       /* Modify needs array() to remove values :-( */
786       if (!count ($this->memberUid)){
787         $this->attrs['memberUid']= array();
788       }
789       if ($this->samba3){
790         if (!$this->smbgroup){
791           $this->attrs['sambaGroupType']= array();
792           $this->attrs['sambaSID']= array();
793         }
794       }
795       $mode= "modify";
796     } else {
797       $mode= "add";
798       $ldap->cd($this->config->current['BASE']);
799       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
800     }
802     /* Write back to ldap */
803     $ldap->cd($this->dn);
804     $this->cleanup();
805     $ldap->$mode($this->attrs);
807     /* Remove ACL dependencies too,
808      */
809     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
810       $tmp = new acl($this->config,$this->parent,$this->dn);
811       $tmp->update_acl_membership($this->orig_dn,$this->dn);
812     }
814     if($this->initially_was_account){
815       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
816     }else{
817       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
818     }
820     $ret= 0;
821     if ( show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/generic with dn '%s' failed."),$this->dn))){
822       $ret= 1;
823     }
825     /* Remove uid lock */
826     del_lock ("uidnumber");
828     /* Post that we've done*/
829     $this->handle_post_events($mode);
831     return ($ret);
832   }
834   function check()
835   {
836     /* Call common method to give check the hook */
837     $message= plugin::check();
839     /* Permissions for that base? */
840     if ($this->base != ""){
841       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
842     } else {
843       $new_dn= $this->dn;
844     }
846     /* must: cn */
847     if ($this->cn == "" && $this->acl_is_writeable("cn")){
848       $message[]= "The required field 'Name' is not set.";
849     }
851     /* Check for valid input */
852     if (!tests::is_uid($this->cn)){
853       $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
854     }
856     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
858       /* Check for used 'cn' */
859       $ldap= $this->config->get_ldap_link();
860       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
861         $ldap->cd(get_groups_ou().$this->base);
862         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
863         if ($ldap->count() != 0){
864           $message[]= _("Value specified as 'Name' is already used.");
865         }
866       }
868     }else{
870       /* Check for used 'cn' */
871       $ldap= $this->config->get_ldap_link();
872       $ldap->cd($this->config->current['BASE']);
873       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
874       if ($ldap->count() != 0){
876         /* New entry? */
877         if ($this->dn == 'new'){
878           $message[]= _("Value specified as 'Name' is already used.");
879         }
881         /* Moved? */
882         elseif ($new_dn != $this->orig_dn){
883           $ldap->fetch();
884           if ($ldap->getDN() != $this->orig_dn){
885             $message[]= _("Value specified as 'Name' is already used.");
886           }
887         }
888       }
889     }
890      
891     /* Check ID */
892     if ($this->force_gid == "1"){
893       if (!tests::is_id($this->gidNumber)){
894         $message[]= _("Value specified as 'GID' is not valid.");
895       } else {
896         if ($this->gidNumber < $this->config->current['MINID']){
897           $message[]= _("Value specified as 'GID' is too small.");
898         }
900       }
901     }
903     return ($message);
904   }
906   function get_next_id($attrib, $dn)
907   {
908     $ids= array();
909     $ldap= $this->config->get_ldap_link();
911     $ldap->cd ($this->config->current['BASE']);
912     if (preg_match('/gidNumber/i', $attrib)){
913       $oc= "posixGroup";
914     } else {
915       $oc= "posixAccount";
916     }
917     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
919     /* Get list of ids */
920     while ($attrs= $ldap->fetch()){
921       $ids[]= (int)$attrs["$attrib"][0];
922     }
924     /* Find out next free id near to UID_BASE */
925     if (!isset($this->config->current['BASE_HOOK'])){
926       $base= $this->config->current['UIDBASE'];
927     } else {
928       /* Call base hook */
929       $base= get_base_from_hook($dn, $attrib);
930     }
931     for ($id= $base; $id++; $id < pow(2,32)){
932       if (!in_array($id, $ids)){
933         return ($id);
934       }
935     }
937     /* Check if id reached maximum */
938     if ($id >= pow(2,32)){
939       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
940       exit;
941     }
942   }
944   function getCopyDialog()
945   {
946     $vars = array("cn");
947   
948     if($this ->force_gid){
949       $used = " checked ";
950       $dis  = "";
951     }else{
952       $used = "";
953       $dis  = " disabled ";
954     }
956     $smarty = get_smarty();
957     $smarty->assign("used",$used);
958     $smarty->assign("dis" ,$dis);
959     $smarty->assign("cn" ,$this->cn);
960     $smarty->assign("gidNumber",$this->gidNumber);
961     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
962     $ret = array();
963     $ret['string'] = $str;
964     $ret['status'] = "";
965     return($ret);
966   }
968   function saveCopyDialog()
969   {
970     if(isset($_POST['cn'])){
971       $this->cn = $_POST['cn'];
972     }
973     if(isset($_POST['force_gid'])){
974       $this->force_gid  = 1;
975       $this->gidNumber= $_POST['gidNumber'];
976     }else{
977       $this->force_gid  = 0;
978       $this->gidNumber  = false;
979     }
980   }
982   
983   /* Return plugin informations for acl handling  */ 
984   static function plInfo()
985   {
986     return (array(  
987           "plShortName" => _("Generic"),
988           "plDescription" => _("Generic group settings"),
989           "plSelfModify"  => FALSE,
990           "plDepends"     => array(),
991           "plPriority"    => 0,
992           "plSection"     => array("admin"),
993           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
995           "plProvidedAcls"    => array(
996             "cn"                => _("Name"),
997             "base"              => _("Base"),
998             "description"       => _("Description"),
1000             "fonGroup"          => _("Phone pickup group"),
1001             "nagiosGroup"       => _("Nagios group"),
1003             "gidNumber"         => _("GID"),
1004             "memberUid"         => _("Group member"),
1005             "sambaGroupType"    => _("Samba group type"),
1006             "sambaDomainName"   => _("Samba domain name"),
1007             "sambaSID"          => _("Samba SID"))
1008         ));
1009   }
1012   function multiple_save_object()
1013   {
1014     if(isset($_POST['group_mulitple_edit'])){
1016       /* Create a base backup and reset the
1017          base directly after calling plugin::save_object();
1018          Base will be set seperatly a few lines below */
1019       $base_tmp = $this->base;
1020       plugin::multiple_save_object();
1021       plugin::save_object();
1022       $this->base = $base_tmp;
1024       foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1025         if(isset($_POST['use_'.$attr])){
1026           $this->multi_boxes[] = $attr;
1027         }
1028       }
1030       /* Get base selection */
1031       if(isset($_POST['base'])){
1032         $tmp = $this->get_allowed_bases();
1033         if(isset($tmp[$_POST['base']])){
1034           $this->base = $_POST['base'];
1035         }
1036       }
1038       foreach (array( "smbgroup"   => "sambaGroupType") as $val => $aclname) {
1039         if ($this->acl_is_writeable($aclname)){
1040           if(isset($_POST["$val"])){
1041             $this->$val=  TRUE;
1042           }else{
1043             $this->$val=  FALSE;
1044           }
1045         }
1046       }
1048       /* Save sambaDomain attribute */
1049       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
1050         $this->sambaDomainName= $_POST['sambaDomainName'];
1051         $this->groupType= $_POST['groupType'];
1052       }
1054       /* Save fon attribute */
1055       if ($this->acl_is_writeable("fon_group")){
1056         if (isset ($_POST['fon_group'])){
1057           $this->fon_group= TRUE;
1058         } else {
1059           $this->fon_group= FALSE;
1060         }
1061       }
1062     }
1063   }
1066   function get_multi_edit_values()
1067   {
1068     $ret = plugin::get_multi_edit_values();
1070     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){    
1071       if(in_array($attr,$this->multi_boxes)){
1072         $ret[$attr] = $this->$attr;
1073       }
1074     }
1075     $ret['memberUid'] = $this->memberUid;
1076     $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1077     return($ret);
1078   }
1080   function multiple_execute()
1081   {
1082     return($this->execute());
1083   }
1086   /* Initialize plugin with given atribute arrays
1087    */
1088   function init_multiple_support($attrs,$all)
1089   {
1090     plugin::init_multiple_support($attrs,$all);
1092     $this->memberUid = array();
1093     $this->memberUid_used_by_some = array();
1094     if (isset($attrs['memberUid'])){
1095       for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1096         $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1097       }
1098       ksort($this->memberUid);
1099     }
1101     if (isset($all['memberUid'])){
1102       for ($i= 0; $i<$all['memberUid']['count']; $i++){
1103         if(!in_array($all['memberUid'][$i],$this->memberUid)){
1104           $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1105         }
1106       }
1107       ksort($this->memberUid_used_by_some);
1108     }
1109   }
1111   function set_multi_edit_values($attrs)
1112   {
1113     $users = array();
1115     /* Update groupMembership, keep optinal group */
1116     foreach($attrs['memberUid_used_by_some'] as $uid){
1117       if(in_array($uid,$this->memberUid)){
1118         $users[$uid] = $uid;
1119       }
1120     }
1122     /* Update groupMembership, add forced groups */
1123     foreach($attrs['memberUid'] as $uid){
1124       $users[$uid] = $uid;
1125     }
1126     plugin::set_multi_edit_values($attrs);
1127     $this->memberUid = $users;
1128   }
1133 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1134 ?>