Code

Added base checks
[gosa.git] / gosa-core / plugins / admin / groups / class_group.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 $sambaSID= "";
39   var $sambaDomainName= "DEFAULT";
40   var $SID= "";
41   var $ridBase= 0;
42   var $members= array();
43   var $users= array();
44   var $member= array();
45   var $allusers= array();
46   var $saved_gidNumber= "";
47   var $oldgroupType= "";
48   var $orig_dn= "";
49   var $orig_cn= "";
50   var $orig_base= "";
51   var $has_mailAccount= FALSE;
52   var $userSelect= 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;
61   var $baseSelector;
63   /* Trustmodel/AccessTo 
64    */
65   var $accessTo= array();
66   var $trustModel= "";
67   var $trustSelect = FALSE;
69   /* attribute list for save action */
70   var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID","accessTo","trustModel");
71   var $objectclasses= array("top", "posixGroup");
73   var $CopyPasteVars  = array("force_gid","fon_group","smbgroup","groupType","sambaSID","sambaDomainName","SID","nagios_group","sambaGroupType");
75   var $multiple_support = TRUE;
77   function group (&$config, $dn= NULL)
78   {
79      /* Set rfc2307bis flag */
80      if ($config->get_cfg_value("rfc2307bis") == "true"){
81        $this->rfc2307bis= TRUE;
82        $this->attributes[]= "member";
83        $this->objectclasses[]= "groupOfNames";
84      }
86     plugin::plugin ($config, $dn);
88     /* Load attributes depending on the samba version */
89     $this->orig_dn= $dn;
90     $this->orig_cn= $this->cn;
92     /* Get member list */
93     if (isset($this->attrs['memberUid'][0])){
94       $tmp= array();
95       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
96         $tmp[$this->attrs['memberUid'][$i]]= $this->attrs['memberUid'][$i];
97       }
98       $this->memberUid= $tmp;
99       ksort ($this->memberUid);
100     }
102     /* Save gidNumber for later use */
103     if (isset($this->attrs['gidNumber'])){
104       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
105     }
107     /* Is a samba group? */
108     if (isset($this->attrs['objectClass'])){
109       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == FALSE ){
110         $this->smbgroup= FALSE;
111       } else {
112         $this->smbgroup= TRUE;
113         if (isset($this->attrs['sambaSID'])){
114           $this->sambaSID= $this->attrs['sambaSID'][0];
115         }
116       }
117       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == FALSE ){
118         $this->fon_group= FALSE;
119       } else {
120         $this->fon_group= TRUE;
121       }
122       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
123         $this->nagios_group= FALSE;
124       } else {
125         $this->nagios_group= TRUE;
126       }
127     }
129     /* Set mail flag */
130     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
131       $this->has_mailAccount= TRUE;
132     }
134     /* Get samba Domain in case of samba 3 */
135     if ($this->sambaSID != ""){
136       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
137       $ldap= $this->config->get_ldap_link();
138       $ldap->cd($this->config->current['BASE']);
139       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
140       if ($ldap->count() != 0){
141         $attrs= $ldap->fetch();
142         if(isset($attrs['sambaAlgorithmicRidBase'])){  
143           $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];  
144         } else {  
145           $this->ridBase= $this->config->get_cfg_value("sambaRidBase");
146         } 
148         /* Get domain name for SID */
149         $this->sambaDomainName= "DEFAULT";
150         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
151           if ($val['SID'] == $this->SID){
152             $this->sambaDomainName= $key;
153             break;
154           }
155         }
156       } else {
157         if ($this->config->get_cfg_value("sambaRidBase") != ""){
158           $this->sambaDomainName= "DEFAULT";
159           $this->ridBase= $this->config->get_cfg_value("sambaRidBase");
160           $this->SID= $this->config->get_cfg_value("sid");
161         } else {
162           msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
163         }
164       }
166       /* Get group type */
167       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
168       if ($this->groupType < 500 || $this->groupType > 553){
169         $this->groupType= 0;
170       }
171       $this->oldgroupType= $this->groupType;
172     }
174     /* Get global filter config */
175     if (!session::is_set("gufilter")){
176       $ui= get_userinfo();
177       $base= get_base_from_people($ui->dn);
178       $gufilter= array( "dselect"       => $base,
179           "regex"           => "*");
180       session::set("gufilter", $gufilter);
181     }
182     $gufilter= session::get('gufilter');
183     $gufilter['SubSearchGroup'] = false;
184     session::set('gufilter',$gufilter);
185   
186     if ($this->dn == "new"){
187       if(session::is_set('CurrentMainBase')){
188         $this->base = session::get('CurrentMainBase');
189       }else{
190         $ui= get_userinfo();
191         $this->base= dn2base($ui->dn);
192       }
193     } else {
195       /* Get object base */
196       $this->base =preg_replace ("/^[^,]+,".preg_quote(get_groups_ou(), '/')."/i","",$this->dn);
197     }
198     $this->orig_base = $this->base;
200     /* Is this account a trustAccount? */
201     if (isset($this->attrs['trustModel'])){
202       $this->trustModel= $this->attrs['trustModel'][0];
203       $this->was_trust_account= TRUE;
204     } else {
205       $this->was_trust_account= FALSE;
206       $this->trustModel= "";
207     }
209     $this->accessTo = array();
210     if (isset($this->attrs['accessTo'])){
211       for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
212         $tmp= $this->attrs['accessTo'][$i];
213         $this->accessTo[$tmp]= $tmp;
214       }
215     }
217     /* Get global filter config */
218     if (!session::is_set("sysfilter")){
219       $ui= get_userinfo();
220       $base= get_base_from_people($ui->dn);
221       $sysfilter= array( "depselect"       => $base,
222           "regex"           => "*");
223       session::set("sysfilter", $sysfilter);
224     }
226     /* This is always an account */
227     $this->is_account= TRUE;
229     /* Instanciate base selector */
230     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
231     $this->baseSelector->setSubmitButton(false);
232     $this->baseSelector->setHeight(300);
233     $this->baseSelector->update(true);
235     $this->reload(TRUE);
236   }
238   function execute()
239   {
240     /* Call parent execute */
241     plugin::execute();
243     /* Log view */
244     if($this->is_account && !$this->view_logged){
245       $this->view_logged = TRUE;
246       new log("view","groups/".get_class($this),$this->dn);
247     }
249     /* Do we represent a valid group? */
250     if (!$this->is_account && $this->parent === NULL){
251       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".msgPool::noValidExtension()."</b>";
252       return ($display);
253     }
255     /* Delete user from group */
256     if (isset($_POST['del_users']) && isset($_POST['members']) && preg_match("/w/",$this->getacl("memberUid"))){
257       foreach ($_POST['members'] as $value){
258         unset ($this->members["$value"]);
259         $this->removeUser($value);
260       }
261       $this->reload();
262     }
264     /* Add objects? */
265     if (isset($_POST["edit_membership"]) && preg_match("/w/",$this->getacl("memberUid"))){
266       $this->userSelect= new userSelect($this->config, get_userinfo());
267       $this->dialog= TRUE;
268     }
270     /* Add objects finished? */
271     if (isset($_POST["add_users_cancel"])){
272       $this->userSelect= NULL;
273       $this->dialog= FALSE;
274     }
276     /* Add user to group */
277     if (isset($_POST['add_users_finish']) && $this->userSelect){
278   
279       $users = $this->userSelect->detectPostActions();
280       if(isset($users['targets'])){
281         $headpage = $this->userSelect->getHeadpage();
282         foreach($users['targets'] as $dn){
283           $attrs = $headpage->getEntry($dn);
284           $value = $attrs['uid'][0];
285           $this->addUser($value);
286           $this->members["$value"]= $this->allusers[$value];
287           $this->reload();
288         }
289       }
290       $this->userSelect= NULL;
291       $this->dialog= FALSE;
292     }
294     /* Dialog handling */
295     if(is_object($this->dialog)){
296       /* Must be called before save_object */
297       $this->dialog->save_object();
299       if($this->dialog->isClosed()){
300         $this->dialog = false;
301       }elseif($this->dialog->isSelected()){
303         /* Check if selected base is valid */
304         $tmp = $this->get_allowed_bases();
305         if(isset($tmp[$this->dialog->isSelected()])){
306           $this->base = $this->dialog->isSelected();
307         }
308         $this->dialog= false;
309       }else{
310         return($this->dialog->execute());
311       }
312     }
315     /* Add user workstation? */
316     if (isset($_POST["add_ws"])){
317       $this->trustSelect= new trustSelect($this->config,get_userinfo());
318       $this->dialog= TRUE;
319     }
321     // Add selected machines to trusted ones.
322     if (isset($_POST["add_ws_finish"]) &&  $this->trustSelect){
323       $trusts = $this->trustSelect->detectPostActions();
324       if(isset($trusts['targets'])){
326         $headpage = $this->trustSelect->getHeadpage();
327         foreach($trusts['targets'] as $id){
328           $attrs = $headpage->getEntry($id);
329           $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
330         }
331         ksort($this->accessTo);
332         $this->is_modified= TRUE;
333       }
334       $this->trustSelect= NULL;
335       $this->dialog= FALSE;
336     }
339     /* Add user workstation? */
340     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
341       foreach($_POST['wslist'] as $ws){
342         $this->accessTo[$ws]= $ws;
343       }
344       ksort($this->accessTo);
345       $this->is_modified= TRUE;
346     }
348     /* Remove user workstations? */
349     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
350       foreach($_POST['workstation_list'] as $name){
351         unset ($this->accessTo[$name]);
352       }
353       $this->is_modified= TRUE;
354     }
356     /* Add user workstation finished? */
357     if (isset($_POST["add_ws_cancel"])){
358       $this->trustSelect= NULL;
359       $this->dialog= FALSE;
360     }
363     /* Show ws dialog */
364     if ($this->trustSelect){
365     
366       // Build up blocklist
367       session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
368       return($this->trustSelect->execute());
369     }
371     $smarty= get_smarty();
372     $smarty->assign("usePrototype", "true");
374     if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
375       $smarty->assign("nagios",true);
376     }else{
377       $smarty->assign("nagios",false);
378     }
379     
380     if($this->config->search("phoneAccount", "CLASS",array('menu'))){
381       $smarty->assign("pickupGroup",true);
382     }else{
383       $smarty->assign("pickupGroup",false);
384     }
386     /* Manage object add dialog */
387     if ($this->userSelect){
388       return($this->userSelect->execute());
389     }
391     /* Create base acls */
392     $smarty->assign("base", $this->baseSelector->render());
394     $domains= array();
395     foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
396       $domains[$name]= $name;
397     }
398     $smarty->assign("sambaDomains", $domains);
399     $smarty->assign("sambaDomainName", $this->sambaDomainName);
400     $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
401         514 => _("Domain guests"));
403     /* Don't loose special groups! If not key'ed above, add it to
404        the combo box... */      
405     if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
406       $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
407     }
409     $smarty->assign("groupTypes", $groupTypes);
410     $smarty->assign("groupType", $this->groupType);
411     
413     /* Members and users */
414     $smarty->assign("members", $this->members);
416     /* Work on trust modes */
417     $smarty->assign("trusthide", " disabled ");
418     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
419     if ($this->trustModel == "fullaccess"){
420       $trustmode= 1;
421       // pervent double disable tag in html code, this will disturb our clean w3c html
422       $smarty->assign("trustmode",  $this->getacl("trustModel"));
424     } elseif ($this->trustModel == "byhost"){
425       $trustmode= 2;
426       $smarty->assign("trusthide", "");
427     } else {
428       // pervent double disable tag in html code, this will disturb our clean w3c html
429       $smarty->assign("trustmode",  $this->getacl("trustModel"));
430       $trustmode= 0;
431     }
432     $smarty->assign("trustmode", $trustmode);
433     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
434           2 => _("allow access to these hosts")));
436     if((count($this->accessTo))==0){
437       $smarty->assign("emptyArrAccess",true);
438     } else{
439       $smarty->assign("emptyArrAccess",false);
441     }
443     $smarty->assign("workstations", $this->accessTo);
447     /* Checkboxes */
448     foreach (array("force_gid", "smbgroup") as $val){
449       if ($this->$val == "1"){
450         $smarty->assign("$val", "checked");
451       } else {
452         $smarty->assign("$val", "");
453       }
454     }
455     if ($this->force_gid != "1"){
456       $smarty->assign("forceMode", "disabled");
457     }else{
458       $smarty->assign("forceMode", "");
459     }
460     if ($this->fon_group){
461       $smarty->assign("fon_group", "checked");
462     } else {
463       $smarty->assign("fon_group", "");
464     }
466     if ($this->nagios_group){
467       $smarty->assign("nagios_group", "checked");
468     } else {
469       $smarty->assign("nagios_group", "");
470     }
472     /* Fields */
473     foreach (array("cn", "description", "gidNumber") as $val){
474       $smarty->assign("$val", $this->$val);
475     }
477     $tmp = $this->plInfo();
478     foreach($tmp['plProvidedAcls'] as $name => $translation){
479       $smarty->assign($name."ACL",$this->getacl($name));
480     }
481     
482     if($this->acl_is_writeable("base")){
483       $smarty->assign("baseSelect",true);
484     }else{
485       $smarty->assign("baseSelect",false);
486     }
488     /* Show main page */
489     $smarty->assign("alphabet", generate_alphabet(10));
490     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
491     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
492     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
493     $smarty->assign("deplist", $this->config->idepartments);
495     /* Multiple edit handling */
496     $smarty->assign("multiple_support",$this->multiple_support_active);
498     $smarty->assign("memberUid_All",$this->memberUid);
499     $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
501     foreach($this->attributes as $val){
502       if(in_array($val,$this->multi_boxes)){
503         $smarty->assign("use_".$val,TRUE);
504       }else{
505         $smarty->assign("use_".$val,FALSE);
506       }
507     }
508     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group","trustmode") as $val){
509       if(in_array($val,$this->multi_boxes)){
510         $smarty->assign("use_".$val,TRUE);
511       }else{
512         $smarty->assign("use_".$val,FALSE);
513       }
514     }
516     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
517   }
519   function addUser($uid)
520   {
521     /* In mutliple edit we have to handle two arrays.
522      *  memberUid               : Containing users used in all groups
523      *  memberUid_used_by_some  : Those which are not used in all groups
524      * So we have to remove the given $uid from the ..used_by_some array first.
525      */
526     if($this->multiple_support_active){
527       if(isset($this->memberUid_used_by_some[$uid])){
528         unset($this->memberUid_used_by_some[$uid]);
529       }
530     }  
532     /* Ensure that the requested object is known to the group class 
533      */
534     if(!isset($this->dnMapping[$uid])){
535       $ldap = $this->config->get_ldap_link();
536       $ldap->cd($this->config->current['BASE']);
537       $ldap->search("(&(objectClass=gosaAccount)(uid=".$uid."))",array("dn", "uid","sn","givenName"));
538       if($ldap->count() == 0 ){
539         msg_dialog::display(_("Error"), 
540             sprintf(_("Adding UID '%s' to group '%s' failed: cannot find user object!"), 
541               $uid,$this->cn), 
542             ERROR_DIALOG);
543         return;
544       }elseif($ldap->count() >= 2){
545         msg_dialog::display(_("Error"), 
546             sprintf(_("Add UID '%s' to group '%s' failed: UID is used more than once!"),
547               $uid,$this->cn), 
548             ERROR_DIALOG);
549         return;
550       }else{
551         while($attrs = $ldap->fetch()){
552           $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
553           $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
554           $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
555         }
556       }
557     }
559     $this->memberUid[$uid]= $uid;
560   }
563   function removeUser($uid)
564   {
565     $temp= array();
566     if(isset($this->memberUid[$uid])){
567       unset($this->memberUid[$uid]);
568     }
570     /* We have two array contianing group members in multiple edit.
571      *  this->memberUid             : Groups used by all currently edited groups 
572      *  this->memberUid_used_by_some: Used by some 
573      * So we have to remove the specified uid from both arrays.
574      */
575     if($this->multiple_support_active){
576       if(isset($this->memberUid_used_by_some[$uid])){
577         unset($this->memberUid_used_by_some[$uid]);
578       }
579     }
580   }
582   /* Reload data */
583   function reload($silent = FALSE)
584   {
585     /* Fix regex string */
586     $gufilter = session::get("gufilter");
587     $regex    = normalizeLdap($gufilter['regex']);
588     $MaxUser  = $this->OnlyShowFirstEntries;
590     /* Prepare ldap link */
591     $ldap= $this->config->get_ldap_link();
592     $ldap->cd($gufilter['dselect']);
595     /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes 
596         (Store gathered sn/givenName informations in $this->allusers too, 
597          to be prepared when adding/deleting users)
598      */    
599     $filter = "";
600     if ($this->config->get_cfg_value("ldapFilterNestingLimit") == "" ||
601         count($this->memberUid) < $this->config->get_cfg_value("ldapFilterNestingLimit")){
602       foreach ($this->memberUid as $value){
603         if(!isset($this->members[$value])){
604           $filter .= "(uid=".normalizeLdap($value).")";
605         }
606       }
607     }
609     if(!empty($filter)){    
610       $ldap->cd($this->config->current['BASE']);
611       $ldap->search("(&(objectClass=gosaAccount)(|".$filter."))",array("dn", "uid","sn","givenName"));
612       while($attrs = $ldap->fetch()){
613         $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
614         $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
615         $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
616       } 
617     }
618   
619     /* check if all uids are resolved */
620     if ($this->config->get_cfg_value("ldapFilterNestingLimit") == "" ||
621         count($this->memberUid) < $this->config->get_cfg_value("ldapFilterNestingLimit")){
622       foreach ($this->memberUid as $value){
623         if(!isset($this->members[$value])){
624           $this->members[$value] =  _("! unknown UID")." [".$value."]"; 
625         }
626       }  
627     }else{
628       foreach ($this->memberUid as $value){
629         $this->members[$value] = $value; 
630       }  
631     }
633     /* Create display list of users matching regex & filter 
634      */
635     $this->displayUsers = array();
636     $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
638     /* Search in current tree or within subtrees depending on the checkbox from filter section */
639     if($gufilter['SubSearchGroup']){
640       $flag = GL_SIZELIMIT | GL_SUBSEARCH;
641       $base = $gufilter['dselect'];
642     }else{
643       $flag = GL_SIZELIMIT ;
644       $base = get_people_ou().$gufilter['dselect'];
645     }
646     $i = 0;
647   
649     $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
651     /* Fetch all users and skip already used users */
652     foreach($res as $attrs){
653       if(in_array($attrs['uid'][0], $this->memberUid)) {
654         continue;
655       }
656       $i ++;
657       if($i > $MaxUser) {
658         break;
659       }
660       $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
661       $this->allusers[$attrs['uid'][0]]     = $this->createResultName($attrs);
662       $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
663     }
664   
665     /* If more than max users are found, display a message to warn the user */
666     if($i == $MaxUser && !$silent){
667       msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries!"), $MaxUser), ERROR_DIALOG);
668     }
669     
670     /* Sort lists */
671     natcasesort($this->members);
672     reset($this->members);
673     natcasesort ($this->displayUsers);
674     reset ($this->displayUsers);
675   }
678   /* Create display name, this was used so often that it is excluded into a seperate function */
679   function createResultName($attrs)
680   {
681     if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
682       $ret =  $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
683     } else {
684       $ret= $attrs['uid'][0];
685     }
686     return($ret);
687   }
690   function remove_from_parent()
691   {
692     plugin::remove_from_parent();
694     $ldap= $this->config->get_ldap_link();
695     $ldap->rmdir($this->dn);
696     if (!$ldap->success()){
697       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
698     }
700     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
702     /* Delete references to object groups */
703     $ldap->cd ($this->config->current['BASE']);
704     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
705     while ($ldap->fetch()){
706       $og= new ogroup($this->config, $ldap->getDN());
707       unset($og->member[$this->dn]);
708       $og->save ();
709     }
711     /* Remove ACL dependencies too,
712      */
713     $ldap = $this->config->get_ldap_link();
714     $ldap->cd($this->config->current['BASE']);
715     $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
716     while($attrs = $ldap->fetch()){
717       $acl = new acl($this->config,$this->parent,$attrs['dn']);
718       foreach($acl->gosaAclEntry as $id => $entry){
719         foreach($entry['members'] as $m_id => $member){
720           if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
721             unset($acl->gosaAclEntry[$id]['members'][$m_id]);
722             gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
723           }
724         }
725       }
726       $acl->save();
727     }
729     /* Remove ACL dependencies, too */
730     acl::remove_acl_for($this->dn);
732     /* Send signal to the world that we've done */
733     $this->handle_post_events("remove");
734   }
737   /* Save data to object */
738   function save_object()
739   {
740     /* Save additional values for possible next step */
741     if (isset($_POST['groupedit'])){
743       /* Create a base backup and reset the 
744           base directly after calling plugin::save_object();  
745          Base will be set seperatly a few lines below */
746       $base_tmp = $this->base;
747       plugin::save_object();
748       $this->base = $base_tmp;
750       /* Refresh base */
751       if ($this->acl_is_moveable($this->base)){
752         if (!$this->baseSelector->update()) {
753           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
754         }
755         if ($this->base != $this->baseSelector->getBase()) {
756           $this->base= $this->baseSelector->getBase();
757           $this->is_modified= TRUE;
758         }
759       }
761       $this->force_gid= 0;
763       /* Only reset sambagroup flag if we are able to write this flag */
764       if($this->acl_is_writeable("sambaGroupType")){
765         $this->smbgroup = 0;
766       }
768       foreach (array(
769             "force_gid"  => "gidNumber", 
770             "smbgroup"   => "sambaGroupType") as $val => $aclname) {
771         if ($this->acl_is_writeable($aclname)  && isset($_POST["$val"])){
772           $this->$val= $_POST["$val"];
773         }
774       }
776       /* Save sambaDomain attribute */
777       if ($this->acl_is_writeable("sambaDomainName") && isset ($_POST['sambaDomainName'])){
778         $this->sambaDomainName= $_POST['sambaDomainName'];
779         $this->groupType= $_POST['groupType'];
780       }
782       /* Save fon attribute */
783       if ($this->acl_is_writeable("fonGroup")){
784         if (isset ($_POST['fon_group'])){
785           $this->fon_group= TRUE;
786         } else {
787           $this->fon_group= FALSE;
788         }
789       }
790       if ($this->acl_is_writeable("nagiosGroup")){
791         if (isset ($_POST['nagios_group'])){
792           $this->nagios_group= TRUE;
793         } else {
794           $this->nagios_group= FALSE;
795         }
796       }
797     }
799     /* Trust mode - special handling */
800     if($this->acl_is_writeable("trustModel")){
801       if (isset($_POST['trustmode'])){
802         $saved= $this->trustModel;
803         if ($_POST['trustmode'] == "1"){
804           $this->trustModel= "fullaccess";
805         } elseif ($_POST['trustmode'] == "2"){
806           $this->trustModel= "byhost";
807         } else {
808           $this->trustModel= "";
809         }
810         if ($this->trustModel != $saved){
811           $this->is_modified= TRUE;
812         }
813       }
814     }
816   }
819   /* Save to LDAP */
820   function save()
821   {
823     /* ID handling */
824     if ($this->force_gid == 0){
825       if ($this->saved_gidNumber != ""){
826         $this->gidNumber= $this->saved_gidNumber;
827       } else {
828         /* Calculate new, lock uids */
829         $wait= 10;
830         while (get_lock("gidnumber") != ""){
831           sleep (1);
833           /* timed out? */
834           if ($wait-- == 0){
835             break;
836           }
837         }
838         add_lock ("gidnumber", "gosa");
839         $this->gidNumber= get_next_id("gidNumber", $this->dn);
840       }
841     }
842   
843     plugin::save(); 
845  /* Trust accounts */
846     $objectclasses= array();
847     foreach ($this->attrs['objectClass'] as $key => $class){
848       if (preg_match('/trustAccount/i', $class)){
849         continue;
850       }
851       $objectclasses[]= $this->attrs['objectClass'][$key];
852     }
853     $this->attrs['objectClass']= $objectclasses;
854     if ($this->trustModel != ""){
855       $this->attrs['objectClass'][]= "trustAccount";
856       $this->attrs['trustModel']= $this->trustModel;
857       $this->attrs['accessTo']= array();
858       if ($this->trustModel == "byhost"){
859         foreach ($this->accessTo as $host){
860           $this->attrs['accessTo'][]= $host;
861         }
862       }
863     } else {
864       if ($this->was_trust_account){
865         $this->attrs['accessTo']= array();
866         $this->attrs['trustModel']= array();
867       }
868     }
872     /* Remove objectClass for samba/phone support */
873     $tmp= array();
874     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
875       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
876           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
877           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
878          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
879         $tmp[]= $this->attrs['objectClass'][$i];
880       }
881     }
882     $this->attrs['objectClass']= $tmp;
883     $ldap= $this->config->get_ldap_link();
885     /* Add samba group functionality */
886     if ($this->smbgroup){
887   
888       /* Fixed undefined index ... 
889        */ 
890       $this->SID = $this->ridBase = "";
891       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
892         $this->SID    = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
893       }else{
894         msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
895       }
896       if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
897         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE']; 
898       }else{
899         msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
900       }
902       $this->attrs['objectClass'][]= 'sambaGroupMapping';
903       $this->attrs['sambaGroupType']= "2";
905       /* Check if we need to create a special entry */
906       if ($this->groupType == 0){
908         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
909           $gidNumber= $this->gidNumber;
910           while(TRUE){
911             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
912             $ldap->cd($this->config->current['BASE']);
913             $ldap->search("(sambaSID=$sid)",array("sambaSID"));
914             if ($ldap->count() == 0){
915               break;
916             }
917             $gidNumber++;
918           }
919           $this->attrs['sambaSID']= $sid;
920           $this->sambaSID= $sid;
921         }
923       } else {
924         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
925       }
927       /* User wants me to fake the idMappings? This is useful for
928          making winbind resolve the group names in a reasonable amount
929          of time in combination with larger databases. */
930       if ($this->config->get_cfg_value("sambaidmapping") == "true"){
931         $this->attrs['objectClass'][]= "sambaIdmapEntry";
932       }
934     }
936     /* Add phone functionality */
937     if ($this->fon_group){
938       $this->attrs['objectClass'][]= "goFonPickupGroup";
939     }
941     /* Add nagios functionality */
942     if ($this->nagios_group){
943       $this->attrs['objectClass'][]= "nagiosContactGroup";
944     }
946     /* Take members array */
947     if (count ($this->memberUid)){
948       $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
949     }
951     /* New accounts need proper 'dn', propagate it to remaining objects */
952     if ($this->dn == 'new'){
953       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
954     }
956     /* Add member dn's for RFC2307bis Support */
957     if ($this->rfc2307bis){
958       $this->attrs['member'] = array();
959       if (count($this->memberUid)){
960         foreach($this->attrs['memberUid'] as $uid) {
962           if(isset($this->dnMapping[$uid])){
963             $this->attrs['member'][]= $this->dnMapping[$uid];
964           }
965         }
966       } else {
967         $this->attrs['member'][]= $this->dn;
968       }
969     }
971     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
972        new entries. So do a check first... */
973     $ldap->cat ($this->dn, array('dn'));
974     if ($ldap->fetch()){
975       /* Modify needs array() to remove values :-( */
976       if (!count ($this->memberUid)){
977         $this->attrs['memberUid']= array();
978       }
979       if (!$this->smbgroup){
980         $this->attrs['sambaGroupType']= array();
981         $this->attrs['sambaSID']= array();
982       }
983       
984       $mode= "modify";
985     } else {
986       $mode= "add";
987       $ldap->cd($this->config->current['BASE']);
988       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
989     }
992     /* Check generated gidNumber, it may be used by another group. 
993      */
994     if($this->gidNumber != ""){
995       $ldap->cd($this->config->current['BASE']);
996       $ldap->search("(&(!(cn=".$this->orig_cn."))(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))",array("cn"));
997       if($ldap->count()){
998         $cns = "";
999         while($attrs = $ldap->fetch()){
1000           $cns .= $attrs['cn'][0].", ";
1001         }
1002         $cns = rtrim($cns,", ");
1003         msg_dialog::display(_("Warning"),sprintf(_("The gidNumber '%s' is already in use by %s!"),$this->gidNumber,$cns) , WARNING_DIALOG );
1004       }
1005     }
1007     /* Write back to ldap */
1008     $ldap->cd($this->dn);
1009     $this->cleanup();
1010     $ldap->$mode($this->attrs);
1012     /* Remove ACL dependencies too,
1013      */
1014     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1015       $tmp = new acl($this->config,$this->parent,$this->dn);
1016       $tmp->update_acl_membership($this->orig_dn,$this->dn);
1017     }
1019     if($this->initially_was_account){
1020       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1021     }else{
1022       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1023     }
1025     $ret= 0;
1026     if (!$ldap->success()){
1027       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1028       $ret= 1;
1029     }
1031     /* Remove uid lock */
1032     del_lock ("gidnumber");
1034     /* Post that we've done*/
1035     $this->handle_post_events($mode);
1037     return ($ret);
1038   }
1040   function check()
1041   {
1042     /* Call common method to give check the hook */
1043     $message= plugin::check();
1045     /* Permissions for that base? */
1046     if ($this->base != ""){
1047       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
1048     } else {
1049       $new_dn= $this->dn;
1050     }
1052     /* must: cn */
1053     if ($this->cn == "" && $this->acl_is_writeable("cn")){
1054       $message[]= msgPool::required(_("Name"));
1055     }
1057     // Check if a wrong base was supplied
1058     if(!$this->baseSelector->checkLastBaseUpdate()){
1059       $message[]= msgPool::check_base();;
1060     }
1062     /* Check for valid input */
1063     if (!tests::is_uid($this->cn)){
1064       if (strict_uid_mode()){
1065         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
1066       } else {
1067         $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/i");
1068       }
1069     }
1071     // Check if a wrong base was supplied
1072     if(!$this->baseSelector->checkLastBaseUpdate()){
1073       $message[]= msgPool::check_base();;
1074     }
1076     if($this->allowGroupsWithSameNameInOtherSubtrees == true){
1078       /* Check for used 'cn' */
1079       $ldap= $this->config->get_ldap_link();
1080       if(($this->cn  != $this->orig_cn) || ($this->orig_dn == "new")){
1081         $ldap->cd(get_groups_ou().$this->base);
1082         $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
1083         if ($ldap->count() != 0){
1084           $message[]= msgPool::duplicated(_("Name"));
1085         }
1086       }
1088     }else{
1090       /* Check for used 'cn' */
1091       $ldap= $this->config->get_ldap_link();
1092       $ldap->cd($this->config->current['BASE']);
1093       $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
1094       if ($ldap->count() != 0){
1096         /* New entry? */
1097         if ($this->dn == 'new'){
1098           $message[]= msgPool::duplicated(_("Name"));
1099         }
1101         /* Moved? */
1102         elseif ($new_dn != $this->orig_dn){
1103           $ldap->fetch();
1104           if ($ldap->getDN() != $this->orig_dn){
1105             $message[]= msgPool::duplicated(_("Name"));
1106           }
1107         }
1108       }
1109     }
1110      
1111     /* Check ID */
1112     if ($this->force_gid == "1"){
1113       if (!tests::is_id($this->gidNumber)){
1114         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1115       } else {
1116         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1117           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1118         }
1120       }
1121     }
1122     
1123     /* Check if we are allowed to create or move this object 
1124      */
1125     if(!$this->orig_dn == "new" || 
1126         $this->orig_base != $this->base || 
1127         $this->cn != $this->orig_cn){
1129       if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1130         $message[] = msgPool::permCreate();
1131       }elseif($this->orig_dn != "new" && !$this->acl_is_moveable($this->base)){
1132         $message[] = msgPool::permMove();
1133       }
1134     }
1136     return ($message);
1137   }
1140   function getCopyDialog()
1141   {
1142     $vars = array("cn");
1143   
1144     if($this ->force_gid){
1145       $used = " checked ";
1146       $dis  = "";
1147     }else{
1148       $used = "";
1149       $dis  = " disabled ";
1150     }
1152     $smarty = get_smarty();
1153     $smarty->assign("used",$used);
1154     $smarty->assign("dis" ,$dis);
1155     $smarty->assign("cn" ,$this->cn);
1156     $smarty->assign("gidNumber",$this->gidNumber);
1157     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1158     $ret = array();
1159     $ret['string'] = $str;
1160     $ret['status'] = "";
1161     return($ret);
1162   }
1164   function saveCopyDialog()
1165   {
1166     if(isset($_POST['cn'])){
1167       $this->cn = $_POST['cn'];
1168     }
1169     if(isset($_POST['force_gid'])){
1170       $this->force_gid  = 1;
1171       $this->gidNumber= $_POST['gidNumber'];
1172     }else{
1173       $this->force_gid  = 0;
1174       $this->gidNumber  = false;
1175     }
1176   }
1178   
1179   /* Return plugin informations for acl handling  */ 
1180   static function plInfo()
1181   {
1182     return (array(  
1183           "plShortName" => _("Generic"),
1184           "plDescription" => _("Generic group settings"),
1185           "plSelfModify"  => FALSE,
1186           "plDepends"     => array(),
1187           "plPriority"    => 0,
1188           "plSection"     => array("administration"),
1189           "plCategory"    => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1191           "plProvidedAcls"    => array(
1192             "cn"                => _("Name"),
1193             "description"       => _("Description"),
1194             "base"              => _("Base"),
1196             "gidNumber"         => _("GID"),
1198             "sambaGroupType"    => _("Samba group type"),
1199             "sambaDomainName"   => _("Samba domain name"),
1200             "trustModel"        => _("System trust"),
1201             "fonGroup"          => _("Phone pickup group"),
1202             "nagiosGroup"       => _("Nagios group"),
1204             "memberUid"         => _("Group member"))
1205         ));
1206   }
1209   function multiple_save_object()
1210   {
1211     if(isset($_POST['group_mulitple_edit'])){
1213       /* Create a base backup and reset the
1214          base directly after calling plugin::save_object();
1215          Base will be set seperatly a few lines below */
1216       $base_tmp = $this->base;
1217       plugin::multiple_save_object();
1218       plugin::save_object();
1219       $this->base = $base_tmp;
1221       foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group","trustmode") as $attr){
1222         if(isset($_POST['use_'.$attr])){
1223           $this->multi_boxes[] = $attr;
1224         }
1225       }
1227       /* Refresh base */
1228       if ($this->acl_is_moveable($this->base)){
1229         if (!$this->baseSelector->update()) {
1230           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
1231         }
1232         if ($this->base != $this->baseSelector->getBase()) {
1233           $this->base= $this->baseSelector->getBase();
1234           $this->is_modified= TRUE;
1235         }
1236       }
1238       foreach (array( "smbgroup"   => "sambaGroupType" ,"nagios_group" => "nagios_group") as $val => $aclname) {
1239         if ($this->acl_is_writeable($aclname)){
1240           if(isset($_POST["$val"])){
1241             $this->$val=  TRUE;
1242           }else{
1243             $this->$val=  FALSE;
1244           }
1245         }
1246       }
1248       /* Save sambaDomain attribute */
1249       if ($this->acl_is_writeable("sambaDomainName") && isset ($_POST['sambaDomainName'])){
1250         $this->sambaDomainName= $_POST['sambaDomainName'];
1251         $this->groupType= $_POST['groupType'];
1252       }
1254       /* Trust mode - special handling */
1255       if($this->acl_is_writeable("trustModel")){
1256         if (isset($_POST['trustmode'])){
1257           $saved= $this->trustModel;
1258           if ($_POST['trustmode'] == "1"){
1259             $this->trustModel= "fullaccess";
1260           } elseif ($_POST['trustmode'] == "2"){
1261             $this->trustModel= "byhost";
1262           } else {
1263             $this->trustModel= "";
1264           }
1265           if ($this->trustModel != $saved){
1266             $this->is_modified= TRUE;
1267           }
1268         }
1269       }
1271       /* Save fon attribute */
1272       if ($this->acl_is_writeable("fonGroup")){
1273         if (isset ($_POST['fon_group'])){
1274           $this->fon_group= TRUE;
1275         } else {
1276           $this->fon_group= FALSE;
1277         }
1278       }
1279     }
1280   }
1283   function get_multi_edit_values()
1284   {
1285     $ret = plugin::get_multi_edit_values();
1287     foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group") as $attr){    
1288       if(in_array($attr,$this->multi_boxes)){
1289         $ret[$attr] = $this->$attr;
1290       }
1291     }
1293     if(in_array("trustmode",$this->multi_boxes)){
1294       $ret['trustModel'] = $this->trustModel;
1295       $ret['accessTo'] = $this->accessTo;
1296     }
1298     $ret['memberUid'] = $this->memberUid;
1299     $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1300     return($ret);
1301   }
1303   function multiple_execute()
1304   {
1305     return($this->execute());
1306   }
1309   /* Initialize plugin with given atribute arrays
1310    */
1311   function init_multiple_support($attrs,$all)
1312   {
1313     plugin::init_multiple_support($attrs,$all);
1315     $this->memberUid = array();
1316     $this->memberUid_used_by_some = array();
1317     if (isset($attrs['memberUid'])){
1318       for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1319         $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1320       }
1321       ksort($this->memberUid);
1322     }
1324     if (isset($all['memberUid'])){
1325       for ($i= 0; $i<$all['memberUid']['count']; $i++){
1326         if(!in_array($all['memberUid'][$i],$this->memberUid)){
1327           $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1328         }
1329       }
1330       ksort($this->memberUid_used_by_some);
1331     }
1332   }
1335   function PrepareForCopyPaste($source)
1336   {
1337     plugin::PrepareForCopyPaste($source);
1339     $this->memberUid = array();
1340     if(isset($source['memberUid'])){
1341       for($i = 0 ; $i < $source['memberUid']['count']; $i ++){
1342         $this->memberUid[] = $source['memberUid'][$i];
1343       }
1344     }
1345     $this->accessTo = array();
1346     if (isset($source['accessTo'])){
1347       for ($i= 0; $i<$source['accessTo']['count']; $i++){
1348         $tmp= $source['accessTo'][$i];
1349         $this->accessTo[$tmp]= $tmp;
1350       }
1351     }
1352   }
1355   function set_multi_edit_values($attrs)
1356   {
1357     $users = array();
1359     /* Update groupMembership, keep optinal group */
1360     foreach($attrs['memberUid_used_by_some'] as $uid){
1361       if(in_array($uid,$this->memberUid)){
1362         $users[$uid] = $uid;
1363       }
1364     }
1366     /* Update groupMembership, add forced groups */
1367     foreach($attrs['memberUid'] as $uid){
1368       $users[$uid] = $uid;
1369     }
1370     plugin::set_multi_edit_values($attrs);
1371     $this->memberUid = $users;
1372   }
1374 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1375 ?>