Code

Updated strings
[gosa.git] / gosa-core / plugins / personal / posix / class_posixAccount.inc
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 /*!
24   \brief   posixAccount plugin
25   \author  Cajus Pollmeier <pollmeier@gonicus.de>
26   \version 2.00
27   \date    24.07.2003
29   This class provides the functionality to read and write all attributes
30   relevant for posixAccounts and shadowAccounts from/to the LDAP. It
31   does syntax checking and displays the formulars required.
32  */
34 class posixAccount extends plugin
35 {
36   /* Definitions */
37   var $plHeadline= "UNIX";
38   var $plDescription= "Edit users POSIX extensions";
40   /* Plugin specific values */
41   var $homeDirectory= "";
42   var $loginShell= "/bin/bash";
43   var $uidNumber= "";
44   var $gidNumber= "";
45   var $gecos= "";
46   var $shadowMin= "0";
47   var $shadowMax= "0";
48   var $shadowWarning= "0";
49   var $shadowLastChange= "0";
50   var $shadowInactive= "0";
51   var $shadowExpire= "0";
52   var $gosaDefaultPrinter= "";
53   var $accessTo= array();
54   var $trustModel= "";
56   var $glist=array();
57   var $status= "";
58   var $loginShellList= array();
59   var $groupMembership= array();
60   var $savedGroupMembership= array();
61   var $savedUidNumber= "";
62   var $savedGidNumber= "";
63   var $activate_shadowMin= "0";
64   var $activate_shadowMax= "0";
65   var $activate_shadowWarning= "0";
66   var $activate_shadowInactive= "0";
67   var $activate_shadowExpire= "0";
68   var $mustchangepassword= "0";
69   var $force_ids= 0;
70   var $group_dialog= FALSE;
71   var $show_ws_dialog= FALSE;
72   var $secondaryGroups= array();
73   var $primaryGroup= 0;
74   var $was_trust_account= FALSE;
75   var $memberGroup = array();
76   var $grouplist  = array();
77   var $ui         = array();
79   var $GroupRegex       = "*";
80   var $GroupUserRegex   = "*";
81   var $SubSearch        = false;
83   var $view_logged = FALSE;
85   /* attribute list for save action */
86   var $CopyPasteVars  = 
87       array("grouplist","groupMembership","activate_shadowMin",
88       "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
89       "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
91   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
92       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
93       "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel");
95   var $objectclasses= array("posixAccount", "shadowAccount");
97   var $uid= "";
98   var $multiple_support = TRUE;
99   var $groupMembership_some = array();
101   /* constructor, if 'dn' is set, the node loads the given
102      'dn' from LDAP */
103   function posixAccount (&$config, $dn= NULL)
104   {
105     /* Configuration is fine, allways */
106     $this->config= $config;
108     /* Load bases attributes */
109     plugin::plugin($config, $dn);
111     /* Setting uid to default */
112     if(isset($this->attrs['uid'][0])){
113       $this->uid = $this->attrs['uid'][0];
114     }
116     $ldap= $this->config->get_ldap_link();
118     if ($dn !== NULL){
120       /* Correct is_account. shadowAccount is not required. */
121       if (isset($this->attrs['objectClass']) &&
122           in_array ('posixAccount', $this->attrs['objectClass'])){
124         $this->is_account= TRUE;
125       }
127       /* Is this account a trustAccount? */
128       if ($this->is_account && isset($this->attrs['trustModel'])){
129         $this->trustModel= $this->attrs['trustModel'][0];
130         $this->was_trust_account= TRUE;
131       } else {
132         $this->was_trust_account= FALSE;
133         $this->trustModel= "";
134       }
136       $this->accessTo = array(); 
137       if ($this->is_account && isset($this->attrs['accessTo'])){
138         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
139           $tmp= $this->attrs['accessTo'][$i];
140           $this->accessTo[$tmp]= $tmp;
141         }
142       }
143       $this->initially_was_account= $this->is_account;
145       /* Fill group */
146       $this->primaryGroup= $this->gidNumber;
148       /* Generate status text */
149       $current= date("U");
151       $current= floor($current / 60 /60 / 24);
153       if (($current >= $this->shadowExpire) && $this->shadowExpire){
154         $this->status= _("expired");
155         if (($current - $this->shadowExpire) < $this->shadowInactive){
156           $this->status.= ", "._("grace time active");
157         }
158       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
159         $this->status= _("active").", "._("password not changable");
160       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
161         $this->status= _("active").", "._("password expired");
162       } else {
163         $this->status= _("active");
164       }
166       /* Get group membership */
167       $ldap->cd($this->config->current['BASE']);
168       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
170       while ($attrs= $ldap->fetch()){
171         if (!isset($attrs["description"][0])){
172           $entry= $attrs["cn"][0];
173         } else {
174           $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
175         }
176         $this->groupMembership[$ldap->getDN()]= $entry;
177       }
178       asort($this->groupMembership);
179       reset($this->groupMembership);
180       $this->savedGroupMembership= $this->groupMembership;
181       $this->savedUidNumber= $this->uidNumber;
182       $this->savedGidNumber= $this->gidNumber;
183     }
185     /* Adjust shadow checkboxes */
186     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
187           "shadowExpire") as $val){
189       if ($this->$val != 0){
190         $oval= "activate_".$val;
191         $this->$oval= "1";
192       }
193     }
195     /* Convert to seconds */
196     $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
198     /* Generate shell list from CONFIG_DIR./shells */
199     if (file_exists(CONFIG_DIR.'/shells')){
200       $shells = file (CONFIG_DIR.'/shells');
201       foreach ($shells as $line){
202         if (!preg_match ("/^#/", $line)){
203           $this->loginShellList[]= trim($line);
204         }
205       }
206     } else {
207       if ($this->loginShell == ""){
208         $this->loginShellList[]= _("unconfigured");
209       }
210     }
212     /* Insert possibly missing loginShell */
213     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
214       $this->loginShellList[]= $this->loginShell;
215     }
217     /* Generate group list */
218     $this->ui = get_userinfo(); 
219     $this->secondaryGroups[]= "- "._("automatic")." -";
220     $ldap->cd($this->config->current['BASE']);
221     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
222     while($attrs = $ldap->fetch()){
223       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
224     }
225     asort ($this->secondaryGroups);
227     /* Get global filter config */
228     if (!session::is_set("sysfilter")){
229       $ui= get_userinfo();
230       $base= get_base_from_people($ui->dn);
231       $sysfilter= array( "depselect"       => $base,
232           "regex"           => "*");
233       session::set("sysfilter", $sysfilter);
234     }
235     $this->ui = get_userinfo();
236   }
239   /* execute generates the html output for this node */
240   function execute($isCopyPaste = false)
241   {
242     /* Call parent execute */
243     plugin::execute();
244     $display= "";
246     /* Log view */
247     if($this->is_account && !$this->view_logged){
248       $this->view_logged = TRUE;
249       new log("view","users/".get_class($this),$this->dn);
250     }
252     /* Department has changed? */
253     if(isset($_POST['depselect'])){
254       session::set('CurrentMainBase',validate($_POST['depselect']));
255     }
257     if($this->multiple_support_active){
258       $this->is_account = TRUE;
259     }
261     if(!$isCopyPaste && ! $this->multiple_support_active){
263       /* Do we need to flip is_account state? */
264       if(isset($_POST['modify_state'])){
265         if($this->is_account && $this->acl_is_removeable()){
266           $this->is_account= FALSE;
267         }elseif(!$this->is_account && $this->acl_is_createable()){
268           $this->is_account= TRUE;
269         }
270       }
272       /* Do we represent a valid posixAccount? */
273       if (!$this->is_account && $this->parent === NULL ){
274         $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
275           msgPool::noValidExtension(_("POSIX"))."</b>";
276         $display.= back_to_main();
277         return ($display);
278       }
281       /* Show tab dialog headers */
282       if ($this->parent !== NULL){
283         if ($this->is_account){
284           if (isset($this->parent->by_object['sambaAccount'])){
285             $obj= $this->parent->by_object['sambaAccount'];
286           }
287           if (isset($obj) && $obj->is_account == TRUE &&
288               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
289               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
291             /* Samba3 dependency on posix accounts are enabled
292                in the moment, because I need to rely on unique
293                uidNumbers. There'll be a better solution later
294                on. */
295             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
296           } else {
297             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
298           }
299         } else {
300           $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
301           return($display);
302         }
303       }
304     }
305     /* Trigger group edit? */
306     if (isset($_POST['edit_groupmembership'])){
307       $this->group_dialog= TRUE;
308       $this->dialog= TRUE;
309     }
311     /* Cancel group edit? */
312     if (isset($_POST['add_groups_cancel']) ||
313         isset($_POST['add_groups_finish'])){
314       $this->group_dialog= FALSE;
315       $this->dialog= FALSE;
316     }
318     /* Add selected groups */
319     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
320         count($_POST['groups'])){
322       $this->addGroup ($_POST['groups']);
323     }
325     /* Delete selected groups */
326     if (isset($_POST['delete_groupmembership']) && 
327         isset($_POST['group_list']) && count($_POST['group_list'])){
329       $this->delGroup ($_POST['group_list']);
330     }
332     /* Add user workstation? */
333     if (isset($_POST["add_ws"])){
334       $this->show_ws_dialog= TRUE;
335       $this->dialog= TRUE;
336     }
338     /* Add user workstation? */
339     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
340       foreach($_POST['wslist'] as $ws){
341         $this->accessTo[$ws]= $ws;
342       }
343       ksort($this->accessTo);
344       $this->is_modified= TRUE;
345     }
347     /* Remove user workstations? */
348     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
349       foreach($_POST['workstation_list'] as $name){
350         unset ($this->accessTo[$name]);
351       }
352       $this->is_modified= TRUE;
353     }
355     /* Add user workstation finished? */
356     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
357       $this->show_ws_dialog= FALSE;
358       $this->dialog= FALSE;
359     }
361     /* Templates now! */
362     $smarty= get_smarty();
364     /* Show ws dialog */
365     if ($this->show_ws_dialog){
366       /* Save data */
367       $sysfilter= session::get("sysfilter");
368       foreach( array("depselect", "regex") as $type){
369         if (isset($_POST[$type])){
370           $sysfilter[$type]= $_POST[$type];
371         }
372       }
373       if (isset($_GET['search'])){
374         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
375         if ($s == "**"){
376           $s= "*";
377         }
378         $sysfilter['regex']= $s;
379       }
380       session::set("sysfilter", $sysfilter);
382       /* Get workstation list */
383       $exclude= "";
384       foreach($this->accessTo as $ws){
385         $exclude.= "(cn=$ws)";
386       }
387       if ($exclude != ""){
388         $exclude= "(!(|$exclude))";
389       }
390       $regex= $sysfilter['regex'];
391       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
393       $deps_a = array(get_ou("serverou"),
394                       get_ou("terminalou"),
395                       get_ou("workstationou")); 
397       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
398       $wslist= array();
399       foreach ($res as $attrs){
400         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
401       }
402       asort($wslist);
403       $smarty->assign("search_image", get_template_path('images/search.png'));
404       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
405       $smarty->assign("tree_image", get_template_path('images/tree.png'));
406       $smarty->assign("deplist", $this->config->idepartments);
407       $smarty->assign("alphabet", generate_alphabet());
408       foreach( array("depselect", "regex") as $type){
409         $smarty->assign("$type", $sysfilter[$type]);
410       }
411       $smarty->assign("hint", print_sizelimit_warning());
412       $smarty->assign("wslist", $wslist);
413       $smarty->assign("apply", apply_filter());
414       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
415       return ($display);
416     }
418     /* Manage group add dialog */
419     if ($this->group_dialog){
421       /* Get global filter config */
422       $this->reload();
424       /* remove already assigned groups */
425       $glist= array();
426       foreach ($this->grouplist as $key => $value){
427         if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
428           $glist[$key]= $value;
429         }
430       }
432       if($this->SubSearch){
433         $smarty->assign("SubSearchCHK"," checked ");
434       }else{
435         $smarty->assign("SubSearchCHK","");
436       }
438       $smarty->assign("regex",$this->GroupRegex);
439       $smarty->assign("guser",$this->GroupUserRegex);
440       $smarty->assign("groups", $glist);
441       $smarty->assign("search_image", get_template_path('images/search.png'));
442       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
443       $smarty->assign("tree_image", get_template_path('images/tree.png'));
444       $smarty->assign("deplist", $this->config->idepartments);
445       $smarty->assign("alphabet", generate_alphabet());
446       $smarty->assign("depselect", session::get('CurrentMainBase'));
447       $smarty->assign("hint", print_sizelimit_warning());
449       $smarty->assign("apply", apply_filter());
450       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
451       return ($display);
452     }
454     /* Show main page */
455     $smarty= get_smarty();
457     /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */ 
458     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
460     /* Depending on pwmode, currently hardcoded because there are no other methods */
461     if ( 1 == 1 ){
462       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
464       $shadowMinACL     =  $this->getacl("shadowMin",$SkipWrite);
465       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
466                                               "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
468       $shadowMaxACL     =  $this->getacl("shadowMax",$SkipWrite);
469       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
470                                               "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
472       $shadowInactiveACL=  $this->getacl("shadowInactive",$SkipWrite);
473       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"), 
474                                               "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
476       $shadowWarningACL =  $this->getacl("shadowWarning",$SkipWrite);
477       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), 
478                                               "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
480       foreach( array("activate_shadowMin", "activate_shadowMax",
481                      "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
482         if ($this->$val == 1){
483           $smarty->assign("$val", "checked");
484         } else {
485           $smarty->assign("$val", "");
486         }
487         $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
488       }
490       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
491     }
493     /* Fill calendar */
494     /* If this $this->shadowExpire is empty 
495         use current date as base for calculating selectbox values.
496        (This attribute is empty if this is a new user )*/ 
497     if(empty($this->shadowExpire)){
498       $date= getdate(time());
499     }else{
500       $date= getdate($this->shadowExpire);
501     }
502  
503     $days= array();
504     for($d= 1; $d<32; $d++){
505       $days[$d]= $d;
506     }
507     $years= array();
508     for($y= $date['year']-10; $y<$date['year']+10; $y++){
509       $years[]= $y;
510     }
511     $months= msgPool::months();
512     $smarty->assign("day", $date["mday"]);
513     $smarty->assign("days", $days);
514     $smarty->assign("months", $months);
515     $smarty->assign("month", $date["mon"]-1);
516     $smarty->assign("years", $years);
517     $smarty->assign("year", $date["year"]);
519     /* Fill arrays */
520     $smarty->assign("shells", $this->loginShellList);
521     $smarty->assign("secondaryGroups", $this->secondaryGroups);
522     $smarty->assign("primaryGroup", $this->primaryGroup);
523     if(!$this->multiple_support_active){
524       if (!count($this->groupMembership)){
525         $smarty->assign("groupMembership", array("&nbsp;"));
526       } else {
527         $smarty->assign("groupMembership", $this->groupMembership);
528       }
529     }else{
530       $smarty->assign("groupMembership", $this->groupMembership);
531       $smarty->assign("groupMembership_some", $this->groupMembership_some);
532     }
533     if (count($this->groupMembership) > 16){
534       $smarty->assign("groups", "too_many_for_nfs");
535     } else {
536       $smarty->assign("groups", "");
537     }
539     /* Avoid "Undefined index: forceMode" */
540     $smarty->assign("forceMode", "");
542     /* Checkboxes */
543     if ($this->force_ids == 1){
544       $smarty->assign("force_ids", "checked");
545       if (session::get('js')){
546         $smarty->assign("forceMode", "");
547       }
548     } else {
549       if (session::get('js')){
550         $smarty->assign("forceMode", "disabled");
551       }
552       $smarty->assign("force_ids", "");
553     }
554     
556     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
558     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
559       if(in_array($val,$this->multi_boxes)){
560         $smarty->assign("use_".$val,TRUE);
561       }else{
562         $smarty->assign("use_".$val,FALSE);
563       }
564     }
567     /* Load attributes and acl's */
568     foreach($this->attributes as $val){
569       if(in_array($val,$this->multi_boxes)){
570         $smarty->assign("use_".$val,TRUE);
571       }else{
572         $smarty->assign("use_".$val,FALSE);
573       }
575       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
576       {
577         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
578         $smarty->assign("$val", $this->$val);
579         continue;
580       }
581       $smarty->assign("$val", $this->$val);
582       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
583     }
584     if($SkipWrite){
585       $smarty->assign("groupMembershipACL","r");
586     }else{
587       $smarty->assign("groupMembershipACL","rw");
588     }
589     $smarty->assign("status", $this->status);
591     /* Work on trust modes */
592     $smarty->assign("trusthide", " disabled ");
593     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
594     if ($this->trustModel == "fullaccess"){
595       $trustmode= 1;
596       // pervent double disable tag in html code, this will disturb our clean w3c html
597       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
599     } elseif ($this->trustModel == "byhost"){
600       $trustmode= 2;
601       $smarty->assign("trusthide", "");
602     } else {
603       // pervent double disable tag in html code, this will disturb our clean w3c html
604       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
605       $trustmode= 0;
606     }
607     $smarty->assign("trustmode", $trustmode);
608     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
609           2 => _("allow access to these hosts")));
613     if((count($this->accessTo))==0)
614       $smarty->assign("emptyArrAccess",true);
615     else
616       $smarty->assign("emptyArrAccess",false);
618       if($this->mustchangepassword){
619         $smarty->assign("mustchangepassword", " checked ");
620       } else {
621         $smarty->assign("mustchangepassword", "");
622       }
624     $smarty->assign("workstations", $this->accessTo);
626     $smarty->assign("apply", apply_filter());
627     $smarty->assign("multiple_support" , $this->multiple_support_active);
628     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
629     return($display);
630   }
633   /* remove object from parent */
634   function remove_from_parent()
635   {
636     /* Cancel if there's nothing to do here */
637     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
638       return;
639     }
641     /* include global link_info */
642     $ldap= $this->config->get_ldap_link();
644     /* Remove and write to LDAP */
645     plugin::remove_from_parent();
647     /* Zero out array */
648     $this->attrs['gosaHostACL']= array();
650     /* Keep uid, because we need it for authentification! */
651     unset($this->attrs['uid']);
652     unset($this->attrs['trustModel']);
654     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
655         $this->attributes, "Save");
656     $ldap->cd($this->dn);
657     $this->cleanup();
658     $ldap->modify ($this->attrs); 
660     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
662     if (!$ldap->success()){
663       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
664     }
666     /* Delete group only if cn is uid and there are no other
667        members inside */
668     $ldap->cd ($this->config->current['BASE']);
669     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
670     if ($ldap->count() != 0){
671       $attrs= $ldap->fetch();
672       if ($attrs['cn'][0] == $this->uid &&
673           !isset($this->attrs['memberUid'])){
675         $ldap->rmDir($ldap->getDN());
676       }
677     }
679     /* Optionally execute a command after we're done */
680     $this->handle_post_events("remove",array("uid" => $this->uid));
681   }
684   function save_object()
685   {
686     if (isset($_POST['posixTab'])){
687       /* Save values to object */
688       plugin::save_object();
691       /* Save force GID checkbox */
692       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
693         if (isset ($_POST['force_ids'])){
694           $data= 1;
695         } else {
696           $data= 0;
697         }
698         if ($this->force_ids != $data){
699           $this->is_modified= TRUE;
700         }
701         $this->force_ids= $data;
702       }
704       /*Save primary group settings */
705       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
706         $data= $_POST['primaryGroup'];
707         if ($this->primaryGroup != $data){
708           $this->is_modified= TRUE;
709         }
710         $this->primaryGroup= $_POST['primaryGroup'];
711       }
713       /* Get seelcted shadow checkboxes */
714       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
715         if($this->acl_is_writeable($var)){
716           $activate_var = "activate_".$var;
717           if(isset($_POST['activate_'.$var])){
718             $this->$activate_var  = true;
719             $this->$var      = $_POST[$var];
720           }else{
721             $this->$activate_var  = false;
722             $this->$var      = 0;
723           }
724         }
725       }
727       /* Force change password ? */
728       if(isset($_POST['mustchangepassword'])){
729         $this->mustchangepassword = TRUE;
730       }else{
731         $this->mustchangepassword = FALSE;
732       }
734       /* Trust mode - special handling */
735       if($this->acl_is_writeable("trustModel")){
736         if (isset($_POST['trustmode'])){
737           $saved= $this->trustModel;
738           if ($_POST['trustmode'] == "1"){
739             $this->trustModel= "fullaccess";
740           } elseif ($_POST['trustmode'] == "2"){
741             $this->trustModel= "byhost";
742           } else {
743             $this->trustModel= "";
744           }
745           if ($this->trustModel != $saved){
746             $this->is_modified= TRUE;
747           }
748         }
749       }
750     }
752     /* Get regex from alphabet */
753     if(isset($_GET['search'])){
754       $this->GroupRegex = $_GET['search']."*";
755     }
757     /* Check checkboxes and regexes */
758     if(isset($_POST["PosixGroupDialogPosted"])){
760       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
761         $this->SubSearch = true;
762       }else{
763         $this->SubSearch = false;
764       }
765       if(isset($_POST['guser'])){
766         $this->GroupUserRegex = $_POST['guser'];
767       }
768       if(isset($_POST['regex'])){
769         $this->GroupRegex = $_POST['regex'];
770       }
771     }
772     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
773     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
774   }
777   /* Save data to LDAP, depending on is_account we save or delete */
778   function save()
779   {
781     /* include global link_info */
782     $ldap= $this->config->get_ldap_link();
784     /* Adapt shadow values */
785     if (!$this->activate_shadowExpire){
786       $this->shadowExpire= "0";
787     } else {
788       /* Transform seconds to days here */
789       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
790     }
791     if (!$this->activate_shadowMax){
792       $this->shadowMax= "0";
793     }
794     if ($this->mustchangepassword){
795       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
796     } else {
797       $this->shadowLastChange= (int)(date("U") / 86400);
798     }
799     if (!$this->activate_shadowWarning){
800       $this->shadowWarning= "0";
801     }
803     /* Check what to do with ID's */
804     if ($this->force_ids == 0){
806       /* Use id's that are already set */
807       if ($this->savedUidNumber != ""){
808         $this->uidNumber= $this->savedUidNumber;
809         $this->gidNumber= $this->savedGidNumber;
810       } else {
812         /* Calculate new id's. We need to place a lock before calling get_next_id
813            to get real unique values. */
814         $wait= 10;
815         while (get_lock("uidnumber") != ""){
816           sleep (1);
818           /* Oups - timed out */
819           if ($wait-- == 0){
820             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
821             break;
822           }
823         }
825         add_lock ("uidnumber", "gosa");
826         $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
827         if ($this->savedGidNumber != ""){
828           $this->gidNumber= $this->savedGidNumber;
829         } else {
830           $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
831         }
832       }
834       if ($this->primaryGroup != 0){
835         $this->gidNumber= $this->primaryGroup;
836       }
837     }
839     if ($this->activate_shadowMin != "1" ) {
840       $this->shadowMin = "";
841     }
843     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
844       $this->shadowMax = "";
845     }
847     if ($this->activate_shadowWarning != "1" ) {
848       $this->shadowWarning = "";
849     }
851     if ($this->activate_shadowInactive != "1" ) {
852       $this->shadowInactive = "";
853     }
855     if ($this->activate_shadowExpire != "1" ) {
856       $this->shadowExpire = "";
857     }
859     /* Fill gecos */
860     if (isset($this->parent) && $this->parent !== NULL){
861       $this->gecos= rewrite($this->parent->by_object['user']->cn);
862       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
863         $this->gecos= "";
864       }
865     }
867     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
868       $this->$attr = (int) $this->$attr;
869     }
870     /* Call parents save to prepare $this->attrs */
871     plugin::save();
873     /* Trust accounts */
874     $objectclasses= array();
875     foreach ($this->attrs['objectClass'] as $key => $class){
876       if (preg_match('/trustAccount/i', $class)){
877         continue;
878       }
879       $objectclasses[]= $this->attrs['objectClass'][$key];
880     }
881     $this->attrs['objectClass']= $objectclasses;
882     if ($this->trustModel != ""){
883       $this->attrs['objectClass'][]= "trustAccount";
884       $this->attrs['trustModel']= $this->trustModel;
885       $this->attrs['accessTo']= array();
886       if ($this->trustModel == "byhost"){
887         foreach ($this->accessTo as $host){
888           $this->attrs['accessTo'][]= $host;
889         }
890       }
891     } else {
892       if ($this->was_trust_account){
893         $this->attrs['accessTo']= array();
894         $this->attrs['trustModel']= array();
895       }
896     }
898     if(empty($this->attrs['gosaDefaultPrinter'])){
899       $thid->attrs['gosaDefaultPrinter']=array();
900     }
903     /* Save data to LDAP */
904     $ldap->cd($this->dn);
905     $this->cleanup();
906  
907     /* This is just a test, we have had duplicated ids 
908         in the past when copy & paste was used. 
909        Normaly this should not happen.
910      */ 
911     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
912       $used = $this->get_used_uid_numbers();
913       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
914         msg_dialog::display(_("Uid number"),_("A duplicated uid number was written for this user, if this was not intended please verify all used uidNumbers."), WARNING_DIALOG);
915       }
916     }
918     unset($this->attrs['uid']);
919     $ldap->modify ($this->attrs); 
921     /* Log last action */ 
922     if($this->initially_was_account){
923       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
924     }else{
925       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
926     }
928     if (!$ldap->success()){
929       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
930     }
932     /* Remove lock needed for unique id generation */
933     del_lock ("uidnumber");
935     /* Posix accounts have group interrelationship, 
936        take care about these here if this is a new user without forced gidNumber. */
937     if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
938       $ldap->cd($this->config->current['BASE']);
939       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
941       /* Create group if it doesn't exist */
942       if ($ldap->count() == 0){
943         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
945         $g= new group($this->config, $groupdn);
946         $g->cn= $this->uid;
947         $g->force_gid= 1;
948         $g->gidNumber= $this->gidNumber;
949         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
950         $g->save ();
951       }
952     }
954     /* Take care about groupMembership values: add to groups */
955     foreach ($this->groupMembership as $key => $value){
956       if (!isset($this->savedGroupMembership[$key])){
957         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
958         $g->set_acl_base($key);
959         $g->by_object['group']->addUser($this->uid);
960         $g->save();
961       }
962     }
964     /* Remove from groups not listed in groupMembership */
965     foreach ($this->savedGroupMembership as $key => $value){
966       if (!isset($this->groupMembership[$key])){
967         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
968         $g->set_acl_base($key);
969         $g->by_object['group']->removeUser ($this->uid);
970         $g->save();
971       }
972     }
974     /* Optionally execute a command after we're done */
975     if ($this->initially_was_account == $this->is_account){
976       if ($this->is_modified){
977         $this->handle_post_events("modify",array("uid" => $this->uid));
978       }
979     } else {
980       $this->handle_post_events("add" ,array("uid"=> $this->uid));
981     }
982   }
984   /* Check formular input */
985   function check()
986   {
987     /* Include global link_info */
988     $ldap= $this->config->get_ldap_link();
990     /* Append groups as memberGroup: to check hook 
991      */
992     $tmp_attributes  = $this->attributes;    
993     $this->attributes[] = "memberGroup";
994     $this->memberGroup = array();
995     foreach($this->groupMembership as $dn => $name){
996       $this->memberGroup[] = $name;
997     }
999     /* Call common method to give check the hook */
1000     $message= plugin::check();
1001     $this->attributes = $tmp_attributes;
1003     /* must: homeDirectory */
1004     if ($this->homeDirectory == ""){
1005       $message[]= msgPool::required(_("Home directory"));
1006     }
1007     if (!tests::is_path($this->homeDirectory)){
1008       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1009     }
1011     /* Check ID's if they are forced by user */
1012     if ($this->force_ids == "1"){
1014       /* Valid uid/gid? */
1015       if (!tests::is_id($this->uidNumber)){
1016         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1017       } else {
1018         if ($this->uidNumber < $this->config->current['MINID']){
1019           $message[]= msgPool::toosmall(_("UID"), $this->config->current['MINID']);
1020         }
1021       }
1022       if (!tests::is_id($this->gidNumber)){
1023         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1024       } else {
1025         if ($this->gidNumber < $this->config->current['MINID']){
1026           $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1027         }
1028       }
1029     }
1031     /* Check shadow settings, well I like spaghetties... */
1032     if ($this->activate_shadowMin){
1033       if (!tests::is_id($this->shadowMin)){
1034         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1035       }
1036     }
1037     if ($this->activate_shadowMax){
1038       if (!tests::is_id($this->shadowMax)){
1039         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1040       }
1041     }
1042     if ($this->activate_shadowWarning){
1043       if (!tests::is_id($this->shadowWarning)){
1044         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1045       }
1046       if (!$this->activate_shadowMax){
1047         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1048       }
1049       if ($this->shadowWarning > $this->shadowMax){
1050         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1051       }
1052       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1053         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1054       }
1055     }
1056     if ($this->activate_shadowInactive){
1057       if (!tests::is_id($this->shadowInactive)){
1058         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1059       }
1060       if (!$this->activate_shadowMax){
1061         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1062       }
1063     }
1064     if ($this->activate_shadowMin && $this->activate_shadowMax){
1065       if ($this->shadowMin > $this->shadowMax){
1066         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1067       }
1068     }
1070     return ($message);
1071   }
1074   function multiple_check()
1075   {
1076     $message = plugin::multiple_check();
1077     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1078       $message[]= msgPool::required(_("Home directory"));
1079     }
1080     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1081       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1082     }
1084     /* Check shadow settings, well I like spaghetties... */
1085     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1086       if (!tests::is_id($this->shadowMin)){
1087         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1088       }
1089     }
1090     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1091       if (!tests::is_id($this->shadowMax)){
1092         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1093       }
1094     }
1095     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1096       if (!tests::is_id($this->shadowWarning)){
1097         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1098       }
1099       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1100         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1101       }
1102       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1103         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1104       }
1105       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1106         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1107       }
1108     }
1109     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1110       if (!tests::is_id($this->shadowInactive)){
1111         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1112       }
1113       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1114         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1115       }
1116     }
1117     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1118       if ($this->shadowMin > $this->shadowMax){
1119         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1120       }
1121     }
1123     return($message);
1124   }
1127   function addGroup ($groups)
1128   {
1129     /* include global link_info */
1130     $ldap= $this->config->get_ldap_link();
1132     /* Walk through groups and add the descriptive entry if not exists */
1133     foreach ($groups as $value){
1135       if (!array_key_exists($value, $this->groupMembership)){
1136         $ldap->cat($value, array('cn', 'description', 'dn'));
1137         $attrs= $ldap->fetch();
1138         error_reporting (0);
1139         if (!isset($attrs['description'][0])){
1140           $entry= $attrs["cn"][0];
1141         } else {
1142           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1143           $entry= $attrs["cn"][0]." [$dsc]";
1144         }
1145         error_reporting (E_ALL | E_STRICT);
1147         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1148           $this->groupMembership[$attrs['dn']]= $entry;
1149           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1150             unset($this->groupMembership_some[$attrs['dn']]);
1151           }
1152         }
1153       }
1154     }
1156     /* Sort groups */
1157     asort ($this->groupMembership);
1158     reset ($this->groupMembership);
1159   }
1162   /* Del posix user from some groups */
1163   function delGroup ($groups)
1164   {
1165     $dest= array();
1166     foreach($groups as $dn_to_del){
1167       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1168         unset($this->groupMembership[$dn_to_del]);
1169       }
1170       if($this->multiple_support_active){
1171         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1172           unset($this->groupMembership_some[$dn_to_del]);
1173         }
1174       }
1175     }
1176   }
1179   /* Adapt from template, using 'dn' */
1180   function adapt_from_template($dn)
1181   {
1182     /* Include global link_info */
1183     $ldap= $this->config->get_ldap_link();
1185     plugin::adapt_from_template($dn);
1186     $template= $this->attrs['uid'][0];
1188     /* Adapt group membership */
1189     $ldap->cd($this->config->current['BASE']);
1190     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1192     while ($this->attrs= $ldap->fetch()){
1193       if (!isset($this->attrs["description"][0])){
1194         $entry= $this->attrs["cn"][0];
1195       } else {
1196         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1197       }
1198       $this->groupMembership[$ldap->getDN()]= $entry;
1199     }
1201     /* Fix primary group settings */
1202     $ldap->cd($this->config->current['BASE']);
1203     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1204     if ($ldap->count() != 1){
1205       $this->primaryGroup= $this->gidNumber;
1206     }
1208     $ldap->cd($this->config->current['BASE']);
1209     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1210     while($attr = $ldap->fetch()){
1211       $tmp = $attr['accessTo'];
1212       unset ($tmp['count']);
1213       $this->accessTo = $tmp;   
1214     }
1216     /* Adjust shadow checkboxes */
1217     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1218       if ($this->$val != 0){
1219         $oval= "activate_".$val;
1220         $this->$oval= "1";
1221       }
1222     }
1224     /* FIXME: NEED review of this section */
1225     /* Need to check shadowExpire separately */
1227     /* 
1228      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1229      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1230      */
1231     if(abs($this->shadowExpire - time())>86400) {
1232       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1233     }
1234     
1235     /* Only enable checkbox, if shadowExpire is in the future */
1236     if($this->shadowExpire > time()) {
1237       $this->activate_shadowExpire= "1";
1238     }
1239   }
1241   function convertToSeconds($val)
1242   {
1243     if ($val != 0){
1244       $val*= 60 * 60 * 24;
1245     } else {
1246       $date= getdate();
1247       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1248     }
1249     return($val);
1250   }
1253   function get_used_uid_numbers()
1254   {
1255     $ids= array();
1256     $ldap= $this->config->get_ldap_link();
1258     $ldap->cd ($this->config->current['BASE']);
1259     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1261     /* Get list of ids */
1262     while ($attrs= $ldap->fetch()){
1263       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1264     }
1265     return($ids);
1266   }
1268   
1269   function get_next_id($attrib, $dn)
1270   {
1271     $ids= array();
1272     $ldap= $this->config->get_ldap_link();
1274     $ldap->cd ($this->config->current['BASE']);
1275     if (preg_match('/gidNumber/i', $attrib)){
1276       $oc= "posixGroup";
1277     } else {
1278       $oc= "posixAccount";
1279     }
1280     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1282     /* Get list of ids */
1283     while ($attrs= $ldap->fetch()){
1284       $ids[]= (int)$attrs["$attrib"][0];
1285     }
1287     /* Add the nobody id */
1288     $ids[]= 65534;
1290     /* get the ranges */
1291     $tmp = array('0'=> 1000); 
1292     if (preg_match('/posixAccount/', $oc) && isset($this->config->current['UIDBASE'])) {
1293       $tmp= split('-',$this->config->current['UIDBASE']);
1294     } elseif(isset($this->config->current['GIDBASE'])){
1295       $tmp= split('-',$this->config->current['GIDBASE']);
1296     }
1298     /* Set hwm to max if not set - for backward compatibility */
1299     $lwm= $tmp[0];
1300     if (isset($tmp[1])){
1301       $hwm= $tmp[1];
1302     } else {
1303       $hwm= pow(2,32);
1304     }
1306     /* Find out next free id near to UID_BASE */
1307     if (!isset($this->config->current['BASE_HOOK'])){
1308       $base= $lwm;
1309     } else {
1310       /* Call base hook */
1311       $base= get_base_from_hook($dn, $attrib);
1312     }
1313     for ($id= $base; $id++; $id < pow(2,32)){
1314       if (!in_array($id, $ids)){
1315         return ($id);
1316       }
1317     }
1319     /* Should not happen */
1320     if ($id == $hwm){
1321       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1322       exit;
1323     }
1325   }
1327   function reload()
1328   {
1329     /* Set base for all searches */
1330     $base      = session::get('CurrentMainBase');
1331     $base     = $base;
1332     $ldap     = $this->config->get_ldap_link();    
1333     $attrs    =  array("cn", "description", "gidNumber");
1334     $Flags    = GL_SIZELIMIT;
1336     /* Get groups */
1337     if ($this->GroupUserRegex == '*'){
1338       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1339     } else {
1340       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1341     }
1342     if($this->SubSearch){
1343       $Flags |= GL_SUBSEARCH;
1344     }else{
1345       $base = get_groups_ou().$base;
1346     }
1348     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1349   
1350     /* check sizelimit */
1351     if (preg_match("/size limit/i", $ldap->get_error())){
1352       session::set('limit_exceeded',TRUE);
1353     }
1355     /* Create a list of users */
1356     $this->grouplist = array();
1357     foreach ($res as $value){
1358       $this->grouplist[$value['gidNumber'][0]]= $value;
1359     }
1361     $tmp=array();
1362     foreach($this->grouplist as $tkey => $val ){
1363       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1364     }
1366     /* Sort index */
1367     ksort($tmp);
1369     /* Recreate index array[dn]=cn[description]*/
1370     $this->grouplist=array();
1371     foreach($tmp as $val){
1372       if(isset($val['description'])){
1373         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1374       }else{
1375         $this->grouplist[$val['dn']]=$val['cn'][0];
1376       }
1377     }
1379     reset ($this->grouplist);
1380   }
1383   /* Get posts from copy & paste dialog */ 
1384   function saveCopyDialog()
1385   {
1386     if(isset($_POST['homeDirectory'])){
1387       $this->homeDirectory = $_POST['homeDirectory'];
1388       if (isset ($_POST['force_ids'])){
1389         $data= 1;
1390         $this->gidNumber = $_POST['gidNumber'];
1391         $this->uidNumber = $_POST['uidNumber'];
1392       } else {
1393         $data= 0;
1394       }
1395       if ($this->force_ids != $data){
1396         $this->is_modified= TRUE;
1397       }
1398       $this->force_ids= $data;
1399     }
1400   }
1401  
1403   /* Create the posix dialog part for copy & paste */
1404   function getCopyDialog()
1405   {
1406     /* Skip dialog creation if this is not a valid account*/
1407     if(!$this->is_account) return("");
1408     if ($this->force_ids == 1){
1409       $force_ids = "checked";
1410       if (session::get('js')){
1411         $forceMode = "";
1412       }
1413     } else {
1414       if (session::get('js')){
1415         if($this->acl != "#none#")
1416           $forceMode ="disabled";
1417       }
1418       $force_ids = "";
1419     }
1421     $sta = "";
1423     /* Open group add dialog */
1424     if(isset($_POST['edit_groupmembership'])){
1425       $this->group_dialog = TRUE;
1426       $sta = "SubDialog";
1427     }
1429     /* If the group-add dialog is closed, call execute 
1430        to ensure that the membership is updatd */
1431     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1432       $this->execute();
1433       $this->group_dialog =FALSE;
1434     }
1436     if($this->group_dialog){
1437       $str = $this->execute(true);
1438       $ret = array();
1439       $ret['string'] = $str;
1440       $ret['status'] = $sta;
1441       return($ret);
1442     }
1444     /* If a group member should be deleted, simply call execute */
1445     if(isset($_POST['delete_groupmembership'])){
1446       $this->execute();
1447     }
1449     /* Assigned informations to smarty */
1450     $smarty = get_smarty();
1451     $smarty->assign("homeDirectory",$this->homeDirectory);
1452     $smarty->assign("uidNumber",$this->uidNumber);
1453     $smarty->assign("gidNumber",$this->gidNumber);
1454     $smarty->assign("forceMode",$forceMode);
1455     $smarty->assign("force_ids",$force_ids);
1456     if (!count($this->groupMembership)){
1457       $smarty->assign("groupMembership", array("&nbsp;"));
1458     } else {
1459       $smarty->assign("groupMembership", $this->groupMembership);
1460     }
1462     /* Display wars message if there are more than 16 group members */
1463     if (count($this->groupMembership) > 16){
1464       $smarty->assign("groups", "too_many_for_nfs");
1465     } else {
1466       $smarty->assign("groups", "");
1467     }
1468     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1470     $ret = array();
1471     $ret['string'] = $str;
1472     $ret['status'] = $sta;
1473     return($ret);
1474   }
1477   function PrepareForCopyPaste($source)
1478   {
1479     plugin::PrepareForCopyPaste($source);
1481     /* Avoid using the same gid/uid number as source user */
1482     $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1483     $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1484   }
1487   function multiple_execute()
1488   {
1489     return($this->execute());
1490   }
1493   static function plInfo()
1494   {
1495     return (array(
1496           "plDescription"     => _("POSIX account"),
1497           "plSelfModify"      => TRUE,
1498           "plDepends"         => array("user"),
1499           "plPriority"        => 2,
1500           "plSection"         => array("personal" => _("My account")),
1501           "plCategory"        => array("users"),
1502           "plOptions"         => array(),
1504           "plProvidedAcls"  => array(
1506             "homeDirectory"       =>  _("Home directory"), 
1507             "loginShell"          =>  _("Shell"),
1508             "uidNumber"           =>  _("User ID"),
1509             "gidNumber"           =>  _("Group ID"),
1511             "mustchangepassword"=>  _("Force password change on login"),
1512             "shadowMin"           =>  _("Shadow min"),
1513             "shadowMax"           =>  _("Shadow max"),
1514             "shadowWarning"       =>  _("Shadow warning"),
1515             "shadowInactive"      =>  _("Shadow inactive"),
1516             "shadowExpire"        =>  _("Shadow expire"),
1517             "trustModel"          =>  _("System trust model")))
1518             );
1519   }
1522   /* Return selected values for multiple edit */
1523   function get_multi_edit_values()
1524   {
1525     $ret = plugin::get_multi_edit_values();
1526     $ret['groupMembership']     = $this->groupMembership;
1527     $ret['groupMembership_some']= $this->groupMembership_some;
1529     if(in_array("primaryGroup",$this->multi_boxes)){
1530       $ret['primaryGroup'] = $this->primaryGroup;
1531     }
1532     if(in_array("trustmode",$this->multi_boxes)){
1533       $ret['trustModel'] = $this->trustModel;
1534       $ret['accessTo'] = $this->accessTo;
1535     }
1536     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1537       $active = "activate_".$entry;
1538       if(in_array($active,$this->multi_boxes)){
1539         $ret[$entry] = $this->$entry;
1540         $ret[$active] = $this->$active;
1541       }
1542     }
1543     if(in_array("mustchangepassword",$this->multi_boxes)){
1544       $ret['mustchangepassword'] = $this->mustchangepassword;
1545     }
1546     return($ret);
1547   }
1550   /* Save posts for multiple edit 
1551    */
1552   function multiple_save_object()
1553   {
1554     if(isset($_POST['posix_mulitple_edit'])){
1555  
1556       /* Backup expire value */ 
1557       $expire_tmp = $this->shadowExpire;
1558   
1559       /* Update all values */
1560       plugin::multiple_save_object();
1562       /* Get selected checkboxes */
1563       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1564         if(isset($_POST["use_".$val])){
1565           $this->multi_boxes[] = $val;
1566         }
1567       }
1569       /* Update special values, checkboxes for posixShadow */
1570       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1571         if($this->acl_is_writeable($var)){
1572           $activate_var = "activate_".$var;
1573           if(in_array($activate_var, $this->multi_boxes)){
1574             if(isset($_POST['activate_'.$var])){
1575               $this->$activate_var  = true;
1576               $this->$var      = $_POST[$var];
1577             }else{
1578               $this->$activate_var  = false;
1579               $this->$var      = 0;
1580             }
1581           }
1582         }
1583       }
1585       /* Restore shadow value, if the shadow attribute isn't used */
1586       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1587         $this->shadowExpire = $expire_tmp;
1588       }
1590       /* Force change password ? */
1591       if(isset($_POST['mustchangepassword'])){
1592         $this->mustchangepassword = TRUE;
1593       }else{
1594         $this->mustchangepassword = FALSE;
1595       }
1597       /* Trust mode - special handling */
1598       if($this->acl_is_writeable("trustModel")){
1599         if (isset($_POST['trustmode'])){
1600           $saved= $this->trustModel;
1601           if ($_POST['trustmode'] == "1"){
1602             $this->trustModel= "fullaccess";
1603           } elseif ($_POST['trustmode'] == "2"){
1604             $this->trustModel= "byhost";
1605           } else {
1606             $this->trustModel= "";
1607           }
1608           if ($this->trustModel != $saved){
1609             $this->is_modified= TRUE;
1610           }
1611         }
1612       }
1614       /* Save primary group settings */
1615       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1616         $data= $_POST['primaryGroup'];
1617         if ($this->primaryGroup != $data){
1618           $this->is_modified= TRUE;
1619         }
1620         $this->primaryGroup= $_POST['primaryGroup'];
1621       }
1622     }
1623   }
1625   
1626   /* Initialize plugin with given atribute arrays 
1627    */
1628   function init_multiple_support($attrs,$all)
1629   {
1630     plugin::init_multiple_support($attrs,$all);
1632     /* Some dummy values */
1633     $groups_some = array();
1634     $groups_all  = array();
1635     $groups_uid  = array();
1636     $uids        = array();
1637     $first       = TRUE;
1639     /* Get all groups used by currently edited users */
1640     $uid_filter="";  
1641     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1642       $uid = $this->multi_attrs_all['uid'][$i];
1643       $uids[] = $uid;
1644       $uid_filter.= "(memberUid=".$uid.")"; 
1645     }
1646     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1647     $ldap = $this->config->get_ldap_link();
1648     $ldap->cd($this->config->current['BASE']);
1649     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1650     while($group = $ldap->fetch()){
1651       $groups_some[$group['dn']] = $group['cn'][0];
1652       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1653         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1654       }
1655     }
1657     /* Create an array, containing all used groups */
1658     $groups_all = $groups_some;
1659     foreach($groups_all as $id => $group){
1660       foreach($uids as $uid){
1661         if(!in_array($uid,$groups_uid[$id])){
1662           unset($groups_all[$id]);
1663           break;
1664         }
1665       }
1666     }
1668     /* Assign group array */
1669     $this->groupMembership = $groups_all;
1671     /* Create an array of all grouops used by all users */
1672     foreach( $groups_all as $dn => $cn){
1673       if(isset($groups_some[$dn])){
1674         unset($groups_some[$dn]);
1675       }
1676     }
1677     $this->groupMembership_some = $groups_some;
1678     $this->primaryGroup = $this->gidNumber;
1680     /* Is this account a trustAccount? */
1681     if (isset($this->multi_attrs['trustModel'])){
1682       $this->trustModel= $this->multi_attrs['trustModel'][0];
1683       $this->was_trust_account= TRUE;
1684       $this->multi_boxes[] = "trustmode";
1685     } else {
1686       $this->was_trust_account= FALSE;
1687       $this->trustModel= "";
1688     }
1690     /* Create access informations */
1691     $this->accessTo = array();
1692     if (isset($this->multi_attrs['accessTo'])){
1693       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1694         $tmp= $this->multi_attrs['accessTo'][$i];
1695         $this->accessTo[$tmp]= $tmp;
1696       }
1697     }
1699     /* Adjust shadow checkboxes */
1700     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1701           "shadowExpire") as $val){
1702       if ($this->$val != 0){
1703         $oval= "activate_".$val;
1704         $this->$oval= "1";
1705       }
1706     }
1708     /* Convert to seconds */
1709     if(isset($this->multi_attrs['shadowExpire'])){
1710       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1711     }else{
1712       $this->activate_shadowExpire = FALSE;
1713     }
1714   }
1717   function set_multi_edit_values($attrs)
1718   {
1719     $groups = array();
1721     /* Update groupMembership, keep optinal group */
1722     foreach($attrs['groupMembership_some'] as $dn => $cn){
1723       if(isset($this->groupMembership[$dn])){
1724         $groups[$dn] = $cn;
1725       }
1726     }
1727     /* Update groupMembership, add forced groups */
1728     foreach($attrs['groupMembership'] as $dn => $cn){
1729       $groups[$dn] = $cn;
1730     }
1731     plugin::set_multi_edit_values($attrs);
1732     $this->groupMembership = $groups;
1733   }
1736 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1737 ?>