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