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