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 /* attribute list for save action */
64 var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID");
65 var $objectclasses= array("top", "posixGroup");
67 var $CopyPasteVars = array("force_gid","fon_group","smbgroup","groupType","sambaSID","sambaDomainName","SID","nagios_group","sambaGroupType");
69 var $multiple_support = TRUE;
72 // Lists
73 private $memberList; // Single group editing
74 private $commonList; // (Multiple support active)
75 private $partialList; // (Multiple support active)
78 function group (&$config, $dn= NULL)
79 {
80 /* Set rfc2307bis flag */
81 if ($config->get_cfg_value("core","rfc2307bis") == "true"){
82 $this->rfc2307bis= TRUE;
83 $this->attributes[]= "member";
84 $this->objectclasses[]= "groupOfNames";
85 }
87 plugin::plugin ($config, $dn);
89 $this->trustModeDialog = new trustModeDialog($this->config, $this->dn,NULL);
90 $this->trustModeDialog->setAcl('groups/group');
92 /* Load attributes depending on the samba version */
93 $this->orig_dn= $dn;
94 $this->orig_cn= $this->cn;
96 /* Get member list */
97 if (isset($this->attrs['memberUid'][0])){
98 $tmp= array();
99 for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
100 $tmp[$this->attrs['memberUid'][$i]]= $this->attrs['memberUid'][$i];
101 }
102 $this->memberUid= $tmp;
103 ksort ($this->memberUid);
104 }
106 /* Save gidNumber for later use */
107 if (isset($this->attrs['gidNumber'])){
108 $this->saved_gidNumber= $this->attrs['gidNumber'][0];
109 }
111 /* Is a samba group? */
112 if (isset($this->attrs['objectClass'])){
113 if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == FALSE ){
114 $this->smbgroup= FALSE;
115 } else {
116 $this->smbgroup= TRUE;
117 if (isset($this->attrs['sambaSID'])){
118 $this->sambaSID= $this->attrs['sambaSID'][0];
119 }
120 }
121 if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == FALSE ){
122 $this->fon_group= FALSE;
123 } else {
124 $this->fon_group= TRUE;
125 }
126 if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == FALSE ){
127 $this->nagios_group= FALSE;
128 } else {
129 $this->nagios_group= TRUE;
130 }
131 }
133 /* Set mail flag */
134 if (isset($this->attrs['objectClass']) && in_array_strict('gosaMailAccount', $this->attrs['objectClass'])){
135 $this->has_mailAccount= TRUE;
136 }
138 /* Get samba Domain in case of samba 3 */
139 if ($this->sambaSID != ""){
140 $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
141 $ldap= $this->config->get_ldap_link();
142 $ldap->cd($this->config->current['BASE']);
143 $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
144 if ($ldap->count() != 0){
145 $attrs= $ldap->fetch();
146 if(isset($attrs['sambaAlgorithmicRidBase'])){
147 $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
148 } else {
149 $this->ridBase= $this->config->get_cfg_value("core","sambaRidBase");
150 }
152 /* Get domain name for SID */
153 $this->sambaDomainName= "DEFAULT";
154 foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
155 if ($val['SID'] == $this->SID){
156 $this->sambaDomainName= $key;
157 break;
158 }
159 }
160 } else {
161 if ($this->config->get_cfg_value("core","sambaRidBase") != ""){
162 $this->sambaDomainName= "DEFAULT";
163 $this->ridBase= $this->config->get_cfg_value("core","sambaRidBase");
164 $this->SID= $this->config->get_cfg_value("core","sambaSID");
165 } else {
166 msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
167 }
168 }
170 /* Get group type */
171 $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
172 if ($this->groupType < 500 || $this->groupType > 553){
173 $this->groupType= 0;
174 }
175 $this->oldgroupType= $this->groupType;
176 }
178 if ($this->dn == "new"){
179 if(session::is_set('CurrentMainBase')){
180 $this->base = session::get('CurrentMainBase');
181 }else{
182 $ui= get_userinfo();
183 $this->base= dn2base($ui->dn);
184 }
185 } else {
187 /* Get object base */
188 $this->base =preg_replace ("/^[^,]+,".preg_quote(get_groups_ou(), '/')."/i","",$this->dn);
189 }
190 $this->orig_base = $this->base;
192 /* This is always an account */
193 $this->is_account= TRUE;
195 /* Instanciate base selector */
196 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
197 $this->baseSelector->setSubmitButton(false);
198 $this->baseSelector->setHeight(300);
199 $this->baseSelector->update(true);
203 // Prepare lists
204 $this->memberList = new sortableListing();
205 $this->memberList->setDeleteable(true);
206 $this->memberList->setInstantDelete(true);
207 $this->memberList->setEditable(false);
208 $this->memberList->setWidth("100%");
209 $this->memberList->setHeight("300px");
210 $this->memberList->setHeader(array('~',_("Given name"),_("Surname"),_("UID")));
211 $this->memberList->setColspecs(array('20px','*','*','*','20px'));
212 $this->memberList->setDefaultSortColumn(1);
214 $this->reload(TRUE);
215 }
217 function execute()
218 {
219 /* Call parent execute */
220 plugin::execute();
222 /* Log view */
223 if($this->is_account && !$this->view_logged){
224 $this->view_logged = TRUE;
225 new log("view","groups/".get_class($this),$this->dn);
226 }
228 /* Do we represent a valid group? */
229 if (!$this->is_account && $this->parent === NULL){
230 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".msgPool::noValidExtension()."</b>";
231 return ($display);
232 }
234 // Act on list events
235 foreach(array('memberList','commonList','partialList') as $list){
237 // Check if list is available, depends on multi- or sinlge- group editing.
238 if($this->$list){
239 $this->$list->save_object();
240 $action = $this->$list->getAction();
241 if($action['action'] == 'delete' && preg_match("/w/",$this->getacl("memberUid"))){
242 foreach ($action['targets'] as $id){
243 $value = $this->$list->getKey($id);
244 unset ($this->members["$value"]);
245 $this->removeUser($value);
246 }
247 $this->reload();
248 }
249 }
250 }
252 /* Add objects? */
253 if (isset($_POST["edit_membership"]) && preg_match("/w/",$this->getacl("memberUid"))){
254 $this->userSelect= new userSelect($this->config, get_userinfo());
255 }
257 /* Add objects finished? */
258 if (isset($_POST["add_users_cancel"])){
259 $this->userSelect= NULL;
260 }
262 /* Add user to group */
263 if (isset($_POST['add_users_finish']) && $this->userSelect){
265 $users = $this->userSelect->detectPostActions();
266 if(isset($users['targets'])){
267 $headpage = $this->userSelect->getHeadpage();
268 foreach($users['targets'] as $dn){
269 $attrs = $headpage->getEntry($dn);
270 $value = $attrs['uid'][0];
271 $this->addUser($value);
272 $this->members["$value"]= $this->allusers[$value];
273 $this->reload();
274 }
275 }
276 $this->userSelect= NULL;
277 }
279 $smarty= get_smarty();
281 // Handle trust mode dialog
282 $this->dialog = FALSE;
283 $trustModeDialog = $this->trustModeDialog->execute();
284 if($this->trustModeDialog->trustSelect){
285 $this->dialog = TRUE;
286 return($trustModeDialog);
287 }
288 $smarty->assign("trustModeDialog" , $trustModeDialog);
292 $smarty->assign("nagios", $this->config->pluginEnabled("nagiosAccount") && class_available("nagiosAccount"));
293 $smarty->assign("pickupGroup", $this->config->pluginEnabled("phoneAccount") && class_available("phoneAccount"));
295 /* Manage object add dialog */
296 if ($this->userSelect){
297 $this->dialog = TRUE;
298 return($this->userSelect->execute());
299 }
301 /* Create base acls */
302 $smarty->assign("base", $this->baseSelector->render());
304 $domains= array();
305 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
306 $domains[$name]= $name;
307 }
308 $smarty->assign("sambaDomains", set_post($domains));
309 $smarty->assign("sambaDomainName", set_post($this->sambaDomainName));
310 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain administrators"), 513 => _("Domain users"),
311 514 => _("Domain guests"));
313 /* Don't loose special groups! If not key'ed above, add it to
314 the combo box... */
315 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
316 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
317 }
319 $smarty->assign("groupTypes", set_post($groupTypes));
320 $smarty->assign("groupType", set_post($this->groupType));
323 /* Members and users */
324 if(!$this->multiple_support_active){
325 $this->memberList->setAcl($this->getacl("memberUid"));
326 $data = $lData = array();
327 foreach($this->members as $uid => $member){
328 $data[$uid] = $member;
329 $givenName = $sn = _("Unknown");
330 if(isset($member['sn'][0])) $sn = $member['sn'][0];
331 if(isset($member['givenName'][0])) $givenName = $member['givenName'][0];
332 $image = image('images/false.png');
333 if(isset($member['sn'])){
334 $image = image('plugins/users/images/select_user.png');
335 }
336 $lData[$uid] = array('data' => array($image,$sn, $givenName, $uid));
337 }
338 $this->memberList->setListData($data,$lData);
339 $this->memberList->update();
340 $smarty->assign("memberList", $this->memberList->render());
341 }else{
343 $this->commonList->setAcl($this->getacl("memberUid"));
344 $this->partialList->setAcl($this->getacl("memberUid"));
346 $data = $lData = array();
347 foreach($this->memberUid as $uid => $member){
348 $member = $this->members[$member];
349 $data[$uid] = $member;
350 $givenName = $sn = _("Unknown");
351 if(isset($member['sn'][0])) $sn = $member['sn'][0];
352 if(isset($member['givenName'][0])) $givenName = $member['givenName'][0];
353 $image = image('images/false.png');
354 if(isset($member['sn'])){
355 $image = image('plugins/users/images/select_user.png');
356 }
357 $lData[$uid] = array('data' => array($image,$sn, $givenName, $uid));
358 }
359 $this->commonList->setListData($data,$lData);
360 $this->commonList->update();
361 $smarty->assign("commonList", $this->commonList->render());
363 $data = $lData = array();
364 foreach($this->memberUid_used_by_some as $uid => $member){
365 $member = $this->members[$member];
366 $data[$uid] = $member;
367 $givenName = $sn = _("Unknown");
368 if(isset($member['sn'][0])) $sn = $member['sn'][0];
369 if(isset($member['givenName'][0])) $givenName = $member['givenName'][0];
370 $image = image('images/false.png');
371 if(isset($member['sn'])){
372 $image = image('plugins/users/images/select_user.png');
373 }
374 $lData[$uid] = array('data' => array($image,$sn, $givenName, $uid));
375 }
376 $this->partialList->setListData($data,$lData);
377 $this->partialList->update();
378 $smarty->assign("partialList", $this->partialList->render());
379 }
381 /* Checkboxes */
382 foreach (array("force_gid", "smbgroup") as $val){
383 if ($this->$val == "1"){
384 $smarty->assign("$val", "checked");
385 } else {
386 $smarty->assign("$val", "");
387 }
388 }
389 if ($this->force_gid != "1"){
390 $smarty->assign("forceMode", "disabled");
391 }else{
392 $smarty->assign("forceMode", "");
393 }
394 if ($this->fon_group){
395 $smarty->assign("fon_group", "checked");
396 } else {
397 $smarty->assign("fon_group", "");
398 }
400 if ($this->nagios_group){
401 $smarty->assign("nagios_group", "checked");
402 } else {
403 $smarty->assign("nagios_group", "");
404 }
406 /* Fields */
407 foreach (array("cn", "description", "gidNumber") as $val){
408 $smarty->assign("$val", set_post($this->$val));
409 }
411 $tmp = $this->plInfo();
412 foreach($tmp['plProvidedAcls'] as $name => $translation){
413 $smarty->assign($name."ACL",$this->getacl($name));
414 }
416 if($this->acl_is_writeable("base")){
417 $smarty->assign("baseSelect",true);
418 }else{
419 $smarty->assign("baseSelect",false);
420 }
422 /* Multiple edit handling */
423 $smarty->assign("multiple_support",$this->multiple_support_active);
425 foreach($this->attributes as $val){
426 if(in_array_strict($val,$this->multi_boxes)){
427 $smarty->assign("use_".$val,TRUE);
428 }else{
429 $smarty->assign("use_".$val,FALSE);
430 }
431 }
432 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group") as $val){
433 if(in_array_strict($val,$this->multi_boxes)){
434 $smarty->assign("use_".$val,TRUE);
435 }else{
436 $smarty->assign("use_".$val,FALSE);
437 }
438 }
440 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
441 }
443 function addUser($uid)
444 {
445 /* In mutliple edit we have to handle two arrays.
446 * memberUid : Containing users used in all groups
447 * memberUid_used_by_some : Those which are not used in all groups
448 * So we have to remove the given $uid from the ..used_by_some array first.
449 */
450 if($this->multiple_support_active){
451 if(isset($this->memberUid_used_by_some[$uid])){
452 unset($this->memberUid_used_by_some[$uid]);
453 }
454 }
456 /* Ensure that the requested object is known to the group class
457 */
458 if(!isset($this->dnMapping[$uid])){
459 $ldap = $this->config->get_ldap_link();
460 $ldap->cd($this->config->current['BASE']);
461 $ldap->search("(&(objectClass=gosaAccount)(uid=".$uid."))",array("dn", "uid","sn","givenName"));
462 if($ldap->count() == 0 ){
463 msg_dialog::display(_("Error"),
464 sprintf(_("Adding UID '%s' to group '%s' failed: cannot find user object!"),
465 $uid,$this->cn),
466 ERROR_DIALOG);
467 return;
468 }elseif($ldap->count() >= 2){
469 msg_dialog::display(_("Error"),
470 sprintf(_("Add UID '%s' to group '%s' failed: UID is used more than once!"),
471 $uid,$this->cn),
472 ERROR_DIALOG);
473 return;
474 }else{
475 while($attrs = $ldap->fetch()){
476 $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
477 $this->members[$attrs['uid'][0]] = $attrs;
478 $this->allusers[$attrs['uid'][0]]= $attrs;
479 }
480 }
481 }
483 $this->memberUid[$uid]= $uid;
484 $this->reload();
485 }
488 function removeUser($uid)
489 {
490 $temp= array();
491 if(isset($this->memberUid[$uid])){
492 unset($this->memberUid[$uid]);
493 }
495 /* We have two array contianing group members in multiple edit.
496 * this->memberUid : Groups used by all currently edited groups
497 * this->memberUid_used_by_some: Used by some
498 * So we have to remove the specified uid from both arrays.
499 */
500 if($this->multiple_support_active){
501 if(isset($this->memberUid_used_by_some[$uid])){
502 unset($this->memberUid_used_by_some[$uid]);
503 }
504 }
505 }
507 /* Reload data */
508 function reload($silent = FALSE)
509 {
510 /* Prepare ldap link */
511 $ldap= $this->config->get_ldap_link();
512 $ldap->cd($this->config->current['BASE']);
515 /* Resolve still unresolved memberuids to fill the list with sn/giveName attributes
516 (Store gathered sn/givenName informations in $this->allusers too,
517 to be prepared when adding/deleting users)
518 */
519 $filter = "";
523 // Merge in partial uids in multiple edit
524 $allUids = array_keys($this->memberUid);
525 if($this->multiple_support_active) {
526 $allUids = array_merge($allUids, array_keys($this->memberUid_used_by_some));
527 }
529 // Do not request data for users we've already fetched before.
530 foreach($allUids as $key => $uid){
531 if(isset($this->dnMapping[$uid])) unset($allUids[$key]);
532 }
533 $allUids = array_values($allUids);
535 // To resolve the usernames out of the 'uid' we've to perform ldap queries.
536 // To keep the amount of queries as short as possible, we combine the query
537 // for 'sn','givenName','..' for serveral users in one single query:
538 // (|(uid=hans)(uid=peter)(uid=hubert)(..))
539 //
540 // Unfortunately there is a filter length limit which causes the query to be invalid,
541 // we've to split these huge query again into shorter query strings.
542 //
543 // maxPerRound specifies the amount of queries we can combine into a
544 // single query.
545 $maxPerRound = $this->config->get_cfg_value("core","ldapFilterNestingLimit");
546 if( $maxPerRound == "" ) {
547 $maxPerRound = count($allUids);
548 }
550 for ( $added = 0; $added < count($allUids); $added += $maxPerRound ) {
552 // First build the query....
553 $start = $added;
554 $end = $added + $maxPerRound;
555 $filter = "";
556 for ( $done = $start; $done < $end; $done++ ) {
557 if(!isset($allUids[$done])) break;
558 $value = $allUids[$done];
559 $filter .= "(uid=".normalizeLdap($value).")";
560 }
562 // Retrieve the data to LDAP
563 $ldap->cd($this->config->current['BASE']);
564 $ldap->search("(&(objectClass=gosaAccount)(|".$filter."))",array("dn", "uid","sn","givenName"));
565 while( $attrs = $ldap->fetch() ) {
566 $this->dnMapping[$attrs['uid'][0]] = $attrs['dn'];
567 $this->members[$attrs['uid'][0]] = $attrs;
568 $this->allusers[$attrs['uid'][0]]= $attrs;
569 }
570 }
572 /* check if all uids are resolved */
573 foreach ($this->memberUid as $value){
574 if(!isset($this->members[$value])){
575 $this->members[$value] = "";
576 }
577 }
578 }
581 /* Create display name, this was used so often that it is excluded into a seperate function */
582 function createResultName($attrs)
583 {
584 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
585 $ret = $attrs["sn"][0].", ".$attrs["givenName"][0]." [".$attrs["uid"][0]."]";
586 } else {
587 $ret= $attrs['uid'][0];
588 }
589 return($ret);
590 }
593 function remove_from_parent()
594 {
595 plugin::remove_from_parent();
597 $ldap= $this->config->get_ldap_link();
598 $ldap->rmdir($this->dn);
599 if (!$ldap->success()){
600 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
601 }
603 new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
605 /* Delete references to object groups */
606 $ldap->cd ($this->config->current['BASE']);
607 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
608 while ($ldap->fetch()){
609 $og= new ogroup($this->config, $ldap->getDN());
610 unset($og->member[$this->dn]);
611 $og->save ();
612 }
614 /* Remove ACL dependencies too,
615 */
616 $ldap = $this->config->get_ldap_link();
617 $ldap->cd($this->config->current['BASE']);
618 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($this->dn)."*))",array("gosaAclEntry","dn"));
619 while($attrs = $ldap->fetch()){
620 $acl = new acl($this->config,$this->parent,$attrs['dn']);
621 foreach($acl->gosaAclEntry as $id => $entry){
622 foreach($entry['members'] as $m_id => $member){
623 if($m_id == "G:".$this->dn || $m_id == "U:".$this->dn){
624 unset($acl->gosaAclEntry[$id]['members'][$m_id]);
625 gosa_log("modify","groups/acl",$attrs['dn'],array(),sprintf("Removed acl for %s on object %s.",$this->dn,$attrs['dn']));
626 }
627 }
628 }
629 $acl->save();
630 }
632 /* Remove ACL dependencies, too */
633 acl::remove_acl_for($this->dn);
635 /* Send signal to the world that we've done */
636 $this->handle_post_events("remove");
637 }
640 /* Save data to object */
641 function save_object()
642 {
643 /* Save additional values for possible next step */
644 if (isset($_POST['groupedit'])){
646 /* Create a base backup and reset the
647 base directly after calling plugin::save_object();
648 Base will be set seperatly a few lines below */
649 $base_tmp = $this->base;
650 plugin::save_object();
651 $this->trustModeDialog->save_object();
652 $this->base = $base_tmp;
654 /* Refresh base */
655 if ($this->acl_is_moveable($this->base)){
656 if (!$this->baseSelector->update()) {
657 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
658 }
659 if ($this->base != $this->baseSelector->getBase()) {
660 $this->base= $this->baseSelector->getBase();
661 $this->is_modified= TRUE;
662 }
663 }
665 $this->force_gid= 0;
667 /* Only reset sambagroup flag if we are able to write this flag */
668 if($this->acl_is_writeable("sambaGroupType")){
669 $this->smbgroup = 0;
670 }
672 foreach (array(
673 "force_gid" => "gidNumber",
674 "smbgroup" => "sambaGroupType") as $val => $aclname) {
675 if ($this->acl_is_writeable($aclname) && isset($_POST["$val"])){
676 $this->$val= get_post($val);
677 }
678 }
680 /* Save sambaDomain attribute */
681 if ($this->acl_is_writeable("sambaDomainName") && isset ($_POST['sambaDomainName'])){
682 $this->sambaDomainName= get_post('sambaDomainName');
683 $this->groupType= get_post('groupType');
684 }
686 /* Save fon attribute */
687 if ($this->acl_is_writeable("fonGroup")){
688 if (isset ($_POST['fon_group'])){
689 $this->fon_group= TRUE;
690 } else {
691 $this->fon_group= FALSE;
692 }
693 }
694 if ($this->acl_is_writeable("nagiosGroup")){
695 if (isset ($_POST['nagios_group'])){
696 $this->nagios_group= TRUE;
697 } else {
698 $this->nagios_group= FALSE;
699 }
700 }
701 }
702 }
705 /* Save to LDAP */
706 function save()
707 {
709 /* ID handling */
710 if ($this->force_gid == 0){
711 if ($this->saved_gidNumber != ""){
712 $this->gidNumber= $this->saved_gidNumber;
713 } else {
714 /* Calculate new, lock uids */
715 $wait= 10;
716 while (get_lock("gidnumber") != ""){
717 sleep (1);
719 /* timed out? */
720 if ($wait-- == 0){
721 break;
722 }
723 }
724 add_lock ("gidnumber", "gosa");
725 $this->gidNumber= get_next_id("gidNumber", $this->dn);
726 }
727 }
729 plugin::save();
731 /* Remove objectClass for samba/phone support */
732 $tmp= array();
733 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
734 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
735 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
736 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
737 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
738 $tmp[]= $this->attrs['objectClass'][$i];
739 }
740 }
741 $this->attrs['objectClass']= $tmp;
742 $ldap= $this->config->get_ldap_link();
744 /* Add samba group functionality */
745 if ($this->smbgroup){
747 /* Fixed undefined index ...
748 */
749 $this->SID = $this->ridBase = "";
750 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
751 $this->SID = $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
752 }else{
753 msg_dialog::display(_("Error"), sprintf(_("Cannot find any SID for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
754 }
755 if(isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'])){
756 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
757 }else{
758 msg_dialog::display(_("Error"), sprintf(_("Cannot find any RIDBASE for '%s'!"), $this->sambaDomainName), ERROR_DIALOG);
759 }
761 $this->attrs['objectClass'][]= 'sambaGroupMapping';
762 $this->attrs['sambaGroupType']= "2";
764 /* Check if we need to create a special entry */
765 if ($this->groupType == 0){
767 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
768 $sid = $this->getSambaSID();
769 $this->attrs['sambaSID']= $sid;
770 $this->sambaSID= $sid;
771 }
773 } else {
774 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
775 }
777 /* User wants me to fake the idMappings? This is useful for
778 making winbind resolve the group names in a reasonable amount
779 of time in combination with larger databases. */
780 if ($this->config->boolValueIsTrue("core","sambaIdMapping")){
781 $this->attrs['objectClass'][]= "sambaIdmapEntry";
782 }
784 }
786 /* Add phone functionality */
787 if ($this->fon_group){
788 $this->attrs['objectClass'][]= "goFonPickupGroup";
789 }
791 /* Add nagios functionality */
792 if ($this->nagios_group){
793 $this->attrs['objectClass'][]= "nagiosContactGroup";
794 }
796 /* Take members array */
797 if (count ($this->memberUid)){
798 $this->attrs['memberUid']= array_values(array_unique($this->memberUid));
799 }
801 /* New accounts need proper 'dn', propagate it to remaining objects */
802 if ($this->dn == 'new'){
803 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
804 }
806 /* Add member dn's for RFC2307bis Support */
807 if ($this->rfc2307bis){
808 $this->attrs['member'] = array();
809 if (count($this->memberUid)){
810 foreach($this->attrs['memberUid'] as $uid) {
812 if(isset($this->dnMapping[$uid])){
813 $this->attrs['member'][]= $this->dnMapping[$uid];
814 }
815 }
816 } else {
817 $this->attrs['member'][]= $this->dn;
818 }
819 }
821 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
822 new entries. So do a check first... */
823 $ldap->cat ($this->dn, array('dn'));
824 if ($ldap->fetch()){
825 /* Modify needs array() to remove values :-( */
826 if (!count ($this->memberUid)){
827 $this->attrs['memberUid']= array();
828 }
829 if (!$this->smbgroup){
830 $this->attrs['sambaGroupType']= array();
831 $this->attrs['sambaSID']= array();
832 }
834 $mode= "modify";
835 } else {
836 $mode= "add";
837 $ldap->cd($this->config->current['BASE']);
838 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
839 }
841 /* Check generated gidNumber, it may be used by another group.
842 */
843 if($this->gidNumber != ""){
844 $ldap->cd($this->config->current['BASE']);
845 $ldap->search("(&(!(cn=".$this->orig_cn."))(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))",array("cn"));
846 if($ldap->count()){
847 $cns = "";
848 while($attrs = $ldap->fetch()){
849 $cns .= $attrs['cn'][0].", ";
850 }
851 $cns = rtrim($cns,", ");
852 msg_dialog::display(_("Warning"),sprintf(_("The gidNumber '%s' is already in use by %s!"),$this->gidNumber,$cns) , WARNING_DIALOG );
853 }
854 }
856 /* Write back to ldap */
857 $ldap->cd($this->dn);
858 $this->cleanup();
859 $ldap->$mode($this->attrs);
861 /* Remove ACL dependencies too,
862 */
863 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
864 $tmp = new acl($this->config,$this->parent,$this->dn);
865 $tmp->update_acl_membership($this->orig_dn,$this->dn);
866 }
868 if($this->initially_was_account){
869 new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
870 }else{
871 new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
872 }
874 $ret= 0;
875 if (!$ldap->success()){
876 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
877 $ret= 1;
878 }
880 $this->trustModeDialog->dn = $this->dn;
881 $this->trustModeDialog->save();
883 /* Remove uid lock */
884 del_lock ("gidnumber");
886 /* Post that we've done*/
887 $this->handle_post_events($mode);
889 return ($ret);
890 }
892 function check()
893 {
894 /* Call common method to give check the hook */
895 $message= plugin::check();
897 /* Permissions for that base? */
898 if ($this->base != ""){
899 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
900 } else {
901 $new_dn= $this->dn;
902 }
904 /* must: cn */
905 if ($this->cn == "" && $this->acl_is_writeable("cn")){
906 $message[]= msgPool::required(_("Name"));
907 }
909 // Check if a wrong base was supplied
910 if(!$this->baseSelector->checkLastBaseUpdate()){
911 $message[]= msgPool::check_base();;
912 }
914 /* Check for valid input */
915 if (!tests::is_uid($this->cn)){
916 if (strict_uid_mode()){
917 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9_-]/");
918 } else {
919 $message[]= msgPool::invalid(_("Name"), $this->cn, "/[a-z0-9 _.-]/i");
920 }
921 }
923 // Check if a wrong base was supplied
924 if(!$this->baseSelector->checkLastBaseUpdate()){
925 $message[]= msgPool::check_base();;
926 }
928 if($this->allowGroupsWithSameNameInOtherSubtrees == true){
930 /* Check for used 'cn' */
931 $ldap= $this->config->get_ldap_link();
932 if(($this->cn != $this->orig_cn) || ($this->orig_dn == "new")){
933 $ldap->cd(get_groups_ou().$this->base);
934 $ldap->ls("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",get_groups_ou().$this->base,array("cn"));
935 if ($ldap->count() != 0){
936 $message[]= msgPool::duplicated(_("Name"));
937 }
938 }
940 }else{
942 /* Check for used 'cn' */
943 $ldap= $this->config->get_ldap_link();
944 $ldap->cd($this->config->current['BASE']);
945 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
946 if ($ldap->count() != 0){
948 /* New entry? */
949 if ($this->dn == 'new'){
950 $message[]= msgPool::duplicated(_("Name"));
951 }
953 /* Moved? */
954 elseif ($new_dn != $this->orig_dn){
955 $ldap->fetch();
956 if ($ldap->getDN() != $this->orig_dn){
957 $message[]= msgPool::duplicated(_("Name"));
958 }
959 }
960 }
961 }
963 /* Check ID */
964 if ($this->force_gid == "1"){
965 if (!tests::is_id($this->gidNumber)){
966 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
967 } else {
968 if ($this->gidNumber < $this->config->get_cfg_value("core","minId")){
969 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("core","minId"));
970 }
972 }
973 }
975 /* Check if we are allowed to create or move this object
976 */
977 if(!$this->orig_dn == "new" ||
978 $this->orig_base != $this->base ||
979 $this->cn != $this->orig_cn){
981 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
982 $message[] = msgPool::permCreate();
983 }elseif($this->orig_dn != "new" && !$this->acl_is_moveable($this->base)){
984 $message[] = msgPool::permMove();
985 }
986 }
988 return ($message);
989 }
992 function getCopyDialog()
993 {
994 $vars = array("cn");
996 if($this ->force_gid){
997 $used = " checked ";
998 $dis = "";
999 }else{
1000 $used = "";
1001 $dis = " disabled ";
1002 }
1004 $smarty = get_smarty();
1005 $smarty->assign("used", set_post($used));
1006 $smarty->assign("dis" , set_post($dis));
1007 $smarty->assign("cn" , set_post($this->cn));
1008 $smarty->assign("gidNumber",set_post($this->gidNumber));
1009 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
1010 $ret = array();
1011 $ret['string'] = $str;
1012 $ret['status'] = "";
1013 return($ret);
1014 }
1016 function saveCopyDialog()
1017 {
1018 if(isset($_POST['cn'])){
1019 $this->cn = get_post('cn');
1020 }
1021 if(isset($_POST['force_gid'])){
1022 $this->force_gid = 1;
1023 $this->gidNumber= get_post('gidNumber');
1024 }else{
1025 $this->force_gid = 0;
1026 $this->gidNumber = false;
1027 }
1028 }
1031 /* Return plugin informations for acl handling */
1032 static function plInfo()
1033 {
1034 return (array(
1035 "plShortName" => _("Generic"),
1036 "plDescription" => _("Generic group settings"),
1037 "plSelfModify" => FALSE,
1038 "plDepends" => array(),
1039 "plPriority" => 0,
1040 "plSection" => array("administration"),
1041 "plCategory" => array("groups" => array("objectClass" => "posixGroup", "description" => _("Groups"))),
1043 "plProperties" =>
1044 array(
1045 array(
1046 "name" => "ogroupRDN",
1047 "type" => "rdn",
1048 "default" => "ou=groups,",
1049 "description" => _("RDN for object group storage."),
1050 "check" => "gosaProperty::isRdn",
1051 "migrate" => "migrate_ogroupRDN",
1052 "group" => "plugin",
1053 "mandatory" => FALSE)),
1056 "plProvidedAcls" => array(
1057 "cn" => _("Name"),
1058 "description" => _("Description"),
1059 "base" => _("Base"),
1061 "gidNumber" => _("GID"),
1063 "sambaGroupType" => _("Samba group type"),
1064 "sambaDomainName" => _("Samba domain name"),
1065 "accessTo" => _("System trust"),
1066 "fonGroup" => _("Phone pickup group"),
1067 "nagiosGroup" => _("Nagios group"),
1069 "memberUid" => _("Group member"))
1070 ));
1071 }
1074 function multiple_save_object()
1075 {
1076 if(isset($_POST['group_mulitple_edit'])){
1078 /* Create a base backup and reset the
1079 base directly after calling plugin::save_object();
1080 Base will be set seperatly a few lines below */
1081 $base_tmp = $this->base;
1082 plugin::multiple_save_object();
1083 plugin::save_object();
1084 $this->trustModeDialog->multiple_save_object();
1085 $this->base = $base_tmp;
1087 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group") as $attr){
1088 if(isset($_POST['use_'.$attr])){
1089 $this->multi_boxes[] = $attr;
1090 }
1091 }
1093 /* Refresh base */
1094 if ($this->acl_is_moveable($this->base)){
1095 if (!$this->baseSelector->update()) {
1096 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
1097 }
1098 if ($this->base != $this->baseSelector->getBase()) {
1099 $this->base= $this->baseSelector->getBase();
1100 $this->is_modified= TRUE;
1101 }
1102 }
1104 foreach (array( "smbgroup" => "sambaGroupType" ,"nagios_group" => "nagiosGroup") as $val => $aclname) {
1105 if ($this->acl_is_writeable($aclname)){
1106 if(isset($_POST["$val"])){
1107 $this->$val= TRUE;
1108 }else{
1109 $this->$val= FALSE;
1110 }
1111 }
1112 }
1114 /* Save sambaDomain attribute */
1115 if ($this->acl_is_writeable("sambaDomainName") && isset ($_POST['sambaDomainName'])){
1116 $this->sambaDomainName= get_post('sambaDomainName');
1117 $this->groupType= get_post('groupType');
1118 }
1120 /* Save fon attribute */
1121 if ($this->acl_is_writeable("fonGroup")){
1122 if (isset ($_POST['fon_group'])){
1123 $this->fon_group= TRUE;
1124 } else {
1125 $this->fon_group= FALSE;
1126 }
1127 }
1128 }
1129 }
1132 function get_multi_edit_values()
1133 {
1134 $ret = plugin::get_multi_edit_values();
1135 $ret = array_merge($ret,$this->trustModeDialog->get_multi_edit_values());
1137 foreach(array("base","smbgroup","groupType","sambaDomainName","fon_group","nagios_group") as $attr){
1138 if(in_array_strict($attr,$this->multi_boxes)){
1139 $ret[$attr] = $this->$attr;
1140 }
1141 }
1143 $ret['memberUid'] = $this->memberUid;
1144 $ret['memberUid_used_by_some'] = $this->memberUid_used_by_some;
1145 return($ret);
1146 }
1148 function multiple_execute()
1149 {
1150 return($this->execute());
1151 }
1154 /* Initialize plugin with given atribute arrays
1155 */
1156 function init_multiple_support($attrs,$all)
1157 {
1158 plugin::init_multiple_support($attrs,$all);
1159 $this->trustModeDialog->init_multiple_support($attrs,$all);
1161 $this->memberUid = array();
1162 $this->memberUid_used_by_some = array();
1163 if (isset($attrs['memberUid'])){
1164 for ($i= 0; $i<$attrs['memberUid']['count']; $i++){
1165 $this->memberUid[$attrs['memberUid'][$i]]= $attrs['memberUid'][$i];
1166 }
1167 ksort($this->memberUid);
1168 }
1170 if (isset($all['memberUid'])){
1171 for ($i= 0; $i<$all['memberUid']['count']; $i++){
1172 if(!in_array_strict($all['memberUid'][$i],$this->memberUid)){
1173 $this->memberUid_used_by_some[$all['memberUid'][$i]]= $all['memberUid'][$i];
1174 }
1175 }
1176 ksort($this->memberUid_used_by_some);
1177 }
1179 $this->reload(TRUE);
1181 // We've two lists in mutliple support
1182 // - one which represents those users which are part of ALL groups.
1183 // - ond one which represents those users which are only part of SOME groups.
1184 $this->commonList = new sortableListing();
1185 $this->commonList->setDeleteable(true);
1186 $this->commonList->setInstantDelete(true);
1187 $this->commonList->setEditable(false);
1188 $this->commonList->setWidth("100%");
1189 $this->commonList->setHeight("120px");
1190 $this->commonList->setHeader(array('~',_("Given name"),_("Surname"),_("UID")));
1191 $this->commonList->setColspecs(array('20px','*','*','*','20px'));
1192 $this->commonList->setDefaultSortColumn(1);
1194 $this->partialList = new sortableListing();
1195 $this->partialList->setDeleteable(true);
1196 $this->partialList->setInstantDelete(true);
1197 $this->partialList->setEditable(false);
1198 $this->partialList->setWidth("100%");
1199 $this->partialList->setHeight("120px");
1200 $this->partialList->setHeader(array('~',_("Given name"),_("Surname"),_("UID")));
1201 $this->partialList->setColspecs(array('20px','*','*','*','20px'));
1202 $this->partialList->setDefaultSortColumn(1);
1203 }
1206 function PrepareForCopyPaste($source)
1207 {
1208 plugin::PrepareForCopyPaste($source);
1210 $this->trustModeDialog->PrepareForCopyPaste($source);
1212 /* Get samba Domain in case of samba 3 */
1213 if ($this->sambaSID != ""){
1214 $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
1215 $ldap= $this->config->get_ldap_link();
1216 $ldap->cd($this->config->current['BASE']);
1217 $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
1218 if ($ldap->count() != 0){
1219 $attrs= $ldap->fetch();
1220 if(isset($attrs['sambaAlgorithmicRidBase'])){
1221 $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
1222 } else {
1223 $this->ridBase= $this->config->get_cfg_value("core","sambaRidBase");
1224 }
1226 /* Get domain name for SID */
1227 $this->sambaDomainName= "DEFAULT";
1228 foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
1229 if ($val['SID'] == $this->SID){
1230 $this->sambaDomainName= $key;
1231 break;
1232 }
1233 }
1234 } else {
1235 if ($this->config->get_cfg_value("core","sambaRidBase") != ""){
1236 $this->sambaDomainName= "DEFAULT";
1237 $this->ridBase= $this->config->get_cfg_value("core","sambaRidBase");
1238 $this->SID= $this->config->get_cfg_value("core","sambaSID");
1239 } else {
1240 msg_dialog::display(_("Configuration error"), _("Cannot find group SID in your configuration!"), ERROR_DIALOG);
1241 }
1242 }
1244 /* Get group type */
1245 $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
1246 if ($this->groupType < 500 || $this->groupType > 553){
1247 $this->groupType= 0;
1248 }
1249 $this->oldgroupType= $this->groupType;
1250 }
1252 // Detect samba groups and adapt its values.
1253 $this->smbgroup = in_array_strict('sambaGroupMapping', $source['objectClass']);
1254 if ($this->smbgroup) {
1255 $this->sambaSID = $this->getSambaSID();
1256 }
1258 $this->memberUid = array();
1259 if(isset($source['memberUid'])){
1260 for($i = 0 ; $i < $source['memberUid']['count']; $i ++){
1261 $this->memberUid[] = $source['memberUid'][$i];
1262 }
1263 }
1264 }
1267 function set_acl_base($base)
1268 {
1269 plugin::set_acl_base($base);
1270 $this->trustModeDialog->set_acl_base($base);
1271 }
1274 /*! \brief Enables multiple support for this plugin
1275 */
1276 function enable_multiple_support()
1277 {
1278 plugin::enable_multiple_support();
1279 $this->trustModeDialog->enable_multiple_support();
1280 }
1283 function set_multi_edit_values($attrs)
1284 {
1285 $users = array();
1287 /* Update groupMembership, keep optinal group */
1288 foreach($attrs['memberUid_used_by_some'] as $uid){
1289 if(in_array_strict($uid,$this->memberUid)){
1290 $users[$uid] = $uid;
1291 }
1292 }
1294 /* Update groupMembership, add forced groups */
1295 foreach($attrs['memberUid'] as $uid){
1296 $users[$uid] = $uid;
1297 }
1298 plugin::set_multi_edit_values($attrs);
1299 $this->trustModeDialog->set_multi_edit_values($attrs);
1300 $this->memberUid = $users;
1301 }
1304 /*! \brief Get a new SambaSID for a group */
1305 function getSambaSID()
1306 {
1307 $ldap = $this->config->get_ldap_link();
1308 $gidNumber= $this->gidNumber;
1309 while(TRUE){
1310 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
1311 $ldap->cd($this->config->current['BASE']);
1312 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
1313 if ($ldap->count() == 0){
1314 break;
1315 }
1316 $gidNumber++;
1317 }
1319 return $sid;
1320 }
1322 }
1323 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1324 ?>