Code

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