Code

Language cleanup
[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>".msgPool::noValidExtension()."</b>";
208       return ($display);
209     }
211     /* Delete user from group */
212     if (isset($_POST['del_users']) && isset($_POST['members'])){
213       foreach ($_POST['members'] as $value){
214         unset ($this->members["$value"]);
215         $this->removeUser($value);
216       }
217       $this->reload();
218     }
220     /* Add objects? */
221     if (isset($_POST["edit_membership"])){
222       $this->group_dialog= TRUE;
223       $this->dialog= TRUE;
224     }
226     /* Add objects finished? */
227     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
228       $this->group_dialog= FALSE;
229       $this->dialog= FALSE;
230     }
232     /* Add user to group */
233     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
234       foreach ($_POST['users'] as $value){
235         $this->members["$value"]= $this->allusers[$value];
236         asort($this->members);
237         $this->addUser($value);
238       }
239       $this->reload();
240     }
242     /* Base select dialog */
243     $once = true;
244     foreach($_POST as $name => $value){
245       if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
246           
247         $once = false;
248         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
249         $this->dialog->setCurrentBase($this->base);
250       }
251     }
253     /* Dialog handling */
254     if(is_object($this->dialog)){
255       /* Must be called before save_object */
256       $this->dialog->save_object();
258       if($this->dialog->isClosed()){
259         $this->dialog = false;
260       }elseif($this->dialog->isSelected()){
262         /* Check if selected base is valid */
263         $tmp = $this->get_allowed_bases();
264         if(isset($tmp[$this->dialog->isSelected()])){
265           $this->base = $this->dialog->isSelected();
266         }
267         $this->dialog= false;
268       }else{
269         return($this->dialog->execute());
270       }
271     }
273    /* Assign templating stuff */
274     $smarty= get_smarty();
275     if ($this->samba3){
276       $smarty->assign("samba3", "true");
277     } else {
278       $smarty->assign("samba3", "");
279     }
281     if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
282       $smarty->assign("nagios",true);
283     }else{
284       $smarty->assign("nagios",false);
285     }
286     
287     if($this->config->search("phoneAccount", "CLASS",array('menu'))){
288       $smarty->assign("pickupGroup",true);
289     }else{
290       $smarty->assign("pickupGroup",false);
291     }
293     /* Manage object add dialog */
294     if ($this->group_dialog){
296       /* Save data */
297       $gufilter= session::get("gufilter");
298       foreach( array("dselect", "regex") as $type){
299         if (isset($_POST[$type])){
300           $gufilter[$type]= $_POST[$type];
301         }
302       }
303       if(isset($_POST['regex'])){
304         if(isset($_POST['SubSearchGroup'])){
305           $gufilter['SubSearchGroup'] = true;
306         }else{
307           $gufilter['SubSearchGroup'] = false;
308         }
309       }
311       if (isset($_GET['search'])){
312         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
313         if ($s == "**"){
314           $s= "*";
315         }
316         $gufilter['regex']= $s;
317       }
318       session::set("gufilter", $gufilter);
319       $this->reload();
321       /* Show dialog */
322       $smarty->assign("search_image", get_template_path('images/search.png'));
323       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
324       $smarty->assign("tree_image", get_template_path('images/tree.png'));
326       $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
327       $smarty->assign("alphabet", generate_alphabet());
328       foreach( array("dselect", "regex","SubSearchGroup") as $type){
329         $smarty->assign("$type", $gufilter[$type]);
330       }
331       $smarty->assign("hint", print_sizelimit_warning());
332       $smarty->assign("users", $this->displayUsers);
333       $smarty->assign("apply", apply_filter());
334       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
335       return ($display);
336     }
338     $smarty->assign("bases", $this->get_allowed_bases());
339     $smarty->assign("base_select", $this->base);
341     if ($this->samba3){
342       $domains= array();
343       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
344         $domains[$name]= $name;
345       }
346       $smarty->assign("sambaDomains", $domains);
347       $smarty->assign("sambaDomainName", $this->sambaDomainName);
348       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
349           514 => _("Domain guests"));
351       /* Don't loose special groups! If not key'ed above, add it to
352          the combo box... */    
353       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
354         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
355       }
357       $smarty->assign("groupTypes", $groupTypes);
358       $smarty->assign("groupType", $this->groupType);
359     }
361     /* Members and users */
362     $smarty->assign("members", $this->members);
364     /* Checkboxes */
365     foreach (array("force_gid", "smbgroup") as $val){
366       if ($this->$val == "1"){
367         $smarty->assign("$val", "checked");
368       } else {
369         $smarty->assign("$val", "");
370       }
371     }
372     if ($this->force_gid != "1"){
373       $smarty->assign("forceMode", "disabled");
374     }else{
375       $smarty->assign("forceMode", "");
376     }
377     if ($this->fon_group){
378       $smarty->assign("fon_group", "checked");
379     } else {
380       $smarty->assign("fon_group", "");
381     }
383     if ($this->nagios_group){
384       $smarty->assign("nagios_group", "checked");
385     } else {
386       $smarty->assign("nagios_group", "");
387     }
389     /* Fields */
390     foreach (array("cn", "description", "gidNumber") as $val){
391       $smarty->assign("$val", $this->$val);
392     }
394     $tmp = $this->plInfo();
395     foreach($tmp['plProvidedAcls'] as $name => $translation){
396       $smarty->assign($name."ACL",$this->getacl($name));
397     }
398     
399     if($this->acl_is_writeable("base")){
400       $smarty->assign("baseSelect",true);
401     }else{
402       $smarty->assign("baseSelect",false);
403     }
405     /* Show main page */
406     $smarty->assign("alphabet", generate_alphabet(10));
407     $smarty->assign("search_image", get_template_path('images/search.png'));
408     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
409     $smarty->assign("tree_image", get_template_path('images/tree.png'));
410     $smarty->assign("deplist", $this->config->idepartments);
412     /* Multiple edit handling */
413     $smarty->assign("multiple_support",$this->multiple_support_active);
415     $smarty->assign("memberUid_All",$this->memberUid);
416     $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
418     foreach($this->attributes as $val){
419       if(in_array($val,$this->multi_boxes)){
420         $smarty->assign("use_".$val,TRUE);
421       }else{
422         $smarty->assign("use_".$val,FALSE);
423       }
424     }
425     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $val){
426       if(in_array($val,$this->multi_boxes)){
427         $smarty->assign("use_".$val,TRUE);
428       }else{
429         $smarty->assign("use_".$val,FALSE);
430       }
431     }
433     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
434   }
436   function addUser($uid)
437   {
438     /* In mutliple edit we have to handle two arrays.
439      *  memberUid               : Containing users used in all groups
440      *  memberUid_used_by_some  : Those which are not used in all groups
441      * So we have to remove the given $uid from the ..used_by_some array first.
442      */
443     if($this->multiple_support_active){
444       if(isset($this->memberUid_used_by_some[$uid])){
445         unset($this->memberUid_used_by_some[$uid]);
446       }
447     }  
449     $this->memberUid[$uid]= $uid;
450   }
453   function removeUser($uid)
454   {
455     $temp= array();
456     if(isset($this->memberUid[$uid])){
457       unset($this->memberUid[$uid]);
458     }
460     /* We have two array contianing group members in multiple edit.
461      *  this->memberUid             : Groups used by all currently edited groups 
462      *  this->memberUid_used_by_some: Used by some 
463      * So we have to remove the specified uid from both arrays.
464      */
465     if($this->multiple_support_active){
466       if(isset($this->memberUid_used_by_some[$uid])){
467         unset($this->memberUid_used_by_some[$uid]);
468       }
469     }
470   }
472   /* Reload data */
473   function reload()
474   {
475     /* Fix regex string */
476     $gufilter = session::get("gufilter");
477     $regex    = normalizeLdap($gufilter['regex']);
478     $MaxUser  = $this->OnlyShowFirstEntries;
480     /* Prepare ldap link */
481     $ldap= $this->config->get_ldap_link();
482     $ldap->cd($gufilter['dselect']);
485     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
486         (Store gathered sn/givenName informations in $this->allusers too, 
487          to be prepared when adding/deleting users)
488      */    
489     $filter = "";
490     foreach ($this->memberUid as $value){
491       if(!isset($this->members[$value])){
492         $filter .= "(uid=".normalizeLdap($value).")";
493       }
494     }
495     if(!empty($filter)){    
496       $ldap->cd($this->config->current['BASE']);
497       $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
498       while($attrs = $ldap->fetch()){
499         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
500         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
501         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
502       } 
503     }
504   
505     /* check if all uids are resolved */
506     foreach ($this->memberUid as $value){
507       if(!isset($this->members[$value])){
508         $this->members[$value] =  _("! unknown id")." [".$value."]"; 
509       }
510     }  
512     /* Create display list of users matching regex & filter 
513      */
514     $this->displayUsers = array();
515     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
517     /* Search in current tree or within subtrees depending on the checkbox from filter section */
518     if($gufilter['SubSearchGroup']){
519       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
520       $base = $gufilter['dselect'];
521     }else{
522       $flag = GL_SIZELIMIT ;
523       $base = get_people_ou().$gufilter['dselect'];
524     }
525     $i = 0;
526   
528     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
530     /* Fetch all users and skip already used users */
531     foreach($res as $attrs){
532       if(in_array($attrs['uid'][0], $this->memberUid)) {
533         continue;
534       }
535       $i ++;
536       if($i > $MaxUser) {
537         break;
538       }
539       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
540       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
541       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
542     }
543   
544     /* If more than max users are found, display a message to warn the user */
545     if($i == $MaxUser){
546       msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries!"), $MaxUser), ERROR_DIALOG);
547     }
548     
549     /* Sort lists */
550     natcasesort($this->members);
551     reset($this->members);
552     natcasesort ($this->displayUsers);
553     reset ($this->displayUsers);
554   }
557   /* Create display name, this was used so often that it is excluded into a seperate function */
558   function createResultName($attrs)
559   {
560     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
561       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
562     } else {
563       $ret= $attrs['uid'][0];
564     }
565     return($ret);
566   }
569   function remove_from_parent()
570   {
571     plugin::remove_from_parent();
573     $ldap= $this->config->get_ldap_link();
574     $ldap->rmdir($this->dn);
575     if (!$ldap->success()){
576       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
577     }
579     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
581     /* Delete references to object groups */
582     $ldap->cd ($this->config->current['BASE']);
583     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
584     while ($ldap->fetch()){
585       $og= new ogroup($this->config, $ldap->getDN());
586       unset($og->member[$this->dn]);
587       $og->save ();
588     }
590     /* Remove ACL dependencies too,
591      */
592     $ldap = $this->config->get_ldap_link();
593     $ldap->cd($this->config->current['BASE']);
594     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
595     while($attrs = $ldap->fetch()){
596       $acl = new acl($this->config,$this->parent,$attrs['dn']);
597       foreach($acl->gosaAclEntry as $id => $entry){
598         foreach($entry['members'] as $m_id => $member){
599           if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
600             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
601             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
602           }
603         }
604       }
605       $acl -> save();
606     }
608     /* Remove ACL dependencies too,
609      */
610     $tmp = new acl($this->config,$this->parent,$this->dn);
611     $tmp->remove_acl();
613     /* Send signal to the world that we've done */
614     $this->handle_post_events("remove");
615   }
618   /* Save data to object */
619   function save_object()
620   {
621     /* Save additional values for possible next step */
622     if (isset($_POST['groupedit'])){
624       /* Create a base backup and reset the 
625           base directly after calling plugin::save_object();  
626          Base will be set seperatly a few lines below */
627       $base_tmp = $this->base;
628       plugin::save_object();
629       $this->base = $base_tmp;
631       $this->force_gid= 0;
633       /* Only reset sambagroup flag if we are able to write this flag */
634       if($this->acl_is_writeable("sambaGroupType")){
635         $this->smbgroup = 0;
636       }
638       /* Get base selection */
639       if(isset($_POST['base'])){
640         $tmp = $this->get_allowed_bases();
641         if(isset($tmp[$_POST['base']])){
642           $this->base = $_POST['base'];
643         }
644       }
646       foreach (array(
647             "force_gid"  => "gidNumber", 
648             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
649         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
650           $this->$val= $_POST["$val"];
651         }
652       }
654       /* Save sambaDomain attribute */
655       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
656         $this->sambaDomainName= $_POST['sambaDomainName'];
657         $this->groupType= $_POST['groupType'];
658       }
660       /* Save fon attribute */
661       if ($this->acl_is_writeable("fon_group")){
662         if (isset ($_POST['fon_group'])){
663           $this->fon_group= TRUE;
664         } else {
665           $this->fon_group= FALSE;
666         }
667       }
668       if ($this->acl_is_writeable("nagios_group")){
669         if (isset ($_POST['nagios_group'])){
670           $this->nagios_group= TRUE;
671         } else {
672           $this->nagios_group= FALSE;
673         }
674       }
675     }
676   }
679   /* Save to LDAP */
680   function save()
681   {
683     /* ID handling */
684     if ($this->force_gid == 0){
685       if ($this->saved_gidNumber != ""){
686         $this->gidNumber= $this->saved_gidNumber;
687       } else {
688         /* Calculate new, lock uids */
689         $wait= 10;
690         while (get_lock("uidnumber") != ""){
691           sleep (1);
693           /* timed out? */
694           if ($wait-- == 0){
695             break;
696           }
697         }
698         add_lock ("uidnumber", "gosa");
699         $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
700       }
701     }
702   
703     plugin::save(); 
705     /* Remove objectClass for samba/phone support */
706     $tmp= array();
707     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
708       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
709           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
710           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
711          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
712         $tmp[]= $this->attrs['objectClass'][$i];
713       }
714     }
715     $this->attrs['objectClass']= $tmp;
716     $ldap= $this->config->get_ldap_link();
718     /* Add samba group functionality */
719     if ($this->samba3 && $this->smbgroup){
720   
721       /* Fixed undefined index ... 
722        */ 
723       $this->SID = $this->ridBase = "";
724       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
725         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
726       }else{
727         msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
728       }
729       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
730         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
731       }else{
732         msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
733       }
735       $this->attrs['objectClass'][]= 'sambaGroupMapping';
736       $this->attrs['sambaGroupType']= "2";
738       /* Check if we need to create a special entry */
739       if ($this->groupType == 0){
741         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
742           $gidNumber= $this->gidNumber;
743           while(TRUE){
744             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
745             $ldap->cd($this->config->current['BASE']);
746             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
747             if ($ldap->count() == 0){
748               break;
749             }
750             $gidNumber++;
751           }
752           $this->attrs['sambaSID']= $sid;
753           $this->sambaSID= $sid;
754         }
756       } else {
757         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
758       }
760       /* User wants me to fake the idMappings? This is useful for
761          making winbind resolve the group names in a reasonable amount
762          of time in combination with larger databases. */
763       if (isset($this->config->current['SAMBAIDMAPPING']) &&
764           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
765         $this->attrs['objectClass'][]= "sambaIdmapEntry";
766       }
768     }
770     /* Add phone functionality */
771     if ($this->fon_group){
772       $this->attrs['objectClass'][]= "goFonPickupGroup";
773     }
775     /* Add nagios functionality */
776     if ($this->nagios_group){
777       $this->attrs['objectClass'][]= "nagiosContactGroup";
778     }
780     /* Take members array */
781     if (count ($this->memberUid)){
782       $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
783     }
785     /* New accounts need proper 'dn', propagate it to remaining objects */
786     if ($this->dn == 'new'){
787       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
788     }
790     /* Add member dn's for RFC2307bis Support */
791     if ($this->rfc2307bis){
792       $this->attrs['member'] = array();
793       if (count($this->memberUid)){
794         foreach($this->attrs['memberUid'] as $uid) {
795           $this->attrs['member'][]= $this->dnMapping[$uid];
796         }
797       } else {
798         $this->attrs['member'][]= $this->dn;
799       }
800     }
802     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
803        new entries. So do a check first... */
804     $ldap->cat ($this->dn, array('dn'));
805     if ($ldap->fetch()){
806       /* Modify needs array() to remove values :-( */
807       if (!count ($this->memberUid)){
808         $this->attrs['memberUid']= array();
809       }
810       if ($this->samba3){
811         if (!$this->smbgroup){
812           $this->attrs['sambaGroupType']= array();
813           $this->attrs['sambaSID']= array();
814         }
815       }
816       $mode= "modify";
817     } else {
818       $mode= "add";
819       $ldap->cd($this->config->current['BASE']);
820       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
821     }
823     /* Write back to ldap */
824     $ldap->cd($this->dn);
825     $this->cleanup();
826     $ldap->$mode($this->attrs);
828     /* Remove ACL dependencies too,
829      */
830     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
831       $tmp = new acl($this->config,$this->parent,$this->dn);
832       $tmp->update_acl_membership($this->orig_dn,$this->dn);
833     }
835     if($this->initially_was_account){
836       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
837     }else{
838       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
839     }
841     $ret= 0;
842     if (!$ldap->success()){
843       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
844       $ret= 1;
845     }
847     /* Remove uid lock */
848     del_lock ("uidnumber");
850     /* Post that we've done*/
851     $this->handle_post_events($mode);
853     return ($ret);
854   }
856   function check()
857   {
858     /* Call common method to give check the hook */
859     $message= plugin::check();
861     /* Permissions for that base? */
862     if ($this->base != ""){
863       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
864     } else {
865       $new_dn= $this->dn;
866     }
868     /* must: cn */
869     if ($this->cn == "" && $this->acl_is_writeable("cn")){
870       $message[]= msgPool::required(_("Name"));
871     }
873     /* Check for valid input */
874     if (!tests::is_uid($this->cn)){
875       if (strict_uid_mode()){
876         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
877       } else {
878         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/i");
879       }
880     }
882     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
884       /* Check for used 'cn' */
885       $ldap= $this->config->get_ldap_link();
886       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
887         $ldap->cd(get_groups_ou().$this->base);
888         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
889         if ($ldap->count() != 0){
890           $message[]= msgPool::duplicated(_("Name"));
891         }
892       }
894     }else{
896       /* Check for used 'cn' */
897       $ldap= $this->config->get_ldap_link();
898       $ldap->cd($this->config->current['BASE']);
899       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
900       if ($ldap->count() != 0){
902         /* New entry? */
903         if ($this->dn == 'new'){
904           $message[]= msgPool::duplicated(_("Name"));
905         }
907         /* Moved? */
908         elseif ($new_dn != $this->orig_dn){
909           $ldap->fetch();
910           if ($ldap->getDN() != $this->orig_dn){
911             $message[]= msgPool::duplicated(_("Name"));
912           }
913         }
914       }
915     }
916      
917     /* Check ID */
918     if ($this->force_gid == "1"){
919       if (!tests::is_id($this->gidNumber)){
920         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
921       } else {
922         if ($this->gidNumber < $this->config->current['MINID']){
923           $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
924         }
926       }
927     }
929     return ($message);
930   }
932   function get_next_id($attrib, $dn)
933   {
934     $ids= array();
935     $ldap= $this->config->get_ldap_link();
937     $ldap->cd ($this->config->current['BASE']);
938     if (preg_match('/gidNumber/i', $attrib)){
939       $oc= "posixGroup";
940     } else {
941       $oc= "posixAccount";
942     }
943     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
945     /* Get list of ids */
946     while ($attrs= $ldap->fetch()){
947       $ids[]= (int)$attrs["$attrib"][0];
948     }
950     /* Find out next free id near to UID_BASE */
951     if (!isset($this->config->current['BASE_HOOK'])){
952       $base= $this->config->current['UIDBASE'];
953     } else {
954       /* Call base hook */
955       $base= get_base_from_hook($dn, $attrib);
956     }
957     for ($id= $base; $id++; $id < pow(2,32)){
958       if (!in_array($id, $ids)){
959         return ($id);
960       }
961     }
963     /* Check if id reached maximum */
964     if ($id >= pow(2,32)){
965       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
966       exit;
967     }
968   }
970   function getCopyDialog()
971   {
972     $vars = array("cn");
973   
974     if($this ->force_gid){
975       $used = " checked ";
976       $dis  = "";
977     }else{
978       $used = "";
979       $dis  = " disabled ";
980     }
982     $smarty = get_smarty();
983     $smarty->assign("used",$used);
984     $smarty->assign("dis" ,$dis);
985     $smarty->assign("cn" ,$this->cn);
986     $smarty->assign("gidNumber",$this->gidNumber);
987     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
988     $ret = array();
989     $ret['string'] = $str;
990     $ret['status'] = "";
991     return($ret);
992   }
994   function saveCopyDialog()
995   {
996     if(isset($_POST['cn'])){
997       $this->cn = $_POST['cn'];
998     }
999     if(isset($_POST['force_gid'])){
1000       $this->force_gid  = 1;
1001       $this->gidNumber= $_POST['gidNumber'];
1002     }else{
1003       $this->force_gid  = 0;
1004       $this->gidNumber  = false;
1005     }
1006   }
1008   
1009   /* Return plugin informations for acl handling  */ 
1010   static function plInfo()
1011   {
1012     return (array(  
1013           "plShortName" => _("Generic"),
1014           "plDescription" => _("Generic group settings"),
1015           "plSelfModify"  => FALSE,
1016           "plDepends"     => array(),
1017           "plPriority"    => 0,
1018           "plSection"     => array("admin"),
1019           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1021           "plProvidedAcls"    => array(
1022             "cn"                => _("Name"),
1023             "base"              => _("Base"),
1024             "description"       => _("Description"),
1026             "fonGroup"          => _("Phone pickup group"),
1027             "nagiosGroup"       => _("Nagios group"),
1029             "gidNumber"         => _("GID"),
1030             "memberUid"         => _("Group member"),
1031             "sambaGroupType"    => _("Samba group type"),
1032             "sambaDomainName"   => _("Samba domain name"),
1033             "sambaSID"          => _("Samba SID"))
1034         ));
1035   }
1038   function multiple_save_object()
1039   {
1040     if(isset($_POST['group_mulitple_edit'])){
1042       /* Create a base backup and reset the
1043          base directly after calling plugin::save_object();
1044          Base will be set seperatly a few lines below */
1045       $base_tmp = $this->base;
1046       plugin::multiple_save_object();
1047       plugin::save_object();
1048       $this->base = $base_tmp;
1050       foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1051         if(isset($_POST['use_'.$attr])){
1052           $this->multi_boxes[] = $attr;
1053         }
1054       }
1056       /* Get base selection */
1057       if(isset($_POST['base'])){
1058         $tmp = $this->get_allowed_bases();
1059         if(isset($tmp[$_POST['base']])){
1060           $this->base = $_POST['base'];
1061         }
1062       }
1064       foreach (array( "smbgroup"   => "sambaGroupType") as $val => $aclname) {
1065         if ($this->acl_is_writeable($aclname)){
1066           if(isset($_POST["$val"])){
1067             $this->$val=  TRUE;
1068           }else{
1069             $this->$val=  FALSE;
1070           }
1071         }
1072       }
1074       /* Save sambaDomain attribute */
1075       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
1076         $this->sambaDomainName= $_POST['sambaDomainName'];
1077         $this->groupType= $_POST['groupType'];
1078       }
1080       /* Save fon attribute */
1081       if ($this->acl_is_writeable("fon_group")){
1082         if (isset ($_POST['fon_group'])){
1083           $this->fon_group= TRUE;
1084         } else {
1085           $this->fon_group= FALSE;
1086         }
1087       }
1088     }
1089   }
1092   function get_multi_edit_values()
1093   {
1094     $ret = plugin::get_multi_edit_values();
1096     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){    
1097       if(in_array($attr,$this->multi_boxes)){
1098         $ret[$attr] = $this->$attr;
1099       }
1100     }
1101     $ret['memberUid'] = $this->memberUid;
1102     $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1103     return($ret);
1104   }
1106   function multiple_execute()
1107   {
1108     return($this->execute());
1109   }
1112   /* Initialize plugin with given atribute arrays
1113    */
1114   function init_multiple_support($attrs,$all)
1115   {
1116     plugin::init_multiple_support($attrs,$all);
1118     $this->memberUid = array();
1119     $this->memberUid_used_by_some = array();
1120     if (isset($attrs['memberUid'])){
1121       for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1122         $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1123       }
1124       ksort($this->memberUid);
1125     }
1127     if (isset($all['memberUid'])){
1128       for ($i= 0; $i<$all['memberUid']['count']; $i++){
1129         if(!in_array($all['memberUid'][$i],$this->memberUid)){
1130           $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1131         }
1132       }
1133       ksort($this->memberUid_used_by_some);
1134     }
1135   }
1137   function set_multi_edit_values($attrs)
1138   {
1139     $users = array();
1141     /* Update groupMembership, keep optinal group */
1142     foreach($attrs['memberUid_used_by_some'] as $uid){
1143       if(in_array($uid,$this->memberUid)){
1144         $users[$uid] = $uid;
1145       }
1146     }
1148     /* Update groupMembership, add forced groups */
1149     foreach($attrs['memberUid'] as $uid){
1150       $users[$uid] = $uid;
1151     }
1152     plugin::set_multi_edit_values($attrs);
1153     $this->memberUid = $users;
1154   }
1159 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1160 ?>