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'];
372 /* Search for systems
373 */
374 $types = array();
375 $types['server'] = array("OU" => get_ou("serverou"), "OC" => "(objectClass=goServer)");
376 $types['workstation'] = array("OU" => get_ou("workstationou"),"OC" => "(objectClass=gotoWorkstation)");
377 $types['terminal'] = array("OU" => get_ou("terminalou"), "OC" => "(objectClass=gotoTerminal)");
379 $res = array();
380 foreach($types as $acl => $data){
381 $filter= "(&".$data['OC']."$exclude(cn=$regex))";
382 $res= array_merge($res,get_sub_list($filter,array($acl),$data['OU'],get_ou("systemsou").$sysfilter['depselect'],
383 array("cn"), GL_SUBSEARCH | GL_SIZELIMIT));
384 }
385 $wslist = array();
386 foreach ($res as $attrs){
387 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
388 }
390 asort($wslist);
391 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
392 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
393 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
394 $smarty->assign("deplist", $this->config->idepartments);
395 $smarty->assign("alphabet", generate_alphabet());
396 foreach( array("depselect", "regex") as $type){
397 $smarty->assign("$type", $sysfilter[$type]);
398 }
399 $smarty->assign("hint", print_sizelimit_warning());
400 $smarty->assign("wslist", $wslist);
401 $smarty->assign("apply", apply_filter());
402 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
403 return ($display);
404 }
406 /* Assign templating stuff */
407 if ($this->samba3){
408 $smarty->assign("samba3", "true");
409 } else {
410 $smarty->assign("samba3", "");
411 }
413 if($this->config->search("nagiosaccount", "CLASS",array('menu'))){
414 $smarty->assign("nagios",true);
415 }else{
416 $smarty->assign("nagios",false);
417 }
419 if($this->config->search("phoneAccount", "CLASS",array('menu'))){
420 $smarty->assign("pickupGroup",true);
421 }else{
422 $smarty->assign("pickupGroup",false);
423 }
425 /* Manage object add dialog */
426 if ($this->group_dialog){
428 /* Save data */
429 $gufilter= session::get("gufilter");
430 foreach( array("dselect", "regex") as $type){
431 if (isset($_POST[$type])){
432 $gufilter[$type]= $_POST[$type];
433 }
434 }
435 if(isset($_POST['regex'])){
436 if(isset($_POST['SubSearchGroup'])){
437 $gufilter['SubSearchGroup'] = true;
438 }else{
439 $gufilter['SubSearchGroup'] = false;
440 }
441 }
443 if (isset($_GET['search'])){
444 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
445 if ($s == "**"){
446 $s= "*";
447 }
448 $gufilter['regex']= $s;
449 }
450 session::set("gufilter", $gufilter);
451 $this->reload();
453 /* Show dialog */
454 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
455 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
456 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
458 $smarty->assign("deplist", $this->get_allowed_bases("users/user"));
459 $smarty->assign("alphabet", generate_alphabet());
460 foreach( array("dselect", "regex","SubSearchGroup") as $type){
461 $smarty->assign("$type", $gufilter[$type]);
462 }
463 $smarty->assign("hint", print_sizelimit_warning());
464 $smarty->assign("users", $this->displayUsers);
465 $smarty->assign("apply", apply_filter());
466 $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
467 return ($display);
468 }
470 $smarty->assign("bases", $this->get_allowed_bases());
471 $smarty->assign("base_select", $this->base);
473 if ($this->samba3){
474 $domains= array();
475 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
476 $domains[$name]= $name;
477 }
478 $smarty->assign("sambaDomains", $domains);
479 $smarty->assign("sambaDomainName", $this->sambaDomainName);
480 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
481 514 => _("Domain guests"));
483 /* Don't loose special groups! If not key'ed above, add it to
484 the combo box... */
485 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
486 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
487 }
489 $smarty->assign("groupTypes", $groupTypes);
490 $smarty->assign("groupType", $this->groupType);
491 }
493 /* Members and users */
494 $smarty->assign("members", $this->members);
496 /* Work on trust modes */
497 $smarty->assign("trusthide", " disabled ");
498 $smarty->assign("trustmodeACL", $this->getacl("trustModel"));
499 if ($this->trustModel == "fullaccess"){
500 $trustmode= 1;
501 // pervent double disable tag in html code, this will disturb our clean w3c html
502 $smarty->assign("trustmode", $this->getacl("trustModel"));
504 } elseif ($this->trustModel == "byhost"){
505 $trustmode= 2;
506 $smarty->assign("trusthide", "");
507 } else {
508 // pervent double disable tag in html code, this will disturb our clean w3c html
509 $smarty->assign("trustmode", $this->getacl("trustModel"));
510 $trustmode= 0;
511 }
512 $smarty->assign("trustmode", $trustmode);
513 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
514 2 => _("allow access to these hosts")));
516 if((count($this->accessTo))==0){
517 $smarty->assign("emptyArrAccess",true);
518 } else{
519 $smarty->assign("emptyArrAccess",false);
521 }
523 $smarty->assign("workstations", $this->accessTo);
527 /* Checkboxes */
528 foreach (array("force_gid", "smbgroup") as $val){
529 if ($this->$val == "1"){
530 $smarty->assign("$val", "checked");
531 } else {
532 $smarty->assign("$val", "");
533 }
534 }
535 if ($this->force_gid != "1"){
536 $smarty->assign("forceMode", "disabled");
537 }else{
538 $smarty->assign("forceMode", "");
539 }
540 if ($this->fon_group){
541 $smarty->assign("fon_group", "checked");
542 } else {
543 $smarty->assign("fon_group", "");
544 }
546 if ($this->nagios_group){
547 $smarty->assign("nagios_group", "checked");
548 } else {
549 $smarty->assign("nagios_group", "");
550 }
552 /* Fields */
553 foreach (array("cn", "description", "gidNumber") as $val){
554 $smarty->assign("$val", $this->$val);
555 }
557 $tmp = $this->plInfo();
558 foreach($tmp['plProvidedAcls'] as $name => $translation){
559 $smarty->assign($name."ACL",$this->getacl($name));
560 }
562 if($this->acl_is_writeable("base")){
563 $smarty->assign("baseSelect",true);
564 }else{
565 $smarty->assign("baseSelect",false);
566 }
568 /* Show main page */
569 $smarty->assign("alphabet", generate_alphabet(10));
570 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
571 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
572 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
573 $smarty->assign("deplist", $this->config->idepartments);
575 /* Multiple edit handling */
576 $smarty->assign("multiple_support",$this->multiple_support_active);
578 $smarty->assign("memberUid_All",$this->memberUid);
579 $smarty->assign("memberUid_Some",$this->memberUid_used_by_some);
581 foreach($this->attributes as $val){
582 if(in_array($val,$this->multi_boxes)){
583 $smarty->assign("use_".$val,TRUE);
584 }else{
585 $smarty->assign("use_".$val,FALSE);
586 }
587 }
588 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $val){
589 if(in_array($val,$this->multi_boxes)){
590 $smarty->assign("use_".$val,TRUE);
591 }else{
592 $smarty->assign("use_".$val,FALSE);
593 }
594 }
596 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
597 }
599 function addUser($uid)
600 {
601 /* In mutliple edit we have to handle two arrays.
602 * memberUid : Containing users used in all groups
603 * memberUid_used_by_some : Those which are not used in all groups
604 * So we have to remove the given $uid from the ..used_by_some array first.
605 */
606 if($this->multiple_support_active){
607 if(isset($this->memberUid_used_by_some[$uid])){
608 unset($this->memberUid_used_by_some[$uid]);
609 }
610 }
612 $this->memberUid[$uid]= $uid;
613 }
616 function removeUser($uid)
617 {
618 $temp= array();
619 if(isset($this->memberUid[$uid])){
620 unset($this->memberUid[$uid]);
621 }
623 /* We have two array contianing group members in multiple edit.
624 * this->memberUid : Groups used by all currently edited groups
625 * this->memberUid_used_by_some: Used by some
626 * So we have to remove the specified uid from both arrays.
627 */
628 if($this->multiple_support_active){
629 if(isset($this->memberUid_used_by_some[$uid])){
630 unset($this->memberUid_used_by_some[$uid]);
631 }
632 }
633 }
635 /* Reload data */
636 function reload()
637 {
638 /* Fix regex string */
639 $gufilter = session::get("gufilter");
640 $regex = normalizeLdap($gufilter['regex']);
641 $MaxUser = $this->OnlyShowFirstEntries;
643 /* Prepare ldap link */
644 $ldap= $this->config->get_ldap_link();
645 $ldap->cd($gufilter['dselect']);
648 /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes
649 (Store gathered sn/givenName informations in $this->allusers too,
650 to be prepared when adding/deleting users)
651 */
652 $filter = "";
653 if(!isset($this->config->current['LDAP_FILTER_NESTING_LIMIT']) ||
654 count($this->memberUid) < $this->config->current['LDAP_FILTER_NESTING_LIMIT']){
655 foreach ($this->memberUid as $value){
656 if(!isset($this->members[$value])){
657 $filter .= "(uid=".normalizeLdap($value).")";
658 }
659 }
660 }
662 if(!empty($filter)){
663 $ldap->cd($this->config->current['BASE']);
664 $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|".$filter."))",array("dn", "uid","sn","givenName"));
665 while($attrs = $ldap->fetch()){
666 $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
667 $this->members[$attrs['uid'][0]] = $this->createResultName($attrs);
668 $this->allusers[$attrs['uid'][0]]= $this->createResultName($attrs);
669 }
670 }
672 /* check if all uids are resolved */
673 if(!isset($this->config->current['LDAP_FILTER_NESTING_LIMIT']) ||
674 count($this->memberUid) < $this->config->current['LDAP_FILTER_NESTING_LIMIT']){
675 foreach ($this->memberUid as $value){
676 if(!isset($this->members[$value])){
677 $this->members[$value] = _("! unknown id")." [".$value."]";
678 }
679 }
680 }else{
681 foreach ($this->memberUid as $value){
682 $this->members[$value] = $value;
683 }
684 }
686 /* Create display list of users matching regex & filter
687 */
688 $this->displayUsers = array();
689 $filter = "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$))(|(uid=".$regex.")(sn=".$regex.")(givenName=".$regex.")))";
691 /* Search in current tree or within subtrees depending on the checkbox from filter section */
692 if($gufilter['SubSearchGroup']){
693 $flag = GL_SIZELIMIT | GL_SUBSEARCH;
694 $base = $gufilter['dselect'];
695 }else{
696 $flag = GL_SIZELIMIT ;
697 $base = get_people_ou().$gufilter['dselect'];
698 }
699 $i = 0;
702 $res = get_list($filter,"users",$base,array("dn", "uid", "sn", "givenName"),$flag);
704 /* Fetch all users and skip already used users */
705 foreach($res as $attrs){
706 if(in_array($attrs['uid'][0], $this->memberUid)) {
707 continue;
708 }
709 $i ++;
710 if($i > $MaxUser) {
711 break;
712 }
713 $this->dnMapping[$attrs['uid'][0]]= $attrs["dn"];
714 $this->allusers[$attrs['uid'][0]] = $this->createResultName($attrs);
715 $this->displayUsers[$attrs['uid'][0]] = $this->createResultName($attrs);
716 }
718 /* If more than max users are found, display a message to warn the user */
719 if($i == $MaxUser){
720 msg_dialog::display(_("Configuration error"), sprintf(_("Search returned too many results. Not displaying more than %s entries!"), $MaxUser), ERROR_DIALOG);
721 }
723 /* Sort lists */
724 natcasesort($this->members);
725 reset($this->members);
726 natcasesort ($this->displayUsers);
727 reset ($this->displayUsers);
728 }
731 /* Create display name, this was used so often that it is excluded into a seperate function */
732 function createResultName($attrs)
733 {
734 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
735 $ret = $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
736 } else {
737 $ret= $attrs['uid'][0];
738 }
739 return($ret);
740 }
743 function remove_from_parent()
744 {
745 plugin::remove_from_parent();
747 $ldap= $this->config->get_ldap_link();
748 $ldap->rmdir($this->dn);
749 if (!$ldap->success()){
750 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
751 }
753 new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
755 /* Delete references to object groups */
756 $ldap->cd ($this->config->current['BASE']);
757 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
758 while ($ldap->fetch()){
759 $og= new ogroup($this->config, $ldap->getDN());
760 unset($og->member[$this->dn]);
761 $og->save ();
762 }
764 /* Remove ACL dependencies too,
765 */
766 $ldap = $this->config->get_ldap_link();
767 $ldap->cd($this->config->current['BASE']);
768 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
769 while($attrs = $ldap->fetch()){
770 $acl = new acl($this->config,$this->parent,$attrs['dn']);
771 foreach($acl->gosaAclEntry as $id => $entry){
772 foreach($entry['members'] as $m_id => $member){
773 if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
774 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
775 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
776 }
777 }
778 }
779 $acl->save();
780 }
782 /* Remove ACL dependencies too,
783 */
784 $tmp = new acl($this->config,$this->parent,$this->dn);
785 $tmp->remove_acl();
787 /* Send signal to the world that we've done */
788 $this->handle_post_events("remove");
789 }
792 /* Save data to object */
793 function save_object()
794 {
795 /* Save additional values for possible next step */
796 if (isset($_POST['groupedit'])){
798 /* Create a base backup and reset the
799 base directly after calling plugin::save_object();
800 Base will be set seperatly a few lines below */
801 $base_tmp = $this->base;
802 plugin::save_object();
803 $this->base = $base_tmp;
805 $this->force_gid= 0;
807 /* Only reset sambagroup flag if we are able to write this flag */
808 if($this->acl_is_writeable("sambaGroupType")){
809 $this->smbgroup = 0;
810 }
812 /* Get base selection */
813 if(isset($_POST['base'])){
814 $tmp = $this->get_allowed_bases();
815 if(isset($tmp[$_POST['base']])){
816 $this->base = $_POST['base'];
817 }
818 }
820 foreach (array(
821 "force_gid" => "gidNumber",
822 "smbgroup" => "sambaGroupType") as $val => $aclname) {
823 if ($this->acl_is_writeable($aclname) && isset($_POST["$val"])){
824 $this->$val= $_POST["$val"];
825 }
826 }
828 /* Save sambaDomain attribute */
829 if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
830 $this->sambaDomainName= $_POST['sambaDomainName'];
831 $this->groupType= $_POST['groupType'];
832 }
834 /* Save fon attribute */
835 if ($this->acl_is_writeable("fonGroup")){
836 if (isset ($_POST['fon_group'])){
837 $this->fon_group= TRUE;
838 } else {
839 $this->fon_group= FALSE;
840 }
841 }
842 if ($this->acl_is_writeable("nagiosGroup")){
843 if (isset ($_POST['nagios_group'])){
844 $this->nagios_group= TRUE;
845 } else {
846 $this->nagios_group= FALSE;
847 }
848 }
849 }
851 /* Trust mode - special handling */
852 if($this->acl_is_writeable("trustModel")){
853 if (isset($_POST['trustmode'])){
854 $saved= $this->trustModel;
855 if ($_POST['trustmode'] == "1"){
856 $this->trustModel= "fullaccess";
857 } elseif ($_POST['trustmode'] == "2"){
858 $this->trustModel= "byhost";
859 } else {
860 $this->trustModel= "";
861 }
862 if ($this->trustModel != $saved){
863 $this->is_modified= TRUE;
864 }
865 }
866 }
868 }
871 /* Save to LDAP */
872 function save()
873 {
875 /* ID handling */
876 if ($this->force_gid == 0){
877 if ($this->saved_gidNumber != ""){
878 $this->gidNumber= $this->saved_gidNumber;
879 } else {
880 /* Calculate new, lock uids */
881 $wait= 10;
882 while (get_lock("uidnumber") != ""){
883 sleep (1);
885 /* timed out? */
886 if ($wait-- == 0){
887 break;
888 }
889 }
890 add_lock ("uidnumber", "gosa");
891 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
892 }
893 }
895 plugin::save();
897 /* Trust accounts */
898 $objectclasses= array();
899 foreach ($this->attrs['objectClass'] as $key => $class){
900 if (preg_match('/trustAccount/i', $class)){
901 continue;
902 }
903 $objectclasses[]= $this->attrs['objectClass'][$key];
904 }
905 $this->attrs['objectClass']= $objectclasses;
906 if ($this->trustModel != ""){
907 $this->attrs['objectClass'][]= "trustAccount";
908 $this->attrs['trustModel']= $this->trustModel;
909 $this->attrs['accessTo']= array();
910 if ($this->trustModel == "byhost"){
911 foreach ($this->accessTo as $host){
912 $this->attrs['accessTo'][]= $host;
913 }
914 }
915 } else {
916 if ($this->was_trust_account){
917 $this->attrs['accessTo']= array();
918 $this->attrs['trustModel']= array();
919 }
920 }
924 /* Remove objectClass for samba/phone support */
925 $tmp= array();
926 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
927 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
928 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
929 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
930 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
931 $tmp[]= $this->attrs['objectClass'][$i];
932 }
933 }
934 $this->attrs['objectClass']= $tmp;
935 $ldap= $this->config->get_ldap_link();
937 /* Add samba group functionality */
938 if ($this->samba3 && $this->smbgroup){
940 /* Fixed undefined index ...
941 */
942 $this->SID = $this->ridBase = "";
943 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
944 $this->SID = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
945 }else{
946 msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
947 }
948 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
949 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
950 }else{
951 msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
952 }
954 $this->attrs['objectClass'][]= 'sambaGroupMapping';
955 $this->attrs['sambaGroupType']= "2";
957 /* Check if we need to create a special entry */
958 if ($this->groupType == 0){
960 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
961 $gidNumber= $this->gidNumber;
962 while(TRUE){
963 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
964 $ldap->cd($this->config->current['BASE']);
965 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
966 if ($ldap->count() == 0){
967 break;
968 }
969 $gidNumber++;
970 }
971 $this->attrs['sambaSID']= $sid;
972 $this->sambaSID= $sid;
973 }
975 } else {
976 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
977 }
979 /* User wants me to fake the idMappings? This is useful for
980 making winbind resolve the group names in a reasonable amount
981 of time in combination with larger databases. */
982 if (isset($this->config->current['SAMBAIDMAPPING']) &&
983 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
984 $this->attrs['objectClass'][]= "sambaIdmapEntry";
985 }
987 }
989 /* Add phone functionality */
990 if ($this->fon_group){
991 $this->attrs['objectClass'][]= "goFonPickupGroup";
992 }
994 /* Add nagios functionality */
995 if ($this->nagios_group){
996 $this->attrs['objectClass'][]= "nagiosContactGroup";
997 }
999 /* Take members array */
1000 if (count ($this->memberUid)){
1001 $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
1002 }
1004 /* New accounts need proper 'dn', propagate it to remaining objects */
1005 if ($this->dn == 'new'){
1006 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
1007 }
1009 /* Add member dn's for RFC2307bis Support */
1010 if ($this->rfc2307bis){
1011 $this->attrs['member'] = array();
1012 if (count($this->memberUid)){
1013 foreach($this->attrs['memberUid'] as $uid) {
1014 $this->attrs['member'][]= $this->dnMapping[$uid];
1015 }
1016 } else {
1017 $this->attrs['member'][]= $this->dn;
1018 }
1019 }
1021 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1022 new entries. So do a check first... */
1023 $ldap->cat ($this->dn, array('dn'));
1024 if ($ldap->fetch()){
1025 /* Modify needs array() to remove values :-( */
1026 if (!count ($this->memberUid)){
1027 $this->attrs['memberUid']= array();
1028 }
1029 if ($this->samba3){
1030 if (!$this->smbgroup){
1031 $this->attrs['sambaGroupType']= array();
1032 $this->attrs['sambaSID']= array();
1033 }
1034 }
1035 $mode= "modify";
1036 } else {
1037 $mode= "add";
1038 $ldap->cd($this->config->current['BASE']);
1039 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1040 }
1042 /* Write back to ldap */
1043 $ldap->cd($this->dn);
1044 $this->cleanup();
1045 $ldap->$mode($this->attrs);
1047 /* Remove ACL dependencies too,
1048 */
1049 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1050 $tmp = new acl($this->config,$this->parent,$this->dn);
1051 $tmp->update_acl_membership($this->orig_dn,$this->dn);
1052 }
1054 if($this->initially_was_account){
1055 new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1056 }else{
1057 new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1058 }
1060 $ret= 0;
1061 if (!$ldap->success()){
1062 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1063 $ret= 1;
1064 }
1066 /* Remove uid lock */
1067 del_lock ("uidnumber");
1069 /* Post that we've done*/
1070 $this->handle_post_events($mode);
1072 return ($ret);
1073 }
1075 function check()
1076 {
1077 /* Call common method to give check the hook */
1078 $message= plugin::check();
1080 /* Permissions for that base? */
1081 if ($this->base != ""){
1082 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
1083 } else {
1084 $new_dn= $this->dn;
1085 }
1087 /* must: cn */
1088 if ($this->cn == "" && $this->acl_is_writeable("cn")){
1089 $message[]= msgPool::required(_("Name"));
1090 }
1092 /* Check for valid input */
1093 if (!tests::is_uid($this->cn)){
1094 if (strict_uid_mode()){
1095 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
1096 } else {
1097 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/i");
1098 }
1099 }
1101 if($this->allowGroupsWithSameNameInOtherSubtrees == true){
1103 /* Check for used 'cn' */
1104 $ldap= $this->config->get_ldap_link();
1105 if(($this->cn != $this->orig_cn) || ($this->orig_dn == "new")){
1106 $ldap->cd(get_groups_ou().$this->base);
1107 $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
1108 if ($ldap->count() != 0){
1109 $message[]= msgPool::duplicated(_("Name"));
1110 }
1111 }
1113 }else{
1115 /* Check for used 'cn' */
1116 $ldap= $this->config->get_ldap_link();
1117 $ldap->cd($this->config->current['BASE']);
1118 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
1119 if ($ldap->count() != 0){
1121 /* New entry? */
1122 if ($this->dn == 'new'){
1123 $message[]= msgPool::duplicated(_("Name"));
1124 }
1126 /* Moved? */
1127 elseif ($new_dn != $this->orig_dn){
1128 $ldap->fetch();
1129 if ($ldap->getDN() != $this->orig_dn){
1130 $message[]= msgPool::duplicated(_("Name"));
1131 }
1132 }
1133 }
1134 }
1136 /* Check ID */
1137 if ($this->force_gid == "1"){
1138 if (!tests::is_id($this->gidNumber)){
1139 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1140 } else {
1141 if ($this->gidNumber < $this->config->current['MINID']){
1142 $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1143 }
1145 }
1146 }
1148 /* Check if we are allowed to create or move this object
1149 */
1150 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1151 $message[] = msgPool::permCreate();
1152 }elseif($this->orig_dn != "new" && $this->orig_base && !$this->acl_is_moveable($this->base)){
1153 $message[] = msgPool::permMove();
1154 }
1156 return ($message);
1157 }
1159 function get_next_id($attrib, $dn)
1160 {
1161 $ids= array();
1162 $ldap= $this->config->get_ldap_link();
1164 $ldap->cd ($this->config->current['BASE']);
1165 if (preg_match('/gidNumber/i', $attrib)){
1166 $oc= "posixGroup";
1167 } else {
1168 $oc= "posixAccount";
1169 }
1170 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1172 /* Get list of ids */
1173 while ($attrs= $ldap->fetch()){
1174 $ids[]= (int)$attrs["$attrib"][0];
1175 }
1177 /* Find out next free id near to UID_BASE */
1178 if (!isset($this->config->current['BASE_HOOK'])){
1179 $base= $this->config->current['UIDBASE'];
1180 } else {
1181 /* Call base hook */
1182 $base= get_base_from_hook($dn, $attrib);
1183 }
1184 for ($id= $base; $id++; $id < pow(2,32)){
1185 if (!in_array($id, $ids)){
1186 return ($id);
1187 }
1188 }
1190 /* Check if id reached maximum */
1191 if ($id >= pow(2,32)){
1192 msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1193 exit;
1194 }
1195 }
1197 function getCopyDialog()
1198 {
1199 $vars = array("cn");
1201 if($this ->force_gid){
1202 $used = " checked ";
1203 $dis = "";
1204 }else{
1205 $used = "";
1206 $dis = " disabled ";
1207 }
1209 $smarty = get_smarty();
1210 $smarty->assign("used",$used);
1211 $smarty->assign("dis" ,$dis);
1212 $smarty->assign("cn" ,$this->cn);
1213 $smarty->assign("gidNumber",$this->gidNumber);
1214 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1215 $ret = array();
1216 $ret['string'] = $str;
1217 $ret['status'] = "";
1218 return($ret);
1219 }
1221 function saveCopyDialog()
1222 {
1223 if(isset($_POST['cn'])){
1224 $this->cn = $_POST['cn'];
1225 }
1226 if(isset($_POST['force_gid'])){
1227 $this->force_gid = 1;
1228 $this->gidNumber= $_POST['gidNumber'];
1229 }else{
1230 $this->force_gid = 0;
1231 $this->gidNumber = false;
1232 }
1233 }
1236 /* Return plugin informations for acl handling */
1237 static function plInfo()
1238 {
1239 return (array(
1240 "plShortName" => _("Generic"),
1241 "plDescription" => _("Generic group settings"),
1242 "plSelfModify" => FALSE,
1243 "plDepends" => array(),
1244 "plPriority" => 0,
1245 "plSection" => array("admin"),
1246 "plCategory" => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1248 "plProvidedAcls" => array(
1249 "cn" => _("Name"),
1250 "description" => _("Description"),
1251 "base" => _("Base"),
1253 "gidNumber" => _("GID"),
1255 "sambaGroupType" => _("Samba group type"),
1256 "sambaDomainName" => _("Samba domain name"),
1257 "trustModel" => _("System trust"),
1258 "fonGroup" => _("Phone pickup group"),
1259 "nagiosGroup" => _("Nagios group"),
1261 "memberUid" => _("Group member"))
1262 ));
1263 }
1266 function multiple_save_object()
1267 {
1268 if(isset($_POST['group_mulitple_edit'])){
1270 /* Create a base backup and reset the
1271 base directly after calling plugin::save_object();
1272 Base will be set seperatly a few lines below */
1273 $base_tmp = $this->base;
1274 plugin::multiple_save_object();
1275 plugin::save_object();
1276 $this->base = $base_tmp;
1278 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1279 if(isset($_POST['use_'.$attr])){
1280 $this->multi_boxes[] = $attr;
1281 }
1282 }
1284 /* Get base selection */
1285 if(isset($_POST['base'])){
1286 $tmp = $this->get_allowed_bases();
1287 if(isset($tmp[$_POST['base']])){
1288 $this->base = $_POST['base'];
1289 }
1290 }
1292 foreach (array( "smbgroup" => "sambaGroupType") as $val => $aclname) {
1293 if ($this->acl_is_writeable($aclname)){
1294 if(isset($_POST["$val"])){
1295 $this->$val= TRUE;
1296 }else{
1297 $this->$val= FALSE;
1298 }
1299 }
1300 }
1302 /* Save sambaDomain attribute */
1303 if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
1304 $this->sambaDomainName= $_POST['sambaDomainName'];
1305 $this->groupType= $_POST['groupType'];
1306 }
1308 /* Save fon attribute */
1309 if ($this->acl_is_writeable("fonGroup")){
1310 if (isset ($_POST['fon_group'])){
1311 $this->fon_group= TRUE;
1312 } else {
1313 $this->fon_group= FALSE;
1314 }
1315 }
1316 }
1317 }
1320 function get_multi_edit_values()
1321 {
1322 $ret = plugin::get_multi_edit_values();
1324 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group") as $attr){
1325 if(in_array($attr,$this->multi_boxes)){
1326 $ret[$attr] = $this->$attr;
1327 }
1328 }
1329 $ret['memberUid'] = $this->memberUid;
1330 $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1331 return($ret);
1332 }
1334 function multiple_execute()
1335 {
1336 return($this->execute());
1337 }
1340 /* Initialize plugin with given atribute arrays
1341 */
1342 function init_multiple_support($attrs,$all)
1343 {
1344 plugin::init_multiple_support($attrs,$all);
1346 $this->memberUid = array();
1347 $this->memberUid_used_by_some = array();
1348 if (isset($attrs['memberUid'])){
1349 for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1350 $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1351 }
1352 ksort($this->memberUid);
1353 }
1355 if (isset($all['memberUid'])){
1356 for ($i= 0; $i<$all['memberUid']['count']; $i++){
1357 if(!in_array($all['memberUid'][$i],$this->memberUid)){
1358 $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1359 }
1360 }
1361 ksort($this->memberUid_used_by_some);
1362 }
1363 }
1366 function PrepareForCopyPaste($source)
1367 {
1368 plugin::PrepareForCopyPaste($source);
1370 $this->memberUid = array();
1371 if(isset($source['memberUid'])){
1372 for($i = 0 ; $i < $source['memberUid']['count']; $i ++){
1373 $this->memberUid[] = $source['memberUid'][$i];
1374 }
1375 }
1376 }
1379 function set_multi_edit_values($attrs)
1380 {
1381 $users = array();
1383 /* Update groupMembership, keep optinal group */
1384 foreach($attrs['memberUid_used_by_some'] as $uid){
1385 if(in_array($uid,$this->memberUid)){
1386 $users[$uid] = $uid;
1387 }
1388 }
1390 /* Update groupMembership, add forced groups */
1391 foreach($attrs['memberUid'] as $uid){
1392 $users[$uid] = $uid;
1393 }
1394 plugin::set_multi_edit_values($attrs);
1395 $this->memberUid = $users;
1396 }
1397 }
1398 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1399 ?>