Code

Added skip values
[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 settings";
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     }
642     /* Remove and write to LDAP */
643     plugin::remove_from_parent();
645     /* Zero out array */
646     $this->attrs['gosaHostACL']= array();
648     /* Keep uid, because we need it for authentification! */
649     unset($this->attrs['uid']);
650     unset($this->attrs['trustModel']);
652     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
653     /* include global link_info */
654      $this->attributes, "Save");
655     $ldap= $this->config->get_ldap_link();
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     /* Adapt shadow values */
782     if (!$this->activate_shadowExpire){
783       $this->shadowExpire= "0";
784     } else {
785       /* Transform seconds to days here */
786       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
787     }
788     if (!$this->activate_shadowMax){
789       $this->shadowMax= "0";
790     }
791     if ($this->mustchangepassword){
792       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
793     } else {
794       $this->shadowLastChange= (int)(date("U") / 86400);
795     }
796     if (!$this->activate_shadowWarning){
797       $this->shadowWarning= "0";
798     }
800     /* Check what to do with ID's */
801     if ($this->force_ids == 0){
803       /* Use id's that are already set */
804       if ($this->savedUidNumber != ""){
805         $this->uidNumber= $this->savedUidNumber;
806         $this->gidNumber= $this->savedGidNumber;
807       } else {
809         /* Calculate new id's. We need to place a lock before calling get_next_id
810            to get real unique values. */
811         $wait= 10;
812         while (get_lock("uidnumber") != ""){
813           sleep (1);
815           /* Oups - timed out */
816           if ($wait-- == 0){
817             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
818             break;
819           }
820         }
822         add_lock ("uidnumber", "gosa");
823         $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
824         if ($this->savedGidNumber != ""){
825           $this->gidNumber= $this->savedGidNumber;
826         } else {
827           $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
828         }
829       }
831       if ($this->primaryGroup != 0){
832         $this->gidNumber= $this->primaryGroup;
833       }
834     }
836     if ($this->activate_shadowMin != "1" ) {
837       $this->shadowMin = "";
838     }
840     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
841       $this->shadowMax = "";
842     }
844     if ($this->activate_shadowWarning != "1" ) {
845       $this->shadowWarning = "";
846     }
848     if ($this->activate_shadowInactive != "1" ) {
849       $this->shadowInactive = "";
850     }
852     if ($this->activate_shadowExpire != "1" ) {
853       $this->shadowExpire = "";
854     }
856     /* Fill gecos */
857     if (isset($this->parent) && $this->parent !== NULL){
858       $this->gecos= rewrite($this->parent->by_object['user']->cn);
859       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
860         $this->gecos= "";
861       }
862     }
864     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
865       $this->$attr = (int) $this->$attr;
866     }
867     /* Call parents save to prepare $this->attrs */
868     plugin::save();
870     /* Trust accounts */
871     $objectclasses= array();
872     foreach ($this->attrs['objectClass'] as $key => $class){
873       if (preg_match('/trustAccount/i', $class)){
874         continue;
875       }
876       $objectclasses[]= $this->attrs['objectClass'][$key];
877     }
878     $this->attrs['objectClass']= $objectclasses;
879     if ($this->trustModel != ""){
880       $this->attrs['objectClass'][]= "trustAccount";
881       $this->attrs['trustModel']= $this->trustModel;
882       $this->attrs['accessTo']= array();
883       if ($this->trustModel == "byhost"){
884         foreach ($this->accessTo as $host){
885           $this->attrs['accessTo'][]= $host;
886         }
887       }
888     } else {
889       if ($this->was_trust_account){
890         $this->attrs['accessTo']= array();
891         $this->attrs['trustModel']= array();
892       }
893     }
895     if(empty($this->attrs['gosaDefaultPrinter'])){
896       $thid->attrs['gosaDefaultPrinter']=array();
897     }
900     /* include global link_info */
901     $this->cleanup();
902  
903     /* This is just a test, we have had duplicated ids 
904         in the past when copy & paste was used. 
905        Normaly this should not happen.
906      */ 
907     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
908       $used = $this->get_used_uid_numbers();
909       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
910         msg_dialog::display(_("Warning"),_("A duplicated UID number was written for this user. If this was not intended please verify all used uidNumbers!"), WARNING_DIALOG);
911       }
912     }
914     $ldap= $this->config->get_ldap_link();
915     $ldap->cd($this->dn);
916     unset($this->attrs['uid']);
917     $ldap->modify ($this->attrs); 
919     /* Log last action */ 
920     if($this->initially_was_account){
921       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
922     }else{
923       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
924     }
926     if (!$ldap->success()){
927       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
928     }
930     /* Remove lock needed for unique id generation */
931     del_lock ("uidnumber");
933     /* Posix accounts have group interrelationship, 
934        take care about these here if this is a new user without forced gidNumber. */
935     if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
936       $ldap->cd($this->config->current['BASE']);
937       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
939       /* Create group if it doesn't exist */
940       if ($ldap->count() == 0){
941         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
943         $g= new group($this->config, $groupdn);
944         $g->cn= $this->uid;
945         $g->force_gid= 1;
946         $g->gidNumber= $this->gidNumber;
947         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
948         $g->save ();
949       }
950     }
952     /* Take care about groupMembership values: add to groups */
953     foreach ($this->groupMembership as $key => $value){
954       if (!isset($this->savedGroupMembership[$key])){
955         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
956         $g->set_acl_base($key);
957         $g->by_object['group']->addUser($this->uid);
958         $g->save();
959       }
960     }
962     /* Remove from groups not listed in groupMembership */
963     foreach ($this->savedGroupMembership as $key => $value){
964       if (!isset($this->groupMembership[$key])){
965         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
966         $g->set_acl_base($key);
967         $g->by_object['group']->removeUser ($this->uid);
968         $g->save();
969       }
970     }
972     /* Optionally execute a command after we're done */
973     if ($this->initially_was_account == $this->is_account){
974       if ($this->is_modified){
975         $this->handle_post_events("modify",array("uid" => $this->uid));
976       }
977     } else {
978       $this->handle_post_events("add" ,array("uid"=> $this->uid));
979     }
980   }
982   /* Check formular input */
983   function check()
984   {
985     /* Include global link_info */
986     $ldap= $this->config->get_ldap_link();
988     /* Append groups as memberGroup: to check hook 
989      */
990     $tmp_attributes  = $this->attributes;    
991     $this->attributes[] = "memberGroup";
992     $this->memberGroup = array();
993     foreach($this->groupMembership as $dn => $name){
994       $this->memberGroup[] = $name;
995     }
997     /* Call common method to give check the hook */
998     $message= plugin::check();
999     $this->attributes = $tmp_attributes;
1001     /* must: homeDirectory */
1002     if ($this->homeDirectory == ""){
1003       $message[]= msgPool::required(_("Home directory"));
1004     }
1005     if (!tests::is_path($this->homeDirectory)){
1006       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1007     }
1009     /* Check ID's if they are forced by user */
1010     if ($this->force_ids == "1"){
1012       /* Valid uid/gid? */
1013       if (!tests::is_id($this->uidNumber)){
1014         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1015       } else {
1016         if ($this->uidNumber < $this->config->current['MINID']){
1017           $message[]= msgPool::toosmall(_("UID"), $this->config->current['MINID']);
1018         }
1019       }
1020       if (!tests::is_id($this->gidNumber)){
1021         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1022       } else {
1023         if ($this->gidNumber < $this->config->current['MINID']){
1024           $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1025         }
1026       }
1027     }
1029     /* Check shadow settings, well I like spaghetties... */
1030     if ($this->activate_shadowMin){
1031       if (!tests::is_id($this->shadowMin)){
1032         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1033       }
1034     }
1035     if ($this->activate_shadowMax){
1036       if (!tests::is_id($this->shadowMax)){
1037         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1038       }
1039     }
1040     if ($this->activate_shadowWarning){
1041       if (!tests::is_id($this->shadowWarning)){
1042         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1043       }
1044       if (!$this->activate_shadowMax){
1045         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1046       }
1047       if ($this->shadowWarning > $this->shadowMax){
1048         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1049       }
1050       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1051         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1052       }
1053     }
1054     if ($this->activate_shadowInactive){
1055       if (!tests::is_id($this->shadowInactive)){
1056         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1057       }
1058       if (!$this->activate_shadowMax){
1059         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1060       }
1061     }
1062     if ($this->activate_shadowMin && $this->activate_shadowMax){
1063       if ($this->shadowMin > $this->shadowMax){
1064         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1065       }
1066     }
1068     return ($message);
1069   }
1072   function multiple_check()
1073   {
1074     $message = plugin::multiple_check();
1075     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1076       $message[]= msgPool::required(_("Home directory"));
1077     }
1078     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1079       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1080     }
1082     /* Check shadow settings, well I like spaghetties... */
1083     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1084       if (!tests::is_id($this->shadowMin)){
1085         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1086       }
1087     }
1088     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1089       if (!tests::is_id($this->shadowMax)){
1090         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1091       }
1092     }
1093     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1094       if (!tests::is_id($this->shadowWarning)){
1095         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1096       }
1097       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1098         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1099       }
1100       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1101         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1102       }
1103       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1104         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1105       }
1106     }
1107     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1108       if (!tests::is_id($this->shadowInactive)){
1109         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1110       }
1111       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1112         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1113       }
1114     }
1115     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1116       if ($this->shadowMin > $this->shadowMax){
1117         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1118       }
1119     }
1121     return($message);
1122   }
1125   function addGroup ($groups)
1126   {
1127     /* include global link_info */
1128     $ldap= $this->config->get_ldap_link();
1130     /* Walk through groups and add the descriptive entry if not exists */
1131     foreach ($groups as $value){
1133       if (!array_key_exists($value, $this->groupMembership)){
1134         $ldap->cat($value, array('cn', 'description', 'dn'));
1135         $attrs= $ldap->fetch();
1136         error_reporting (0);
1137         if (!isset($attrs['description'][0])){
1138           $entry= $attrs["cn"][0];
1139         } else {
1140           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1141           $entry= $attrs["cn"][0]." [$dsc]";
1142         }
1143         error_reporting (E_ALL | E_STRICT);
1145         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1146           $this->groupMembership[$attrs['dn']]= $entry;
1147           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1148             unset($this->groupMembership_some[$attrs['dn']]);
1149           }
1150         }
1151       }
1152     }
1154     /* Sort groups */
1155     asort ($this->groupMembership);
1156     reset ($this->groupMembership);
1157   }
1160   /* Del posix user from some groups */
1161   function delGroup ($groups)
1162   {
1163     $dest= array();
1164     foreach($groups as $dn_to_del){
1165       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1166         unset($this->groupMembership[$dn_to_del]);
1167       }
1168       if($this->multiple_support_active){
1169         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1170           unset($this->groupMembership_some[$dn_to_del]);
1171         }
1172       }
1173     }
1174   }
1177   /* Adapt from template, using 'dn' */
1178   function adapt_from_template($dn, $skip= array())
1179   {
1180     /* Include global link_info */
1181     $ldap= $this->config->get_ldap_link();
1183     plugin::adapt_from_template($dn, $skip);
1184     $template= $this->attrs['uid'][0];
1186     /* Adapt group membership */
1187     $ldap->cd($this->config->current['BASE']);
1188     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1190     while ($this->attrs= $ldap->fetch()){
1191       if (!isset($this->attrs["description"][0])){
1192         $entry= $this->attrs["cn"][0];
1193       } else {
1194         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1195       }
1196       $this->groupMembership[$ldap->getDN()]= $entry;
1197     }
1199     /* Fix primary group settings */
1200     $ldap->cd($this->config->current['BASE']);
1201     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1202     if ($ldap->count() != 1){
1203       $this->primaryGroup= $this->gidNumber;
1204     }
1206     $ldap->cd($this->config->current['BASE']);
1207     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1208     while($attr = $ldap->fetch()){
1209       $tmp = $attr['accessTo'];
1210       unset ($tmp['count']);
1211       $this->accessTo = $tmp;   
1212     }
1214     /* Adjust shadow checkboxes */
1215     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1216       if ($this->$val != 0){
1217         $oval= "activate_".$val;
1218         $this->$oval= "1";
1219       }
1220     }
1222     /* FIXME: NEED review of this section */
1223     /* Need to check shadowExpire separately */
1225     /* 
1226      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1227      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1228      */
1229     if(abs($this->shadowExpire - time())>86400) {
1230       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1231     }
1232     
1233     /* Only enable checkbox, if shadowExpire is in the future */
1234     if($this->shadowExpire > time()) {
1235       $this->activate_shadowExpire= "1";
1236     }
1237   }
1239   function convertToSeconds($val)
1240   {
1241     if ($val != 0){
1242       $val*= 60 * 60 * 24;
1243     } else {
1244       $date= getdate();
1245       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1246     }
1247     return($val);
1248   }
1251   function get_used_uid_numbers()
1252   {
1253     $ids= array();
1254     $ldap= $this->config->get_ldap_link();
1256     $ldap->cd ($this->config->current['BASE']);
1257     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1259     /* Get list of ids */
1260     while ($attrs= $ldap->fetch()){
1261       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1262     }
1263     return($ids);
1264   }
1266   
1267   function get_next_id($attrib, $dn)
1268   {
1269     $ids= array();
1270     $ldap= $this->config->get_ldap_link();
1272     $ldap->cd ($this->config->current['BASE']);
1273     if (preg_match('/gidNumber/i', $attrib)){
1274       $oc= "posixGroup";
1275     } else {
1276       $oc= "posixAccount";
1277     }
1278     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1280     /* Get list of ids */
1281     while ($attrs= $ldap->fetch()){
1282       $ids[]= (int)$attrs["$attrib"][0];
1283     }
1285     /* Add the nobody id */
1286     $ids[]= 65534;
1288     /* get the ranges */
1289     $tmp = array('0'=> 1000); 
1290     if (preg_match('/posixAccount/', $oc) && isset($this->config->current['UIDBASE'])) {
1291       $tmp= split('-',$this->config->current['UIDBASE']);
1292     } elseif(isset($this->config->current['GIDBASE'])){
1293       $tmp= split('-',$this->config->current['GIDBASE']);
1294     }
1296     /* Set hwm to max if not set - for backward compatibility */
1297     $lwm= $tmp[0];
1298     if (isset($tmp[1])){
1299       $hwm= $tmp[1];
1300     } else {
1301       $hwm= pow(2,32);
1302     }
1304     /* Find out next free id near to UID_BASE */
1305     if (!isset($this->config->current['BASE_HOOK'])){
1306       $base= $lwm;
1307     } else {
1308       /* Call base hook */
1309       $base= get_base_from_hook($dn, $attrib);
1310     }
1311     for ($id= $base; $id++; $id < pow(2,32)){
1312       if (!in_array($id, $ids)){
1313         return ($id);
1314       }
1315     }
1317     /* Should not happen */
1318     if ($id == $hwm){
1319       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1320       exit;
1321     }
1323   }
1325   function reload()
1326   {
1327     /* Set base for all searches */
1328     $base      = session::get('CurrentMainBase');
1329     $base     = $base;
1330     $ldap     = $this->config->get_ldap_link();    
1331     $attrs    =  array("cn", "description", "gidNumber");
1332     $Flags    = GL_SIZELIMIT;
1334     /* Get groups */
1335     if ($this->GroupUserRegex == '*'){
1336       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1337     } else {
1338       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1339     }
1340     if($this->SubSearch){
1341       $Flags |= GL_SUBSEARCH;
1342     }else{
1343       $base = get_groups_ou().$base;
1344     }
1346     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1347   
1348     /* check sizelimit */
1349     if (preg_match("/size limit/i", $ldap->get_error())){
1350       session::set('limit_exceeded',TRUE);
1351     }
1353     /* Create a list of users */
1354     $this->grouplist = array();
1355     foreach ($res as $value){
1356       $this->grouplist[$value['gidNumber'][0]]= $value;
1357     }
1359     $tmp=array();
1360     foreach($this->grouplist as $tkey => $val ){
1361       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1362     }
1364     /* Sort index */
1365     ksort($tmp);
1367     /* Recreate index array[dn]=cn[description]*/
1368     $this->grouplist=array();
1369     foreach($tmp as $val){
1370       if(isset($val['description'])){
1371         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1372       }else{
1373         $this->grouplist[$val['dn']]=$val['cn'][0];
1374       }
1375     }
1377     reset ($this->grouplist);
1378   }
1381   /* Get posts from copy & paste dialog */ 
1382   function saveCopyDialog()
1383   {
1384     if(isset($_POST['homeDirectory'])){
1385       $this->homeDirectory = $_POST['homeDirectory'];
1386       if (isset ($_POST['force_ids'])){
1387         $data= 1;
1388         $this->gidNumber = $_POST['gidNumber'];
1389         $this->uidNumber = $_POST['uidNumber'];
1390       } else {
1391         $data= 0;
1392       }
1393       if ($this->force_ids != $data){
1394         $this->is_modified= TRUE;
1395       }
1396       $this->force_ids= $data;
1397     }
1398   }
1399  
1401   /* Create the posix dialog part for copy & paste */
1402   function getCopyDialog()
1403   {
1404     /* Skip dialog creation if this is not a valid account*/
1405     if(!$this->is_account) return("");
1406     if ($this->force_ids == 1){
1407       $force_ids = "checked";
1408       if (session::get('js')){
1409         $forceMode = "";
1410       }
1411     } else {
1412       if (session::get('js')){
1413         if($this->acl != "#none#")
1414           $forceMode ="disabled";
1415       }
1416       $force_ids = "";
1417     }
1419     $sta = "";
1421     /* Open group add dialog */
1422     if(isset($_POST['edit_groupmembership'])){
1423       $this->group_dialog = TRUE;
1424       $sta = "SubDialog";
1425     }
1427     /* If the group-add dialog is closed, call execute 
1428        to ensure that the membership is updatd */
1429     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1430       $this->execute();
1431       $this->group_dialog =FALSE;
1432     }
1434     if($this->group_dialog){
1435       $str = $this->execute(true);
1436       $ret = array();
1437       $ret['string'] = $str;
1438       $ret['status'] = $sta;
1439       return($ret);
1440     }
1442     /* If a group member should be deleted, simply call execute */
1443     if(isset($_POST['delete_groupmembership'])){
1444       $this->execute();
1445     }
1447     /* Assigned informations to smarty */
1448     $smarty = get_smarty();
1449     $smarty->assign("homeDirectory",$this->homeDirectory);
1450     $smarty->assign("uidNumber",$this->uidNumber);
1451     $smarty->assign("gidNumber",$this->gidNumber);
1452     $smarty->assign("forceMode",$forceMode);
1453     $smarty->assign("force_ids",$force_ids);
1454     if (!count($this->groupMembership)){
1455       $smarty->assign("groupMembership", array("&nbsp;"));
1456     } else {
1457       $smarty->assign("groupMembership", $this->groupMembership);
1458     }
1460     /* Display wars message if there are more than 16 group members */
1461     if (count($this->groupMembership) > 16){
1462       $smarty->assign("groups", "too_many_for_nfs");
1463     } else {
1464       $smarty->assign("groups", "");
1465     }
1466     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1468     $ret = array();
1469     $ret['string'] = $str;
1470     $ret['status'] = $sta;
1471     return($ret);
1472   }
1475   function PrepareForCopyPaste($source)
1476   {
1477     plugin::PrepareForCopyPaste($source);
1479     /* Avoid using the same gid/uid number as source user */
1480     $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1481     $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1482   }
1485   function multiple_execute()
1486   {
1487     return($this->execute());
1488   }
1491   static function plInfo()
1492   {
1493     return (array(
1494           "plDescription"     => _("POSIX account"),
1495           "plSelfModify"      => TRUE,
1496           "plDepends"         => array("user"),
1497           "plPriority"        => 2,
1498           "plSection"         => array("personal" => _("My account")),
1499           "plCategory"        => array("users"),
1500           "plOptions"         => array(),
1502           "plProvidedAcls"  => array(
1504             "homeDirectory"       =>  _("Home directory"), 
1505             "loginShell"          =>  _("Shell"),
1506             "uidNumber"           =>  _("User ID"),
1507             "gidNumber"           =>  _("Group ID"),
1509             "mustchangepassword"=>  _("Force password change on login"),
1510             "shadowMin"           =>  _("Shadow min"),
1511             "shadowMax"           =>  _("Shadow max"),
1512             "shadowWarning"       =>  _("Shadow warning"),
1513             "shadowInactive"      =>  _("Shadow inactive"),
1514             "shadowExpire"        =>  _("Shadow expire"),
1515             "trustModel"          =>  _("System trust model")))
1516             );
1517   }
1520   /* Return selected values for multiple edit */
1521   function get_multi_edit_values()
1522   {
1523     $ret = plugin::get_multi_edit_values();
1524     $ret['groupMembership']     = $this->groupMembership;
1525     $ret['groupMembership_some']= $this->groupMembership_some;
1527     if(in_array("primaryGroup",$this->multi_boxes)){
1528       $ret['primaryGroup'] = $this->primaryGroup;
1529     }
1530     if(in_array("trustmode",$this->multi_boxes)){
1531       $ret['trustModel'] = $this->trustModel;
1532       $ret['accessTo'] = $this->accessTo;
1533     }
1534     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1535       $active = "activate_".$entry;
1536       if(in_array($active,$this->multi_boxes)){
1537         $ret[$entry] = $this->$entry;
1538         $ret[$active] = $this->$active;
1539       }
1540     }
1541     if(in_array("mustchangepassword",$this->multi_boxes)){
1542       $ret['mustchangepassword'] = $this->mustchangepassword;
1543     }
1544     return($ret);
1545   }
1548   /* Save posts for multiple edit 
1549    */
1550   function multiple_save_object()
1551   {
1552     if(isset($_POST['posix_mulitple_edit'])){
1553  
1554       /* Backup expire value */ 
1555       $expire_tmp = $this->shadowExpire;
1556   
1557       /* Update all values */
1558       plugin::multiple_save_object();
1560       /* Get selected checkboxes */
1561       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1562         if(isset($_POST["use_".$val])){
1563           $this->multi_boxes[] = $val;
1564         }
1565       }
1567       /* Update special values, checkboxes for posixShadow */
1568       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1569         if($this->acl_is_writeable($var)){
1570           $activate_var = "activate_".$var;
1571           if(in_array($activate_var, $this->multi_boxes)){
1572             if(isset($_POST['activate_'.$var])){
1573               $this->$activate_var  = true;
1574               $this->$var      = $_POST[$var];
1575             }else{
1576               $this->$activate_var  = false;
1577               $this->$var      = 0;
1578             }
1579           }
1580         }
1581       }
1583       /* Restore shadow value, if the shadow attribute isn't used */
1584       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1585         $this->shadowExpire = $expire_tmp;
1586       }
1588       /* Force change password ? */
1589       if(isset($_POST['mustchangepassword'])){
1590         $this->mustchangepassword = TRUE;
1591       }else{
1592         $this->mustchangepassword = FALSE;
1593       }
1595       /* Trust mode - special handling */
1596       if($this->acl_is_writeable("trustModel")){
1597         if (isset($_POST['trustmode'])){
1598           $saved= $this->trustModel;
1599           if ($_POST['trustmode'] == "1"){
1600             $this->trustModel= "fullaccess";
1601           } elseif ($_POST['trustmode'] == "2"){
1602             $this->trustModel= "byhost";
1603           } else {
1604             $this->trustModel= "";
1605           }
1606           if ($this->trustModel != $saved){
1607             $this->is_modified= TRUE;
1608           }
1609         }
1610       }
1612       /* Save primary group settings */
1613       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1614         $data= $_POST['primaryGroup'];
1615         if ($this->primaryGroup != $data){
1616           $this->is_modified= TRUE;
1617         }
1618         $this->primaryGroup= $_POST['primaryGroup'];
1619       }
1620     }
1621   }
1623   
1624   /* Initialize plugin with given atribute arrays 
1625    */
1626   function init_multiple_support($attrs,$all)
1627   {
1628     plugin::init_multiple_support($attrs,$all);
1630     /* Some dummy values */
1631     $groups_some = array();
1632     $groups_all  = array();
1633     $groups_uid  = array();
1634     $uids        = array();
1635     $first       = TRUE;
1637     /* Get all groups used by currently edited users */
1638     $uid_filter="";  
1639     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1640       $uid = $this->multi_attrs_all['uid'][$i];
1641       $uids[] = $uid;
1642       $uid_filter.= "(memberUid=".$uid.")"; 
1643     }
1644     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1645     $ldap = $this->config->get_ldap_link();
1646     $ldap->cd($this->config->current['BASE']);
1647     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1648     while($group = $ldap->fetch()){
1649       $groups_some[$group['dn']] = $group['cn'][0];
1650       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1651         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1652       }
1653     }
1655     /* Create an array, containing all used groups */
1656     $groups_all = $groups_some;
1657     foreach($groups_all as $id => $group){
1658       foreach($uids as $uid){
1659         if(!in_array($uid,$groups_uid[$id])){
1660           unset($groups_all[$id]);
1661           break;
1662         }
1663       }
1664     }
1666     /* Assign group array */
1667     $this->groupMembership = $groups_all;
1669     /* Create an array of all grouops used by all users */
1670     foreach( $groups_all as $dn => $cn){
1671       if(isset($groups_some[$dn])){
1672         unset($groups_some[$dn]);
1673       }
1674     }
1675     $this->groupMembership_some = $groups_some;
1676     $this->primaryGroup = $this->gidNumber;
1678     /* Is this account a trustAccount? */
1679     if (isset($this->multi_attrs['trustModel'])){
1680       $this->trustModel= $this->multi_attrs['trustModel'][0];
1681       $this->was_trust_account= TRUE;
1682       $this->multi_boxes[] = "trustmode";
1683     } else {
1684       $this->was_trust_account= FALSE;
1685       $this->trustModel= "";
1686     }
1688     /* Create access informations */
1689     $this->accessTo = array();
1690     if (isset($this->multi_attrs['accessTo'])){
1691       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1692         $tmp= $this->multi_attrs['accessTo'][$i];
1693         $this->accessTo[$tmp]= $tmp;
1694       }
1695     }
1697     /* Adjust shadow checkboxes */
1698     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1699           "shadowExpire") as $val){
1700       if ($this->$val != 0){
1701         $oval= "activate_".$val;
1702         $this->$oval= "1";
1703       }
1704     }
1706     /* Convert to seconds */
1707     if(isset($this->multi_attrs['shadowExpire'])){
1708       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1709     }else{
1710       $this->activate_shadowExpire = FALSE;
1711     }
1712   }
1715   function set_multi_edit_values($attrs)
1716   {
1717     $groups = array();
1719     /* Update groupMembership, keep optinal group */
1720     foreach($attrs['groupMembership_some'] as $dn => $cn){
1721       if(isset($this->groupMembership[$dn])){
1722         $groups[$dn] = $cn;
1723       }
1724     }
1725     /* Update groupMembership, add forced groups */
1726     foreach($attrs['groupMembership'] as $dn => $cn){
1727       $groups[$dn] = $cn;
1728     }
1729     plugin::set_multi_edit_values($attrs);
1730     $this->groupMembership = $groups;
1731   }
1734 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1735 ?>