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 $orig_base= "";
52 var $has_mailAccount= FALSE;
53 var $group_dialog= FALSE;
54 var $nagios_group =FALSE;
55 var $sambaGroupType;
56 var $dialog;
57 var $rfc2307bis= FALSE;
58 var $OnlyShowFirstEntries =200;
59 var $dnMapping= array();
60 var $view_logged = FALSE;
61 var $allowGroupsWithSameNameInOtherSubtrees = true;
63 /* Trustmodel/AccessTo
64 */
65 var $accessTo= array();
66 var $trustModel= "";
67 var $show_ws_dialog = 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 (isset($config->current['RFC2307BIS']) && ($config->current['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->samba3= ($config->current['SAMBAVERSION'] == 3);
90 $this->orig_dn= $dn;
91 $this->orig_cn= $this->cn;
93 /* Get member list */
94 if (isset($this->attrs['memberUid'][0])){
95 $tmp= array();
96 for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
97 $tmp[$this->attrs['memberUid'][$i]]= $this->attrs['memberUid'][$i];
98 }
99 $this->memberUid= $tmp;
100 ksort ($this->memberUid);
101 }
103 /* Save gidNumber for later use */
104 if (isset($this->attrs['gidNumber'])){
105 $this->saved_gidNumber= $this->attrs['gidNumber'][0];
106 }
108 /* Is a samba group? */
109 if (isset($this->attrs['objectClass'])){
110 if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == FALSE ){
111 $this->smbgroup= FALSE;
112 } else {
113 $this->smbgroup= TRUE;
114 if (isset($this->attrs['sambaSID'])){
115 $this->sambaSID= $this->attrs['sambaSID'][0];
116 }
117 }
118 if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == FALSE ){
119 $this->fon_group= FALSE;
120 } else {
121 $this->fon_group= TRUE;
122 }
123 if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
124 $this->nagios_group= FALSE;
125 } else {
126 $this->nagios_group= TRUE;
127 }
128 }
130 /* Set mail flag */
131 if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
132 $this->has_mailAccount= TRUE;
133 }
135 /* Get samba Domain in case of samba 3 */
136 if ($this->samba3 && $this->sambaSID != ""){
137 $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
138 $ldap= $this->config->get_ldap_link();
139 $ldap->cd($this->config->current['BASE']);
140 $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
141 if ($ldap->count() != 0){
142 $attrs= $ldap->fetch();
143 if(isset($attrs['sambaAlgorithmicRidBase'])){
144 $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
145 } else {
146 $this->ridBase= $this->config->current['RIDBASE'];
147 }
149 /* Get domain name for SID */
150 $this->sambaDomainName= "DEFAULT";
151 foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
152 if ($val['SID'] == $this->SID){
153 $this->sambaDomainName= $key;
154 break;
155 }
156 }
157 } else {
158 if (isset($this->config->current['RIDBASE'])){
159 $this->sambaDomainName= "DEFAULT";
160 $this->ridBase= $this->config->current['RIDBASE'];
161 $this->SID= $this->config->current['SID'];
162 } else {
163 msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
164 }
165 }
167 /* Get group type */
168 $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
169 if ($this->groupType < 500 || $this->groupType > 553){
170 $this->groupType= 0;
171 }
172 $this->oldgroupType= $this->groupType;
173 }
175 /* Get global filter config */
176 if (!session::is_set("gufilter")){
177 $ui= get_userinfo();
178 $base= get_base_from_people($ui->dn);
179 $gufilter= array( "dselect" => $base,
180 "regex" => "*");
181 session::set("gufilter", $gufilter);
182 }
183 $gufilter= session::get('gufilter');
184 $gufilter['SubSearchGroup'] = false;
185 session::set('gufilter',$gufilter);
187 if ($this->dn == "new"){
188 if(session::is_set('CurrentMainBase')){
189 $this->base = session::get('CurrentMainBase');
190 }else{
191 $ui= get_userinfo();
192 $this->base= dn2base($ui->dn);
193 }
194 } else {
196 /* Get object base */
197 $this->base =preg_replace ("/^[^,]+,".normalizePreg(get_groups_ou())."/","",$this->dn);
198 }
199 $this->orig_base = $this->base;
201 /* Is this account a trustAccount? */
202 if (isset($this->attrs['trustModel'])){
203 $this->trustModel= $this->attrs['trustModel'][0];
204 $this->was_trust_account= TRUE;
205 } else {
206 $this->was_trust_account= FALSE;
207 $this->trustModel= "";
208 }
210 $this->accessTo = array();
211 if (isset($this->attrs['accessTo'])){
212 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
213 $tmp= $this->attrs['accessTo'][$i];
214 $this->accessTo[$tmp]= $tmp;
215 }
216 }
218 /* Get global filter config */
219 if (!session::is_set("sysfilter")){
220 $ui= get_userinfo();
221 $base= get_base_from_people($ui->dn);
222 $sysfilter= array( "depselect" => $base,
223 "regex" => "*");
224 session::set("sysfilter", $sysfilter);
225 }
227 /* This is always an account */
228 $this->is_account= TRUE;
229 $this->reload();
230 }
232 function execute()
233 {
234 /* Call parent execute */
235 plugin::execute();
237 /* Log view */
238 if($this->is_account && !$this->view_logged){
239 $this->view_logged = TRUE;
240 new log("view","groups/".get_class($this),$this->dn);
241 }
243 /* Do we represent a valid group? */
244 if (!$this->is_account && $this->parent === NULL){
245 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".msgPool::noValidExtension()."</b>";
246 return ($display);
247 }
249 /* Delete user from group */
250 if (isset($_POST['del_users']) && isset($_POST['members']) && preg_match("/w/",$this->getacl("memberUid"))){
251 foreach ($_POST['members'] as $value){
252 unset ($this->members["$value"]);
253 $this->removeUser($value);
254 }
255 $this->reload();
256 }
258 /* Add objects? */
259 if (isset($_POST["edit_membership"]) && preg_match("/w/",$this->getacl("memberUid"))){
260 $this->group_dialog= TRUE;
261 $this->dialog= TRUE;
262 }
264 /* Add objects finished? */
265 if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
266 $this->group_dialog= FALSE;
267 $this->dialog= FALSE;
268 }
270 /* Add user to group */
271 if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
272 foreach ($_POST['users'] as $value){
273 $this->members["$value"]= $this->allusers[$value];
274 asort($this->members);
275 $this->addUser($value);
276 }
277 $this->reload();
278 }
280 /* Base select dialog */
281 $once = true;
282 foreach($_POST as $name => $value){
283 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
285 $once = false;
286 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
287 $this->dialog->setCurrentBase($this->base);
288 }
289 }
291 /* Dialog handling */
292 if(is_object($this->dialog)){
293 /* Must be called before save_object */
294 $this->dialog->save_object();
296 if($this->dialog->isClosed()){
297 $this->dialog = false;
298 }elseif($this->dialog->isSelected()){
300 /* Check if selected base is valid */
301 $tmp = $this->get_allowed_bases();
302 if(isset($tmp[$this->dialog->isSelected()])){
303 $this->base = $this->dialog->isSelected();
304 }
305 $this->dialog= false;
306 }else{
307 return($this->dialog->execute());
308 }
309 }
312 /* Add user workstation? */
313 if (isset($_POST["add_ws"])){
314 $this->show_ws_dialog= TRUE;
315 $this->dialog= TRUE;
316 }
318 /* Add user workstation? */
319 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
320 foreach($_POST['wslist'] as $ws){
321 $this->accessTo[$ws]= $ws;
322 }
323 ksort($this->accessTo);
324 $this->is_modified= TRUE;
325 }
327 /* Remove user workstations? */
328 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
329 foreach($_POST['workstation_list'] as $name){
330 unset ($this->accessTo[$name]);
331 }
332 $this->is_modified= TRUE;
333 }
335 /* Add user workstation finished? */
336 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
337 $this->show_ws_dialog= FALSE;
338 $this->dialog= FALSE;
339 }
341 $smarty= get_smarty();
343 /* Show ws dialog */
344 if ($this->show_ws_dialog){
346 /* Save data */
347 $sysfilter= session::get("sysfilter");
348 foreach( array("depselect", "regex") as $type){
349 if (isset($_POST[$type])){
350 $sysfilter[$type]= $_POST[$type];
351 }
352 }
353 if (isset($_GET['search'])){
354 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
355 if ($s == "**"){
356 $s= "*";
357 }
358 $sysfilter['regex']= $s;
359 }
360 session::set("sysfilter", $sysfilter);
362 /* Get workstation list */
363 $exclude= "";
364 foreach($this->accessTo as $ws){
365 $exclude.= "(cn=$ws)";
366 }
367 if ($exclude != ""){
368 $exclude= "(!(|$exclude))";
369 }
370 $regex= $sysfilter['regex'];
371 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
373 $deps_a = array(get_ou("serverou"),
374 get_ou("terminalou"),
375 get_ou("workstationou"));
377 $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
378 $wslist= array();
379 foreach ($res as $attrs){
380 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
381 }
382 asort($wslist);
383 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
384 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
385 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
386 $smarty->assign("deplist", $this->config->idepartments);
387 $smarty->assign("alphabet", generate_alphabet());
388 foreach( array("depselect", "regex") as $type){
389 $smarty->assign("$type", $sysfilter[$type]);
390 }
391 $smarty->assign("hint", print_sizelimit_warning());
392 $smarty->assign("wslist", $wslist);
393 $smarty->assign("apply", apply_filter());
394 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
395 return ($display);
396 }
398 /* Assign templating stuff */
399 if ($this->samba3){
400 $smarty->assign("samba3", "true");
401 } else {
402 $smarty->assign("samba3", "");
403 }
405 if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
406 $smarty->assign("nagios",true);
407 }else{
408 $smarty->assign("nagios",false);
409 }
411 if($this->config->search("phoneAccount", "CLASS",array('menu'))){
412 $smarty->assign("pickupGroup",true);
413 }else{
414 $smarty->assign("pickupGroup",false);
415 }
417 /* Manage object add dialog */
418 if ($this->group_dialog){
420 /* Save data */
421 $gufilter= session::get("gufilter");
422 foreach( array("dselect", "regex") as $type){
423 if (isset($_POST[$type])){
424 $gufilter[$type]= $_POST[$type];
425 }
426 }
427 if(isset($_POST['regex'])){
428 if(isset($_POST['SubSearchGroup'])){
429 $gufilter['SubSearchGroup'] = true;
430 }else{
431 $gufilter['SubSearchGroup'] = false;
432 }
433 }
435 if (isset($_GET['search'])){
436 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
437 if ($s == "**"){
438 $s= "*";
439 }
440 $gufilter['regex']= $s;
441 }
442 session::set("gufilter", $gufilter);
443 $this->reload();
445 /* Show dialog */
446 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
447 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
448 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
450 $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
451 $smarty->assign("alphabet", generate_alphabet());
452 foreach( array("dselect", "regex","SubSearchGroup") as $type){
453 $smarty->assign("$type", $gufilter[$type]);
454 }
455 $smarty->assign("hint", print_sizelimit_warning());
456 $smarty->assign("users", $this->displayUsers);
457 $smarty->assign("apply", apply_filter());
458 $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
459 return ($display);
460 }
462 $smarty->assign("bases", $this->get_allowed_bases());
463 $smarty->assign("base_select", $this->base);
465 if ($this->samba3){
466 $domains= array();
467 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
468 $domains[$name]= $name;
469 }
470 $smarty->assign("sambaDomains", $domains);
471 $smarty->assign("sambaDomainName", $this->sambaDomainName);
472 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
473 514 => _("Domain guests"));
475 /* Don't loose special groups! If not key'ed above, add it to
476 the combo box... */
477 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
478 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
479 }
481 $smarty->assign("groupTypes", $groupTypes);
482 $smarty->assign("groupType", $this->groupType);
483 }
485 /* Members and users */
486 $smarty->assign("members", $this->members);
488 /* Work on trust modes */
489 $smarty->assign("trusthide", " disabled ");
490 $smarty->assign("trustmodeACL", $this->getacl("trustModel"));
491 if ($this->trustModel == "fullaccess"){
492 $trustmode= 1;
493 // pervent double disable tag in html code, this will disturb our clean w3c html
494 $smarty->assign("trustmode", $this->getacl("trustModel"));
496 } elseif ($this->trustModel == "byhost"){
497 $trustmode= 2;
498 $smarty->assign("trusthide", "");
499 } else {
500 // pervent double disable tag in html code, this will disturb our clean w3c html
501 $smarty->assign("trustmode", $this->getacl("trustModel"));
502 $trustmode= 0;
503 }
504 $smarty->assign("trustmode", $trustmode);
505 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
506 2 => _("allow access to these hosts")));
508 if((count($this->accessTo))==0){
509 $smarty->assign("emptyArrAccess",true);
510 } else{
511 $smarty->assign("emptyArrAccess",false);
513 }
515 $smarty->assign("workstations", $this->accessTo);
519 /* Checkboxes */
520 foreach (array("force_gid", "smbgroup") as $val){
521 if ($this->$val == "1"){
522 $smarty->assign("$val", "checked");
523 } else {
524 $smarty->assign("$val", "");
525 }
526 }
527 if ($this->force_gid != "1"){
528 $smarty->assign("forceMode", "disabled");
529 }else{
530 $smarty->assign("forceMode", "");
531 }
532 if ($this->fon_group){
533 $smarty->assign("fon_group", "checked");
534 } else {
535 $smarty->assign("fon_group", "");
536 }
538 if ($this->nagios_group){
539 $smarty->assign("nagios_group", "checked");
540 } else {
541 $smarty->assign("nagios_group", "");
542 }
544 /* Fields */
545 foreach (array("cn", "description", "gidNumber") as $val){
546 $smarty->assign("$val", $this->$val);
547 }
549 $tmp = $this->plInfo();
550 foreach($tmp['plProvidedAcls'] as $name => $translation){
551 $smarty->assign($name."ACL",$this->getacl($name));
552 }
554 if($this->acl_is_writeable("base")){
555 $smarty->assign("baseSelect",true);
556 }else{
557 $smarty->assign("baseSelect",false);
558 }
560 /* Show main page */
561 $smarty->assign("alphabet", generate_alphabet(10));
562 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
563 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
564 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
565 $smarty->assign("deplist", $this->config->idepartments);
567 /* Multiple edit handling */
568 $smarty->assign("multiple_support",$this->multiple_support_active);
570 $smarty->assign("memberUid_All",$this->memberUid);
571 $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
573 foreach($this->attributes as $val){
574 if(in_array($val,$this->multi_boxes)){
575 $smarty->assign("use_".$val,TRUE);
576 }else{
577 $smarty->assign("use_".$val,FALSE);
578 }
579 }
580 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $val){
581 if(in_array($val,$this->multi_boxes)){
582 $smarty->assign("use_".$val,TRUE);
583 }else{
584 $smarty->assign("use_".$val,FALSE);
585 }
586 }
588 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
589 }
591 function addUser($uid)
592 {
593 /* In mutliple edit we have to handle two arrays.
594 * memberUid : Containing users used in all groups
595 * memberUid_used_by_some : Those which are not used in all groups
596 * So we have to remove the given $uid from the ..used_by_some array first.
597 */
598 if($this->multiple_support_active){
599 if(isset($this->memberUid_used_by_some[$uid])){
600 unset($this->memberUid_used_by_some[$uid]);
601 }
602 }
604 $this->memberUid[$uid]= $uid;
605 }
608 function removeUser($uid)
609 {
610 $temp= array();
611 if(isset($this->memberUid[$uid])){
612 unset($this->memberUid[$uid]);
613 }
615 /* We have two array contianing group members in multiple edit.
616 * this->memberUid : Groups used by all currently edited groups
617 * this->memberUid_used_by_some: Used by some
618 * So we have to remove the specified uid from both arrays.
619 */
620 if($this->multiple_support_active){
621 if(isset($this->memberUid_used_by_some[$uid])){
622 unset($this->memberUid_used_by_some[$uid]);
623 }
624 }
625 }
627 /* Reload data */
628 function reload()
629 {
630 /* Fix regex string */
631 $gufilter = session::get("gufilter");
632 $regex = normalizeLdap($gufilter['regex']);
633 $MaxUser = $this->OnlyShowFirstEntries;
635 /* Prepare ldap link */
636 $ldap= $this->config->get_ldap_link();
637 $ldap->cd($gufilter['dselect']);
640 /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes
641 (Store gathered sn/givenName informations in $this->allusers too,
642 to be prepared when adding/deleting users)
643 */
644 $filter = "";
645 if(!isset($this->config->current['LDAP_FILTER_NESTING_LIMIT']) ||
646 count($this->memberUid) < $this->config->current['LDAP_FILTER_NESTING_LIMIT']){
647 foreach ($this->memberUid as $value){
648 if(!isset($this->members[$value])){
649 $filter .= "(uid=".normalizeLdap($value).")";
650 }
651 }
652 }
654 if(!empty($filter)){
655 $ldap->cd($this->config->current['BASE']);
656 $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
657 while($attrs = $ldap->fetch()){
658 $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
659 $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
660 $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
661 }
662 }
664 /* check if all uids are resolved */
665 if(!isset($this->config->current['LDAP_FILTER_NESTING_LIMIT']) ||
666 count($this->memberUid) < $this->config->current['LDAP_FILTER_NESTING_LIMIT']){
667 foreach ($this->memberUid as $value){
668 if(!isset($this->members[$value])){
669 $this->members[$value] = _("! unknown id")." [".$value."]";
670 }
671 }
672 }else{
673 foreach ($this->memberUid as $value){
674 $this->members[$value] = $value;
675 }
676 }
678 /* Create display list of users matching regex & filter
679 */
680 $this->displayUsers = array();
681 $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
683 /* Search in current tree or within subtrees depending on the checkbox from filter section */
684 if($gufilter['SubSearchGroup']){
685 $flag = GL_SIZELIMIT | GL_SUBSEARCH;
686 $base = $gufilter['dselect'];
687 }else{
688 $flag = GL_SIZELIMIT ;
689 $base = get_people_ou().$gufilter['dselect'];
690 }
691 $i = 0;
694 $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
696 /* Fetch all users and skip already used users */
697 foreach($res as $attrs){
698 if(in_array($attrs['uid'][0], $this->memberUid)) {
699 continue;
700 }
701 $i ++;
702 if($i > $MaxUser) {
703 break;
704 }
705 $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
706 $this->allusers[$attrs['uid'][0]] = $this->createResultName($attrs);
707 $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
708 }
710 /* If more than max users are found, display a message to warn the user */
711 if($i == $MaxUser){
712 msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries!"), $MaxUser), ERROR_DIALOG);
713 }
715 /* Sort lists */
716 natcasesort($this->members);
717 reset($this->members);
718 natcasesort ($this->displayUsers);
719 reset ($this->displayUsers);
720 }
723 /* Create display name, this was used so often that it is excluded into a seperate function */
724 function createResultName($attrs)
725 {
726 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
727 $ret = $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
728 } else {
729 $ret= $attrs['uid'][0];
730 }
731 return($ret);
732 }
735 function remove_from_parent()
736 {
737 plugin::remove_from_parent();
739 $ldap= $this->config->get_ldap_link();
740 $ldap->rmdir($this->dn);
741 if (!$ldap->success()){
742 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
743 }
745 new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
747 /* Delete references to object groups */
748 $ldap->cd ($this->config->current['BASE']);
749 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
750 while ($ldap->fetch()){
751 $og= new ogroup($this->config, $ldap->getDN());
752 unset($og->member[$this->dn]);
753 $og->save ();
754 }
756 /* Remove ACL dependencies too,
757 */
758 $ldap = $this->config->get_ldap_link();
759 $ldap->cd($this->config->current['BASE']);
760 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
761 while($attrs = $ldap->fetch()){
762 $acl = new acl($this->config,$this->parent,$attrs['dn']);
763 foreach($acl->gosaAclEntry as $id => $entry){
764 foreach($entry['members'] as $m_id => $member){
765 if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
766 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
767 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
768 }
769 }
770 }
771 $acl->save();
772 }
774 /* Remove ACL dependencies too,
775 */
776 $tmp = new acl($this->config,$this->parent,$this->dn);
777 $tmp->remove_acl();
779 /* Send signal to the world that we've done */
780 $this->handle_post_events("remove");
781 }
784 /* Save data to object */
785 function save_object()
786 {
787 /* Save additional values for possible next step */
788 if (isset($_POST['groupedit'])){
790 /* Create a base backup and reset the
791 base directly after calling plugin::save_object();
792 Base will be set seperatly a few lines below */
793 $base_tmp = $this->base;
794 plugin::save_object();
795 $this->base = $base_tmp;
797 $this->force_gid= 0;
799 /* Only reset sambagroup flag if we are able to write this flag */
800 if($this->acl_is_writeable("sambaGroupType")){
801 $this->smbgroup = 0;
802 }
804 /* Get base selection */
805 if(isset($_POST['base'])){
806 $tmp = $this->get_allowed_bases();
807 if(isset($tmp[$_POST['base']])){
808 $this->base = $_POST['base'];
809 }
810 }
812 foreach (array(
813 "force_gid" => "gidNumber",
814 "smbgroup" => "sambaGroupType") as $val => $aclname) {
815 if ($this->acl_is_writeable($aclname) && isset($_POST["$val"])){
816 $this->$val= $_POST["$val"];
817 }
818 }
820 /* Save sambaDomain attribute */
821 if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
822 $this->sambaDomainName= $_POST['sambaDomainName'];
823 $this->groupType= $_POST['groupType'];
824 }
826 /* Save fon attribute */
827 if ($this->acl_is_writeable("fonGroup")){
828 if (isset ($_POST['fon_group'])){
829 $this->fon_group= TRUE;
830 } else {
831 $this->fon_group= FALSE;
832 }
833 }
834 if ($this->acl_is_writeable("nagiosGroup")){
835 if (isset ($_POST['nagios_group'])){
836 $this->nagios_group= TRUE;
837 } else {
838 $this->nagios_group= FALSE;
839 }
840 }
841 }
843 /* Trust mode - special handling */
844 if($this->acl_is_writeable("trustModel")){
845 if (isset($_POST['trustmode'])){
846 $saved= $this->trustModel;
847 if ($_POST['trustmode'] == "1"){
848 $this->trustModel= "fullaccess";
849 } elseif ($_POST['trustmode'] == "2"){
850 $this->trustModel= "byhost";
851 } else {
852 $this->trustModel= "";
853 }
854 if ($this->trustModel != $saved){
855 $this->is_modified= TRUE;
856 }
857 }
858 }
860 }
863 /* Save to LDAP */
864 function save()
865 {
867 /* ID handling */
868 if ($this->force_gid == 0){
869 if ($this->saved_gidNumber != ""){
870 $this->gidNumber= $this->saved_gidNumber;
871 } else {
872 /* Calculate new, lock uids */
873 $wait= 10;
874 while (get_lock("uidnumber") != ""){
875 sleep (1);
877 /* timed out? */
878 if ($wait-- == 0){
879 break;
880 }
881 }
882 add_lock ("uidnumber", "gosa");
883 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
884 }
885 }
887 plugin::save();
889 /* Trust accounts */
890 $objectclasses= array();
891 foreach ($this->attrs['objectClass'] as $key => $class){
892 if (preg_match('/trustAccount/i', $class)){
893 continue;
894 }
895 $objectclasses[]= $this->attrs['objectClass'][$key];
896 }
897 $this->attrs['objectClass']= $objectclasses;
898 if ($this->trustModel != ""){
899 $this->attrs['objectClass'][]= "trustAccount";
900 $this->attrs['trustModel']= $this->trustModel;
901 $this->attrs['accessTo']= array();
902 if ($this->trustModel == "byhost"){
903 foreach ($this->accessTo as $host){
904 $this->attrs['accessTo'][]= $host;
905 }
906 }
907 } else {
908 if ($this->was_trust_account){
909 $this->attrs['accessTo']= array();
910 $this->attrs['trustModel']= array();
911 }
912 }
916 /* Remove objectClass for samba/phone support */
917 $tmp= array();
918 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
919 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
920 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
921 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
922 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
923 $tmp[]= $this->attrs['objectClass'][$i];
924 }
925 }
926 $this->attrs['objectClass']= $tmp;
927 $ldap= $this->config->get_ldap_link();
929 /* Add samba group functionality */
930 if ($this->samba3 && $this->smbgroup){
932 /* Fixed undefined index ...
933 */
934 $this->SID = $this->ridBase = "";
935 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
936 $this->SID = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
937 }else{
938 msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
939 }
940 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
941 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
942 }else{
943 msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
944 }
946 $this->attrs['objectClass'][]= 'sambaGroupMapping';
947 $this->attrs['sambaGroupType']= "2";
949 /* Check if we need to create a special entry */
950 if ($this->groupType == 0){
952 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
953 $gidNumber= $this->gidNumber;
954 while(TRUE){
955 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
956 $ldap->cd($this->config->current['BASE']);
957 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
958 if ($ldap->count() == 0){
959 break;
960 }
961 $gidNumber++;
962 }
963 $this->attrs['sambaSID']= $sid;
964 $this->sambaSID= $sid;
965 }
967 } else {
968 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
969 }
971 /* User wants me to fake the idMappings? This is useful for
972 making winbind resolve the group names in a reasonable amount
973 of time in combination with larger databases. */
974 if (isset($this->config->current['SAMBAIDMAPPING']) &&
975 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
976 $this->attrs['objectClass'][]= "sambaIdmapEntry";
977 }
979 }
981 /* Add phone functionality */
982 if ($this->fon_group){
983 $this->attrs['objectClass'][]= "goFonPickupGroup";
984 }
986 /* Add nagios functionality */
987 if ($this->nagios_group){
988 $this->attrs['objectClass'][]= "nagiosContactGroup";
989 }
991 /* Take members array */
992 if (count ($this->memberUid)){
993 $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
994 }
996 /* New accounts need proper 'dn', propagate it to remaining objects */
997 if ($this->dn == 'new'){
998 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
999 }
1001 /* Add member dn's for RFC2307bis Support */
1002 if ($this->rfc2307bis){
1003 $this->attrs['member'] = array();
1004 if (count($this->memberUid)){
1005 foreach($this->attrs['memberUid'] as $uid) {
1006 $this->attrs['member'][]= $this->dnMapping[$uid];
1007 }
1008 } else {
1009 $this->attrs['member'][]= $this->dn;
1010 }
1011 }
1013 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1014 new entries. So do a check first... */
1015 $ldap->cat ($this->dn, array('dn'));
1016 if ($ldap->fetch()){
1017 /* Modify needs array() to remove values :-( */
1018 if (!count ($this->memberUid)){
1019 $this->attrs['memberUid']= array();
1020 }
1021 if ($this->samba3){
1022 if (!$this->smbgroup){
1023 $this->attrs['sambaGroupType']= array();
1024 $this->attrs['sambaSID']= array();
1025 }
1026 }
1027 $mode= "modify";
1028 } else {
1029 $mode= "add";
1030 $ldap->cd($this->config->current['BASE']);
1031 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1032 }
1034 /* Write back to ldap */
1035 $ldap->cd($this->dn);
1036 $this->cleanup();
1037 $ldap->$mode($this->attrs);
1039 /* Remove ACL dependencies too,
1040 */
1041 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1042 $tmp = new acl($this->config,$this->parent,$this->dn);
1043 $tmp->update_acl_membership($this->orig_dn,$this->dn);
1044 }
1046 if($this->initially_was_account){
1047 new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1048 }else{
1049 new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1050 }
1052 $ret= 0;
1053 if (!$ldap->success()){
1054 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1055 $ret= 1;
1056 }
1058 /* Remove uid lock */
1059 del_lock ("uidnumber");
1061 /* Post that we've done*/
1062 $this->handle_post_events($mode);
1064 return ($ret);
1065 }
1067 function check()
1068 {
1069 /* Call common method to give check the hook */
1070 $message= plugin::check();
1072 /* Permissions for that base? */
1073 if ($this->base != ""){
1074 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
1075 } else {
1076 $new_dn= $this->dn;
1077 }
1079 /* must: cn */
1080 if ($this->cn == "" && $this->acl_is_writeable("cn")){
1081 $message[]= msgPool::required(_("Name"));
1082 }
1084 /* Check for valid input */
1085 if (!tests::is_uid($this->cn)){
1086 if (strict_uid_mode()){
1087 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
1088 } else {
1089 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/i");
1090 }
1091 }
1093 if($this->allowGroupsWithSameNameInOtherSubtrees == true){
1095 /* Check for used 'cn' */
1096 $ldap= $this->config->get_ldap_link();
1097 if(($this->cn != $this->orig_cn) || ($this->orig_dn == "new")){
1098 $ldap->cd(get_groups_ou().$this->base);
1099 $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
1100 if ($ldap->count() != 0){
1101 $message[]= msgPool::duplicated(_("Name"));
1102 }
1103 }
1105 }else{
1107 /* Check for used 'cn' */
1108 $ldap= $this->config->get_ldap_link();
1109 $ldap->cd($this->config->current['BASE']);
1110 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
1111 if ($ldap->count() != 0){
1113 /* New entry? */
1114 if ($this->dn == 'new'){
1115 $message[]= msgPool::duplicated(_("Name"));
1116 }
1118 /* Moved? */
1119 elseif ($new_dn != $this->orig_dn){
1120 $ldap->fetch();
1121 if ($ldap->getDN() != $this->orig_dn){
1122 $message[]= msgPool::duplicated(_("Name"));
1123 }
1124 }
1125 }
1126 }
1128 /* Check ID */
1129 if ($this->force_gid == "1"){
1130 if (!tests::is_id($this->gidNumber)){
1131 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1132 } else {
1133 if ($this->gidNumber < $this->config->current['MINID']){
1134 $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1135 }
1137 }
1138 }
1140 /* Check if we are allowed to create or move this object
1141 */
1142 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1143 $message[] = msgPool::permCreate();
1144 }elseif($this->orig_dn != "new" && $this->orig_base && !$this->acl_is_moveable($this->base)){
1145 $message[] = msgPool::permMove();
1146 }
1148 return ($message);
1149 }
1151 function get_next_id($attrib, $dn)
1152 {
1153 $ids= array();
1154 $ldap= $this->config->get_ldap_link();
1156 $ldap->cd ($this->config->current['BASE']);
1157 if (preg_match('/gidNumber/i', $attrib)){
1158 $oc= "posixGroup";
1159 } else {
1160 $oc= "posixAccount";
1161 }
1162 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1164 /* Get list of ids */
1165 while ($attrs= $ldap->fetch()){
1166 $ids[]= (int)$attrs["$attrib"][0];
1167 }
1169 /* Find out next free id near to UID_BASE */
1170 if (!isset($this->config->current['BASE_HOOK'])){
1171 $base= $this->config->current['UIDBASE'];
1172 } else {
1173 /* Call base hook */
1174 $base= get_base_from_hook($dn, $attrib);
1175 }
1176 for ($id= $base; $id++; $id < pow(2,32)){
1177 if (!in_array($id, $ids)){
1178 return ($id);
1179 }
1180 }
1182 /* Check if id reached maximum */
1183 if ($id >= pow(2,32)){
1184 msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1185 exit;
1186 }
1187 }
1189 function getCopyDialog()
1190 {
1191 $vars = array("cn");
1193 if($this ->force_gid){
1194 $used = " checked ";
1195 $dis = "";
1196 }else{
1197 $used = "";
1198 $dis = " disabled ";
1199 }
1201 $smarty = get_smarty();
1202 $smarty->assign("used",$used);
1203 $smarty->assign("dis" ,$dis);
1204 $smarty->assign("cn" ,$this->cn);
1205 $smarty->assign("gidNumber",$this->gidNumber);
1206 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1207 $ret = array();
1208 $ret['string'] = $str;
1209 $ret['status'] = "";
1210 return($ret);
1211 }
1213 function saveCopyDialog()
1214 {
1215 if(isset($_POST['cn'])){
1216 $this->cn = $_POST['cn'];
1217 }
1218 if(isset($_POST['force_gid'])){
1219 $this->force_gid = 1;
1220 $this->gidNumber= $_POST['gidNumber'];
1221 }else{
1222 $this->force_gid = 0;
1223 $this->gidNumber = false;
1224 }
1225 }
1228 /* Return plugin informations for acl handling */
1229 static function plInfo()
1230 {
1231 return (array(
1232 "plShortName" => _("Generic"),
1233 "plDescription" => _("Generic group settings"),
1234 "plSelfModify" => FALSE,
1235 "plDepends" => array(),
1236 "plPriority" => 0,
1237 "plSection" => array("admin"),
1238 "plCategory" => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1240 "plProvidedAcls" => array(
1241 "cn" => _("Name"),
1242 "description" => _("Description"),
1243 "base" => _("Base"),
1245 "gidNumber" => _("GID"),
1247 "sambaGroupType" => _("Samba group type"),
1248 "sambaDomainName" => _("Samba domain name"),
1249 "trustModel" => _("System trust"),
1250 "fonGroup" => _("Phone pickup group"),
1251 "nagiosGroup" => _("Nagios group"),
1253 "memberUid" => _("Group member"))
1254 ));
1255 }
1258 function multiple_save_object()
1259 {
1260 if(isset($_POST['group_mulitple_edit'])){
1262 /* Create a base backup and reset the
1263 base directly after calling plugin::save_object();
1264 Base will be set seperatly a few lines below */
1265 $base_tmp = $this->base;
1266 plugin::multiple_save_object();
1267 plugin::save_object();
1268 $this->base = $base_tmp;
1270 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1271 if(isset($_POST['use_'.$attr])){
1272 $this->multi_boxes[] = $attr;
1273 }
1274 }
1276 /* Get base selection */
1277 if(isset($_POST['base'])){
1278 $tmp = $this->get_allowed_bases();
1279 if(isset($tmp[$_POST['base']])){
1280 $this->base = $_POST['base'];
1281 }
1282 }
1284 foreach (array( "smbgroup" => "sambaGroupType") as $val => $aclname) {
1285 if ($this->acl_is_writeable($aclname)){
1286 if(isset($_POST["$val"])){
1287 $this->$val= TRUE;
1288 }else{
1289 $this->$val= FALSE;
1290 }
1291 }
1292 }
1294 /* Save sambaDomain attribute */
1295 if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
1296 $this->sambaDomainName= $_POST['sambaDomainName'];
1297 $this->groupType= $_POST['groupType'];
1298 }
1300 /* Save fon attribute */
1301 if ($this->acl_is_writeable("fonGroup")){
1302 if (isset ($_POST['fon_group'])){
1303 $this->fon_group= TRUE;
1304 } else {
1305 $this->fon_group= FALSE;
1306 }
1307 }
1308 }
1309 }
1312 function get_multi_edit_values()
1313 {
1314 $ret = plugin::get_multi_edit_values();
1316 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1317 if(in_array($attr,$this->multi_boxes)){
1318 $ret[$attr] = $this->$attr;
1319 }
1320 }
1321 $ret['memberUid'] = $this->memberUid;
1322 $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1323 return($ret);
1324 }
1326 function multiple_execute()
1327 {
1328 return($this->execute());
1329 }
1332 /* Initialize plugin with given atribute arrays
1333 */
1334 function init_multiple_support($attrs,$all)
1335 {
1336 plugin::init_multiple_support($attrs,$all);
1338 $this->memberUid = array();
1339 $this->memberUid_used_by_some = array();
1340 if (isset($attrs['memberUid'])){
1341 for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1342 $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1343 }
1344 ksort($this->memberUid);
1345 }
1347 if (isset($all['memberUid'])){
1348 for ($i= 0; $i<$all['memberUid']['count']; $i++){
1349 if(!in_array($all['memberUid'][$i],$this->memberUid)){
1350 $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1351 }
1352 }
1353 ksort($this->memberUid_used_by_some);
1354 }
1355 }
1358 function PrepareForCopyPaste($source)
1359 {
1360 plugin::PrepareForCopyPaste($source);
1362 $this->memberUid = array();
1363 if(isset($source['memberUid'])){
1364 for($i = 0 ; $i < $source['memberUid']['count']; $i ++){
1365 $this->memberUid[] = $source['memberUid'][$i];
1366 }
1367 }
1368 }
1371 function set_multi_edit_values($attrs)
1372 {
1373 $users = array();
1375 /* Update groupMembership, keep optinal group */
1376 foreach($attrs['memberUid_used_by_some'] as $uid){
1377 if(in_array($uid,$this->memberUid)){
1378 $users[$uid] = $uid;
1379 }
1380 }
1382 /* Update groupMembership, add forced groups */
1383 foreach($attrs['memberUid'] as $uid){
1384 $users[$uid] = $uid;
1385 }
1386 plugin::set_multi_edit_values($attrs);
1387 $this->memberUid = $users;
1388 }
1389 }
1390 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1391 ?>