Code

Enabled prototype
[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= "";
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 $gotoLastSystemLogin= "";
71   var $group_dialog= FALSE;
72   var $show_ws_dialog= FALSE;
73   var $secondaryGroups= array();
74   var $primaryGroup= 0;
75   var $was_trust_account= FALSE;
76   var $memberGroup= array();
77   var $grouplist= array();
78   var $ui= array();
79   var $ssh= null;
80   var $sshAcl= "";
82   var $GroupRegex= "*";
83   var $GroupUserRegex= "*";
84   var $SubSearch= false;
86   var $view_logged= false;
88   /* attribute list for save action */
89   var $CopyPasteVars  = 
90       array("grouplist","groupMembership","activate_shadowMin",
91       "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
92       "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
94   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
95       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
96       "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel", "gotoLastSystemLogin");
98   var $objectclasses= array("posixAccount", "shadowAccount");
100   var $uid= "";
101   var $multiple_support = TRUE;
102   var $groupMembership_some = array();
104   /* constructor, if 'dn' is set, the node loads the given
105      'dn' from LDAP */
106   function posixAccount (&$config, $dn= NULL)
107   {
108     global $class_mapping;
110     /* Configuration is fine, allways */
111     $this->config= $config;
113     /* Load bases attributes */
114     plugin::plugin($config, $dn);
116     /*  If gotoLastSystemLogin is available read it from ldap and create a readable
117         date time string, fallback to sambaLogonTime if available.
118      */
119     if(isset($this->attrs['gotoLastSystemLogin'][0]) && preg_match("/^[0-9]*$/",$this->attrs['gotoLastSystemLogin'][0])){
120       $this->gotoLastSystemLogin = date("d.m.Y H:i:s", strtotime($this->attrs['gotoLastSystemLogin'][0]));
121     } else if(isset($this->attrs['sambaLogonTime'][0]) && preg_match("/^[0-9]*$/",$this->attrs['sambaLogonTime'][0])){
122       $this->gotoLastSystemLogin = date("d.m.Y H:i:s", $this->attrs['sambaLogonTime'][0]);
123     }
125     /* Setting uid to default */
126     if(isset($this->attrs['uid'][0])){
127       $this->uid = $this->attrs['uid'][0];
128     }
130     $ldap= $this->config->get_ldap_link();
132     if ($dn !== NULL){
134       /* Correct is_account. shadowAccount is not required. */
135       if (isset($this->attrs['objectClass']) &&
136           in_array ('posixAccount', $this->attrs['objectClass'])){
138         $this->is_account= TRUE;
139       }
141       /* Is this account a trustAccount? */
142       if ($this->is_account && isset($this->attrs['trustModel'])){
143         $this->trustModel= $this->attrs['trustModel'][0];
144         $this->was_trust_account= TRUE;
145       } else {
146         $this->was_trust_account= FALSE;
147         $this->trustModel= "";
148       }
150       $this->accessTo = array(); 
151       if ($this->is_account && isset($this->attrs['accessTo'])){
152         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
153           $tmp= $this->attrs['accessTo'][$i];
154           $this->accessTo[$tmp]= $tmp;
155         }
156       }
157       $this->initially_was_account= $this->is_account;
159       /* Fill group */
160       $this->primaryGroup= $this->gidNumber;
162       /* Generate status text */
163       $current= date("U");
165       $current= floor($current / 60 /60 / 24);
167       if (($current >= $this->shadowExpire) && $this->shadowExpire){
168         $this->status= _("expired");
169         if (($current - $this->shadowExpire) < $this->shadowInactive){
170           $this->status.= ", "._("grace time active");
171         }
172       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
173         $this->status= _("active").", "._("password not changeable");
174       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
175         $this->status= _("active").", "._("password expired");
176       } else {
177         $this->status= _("active");
178       }
180       /* Get group membership */
181       $ldap->cd($this->config->current['BASE']);
182       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
184       while ($attrs= $ldap->fetch()){
185         if (!isset($attrs["description"][0])){
186           $entry= $attrs["cn"][0];
187         } else {
188           $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
189         }
190         $this->groupMembership[$ldap->getDN()]= $entry;
191       }
192       asort($this->groupMembership);
193       reset($this->groupMembership);
194       $this->savedGroupMembership= $this->groupMembership;
195       $this->savedUidNumber= $this->uidNumber;
196       $this->savedGidNumber= $this->gidNumber;
198       // Instanciate SSH object if available
199       if (isset($class_mapping["sshPublicKey"])){
200         if (empty($this->acl_base)){
201           $this->acl_base= $config->current['BASE'];
202         }
204         $this->sshAcl= $this->getacl("sshPublicKey");
205         $this->ssh= new sshPublicKey($this->config, $this->dn, $this->sshAcl);
206       }
207     }
209     /* Adjust shadow checkboxes */
210     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
211           "shadowExpire") as $val){
213       if ($this->$val != 0){
214         $oval= "activate_".$val;
215         $this->$oval= "1";
216       }
217     }
219     /* Convert shadowExpire for usage */
220     if ($this->shadowExpire == 0){
221       $this->shadowExpire= "";
222     } else {
223       $this->shadowExpire= date('d.m.Y', $this->shadowExpire * 60 * 60 * 24);
224     }
227     /* Generate shell list from CONFIG_DIR./shells */
228     if (file_exists(CONFIG_DIR.'/shells')){
229       $shells = file (CONFIG_DIR.'/shells');
230       foreach ($shells as $line){
231         if (!preg_match ("/^#/", $line)){
232           $this->loginShellList[]= trim($line);
233         }
234       }
235     } else {
236       if ($this->loginShell == ""){
237         $this->loginShellList[]= _("unconfigured");
238       }
239     }
241     /* Insert possibly missing loginShell */
242     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
243       $this->loginShellList[]= $this->loginShell;
244     }
246     /* Generate group list */
247     $this->ui = get_userinfo(); 
248     $this->secondaryGroups[]= "- "._("automatic")." -";
249     $ldap->cd($this->config->current['BASE']);
250     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
251     while($attrs = $ldap->fetch()){
252       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
253     }
254     asort ($this->secondaryGroups);
256     /* Get global filter config */
257     if (!session::is_set("sysfilter")){
258       $ui= get_userinfo();
259       $base= get_base_from_people($ui->dn);
260       $sysfilter= array( "depselect"       => $base,
261           "regex"           => "*");
262       session::set("sysfilter", $sysfilter);
263     }
264     $this->ui = get_userinfo();
265   }
268   /* execute generates the html output for this node */
269   function execute($isCopyPaste = false)
270   {
271     /* Call parent execute */
272     plugin::execute();
273     $display= "";
275     /* Log view */
276     if($this->is_account && !$this->view_logged){
277       $this->view_logged = TRUE;
278       new log("view","users/".get_class($this),$this->dn);
279     }
281     /* Department has changed? */
282     if(isset($_POST['depselect'])){
283       session::set('CurrentMainBase',validate($_POST['depselect']));
284     }
286     if($this->multiple_support_active){
287       $this->is_account = TRUE;
288     }
290     if(!$isCopyPaste && ! $this->multiple_support_active){
292       /* Do we need to flip is_account state? */
293       if(isset($_POST['modify_state'])){
294         if($this->is_account && $this->acl_is_removeable()){
295           $this->is_account= FALSE;
296         }elseif(!$this->is_account && $this->acl_is_createable()){
297           $this->is_account= TRUE;
298         }
299       }
301       /* Do we represent a valid posixAccount? */
302       if (!$this->is_account && $this->parent === NULL ){
303         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
304           msgPool::noValidExtension(_("POSIX"))."</b>";
305         $display.= back_to_main();
306         return ($display);
307       }
310       /* Show tab dialog headers */
311       if ($this->parent !== NULL){
312         if ($this->is_account){
313           if (isset($this->parent->by_object['sambaAccount'])){
314             $obj= $this->parent->by_object['sambaAccount'];
315           }
316           if (isset($obj) && $obj->is_account == TRUE &&
317               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
318               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
320             /* Samba3 dependency on posix accounts are enabled
321                in the moment, because I need to rely on unique
322                uidNumbers. There'll be a better solution later
323                on. */
324             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
325           } else {
326             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
327           }
328         } else {
329           $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
330           return($display);
331         }
332       }
333     }
335     /* Trigger group edit? */
336     if (isset($_POST['edit_groupmembership'])){
337       $this->group_dialog= TRUE;
338       $this->dialog= TRUE;
339     }
341     /* Cancel group edit? */
342     if (isset($_POST['add_groups_cancel']) ||
343         isset($_POST['add_groups_finish'])){
344       $this->group_dialog= FALSE;
345       $this->dialog= FALSE;
346     }
348     /* Add selected groups */
349     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
350         count($_POST['groups'])){
352       $this->addGroup ($_POST['groups']);
353     }
355     /* Delete selected groups */
356     if (isset($_POST['delete_groupmembership']) && 
357         isset($_POST['group_list']) && count($_POST['group_list'])){
359       $this->delGroup ($_POST['group_list']);
360     }
362     /* Add user workstation? */
363     if (isset($_POST["add_ws"])){
364       $this->show_ws_dialog= TRUE;
365       $this->dialog= TRUE;
366     }
368     /* Add user workstation? */
369     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
370       foreach($_POST['wslist'] as $ws){
371         $this->accessTo[$ws]= $ws;
372       }
373       ksort($this->accessTo);
374       $this->is_modified= TRUE;
375     }
377     /* Remove user workstations? */
378     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
379       foreach($_POST['workstation_list'] as $name){
380         unset ($this->accessTo[$name]);
381       }
382       $this->is_modified= TRUE;
383     }
385     /* Add user workstation finished? */
386     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
387       $this->show_ws_dialog= FALSE;
388       $this->dialog= FALSE;
389     }
391     /* Templates now! */
392     $smarty= get_smarty();
393     $smarty->assign("usePrototype", "true");
395     /* Show ws dialog */
396     if ($this->show_ws_dialog){
397       /* Save data */
398       $sysfilter= session::get("sysfilter");
399       foreach( array("depselect", "regex") as $type){
400         if (isset($_POST[$type])){
401           $sysfilter[$type]= $_POST[$type];
402         }
403       }
404       if (isset($_GET['search'])){
405         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
406         if ($s == "**"){
407           $s= "*";
408         }
409         $sysfilter['regex']= $s;
410       }
411       session::set("sysfilter", $sysfilter);
413       /* Get workstation list */
414       $exclude= "";
415       foreach($this->accessTo as $ws){
416         $exclude.= "(cn=$ws)";
417       }
418       if ($exclude != ""){
419         $exclude= "(!(|$exclude))";
420       }
421       $regex= $sysfilter['regex'];
423       $acls = array();
424       if(class_available("servgeneric")) $acls[] = "server";
425       if(class_available("workgeneric")) $acls[] = "worstation";
426       if(class_available("termgeneric")) $acls[] = "terminal";
428       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
430       $deps_a = array(get_ou("serverRDN"),
431                       get_ou("terminalRDN"),
432                       get_ou("workstationRDN")); 
434       $res= get_sub_list($filter, $acls, $deps_a, get_ou("systemRDN").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
435       $wslist= array();
436       foreach ($res as $attrs){
437         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
438       }
439       asort($wslist);
440       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
441       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
442       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
443       $smarty->assign("deplist", $this->config->idepartments);
444       $smarty->assign("alphabet", generate_alphabet());
445       foreach( array("depselect", "regex") as $type){
446         $smarty->assign("$type", $sysfilter[$type]);
447       }
448       $smarty->assign("hint", print_sizelimit_warning());
449       $smarty->assign("wslist", $wslist);
450       $smarty->assign("apply", apply_filter());
451       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
452       return ($display);
453     }
455     /* Manage group add dialog */
456     if ($this->group_dialog){
458       /* Get global filter config */
459       $this->reload();
461       /* remove already assigned groups */
462       $glist= array();
463       foreach ($this->grouplist as $key => $value){
464         if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
465           $glist[$key]= $value;
466         }
467       }
469       if($this->SubSearch){
470         $smarty->assign("SubSearchCHK"," checked ");
471       }else{
472         $smarty->assign("SubSearchCHK","");
473       }
475       $smarty->assign("regex",$this->GroupRegex);
476       $smarty->assign("guser",$this->GroupUserRegex);
477       $smarty->assign("groups", $glist);
478       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
479       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
480       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
481       $smarty->assign("deplist", $this->config->idepartments);
482       $smarty->assign("alphabet", generate_alphabet());
483       $smarty->assign("depselect", session::get('CurrentMainBase'));
484       $smarty->assign("hint", print_sizelimit_warning());
485       $smarty->assign("apply", apply_filter());
487       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
488       return ($display);
489     }
491     // Handle ssh dialog?
492     if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
493         $smarty->assign("usePrototype", "false");
494        if ($result= $this->ssh->execute()) {
495          $this->dialog= true;
496          return $result;
497        }
498        $this->dialog= false;
499     }
502     /* Show main page */
503     $smarty= get_smarty();
504     $smarty->assign("usePrototype", "true");
506     /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */ 
507     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
509     $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey", $SkipWrite));
511     /* Depending on pwmode, currently hardcoded because there are no other methods */
512     if ( 1 == 1 ){
513       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
515       $shadowMinACL     =  $this->getacl("shadowMin",$SkipWrite);
516       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
517                                               "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
519       $shadowMaxACL     =  $this->getacl("shadowMax",$SkipWrite);
520       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
521                                               "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
523       $shadowInactiveACL=  $this->getacl("shadowInactive",$SkipWrite);
524       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"), 
525                                               "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
527       $shadowWarningACL =  $this->getacl("shadowWarning",$SkipWrite);
528       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"), 
529                                               "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
531       foreach( array("activate_shadowMin", "activate_shadowMax",
532                      "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
533         if ($this->$val == 1){
534           $smarty->assign("$val", "checked");
535         } else {
536           $smarty->assign("$val", "");
537         }
538         $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
539       }
541       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
542     }
544     // Set last system login
545     $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
547     /* Fill arrays */
548     $smarty->assign("shells", $this->loginShellList);
549     $smarty->assign("secondaryGroups", $this->secondaryGroups);
550     $smarty->assign("primaryGroup", $this->primaryGroup);
551     if(!$this->multiple_support_active){
552       if (!count($this->groupMembership)){
553         $smarty->assign("groupMembership", array("&nbsp;"));
554       } else {
555         $smarty->assign("groupMembership", $this->groupMembership);
556       }
557     }else{
558       $smarty->assign("groupMembership", $this->groupMembership);
559       $smarty->assign("groupMembership_some", $this->groupMembership_some);
560     }
561     if (count($this->groupMembership) > 16){
562       $smarty->assign("groups", "too_many_for_nfs");
563     } else {
564       $smarty->assign("groups", "");
565     }
567     /* Avoid "Undefined index: forceMode" */
568     $smarty->assign("forceMode", "");
570     /* Checkboxes */
571     if ($this->force_ids == 1){
572       $smarty->assign("force_ids", "checked");
573       if (session::get('js')){
574         $smarty->assign("forceMode", "");
575       }
576     } else {
577       if (session::get('js')){
578         $smarty->assign("forceMode", "disabled");
579       }
580       $smarty->assign("force_ids", "");
581     }
583     /* Create onClick="" action string for the "Force UID/GID" option 
584      */
585     $onClickIDS ="";
586     if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
587       $onClickIDS .= "changeState('uidNumber');";
588     }
589     if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
590       $onClickIDS .= "changeState('gidNumber');";
591     }
592     $smarty->assign("onClickIDS", $onClickIDS);
593     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
595     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
596       if(in_array($val,$this->multi_boxes)){
597         $smarty->assign("use_".$val,TRUE);
598       }else{
599         $smarty->assign("use_".$val,FALSE);
600       }
601     }
604     /* Load attributes and acl's */
605     foreach($this->attributes as $val){
606       if(in_array($val,$this->multi_boxes)){
607         $smarty->assign("use_".$val,TRUE);
608       }else{
609         $smarty->assign("use_".$val,FALSE);
610       }
612       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
613       {
614         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
615         $smarty->assign("$val", $this->$val);
616         continue;
617       }
618       $smarty->assign("$val", $this->$val);
619       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
620     }
621     if($SkipWrite){
622       $smarty->assign("groupMembershipACL","r");
623     }else{
624       $smarty->assign("groupMembershipACL","rw");
625     }
626     $smarty->assign("status", $this->status);
628     /* Work on trust modes */
629     $smarty->assign("trusthide", " disabled ");
630     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
631     if ($this->trustModel == "fullaccess"){
632       $trustmode= 1;
633       // pervent double disable tag in html code, this will disturb our clean w3c html
634       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
636     } elseif ($this->trustModel == "byhost"){
637       $trustmode= 2;
638       $smarty->assign("trusthide", "");
639     } else {
640       // pervent double disable tag in html code, this will disturb our clean w3c html
641       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
642       $trustmode= 0;
643     }
644     $smarty->assign("trustmode", $trustmode);
645     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
646           2 => _("allow access to these hosts")));
650     if((count($this->accessTo))==0)
651       $smarty->assign("emptyArrAccess",true);
652     else
653       $smarty->assign("emptyArrAccess",false);
655       if($this->mustchangepassword){
656         $smarty->assign("mustchangepassword", " checked ");
657       } else {
658         $smarty->assign("mustchangepassword", "");
659       }
661     $smarty->assign("workstations", $this->accessTo);
663     // Add SSH button if available
664     $smarty->assign("sshPublicKey", $this->ssh?1:0);
666     $smarty->assign("apply", apply_filter());
667     $smarty->assign("multiple_support" , $this->multiple_support_active);
668     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
669     return($display);
670   }
673   /* remove object from parent */
674   function remove_from_parent()
675   {
676     /* Cancel if there's nothing to do here */
677     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
678       return;
679     }
682     /* Remove and write to LDAP */
683     plugin::remove_from_parent();
685     /* Zero out array */
686     $this->attrs['gosaHostACL']= array();
688     /* Keep uid, because we need it for authentification! */
689     unset($this->attrs['uid']);
690     unset($this->attrs['trustModel']);
692     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
693     /* include global link_info */
694      $this->attributes, "Save");
695     $ldap= $this->config->get_ldap_link();
696     $ldap->cd($this->dn);
697     $this->cleanup();
698     $ldap->modify ($this->attrs); 
700     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
702     if (!$ldap->success()){
703       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
704     }
706     /* Delete group only if cn is uid and there are no other
707        members inside */
708     $ldap->cd ($this->config->current['BASE']);
709     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
710     if ($ldap->count() != 0){
711       $attrs= $ldap->fetch();
712       if ($attrs['cn'][0] == $this->uid &&
713           !isset($this->attrs['memberUid'])){
715         $ldap->rmDir($ldap->getDN());
716       }
717     }
719     /* Optionally execute a command after we're done */
720     $this->handle_post_events("remove",array("uid" => $this->uid));
721   }
724   function save_object()
725   {
726     if (isset($_POST['posixTab'])){
727       /* Save values to object */
728       plugin::save_object();
731       /* Save force GID checkbox */
732       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
733         if (isset ($_POST['force_ids'])){
734           $data= 1;
735         } else {
736           $data= 0;
737         }
738         if ($this->force_ids != $data){
739           $this->is_modified= TRUE;
740         }
741         $this->force_ids= $data;
742       }
744       /*Save primary group settings */
745       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
746         $data= $_POST['primaryGroup'];
747         if ($this->primaryGroup != $data){
748           $this->is_modified= TRUE;
749         }
750         $this->primaryGroup= $_POST['primaryGroup'];
751       }
753       /* Get seelcted shadow checkboxes */
754       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
755         if($this->acl_is_writeable($var)){
756           $activate_var = "activate_".$var;
757           if(isset($_POST['activate_'.$var])){
758             $this->$activate_var  = true;
759             $this->$var      = $_POST[$var];
760           }else{
761             $this->$activate_var  = false;
762             if ($var != "shadowExpire") {
763               $this->$var      = 0;
764             }
765           }
766         }
767       }
769       /* Force change password ? */
770       if(isset($_POST['mustchangepassword'])){
771         $this->mustchangepassword = TRUE;
772       }else{
773         $this->mustchangepassword = FALSE;
774       }
776       /* Trust mode - special handling */
777       if($this->acl_is_writeable("trustModel")){
778         if (isset($_POST['trustmode'])){
779           $saved= $this->trustModel;
780           if ($_POST['trustmode'] == "1"){
781             $this->trustModel= "fullaccess";
782           } elseif ($_POST['trustmode'] == "2"){
783             $this->trustModel= "byhost";
784           } else {
785             $this->trustModel= "";
786           }
787           if ($this->trustModel != $saved){
788             $this->is_modified= TRUE;
789           }
790         }
791       }
792     }
794     /* Get regex from alphabet */
795     if(isset($_GET['search'])){
796       $this->GroupRegex = $_GET['search']."*";
797     }
799     /* Check checkboxes and regexes */
800     if(isset($_POST["PosixGroupDialogPosted"])){
802       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
803         $this->SubSearch = true;
804       }else{
805         $this->SubSearch = false;
806       }
807       if(isset($_POST['guser'])){
808         $this->GroupUserRegex = $_POST['guser'];
809       }
810       if(isset($_POST['regex'])){
811         $this->GroupRegex = $_POST['regex'];
812       }
813     }
814     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
815     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
816   }
819   /* Save data to LDAP, depending on is_account we save or delete */
820   function save()
821   {
823     /* Adapt shadow values */
824     if (!$this->activate_shadowExpire){
825       $this->shadowExpire= "0";
826     } else {
827       /* Transform date to days since the beginning */
828       list($day, $month, $year)= split('\.', $this->shadowExpire, 3);
829       $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
830     }
831     if (!$this->activate_shadowMax){
832       $this->shadowMax= "0";
833     }
834     if ($this->mustchangepassword){
835       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
836     } else {
837       $this->shadowLastChange= (int)(date("U") / 86400);
838     }
839     if (!$this->activate_shadowWarning){
840       $this->shadowWarning= "0";
841     }
843     /* Check what to do with ID's 
844         Nothing forced, so we may have to generate our own IDs, if not done already.
845      */
846     if ($this->force_ids == 0){
848       /* Handle uidNumber. 
849        * - use existing number if possible
850        * - if not, try to create a new uniqe one.
851        * */
852       if ($this->savedUidNumber != ""){
853         $this->uidNumber= $this->savedUidNumber;
854       } else {
856         /* Calculate new id's. We need to place a lock before calling get_next_id
857            to get real unique values. 
858          */
859         $wait= 10;
860         while (get_lock("uidnumber") != ""){
861           sleep (1);
863           /* Oups - timed out */
864           if ($wait-- == 0){
865             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
866             break;
867           }
868         }
869         add_lock ("uidnumber", "gosa");
870         $this->uidNumber= get_next_id("uidNumber", $this->dn);
871       }
872     }
875     /* Handle gidNumber 
876      * - If we do not have a primary group selected (automatic), we will check if there 
877      *    is already a group  with the same name and use this as primary.
878      * - .. if we couldn't find a group with the same name, we will create a new one, 
879      *    using the users uid as cn and a generated uniqe gidNumber. 
880      * */
881     if ($this->primaryGroup == 0 || $this->force_ids){
883       /* Search for existing group */
884       $ldap = $this->config->get_ldap_link();
885       $ldap->cd($this->config->current['BASE']);
887       /* Are we forced to use a special gidNumber? */ 
888       if($this->force_ids){
889         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
890       }else{
891         $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
892       }
894       /* No primary group found, create a new one */
895       if ($ldap->count() == 0){
897         $groupcn = $this->uid;
898         $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
899         $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
900             'cn='.$groupcn.','.get_groups_ou(), $this->dn);
902         /* Request a new and uniqe gidNumber, if required */
903         if(!$this->force_ids){
904           $this->gidNumber= get_next_id("gidNumber", $this->dn);
905         }
907         /* If forced gidNumber could not be found, then check if the given group name already exists 
908            we do not want to modify the gidNumber of an existing group.
909          */
910         $cnt= 0; 
911         while($ldap->dn_exists($groupdn) && ($cnt < 100)){
912           $cnt ++;
913           $groupcn = $this->uid."_".$cnt;
914           $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
915               'cn='.$groupcn.','.get_groups_ou(), $this->dn);
916         }
918         /* Create new primary group and enforce the new gidNumber */
919         $g= new group($this->config, $groupdn);
920         $g->cn= $groupcn;
921         $g->force_gid= 1;
922         $g->gidNumber= $this->gidNumber;
923         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
924         $g->save ();
926         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
927             sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
928       }else{
929         $attrs = $ldap->fetch();
930         $this->gidNumber = $attrs['gidNumber'][0];
931         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
932             "Found and used: <i>".$attrs['dn']."</i>",
933             sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
934       }
935     }else{
937       /* Primary group was selected by user
938        */
939       $this->gidNumber = $this->primaryGroup;
940       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
941           sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
942     }
944     if ($this->activate_shadowMin != "1" ) {
945       $this->shadowMin = "";
946     }
948     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
949       $this->shadowMax = "";
950     }
952     if ($this->activate_shadowWarning != "1" ) {
953       $this->shadowWarning = "";
954     }
956     if ($this->activate_shadowInactive != "1" ) {
957       $this->shadowInactive = "";
958     }
960     if ($this->activate_shadowExpire != "1" ) {
961       $this->shadowExpire = "";
962     }
964     /* Fill gecos */
965     if (isset($this->parent) && $this->parent !== NULL){
966       $this->gecos= rewrite($this->parent->by_object['user']->cn);
967       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
968         $this->gecos= "";
969       }
970     }
972     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
973       $this->$attr = (int) $this->$attr;
974     }
975     /* Call parents save to prepare $this->attrs */
976     plugin::save();
978     /* Trust accounts */
979     $objectclasses= array();
980     foreach ($this->attrs['objectClass'] as $key => $class){
981       if (preg_match('/trustAccount/i', $class)){
982         continue;
983       }
984       $objectclasses[]= $this->attrs['objectClass'][$key];
985     }
986     $this->attrs['objectClass']= $objectclasses;
987     if ($this->trustModel != ""){
988       $this->attrs['objectClass'][]= "trustAccount";
989       $this->attrs['trustModel']= $this->trustModel;
990       $this->attrs['accessTo']= array();
991       if ($this->trustModel == "byhost"){
992         foreach ($this->accessTo as $host){
993           $this->attrs['accessTo'][]= $host;
994         }
995       }
996     } else {
997       if ($this->was_trust_account){
998         $this->attrs['accessTo']= array();
999         $this->attrs['trustModel']= array();
1000       }
1001     }
1003     if(empty($this->attrs['gosaDefaultPrinter'])){
1004       $thid->attrs['gosaDefaultPrinter']=array();
1005     }
1008     /* include global link_info */
1009     $this->cleanup();
1010  
1011     /* This is just a test, we have had duplicated ids 
1012         in the past when copy & paste was used. 
1013        Normaly this should not happen.
1014      */ 
1015     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
1016       $used = $this->get_used_uid_numbers();
1017       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
1018         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);
1019       }
1020     }
1022     $ldap= $this->config->get_ldap_link();
1023     $ldap->cd($this->dn);
1024     unset($this->attrs['uid']);
1025     $ldap->modify ($this->attrs); 
1027     /* Log last action */ 
1028     if($this->initially_was_account){
1029       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1030     }else{
1031       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1032     }
1034     if (!$ldap->success()){
1035       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1036     }
1038     /* Remove lock needed for unique id generation */
1039     del_lock ("uidnumber");
1041     // Save ssh stuff if needed
1042     if ($this->ssh) {
1043       $this->ssh->setDN($this->dn);
1044       $this->ssh->save();
1045     }
1047     /* Take care about groupMembership values: add to groups */
1048     foreach ($this->groupMembership as $key => $value){
1049       if (!isset($this->savedGroupMembership[$key])){
1050         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1051         $g->set_acl_base($key);
1052         $g->by_object['group']->addUser($this->uid);
1053         $g->save();
1054       }
1055     }
1057     /* Remove groups not listed in groupMembership */
1058     foreach ($this->savedGroupMembership as $key => $value){
1059       if (!isset($this->groupMembership[$key])){
1060         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1061         $g->set_acl_base($key);
1062         $g->by_object['group']->removeUser ($this->uid);
1063         $g->save();
1064       }
1065     }
1067     /* Optionally execute a command after we're done */
1068     if ($this->initially_was_account == $this->is_account){
1069       if ($this->is_modified){
1070         $this->handle_post_events("modify",array("uid" => $this->uid));
1071       }
1072     } else {
1073       $this->handle_post_events("add" ,array("uid"=> $this->uid));
1074     }
1075   }
1078   /* Check formular input */
1079   function check()
1080   {
1081     /* Include global link_info */
1082     $ldap= $this->config->get_ldap_link();
1084     /* Append groups as memberGroup: to check hook 
1085      */
1086     $tmp_attributes  = $this->attributes;    
1087     $this->attributes[] = "memberGroup";
1088     $this->memberGroup = array();
1089     foreach($this->groupMembership as $dn => $name){
1090       $this->memberGroup[] = $name;
1091     }
1093     /* Call common method to give check the hook */
1094     $message= plugin::check();
1095     $this->attributes = $tmp_attributes;
1097     /* must: homeDirectory */
1098     if ($this->homeDirectory == ""){
1099       $message[]= msgPool::required(_("Home directory"));
1100     }
1101     if (!tests::is_path($this->homeDirectory)){
1102       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1103     }
1105     /* Check ID's if they are forced by user */
1106     if ($this->force_ids == "1"){
1108       /* Valid uid/gid? */
1109       if (!tests::is_id($this->uidNumber)){
1110         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1111       } else {
1112         if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1113           $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1114         }
1115       }
1116       if (!tests::is_id($this->gidNumber)){
1117         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1118       } else {
1119         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1120           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1121         }
1122       }
1123     }
1125     /* Check dates */
1126     if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
1127       $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
1128     }
1130     /* Check shadow settings, well I like spaghetties... */
1131     if ($this->activate_shadowMin){
1132       if (!tests::is_id($this->shadowMin)){
1133         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1134       }
1135     }
1136     if ($this->activate_shadowMax){
1137       if (!tests::is_id($this->shadowMax)){
1138         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1139       }
1140     }
1141     if ($this->activate_shadowWarning){
1142       if (!tests::is_id($this->shadowWarning)){
1143         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1144       }
1145       if (!$this->activate_shadowMax){
1146         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1147       }
1148       if ($this->shadowWarning > $this->shadowMax){
1149         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1150       }
1151       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1152         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1153       }
1154     }
1155     if ($this->activate_shadowInactive){
1156       if (!tests::is_id($this->shadowInactive)){
1157         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1158       }
1159       if (!$this->activate_shadowMax){
1160         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1161       }
1162     }
1163     if ($this->activate_shadowMin && $this->activate_shadowMax){
1164       if ($this->shadowMin > $this->shadowMax){
1165         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1166       }
1167     }
1169     return ($message);
1170   }
1173   function multiple_check()
1174   {
1175     $message = plugin::multiple_check();
1176     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1177       $message[]= msgPool::required(_("Home directory"));
1178     }
1179     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1180       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1181     }
1183     /* Check shadow settings, well I like spaghetties... */
1184     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1185       if (!tests::is_id($this->shadowMin)){
1186         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1187       }
1188     }
1189     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1190       if (!tests::is_id($this->shadowMax)){
1191         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1192       }
1193     }
1194     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1195       if (!tests::is_id($this->shadowWarning)){
1196         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1197       }
1198       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1199         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1200       }
1201       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1202         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1203       }
1204       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1205         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1206       }
1207     }
1208     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1209       if (!tests::is_id($this->shadowInactive)){
1210         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1211       }
1212       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1213         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1214       }
1215     }
1216     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1217       if ($this->shadowMin > $this->shadowMax){
1218         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1219       }
1220     }
1222     return($message);
1223   }
1226   function addGroup ($groups)
1227   {
1228     /* include global link_info */
1229     $ldap= $this->config->get_ldap_link();
1231     /* Walk through groups and add the descriptive entry if not exists */
1232     foreach ($groups as $value){
1234       if (!array_key_exists($value, $this->groupMembership)){
1235         $ldap->cat($value, array('cn', 'description', 'dn'));
1236         $attrs= $ldap->fetch();
1237         error_reporting (0);
1238         if (!isset($attrs['description'][0])){
1239           $entry= $attrs["cn"][0];
1240         } else {
1241           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1242           $entry= $attrs["cn"][0]." [$dsc]";
1243         }
1244         error_reporting (E_ALL | E_STRICT);
1246         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1247           $this->groupMembership[$attrs['dn']]= $entry;
1248           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1249             unset($this->groupMembership_some[$attrs['dn']]);
1250           }
1251         }
1252       }
1253     }
1255     /* Sort groups */
1256     asort ($this->groupMembership);
1257     reset ($this->groupMembership);
1258   }
1261   /* Del posix user from some groups */
1262   function delGroup ($groups)
1263   {
1264     $dest= array();
1265     foreach($groups as $dn_to_del){
1266       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1267         unset($this->groupMembership[$dn_to_del]);
1268       }
1269       if($this->multiple_support_active){
1270         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1271           unset($this->groupMembership_some[$dn_to_del]);
1272         }
1273       }
1274     }
1275   }
1278   /* Adapt from template, using 'dn' */
1279   function adapt_from_template($dn, $skip= array())
1280   {
1281     /* Include global link_info */
1282     $ldap= $this->config->get_ldap_link();
1284     plugin::adapt_from_template($dn, $skip);
1285     $template= $this->attrs['uid'][0];
1287     /* Adapt group membership */
1288     $ldap->cd($this->config->current['BASE']);
1289     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1291     while ($this->attrs= $ldap->fetch()){
1292       if (!isset($this->attrs["description"][0])){
1293         $entry= $this->attrs["cn"][0];
1294       } else {
1295         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1296       }
1297       $this->groupMembership[$ldap->getDN()]= $entry;
1298     }
1300     /* Fix primary group settings */
1301     $ldap->cd($this->config->current['BASE']);
1302     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1303     if ($ldap->count() != 1){
1304       $this->primaryGroup= $this->gidNumber;
1305     }
1307     $ldap->cd($this->config->current['BASE']);
1308     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1309     while($attr = $ldap->fetch()){
1310       $tmp = $attr['accessTo'];
1311       unset ($tmp['count']);
1312       $this->accessTo = $tmp;   
1313     }
1315     /* Adjust shadow checkboxes */
1316     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1317       if ($this->$val != 0){
1318         $oval= "activate_".$val;
1319         $this->$oval= "1";
1320       }
1321     }
1323     /* FIXME: NEED review of this section */
1324     /* Need to check shadowExpire separately */
1326     /* 
1327      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1328      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1329      */
1330     if(abs($this->shadowExpire - time())>86400) {
1331       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1332     }
1333     
1334     /* Only enable checkbox, if shadowExpire is in the future */
1335     if($this->shadowExpire > time()) {
1336       $this->activate_shadowExpire= "1";
1337     }
1338   }
1340   function convertToSeconds($val)
1341   {
1342     if ($val != 0){
1343       $val*= 60 * 60 * 24;
1344     } else {
1345       $date= getdate();
1346       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1347     }
1348     return($val);
1349   }
1352   function get_used_uid_numbers()
1353   {
1354     $ids= array();
1355     $ldap= $this->config->get_ldap_link();
1357     $ldap->cd ($this->config->current['BASE']);
1358     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1360     /* Get list of ids */
1361     while ($attrs= $ldap->fetch()){
1362       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1363     }
1364     return($ids);
1365   }
1367   
1368   function reload()
1369   {
1370     /* Set base for all searches */
1371     $base      = session::get('CurrentMainBase');
1372     $base     = $base;
1373     $ldap     = $this->config->get_ldap_link();    
1374     $attrs    =  array("cn", "description", "gidNumber");
1375     $Flags    = GL_SIZELIMIT;
1377     /* Get groups */
1378     if ($this->GroupUserRegex == '*'){
1379       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1380     } else {
1381       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1382     }
1383     if($this->SubSearch){
1384       $Flags |= GL_SUBSEARCH;
1385     }else{
1386       $base = get_groups_ou().$base;
1387     }
1389     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1390   
1391     /* check sizelimit */
1392     if (preg_match("/size limit/i", $ldap->get_error())){
1393       session::set('limit_exceeded',TRUE);
1394     }
1396     /* Create a list of users */
1397     $this->grouplist = array();
1398     foreach ($res as $value){
1399       $this->grouplist[$value['gidNumber'][0]]= $value;
1400     }
1402     $tmp=array();
1403     foreach($this->grouplist as $tkey => $val ){
1404       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1405     }
1407     /* Sort index */
1408     ksort($tmp);
1410     /* Recreate index array[dn]=cn[description]*/
1411     $this->grouplist=array();
1412     foreach($tmp as $val){
1413       if(isset($val['description'])){
1414         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1415       }else{
1416         $this->grouplist[$val['dn']]=$val['cn'][0];
1417       }
1418     }
1420     reset ($this->grouplist);
1421   }
1424   /* Get posts from copy & paste dialog */ 
1425   function saveCopyDialog()
1426   {
1427     if(isset($_POST['homeDirectory'])){
1428       $this->homeDirectory = $_POST['homeDirectory'];
1429       if (isset ($_POST['force_ids'])){
1430         $data= 1;
1431         $this->gidNumber = $_POST['gidNumber'];
1432         $this->uidNumber = $_POST['uidNumber'];
1433       } else {
1434         $data= 0;
1435       }
1436       if ($this->force_ids != $data){
1437         $this->is_modified= TRUE;
1438       }
1439       $this->force_ids= $data;
1440       $data= $_POST['primaryGroup'];
1441       if ($this->primaryGroup != $data){
1442         $this->is_modified= TRUE;
1443       }
1444       $this->primaryGroup= $_POST['primaryGroup'];
1445     }
1446   }
1447  
1449   /* Create the posix dialog part for copy & paste */
1450   function getCopyDialog()
1451   {
1452     /* Skip dialog creation if this is not a valid account*/
1453     if(!$this->is_account) return("");
1454     if ($this->force_ids == 1){
1455       $force_ids = "checked";
1456       if (session::get('js')){
1457         $forceMode = "";
1458       }
1459     } else {
1460       if (session::get('js')){
1461         if($this->acl != "#none#")
1462           $forceMode ="disabled";
1463       }
1464       $force_ids = "";
1465     }
1467     $sta = "";
1469     /* Open group add dialog */
1470     if(isset($_POST['edit_groupmembership'])){
1471       $this->group_dialog = TRUE;
1472       $sta = "SubDialog";
1473     }
1475     /* If the group-add dialog is closed, call execute 
1476        to ensure that the membership is updatd */
1477     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1478       $this->execute();
1479       $this->group_dialog =FALSE;
1480     }
1482     if($this->group_dialog){
1483       $str = $this->execute(true);
1484       $ret = array();
1485       $ret['string'] = $str;
1486       $ret['status'] = $sta;
1487       return($ret);
1488     }
1490     /* If a group member should be deleted, simply call execute */
1491     if(isset($_POST['delete_groupmembership'])){
1492       $this->execute();
1493     }
1495     /* Assigned informations to smarty */
1496     $smarty = get_smarty();
1497     $smarty->assign("homeDirectory",$this->homeDirectory);
1498     $smarty->assign("secondaryGroups",$this->secondaryGroups);
1499     $smarty->assign("primaryGroup",$this->primaryGroup);
1500  
1501     $smarty->assign("uidNumber",$this->uidNumber);
1502     $smarty->assign("gidNumber",$this->gidNumber);
1503     $smarty->assign("forceMode",$forceMode);
1504     $smarty->assign("force_ids",$force_ids);
1505     if (!count($this->groupMembership)){
1506       $smarty->assign("groupMembership", array("&nbsp;"));
1507     } else {
1508       $smarty->assign("groupMembership", $this->groupMembership);
1509     }
1511     /* Display wars message if there are more than 16 group members */
1512     if (count($this->groupMembership) > 16){
1513       $smarty->assign("groups", "too_many_for_nfs");
1514     } else {
1515       $smarty->assign("groups", "");
1516     }
1517     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1519     $ret = array();
1520     $ret['string'] = $str;
1521     $ret['status'] = $sta;
1522     return($ret);
1523   }
1526   function PrepareForCopyPaste($source)
1527   {
1528     plugin::PrepareForCopyPaste($source);
1530     /* Avoid using the same gid/uid number as source user 
1531         empty numbers to enforce new ones. */
1532     $this->savedUidNumber = "";
1533     $this->savedGidNumber = "";
1535     /* Get group membership */
1536     $ldap = $this->config->get_ldap_link();
1537     $ldap->cd($this->config->current['BASE']);
1538     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1540     while ($attrs= $ldap->fetch()){
1541       if (!isset($attrs["description"][0])){
1542         $entry= $attrs["cn"][0];
1543       } else {
1544         $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1545       }
1546       $this->groupMembership[$ldap->getDN()]= $entry;
1547     }
1548     asort($this->groupMembership);
1549     reset($this->groupMembership);
1551     /* Fill group */
1552     if(isset($source['gidNumber'][0])){
1553       $this->primaryGroup= $source['gidNumber'][0];
1554     }
1556   }
1559   function multiple_execute()
1560   {
1561     return($this->execute());
1562   }
1565   static function plInfo()
1566   {
1567     return (array(
1568           "plDescription"     => _("POSIX account"),
1569           "plSelfModify"      => TRUE,
1570           "plDepends"         => array("user"),
1571           "plPriority"        => 2,
1572           "plSection"         => array("personal" => _("My account")),
1573           "plCategory"        => array("users"),
1574           "plOptions"         => array(),
1576           "plProvidedAcls"  => array(
1578             "homeDirectory"       =>  _("Home directory"), 
1579             "loginShell"          =>  _("Shell"),
1580             "uidNumber"           =>  _("User ID"),
1581             "gidNumber"           =>  _("Group ID"),
1583             "mustchangepassword"=>  _("Force password change on login"),
1584             "shadowMin"           =>  _("Shadow min"),
1585             "shadowMax"           =>  _("Shadow max"),
1586             "shadowWarning"       =>  _("Shadow warning"),
1587             "shadowInactive"      =>  _("Shadow inactive"),
1588             "shadowExpire"        =>  _("Shadow expire"),
1589             "sshPublickey"        =>  _("Public SSH key"),
1590             "trustModel"          =>  _("System trust model")))
1591             );
1592   }
1595   /* Return selected values for multiple edit */
1596   function get_multi_edit_values()
1597   {
1598     $ret = plugin::get_multi_edit_values();
1599     $ret['groupMembership']     = $this->groupMembership;
1600     $ret['groupMembership_some']= $this->groupMembership_some;
1602     if(in_array("primaryGroup",$this->multi_boxes)){
1603       $ret['primaryGroup'] = $this->primaryGroup;
1604     }
1605     if(in_array("trustmode",$this->multi_boxes)){
1606       $ret['trustModel'] = $this->trustModel;
1607       $ret['accessTo'] = $this->accessTo;
1608     }
1609     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1610       $active = "activate_".$entry;
1611       if(in_array($active,$this->multi_boxes)){
1612         $ret[$entry] = $this->$entry;
1613         $ret[$active] = $this->$active;
1614       }
1615     }
1616     if(in_array("mustchangepassword",$this->multi_boxes)){
1617       $ret['mustchangepassword'] = $this->mustchangepassword;
1618     }
1619     return($ret);
1620   }
1623   /* Save posts for multiple edit 
1624    */
1625   function multiple_save_object()
1626   {
1627     if(isset($_POST['posix_mulitple_edit'])){
1628  
1629       /* Backup expire value */ 
1630       $expire_tmp = $this->shadowExpire;
1631   
1632       /* Update all values */
1633       plugin::multiple_save_object();
1635       /* Get selected checkboxes */
1636       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1637         if(isset($_POST["use_".$val])){
1638           $this->multi_boxes[] = $val;
1639         }
1640       }
1642       /* Update special values, checkboxes for posixShadow */
1643       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1644         if($this->acl_is_writeable($var)){
1645           $activate_var = "activate_".$var;
1646           if(in_array($activate_var, $this->multi_boxes)){
1647             if(isset($_POST['activate_'.$var])){
1648               $this->$activate_var  = true;
1649               $this->$var      = $_POST[$var];
1650             }else{
1651               $this->$activate_var  = false;
1652               $this->$var      = 0;
1653             }
1654           }
1655         }
1656       }
1658       /* Restore shadow value, if the shadow attribute isn't used */
1659       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1660         $this->shadowExpire = $expire_tmp;
1661       }
1663       /* Force change password ? */
1664       if(isset($_POST['mustchangepassword'])){
1665         $this->mustchangepassword = TRUE;
1666       }else{
1667         $this->mustchangepassword = FALSE;
1668       }
1670       /* Trust mode - special handling */
1671       if($this->acl_is_writeable("trustModel")){
1672         if (isset($_POST['trustmode'])){
1673           $saved= $this->trustModel;
1674           if ($_POST['trustmode'] == "1"){
1675             $this->trustModel= "fullaccess";
1676           } elseif ($_POST['trustmode'] == "2"){
1677             $this->trustModel= "byhost";
1678           } else {
1679             $this->trustModel= "";
1680           }
1681           if ($this->trustModel != $saved){
1682             $this->is_modified= TRUE;
1683           }
1684         }
1685       }
1687       /* Save primary group settings */
1688       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1689         $data= $_POST['primaryGroup'];
1690         if ($this->primaryGroup != $data){
1691           $this->is_modified= TRUE;
1692         }
1693         $this->primaryGroup= $_POST['primaryGroup'];
1694       }
1695     }
1696   }
1698   
1699   /* Initialize plugin with given atribute arrays 
1700    */
1701   function init_multiple_support($attrs,$all)
1702   {
1703     plugin::init_multiple_support($attrs,$all);
1705     /* Some dummy values */
1706     $groups_some = array();
1707     $groups_all  = array();
1708     $groups_uid  = array();
1709     $uids        = array();
1710     $first       = TRUE;
1712     /* Get all groups used by currently edited users */
1713     $uid_filter="";  
1714     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1715       $uid = $this->multi_attrs_all['uid'][$i];
1716       $uids[] = $uid;
1717       $uid_filter.= "(memberUid=".$uid.")"; 
1718     }
1719     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1720     $ldap = $this->config->get_ldap_link();
1721     $ldap->cd($this->config->current['BASE']);
1722     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1723     while($group = $ldap->fetch()){
1724       $groups_some[$group['dn']] = $group['cn'][0];
1725       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1726         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1727       }
1728     }
1730     /* Create an array, containing all used groups */
1731     $groups_all = $groups_some;
1732     foreach($groups_all as $id => $group){
1733       foreach($uids as $uid){
1734         if(!in_array($uid,$groups_uid[$id])){
1735           unset($groups_all[$id]);
1736           break;
1737         }
1738       }
1739     }
1741     /* Assign group array */
1742     $this->groupMembership = $groups_all;
1744     /* Create an array of all grouops used by all users */
1745     foreach( $groups_all as $dn => $cn){
1746       if(isset($groups_some[$dn])){
1747         unset($groups_some[$dn]);
1748       }
1749     }
1750     $this->groupMembership_some = $groups_some;
1751     $this->primaryGroup = $this->gidNumber;
1753     /* Is this account a trustAccount? */
1754     if (isset($this->multi_attrs['trustModel'])){
1755       $this->trustModel= $this->multi_attrs['trustModel'][0];
1756       $this->was_trust_account= TRUE;
1757       $this->multi_boxes[] = "trustmode";
1758     } else {
1759       $this->was_trust_account= FALSE;
1760       $this->trustModel= "";
1761     }
1763     /* Create access informations */
1764     $this->accessTo = array();
1765     if (isset($this->multi_attrs['accessTo'])){
1766       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1767         $tmp= $this->multi_attrs['accessTo'][$i];
1768         $this->accessTo[$tmp]= $tmp;
1769       }
1770     }
1772     /* Adjust shadow checkboxes */
1773     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1774           "shadowExpire") as $val){
1775       if ($this->$val != 0){
1776         $oval= "activate_".$val;
1777         $this->$oval= "1";
1778       }
1779     }
1781     /* Convert to seconds */
1782     if(isset($this->multi_attrs['shadowExpire'])){
1783       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1784     }else{
1785       $this->activate_shadowExpire = FALSE;
1786     }
1787   }
1790   function set_multi_edit_values($attrs)
1791   {
1792     $groups = array();
1794     /* Update groupMembership, keep optinal group */
1795     foreach($attrs['groupMembership_some'] as $dn => $cn){
1796       if(isset($this->groupMembership[$dn])){
1797         $groups[$dn] = $cn;
1798       }
1799     }
1800     /* Update groupMembership, add forced groups */
1801     foreach($attrs['groupMembership'] as $dn => $cn){
1802       $groups[$dn] = $cn;
1803     }
1804     plugin::set_multi_edit_values($attrs);
1805     $this->groupMembership = $groups;
1806   }
1809 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1810 ?>