Code

Added myAccount path title
[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 $groupSelect= FALSE;
72   var $trustSelect= 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     $this->ui = get_userinfo();
257   }
260   /* execute generates the html output for this node */
261   function execute($isCopyPaste = false)
262   {
263     /* Call parent execute */
264     plugin::execute();
265     $display= "";
267     /* Log view */
268     if($this->is_account && !$this->view_logged){
269       $this->view_logged = TRUE;
270       new log("view","users/".get_class($this),$this->dn);
271     }
273     if($this->multiple_support_active){
274       $this->is_account = TRUE;
275     }
277     if(!$isCopyPaste && ! $this->multiple_support_active){
279       /* Do we need to flip is_account state? */
280       if(isset($_POST['modify_state'])){
281         if($this->is_account && $this->acl_is_removeable()){
282           $this->is_account= FALSE;
283         }elseif(!$this->is_account && $this->acl_is_createable()){
284           $this->is_account= TRUE;
285         }
286       }
288       /* Do we represent a valid posixAccount? */
289       if (!$this->is_account && $this->parent === NULL ){
290         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
291           msgPool::noValidExtension(_("POSIX"))."</b>";
292         $display.= back_to_main();
293         return ($display);
294       }
297       /* Show tab dialog headers */
298       if ($this->parent !== NULL){
299         if ($this->is_account){
300           if (isset($this->parent->by_object['sambaAccount'])){
301             $obj= $this->parent->by_object['sambaAccount'];
302           }
303           if (isset($obj) && $obj->is_account == TRUE &&
304               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
305               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
307             /* Samba3 dependency on posix accounts are enabled
308                in the moment, because I need to rely on unique
309                uidNumbers. There'll be a better solution later
310                on. */
311             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
312           } else {
313             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
314           }
315         } else {
316           $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
317           return($display);
318         }
319       }
320     }
322     // Display dialog to allow selection of groups
323     if (isset($_POST['edit_groupmembership'])){
324       $this->groupSelect = new groupSelect($this->config,get_userinfo());
325       $this->dialog= TRUE;
326     }
328     // Allow to select trusted machines from a list
329     if (isset($_POST["add_ws"])){
330       $this->trustSelect= new trustSelect($this->config,get_userinfo());
331       $this->dialog= TRUE;
332     }
334     // Cancel trust and group dialog
335     if (isset($_POST['add_groups_cancel']) || isset($_POST['add_ws_cancel'])){
336       $this->groupSelect= NULL;
337       $this->trustSelect= NULL;
338       $this->dialog= FALSE;
339     }
341     // Add groups selected in groupSelect dialog to ours.
342     if (isset($_POST['add_groups_finish']) && $this->groupSelect){
343       $groups = $this->groupSelect->detectPostActions();
344       if(isset($groups['targets'])){
345         $this->addGroup ($groups['targets']);
346         $this->is_modified= TRUE;
347       }
348       $this->groupSelect= NULL;
349       $this->dialog= FALSE;
350     }
352     // Remove groups from currently selected groups.
353     if (isset($_POST['delete_groupmembership']) && 
354         isset($_POST['group_list']) && count($_POST['group_list'])){
356       $this->delGroup ($_POST['group_list']);
357     }
359     // Add selected machines to trusted ones.
360     if (isset($_POST["add_ws_finish"]) &&  $this->trustSelect){
361       $trusts = $this->trustSelect->detectPostActions();
362       if(isset($trusts['targets'])){
364         $headpage = $this->trustSelect->getHeadpage();              
365         foreach($trusts['targets'] as $id){
366           $attrs = $headpage->getEntry($id);
367           $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
368         }
369         ksort($this->accessTo);
370         $this->is_modified= TRUE;
371       }
372       $this->trustSelect= NULL;
373       $this->dialog= FALSE;
374     }
376     // Remove machine from trusted ones.
377     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
378       foreach($_POST['workstation_list'] as $name){
379         unset ($this->accessTo[$name]);
380       }
381       $this->is_modified= TRUE;
382     }
386     /* Templates now! */
387     $smarty= get_smarty();
388     $smarty->assign("usePrototype", "true");
390     /* Show ws dialog */
391     if ($this->trustSelect){
393       // Build up blocklist
394       session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
395       return($this->trustSelect->execute());
396     }
398     /* Manage group add dialog */
399     if ($this->groupSelect){
401       // Build up blocklist
402       session::set('filterBlacklist', array('dn' => array_keys($this->groupMembership)));
403       return($this->groupSelect->execute());
404     }
406     // Handle ssh dialog?
407     if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
408       $smarty->assign("usePrototype", "false");
409       if ($result= $this->ssh->execute()) {
410         $this->dialog= true;
411         pathNavigator::registerPlugin("SSH keys");
412         return $result;
413       }
414       $this->dialog= false;
415     }
418     /* Show main page */
419     $smarty= get_smarty();
420     $smarty->assign("usePrototype", "true");
422     $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey"));
424     /* Depending on pwmode, currently hardcoded because there are no other methods */
425     if ( 1 == 1 ){
426       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
428       $shadowMinACL     =  $this->getacl("shadowMin");
429       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
430             "<input type='text' name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
432       $shadowMaxACL     =  $this->getacl("shadowMax");
433       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
434             "<input type='text' name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
436       $shadowInactiveACL=  $this->getacl("shadowInactive");
437       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"), 
438             "<input type='text' name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
440       $shadowWarningACL =  $this->getacl("shadowWarning");
441       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"), 
442             "<input type='text' name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
444       foreach( array("activate_shadowMin", "activate_shadowMax",
445             "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
446         if ($this->$val == 1){
447           $smarty->assign("$val", "checked");
448         } else {
449           $smarty->assign("$val", "");
450         }
451         $smarty->assign("$val"."ACL", $this->getacl($val));
452       }
454       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword"));
455     }
457     // Set last system login
458     $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
460     /* Fill arrays */
461     $smarty->assign("shells", $this->loginShellList);
462     $smarty->assign("secondaryGroups", $this->secondaryGroups);
463     $smarty->assign("primaryGroup", $this->primaryGroup);
464     if(!$this->multiple_support_active){
465       if (!count($this->groupMembership)){
466         $smarty->assign("groupMembership", array("&nbsp;"));
467       } else {
468         $smarty->assign("groupMembership", $this->groupMembership);
469       }
470     }else{
471       $smarty->assign("groupMembership", $this->groupMembership);
472       $smarty->assign("groupMembership_some", $this->groupMembership_some);
473     }
474     if (count($this->groupMembership) > 16){
475       $smarty->assign("groups", "too_many_for_nfs");
476     } else {
477       $smarty->assign("groups", "");
478     }
480     /* Avoid "Undefined index: forceMode" */
481     $smarty->assign("forceMode", "");
483     /* Checkboxes */
484     if ($this->force_ids == 1){
485       $smarty->assign("force_ids", "checked");
486       if (session::get('js')){
487         $smarty->assign("forceMode", "");
488       }
489     } else {
490       if (session::get('js')){
491         $smarty->assign("forceMode", "disabled");
492       }
493       $smarty->assign("force_ids", "");
494     }
496     /* Create onClick="" action string for the "Force UID/GID" option 
497      */
498     $onClickIDS ="";
499     if(preg_match("/w/",$this->getacl("uidNumber"))){
500       $onClickIDS .= "changeState('uidNumber');";
501     }
502     if(preg_match("/w/",$this->getacl("gidNumber"))){
503       $onClickIDS .= "changeState('gidNumber');";
504     }
505     $smarty->assign("onClickIDS", $onClickIDS);
506     $smarty->assign("force_idsACL", $this->getacl("uidNumber").$this->getacl("gidNumber"));
508     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
509       if(in_array($val,$this->multi_boxes)){
510         $smarty->assign("use_".$val,TRUE);
511       }else{
512         $smarty->assign("use_".$val,FALSE);
513       }
514     }
517     /* Load attributes and acl's */
518     foreach($this->attributes as $val){
519       if(in_array($val,$this->multi_boxes)){
520         $smarty->assign("use_".$val,TRUE);
521       }else{
522         $smarty->assign("use_".$val,FALSE);
523       }
525       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
526       {
527         $smarty->assign("$val"."ACL",$this->getacl($val));
528         $smarty->assign("$val", $this->$val);
529         continue;
530       }
531       $smarty->assign("$val", $this->$val);
532       $smarty->assign("$val"."ACL", $this->getacl($val));
533     }
535     if($this->read_only){
536       $smarty->assign("groupMembershipACL","r");
537     }else{
538       $smarty->assign("groupMembershipACL","rw");
539     }
540     $smarty->assign("status", $this->status);
542     /* Work on trust modes */
543     $smarty->assign("trusthide", " disabled ");
544     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
545     if ($this->trustModel == "fullaccess"){
546       $trustmode= 1;
547       // pervent double disable tag in html code, this will disturb our clean w3c html
548       $smarty->assign("trustmode",  $this->getacl("trustModel"));
550     } elseif ($this->trustModel == "byhost"){
551       $trustmode= 2;
552       $smarty->assign("trusthide", "");
553     } else {
554       // pervent double disable tag in html code, this will disturb our clean w3c html
555       $smarty->assign("trustmode",  $this->getacl("trustModel"));
556       $trustmode= 0;
557     }
558     $smarty->assign("trustmode", $trustmode);
559     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
560           2 => _("allow access to these hosts")));
564     if((count($this->accessTo))==0)
565       $smarty->assign("emptyArrAccess",true);
566     else
567       $smarty->assign("emptyArrAccess",false);
569     if($this->mustchangepassword){
570       $smarty->assign("mustchangepassword", " checked ");
571     } else {
572       $smarty->assign("mustchangepassword", "");
573     }
575     $smarty->assign("workstations", $this->accessTo);
577     // Add SSH button if available
578     $smarty->assign("sshPublicKey", $this->ssh?1:0);
580     $smarty->assign("apply", apply_filter());
581     $smarty->assign("multiple_support" , $this->multiple_support_active);
582     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
583     return($display);
584   }
587   /* remove object from parent */
588   function remove_from_parent()
589   {
590     /* Cancel if there's nothing to do here */
591     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
592       return;
593     }
596     /* Remove and write to LDAP */
597     plugin::remove_from_parent();
599     /* Zero out array */
600     $this->attrs['gosaHostACL']= array();
602     /* Keep uid, because we need it for authentification! */
603     unset($this->attrs['uid']);
604     unset($this->attrs['trustModel']);
606     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
607         /* include global link_info */
608         $this->attributes, "Save");
609     $ldap= $this->config->get_ldap_link();
610     $ldap->cd($this->dn);
611     $this->cleanup();
612     $ldap->modify ($this->attrs); 
614     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
616     if (!$ldap->success()){
617       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
618     }
620     /* Delete group only if cn is uid and there are no other
621        members inside */
622     $ldap->cd ($this->config->current['BASE']);
623     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
624     if ($ldap->count() != 0){
625       $attrs= $ldap->fetch();
626       if ($attrs['cn'][0] == $this->uid &&
627           !isset($this->attrs['memberUid'])){
629         $ldap->rmDir($ldap->getDN());
630       }
631     }
633     /* Optionally execute a command after we're done */
634     $this->handle_post_events("remove",array("uid" => $this->uid));
635   }
638   function save_object()
639   {
640     if (isset($_POST['posixTab'])){
641       /* Save values to object */
642       plugin::save_object();
645       /* Save force GID checkbox */
646       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
647         if (isset ($_POST['force_ids'])){
648           $data= 1;
649         } else {
650           $data= 0;
651         }
652         if ($this->force_ids != $data){
653           $this->is_modified= TRUE;
654         }
655         $this->force_ids= $data;
656       }
658       /*Save primary group settings */
659       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
660         $data= $_POST['primaryGroup'];
661         if ($this->primaryGroup != $data){
662           $this->is_modified= TRUE;
663         }
664         $this->primaryGroup= $_POST['primaryGroup'];
665       }
667       /* Get seelcted shadow checkboxes */
668       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
669         if($this->acl_is_writeable($var)){
670           $activate_var = "activate_".$var;
671           if(isset($_POST['activate_'.$var])){
672             $this->$activate_var  = true;
673             $this->$var      = $_POST[$var];
674           }else{
675             $this->$activate_var  = false;
676             if ($var != "shadowExpire") {
677               $this->$var      = 0;
678             }
679           }
680         }
681       }
683       /* Force change password ? */
684       if(isset($_POST['mustchangepassword'])){
685         $this->mustchangepassword = TRUE;
686       }else{
687         $this->mustchangepassword = FALSE;
688       }
690       /* Trust mode - special handling */
691       if($this->acl_is_writeable("trustModel")){
692         if (isset($_POST['trustmode'])){
693           $saved= $this->trustModel;
694           if ($_POST['trustmode'] == "1"){
695             $this->trustModel= "fullaccess";
696           } elseif ($_POST['trustmode'] == "2"){
697             $this->trustModel= "byhost";
698           } else {
699             $this->trustModel= "";
700           }
701           if ($this->trustModel != $saved){
702             $this->is_modified= TRUE;
703           }
704         }
705       }
706     }
708     /* Get regex from alphabet */
709     if(isset($_GET['search'])){
710       $this->GroupRegex = $_GET['search']."*";
711     }
713     /* Check checkboxes and regexes */
714     if(isset($_POST["PosixGroupDialogPosted"])){
716       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
717         $this->SubSearch = true;
718       }else{
719         $this->SubSearch = false;
720       }
721       if(isset($_POST['guser'])){
722         $this->GroupUserRegex = $_POST['guser'];
723       }
724       if(isset($_POST['regex'])){
725         $this->GroupRegex = $_POST['regex'];
726       }
727     }
728     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
729     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
730   }
733   /* Save data to LDAP, depending on is_account we save or delete */
734   function save()
735   {
737     /* Adapt shadow values */
738     if (!$this->activate_shadowExpire){
739       $this->shadowExpire= "0";
740     } else {
741       /* Transform date to days since the beginning */
742       list($day, $month, $year)= explode('.', $this->shadowExpire, 3);
743       $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
744     }
745     if (!$this->activate_shadowMax){
746       $this->shadowMax= "0";
747     }
748     if ($this->mustchangepassword){
749       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
750     } else {
751       $this->shadowLastChange= (int)(date("U") / 86400);
752     }
753     if (!$this->activate_shadowWarning){
754       $this->shadowWarning= "0";
755     }
757     /* Check what to do with ID's 
758        Nothing forced, so we may have to generate our own IDs, if not done already.
759      */
760     if ($this->force_ids == 0){
762       /* Handle uidNumber. 
763        * - use existing number if possible
764        * - if not, try to create a new uniqe one.
765        * */
766       if ($this->savedUidNumber != ""){
767         $this->uidNumber= $this->savedUidNumber;
768       } else {
770         /* Calculate new id's. We need to place a lock before calling get_next_id
771            to get real unique values. 
772          */
773         $wait= 10;
774         while (get_lock("uidnumber") != ""){
775           sleep (1);
777           /* Oups - timed out */
778           if ($wait-- == 0){
779             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
780             break;
781           }
782         }
783         add_lock ("uidnumber", "gosa");
784         $this->uidNumber= get_next_id("uidNumber", $this->dn);
785       }
786     }
789     /* Handle gidNumber 
790      * - If we do not have a primary group selected (automatic), we will check if there 
791      *    is already a group  with the same name and use this as primary.
792      * - .. if we couldn't find a group with the same name, we will create a new one, 
793      *    using the users uid as cn and a generated uniqe gidNumber. 
794      * */
795     if ($this->primaryGroup == 0 || $this->force_ids){
797       /* Search for existing group */
798       $ldap = $this->config->get_ldap_link();
799       $ldap->cd($this->config->current['BASE']);
801       /* Are we forced to use a special gidNumber? */ 
802       if($this->force_ids){
803         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
804       }else{
805         $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
806       }
808       /* No primary group found, create a new one */
809       if ($ldap->count() == 0){
811         $groupcn = $this->uid;
812         $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
813         $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
814             'cn='.$groupcn.','.get_groups_ou(), $this->dn);
816         /* Request a new and uniqe gidNumber, if required */
817         if(!$this->force_ids){
818           $this->gidNumber= get_next_id("gidNumber", $this->dn);
819         }
821         /* If forced gidNumber could not be found, then check if the given group name already exists 
822            we do not want to modify the gidNumber of an existing group.
823          */
824         $cnt= 0; 
825         while($ldap->dn_exists($groupdn) && ($cnt < 100)){
826           $cnt ++;
827           $groupcn = $this->uid."_".$cnt;
828           $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
829               'cn='.$groupcn.','.get_groups_ou(), $this->dn);
830         }
832         /* Create new primary group and enforce the new gidNumber */
833         $g= new group($this->config, $groupdn);
834         $g->cn= $groupcn;
835         $g->force_gid= 1;
836         $g->gidNumber= $this->gidNumber;
837         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
838         $g->save ();
840         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
841             sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
842       }else{
843         $attrs = $ldap->fetch();
844         $this->gidNumber = $attrs['gidNumber'][0];
845         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
846             "Found and used: <i>".$attrs['dn']."</i>",
847             sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
848       }
849     }else{
851       /* Primary group was selected by user
852        */
853       $this->gidNumber = $this->primaryGroup;
854       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
855           sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
856     }
858     if ($this->activate_shadowMin != "1" ) {
859       $this->shadowMin = "";
860     }
862     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
863       $this->shadowMax = "";
864     }
866     if ($this->activate_shadowWarning != "1" ) {
867       $this->shadowWarning = "";
868     }
870     if ($this->activate_shadowInactive != "1" ) {
871       $this->shadowInactive = "";
872     }
874     if ($this->activate_shadowExpire != "1" ) {
875       $this->shadowExpire = "";
876     }
878     /* Fill gecos */
879     if (isset($this->parent) && $this->parent !== NULL){
880       $this->gecos= rewrite($this->parent->by_object['user']->cn);
881       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
882         $this->gecos= "";
883       }
884     }
886     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
887       $this->$attr = (int) $this->$attr;
888     }
889     /* Call parents save to prepare $this->attrs */
890     plugin::save();
892     /* Trust accounts */
893     $objectclasses= array();
894     foreach ($this->attrs['objectClass'] as $key => $class){
895       if (preg_match('/trustAccount/i', $class)){
896         continue;
897       }
898       $objectclasses[]= $this->attrs['objectClass'][$key];
899     }
900     $this->attrs['objectClass']= $objectclasses;
901     if ($this->trustModel != ""){
902       $this->attrs['objectClass'][]= "trustAccount";
903       $this->attrs['trustModel']= $this->trustModel;
904       $this->attrs['accessTo']= array();
905       if ($this->trustModel == "byhost"){
906         foreach ($this->accessTo as $host){
907           $this->attrs['accessTo'][]= $host;
908         }
909       }
910     } else {
911       if ($this->was_trust_account){
912         $this->attrs['accessTo']= array();
913         $this->attrs['trustModel']= array();
914       }
915     }
917     if(empty($this->attrs['gosaDefaultPrinter'])){
918       $thid->attrs['gosaDefaultPrinter']=array();
919     }
922     /* include global link_info */
923     $this->cleanup();
925     /* This is just a test, we have had duplicated ids 
926        in the past when copy & paste was used. 
927        Normaly this should not happen.
928      */ 
929     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
930       $used = $this->get_used_uid_numbers();
931       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
932         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);
933       }
934     }
936     $ldap= $this->config->get_ldap_link();
937     $ldap->cd($this->dn);
938     unset($this->attrs['uid']);
939     $ldap->modify ($this->attrs); 
941     /* Log last action */ 
942     if($this->initially_was_account){
943       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
944     }else{
945       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
946     }
948     if (!$ldap->success()){
949       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
950     }
952     /* Remove lock needed for unique id generation */
953     del_lock ("uidnumber");
955     // Save ssh stuff if needed
956     if ($this->ssh) {
957       $this->ssh->setDN($this->dn);
958       $this->ssh->save();
959     }
961     /* Take care about groupMembership values: add to groups */
962     foreach ($this->groupMembership as $key => $value){
963       if (!isset($this->savedGroupMembership[$key])){
964         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
965         $g->set_acl_base($key);
966         $g->by_object['group']->addUser($this->uid);
967         $g->save();
968       }
969     }
971     /* Remove groups not listed in groupMembership */
972     foreach ($this->savedGroupMembership as $key => $value){
973       if (!isset($this->groupMembership[$key])){
974         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
975         $g->set_acl_base($key);
976         $g->by_object['group']->removeUser ($this->uid);
977         $g->save();
978       }
979     }
981     /* Optionally execute a command after we're done */
982     if ($this->initially_was_account == $this->is_account){
983       if ($this->is_modified){
984         $this->handle_post_events("modify",array("uid" => $this->uid));
985       }
986     } else {
987       $this->handle_post_events("add" ,array("uid"=> $this->uid));
988     }
989   }
992   /* Check formular input */
993   function check()
994   {
995     /* Include global link_info */
996     $ldap= $this->config->get_ldap_link();
998     /* Append groups as memberGroup: to check hook 
999      */
1000     $tmp_attributes  = $this->attributes;    
1001     $this->attributes[] = "memberGroup";
1002     $this->memberGroup = array();
1003     foreach($this->groupMembership as $dn => $name){
1004       $this->memberGroup[] = $name;
1005     }
1007     /* Call common method to give check the hook */
1008     $message= plugin::check();
1009     $this->attributes = $tmp_attributes;
1011     /* must: homeDirectory */
1012     if ($this->homeDirectory == ""){
1013       $message[]= msgPool::required(_("Home directory"));
1014     }
1015     if (!tests::is_path($this->homeDirectory)){
1016       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1017     }
1019     /* Check ID's if they are forced by user */
1020     if ($this->force_ids == "1"){
1022       /* Valid uid/gid? */
1023       if (!tests::is_id($this->uidNumber)){
1024         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1025       } else {
1026         if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1027           $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1028         }
1029       }
1030       if (!tests::is_id($this->gidNumber)){
1031         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1032       } else {
1033         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1034           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1035         }
1036       }
1037     }
1039     /* Check dates */
1040     if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
1041       $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
1042     }
1044     /* Check shadow settings, well I like spaghetties... */
1045     if ($this->activate_shadowMin){
1046       if (!tests::is_id($this->shadowMin)){
1047         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1048       }
1049     }
1050     if ($this->activate_shadowMax){
1051       if (!tests::is_id($this->shadowMax)){
1052         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1053       }
1054     }
1055     if ($this->activate_shadowWarning){
1056       if (!tests::is_id($this->shadowWarning)){
1057         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1058       }
1059       if (!$this->activate_shadowMax){
1060         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1061       }
1062       if ($this->shadowWarning > $this->shadowMax){
1063         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1064       }
1065       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1066         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1067       }
1068     }
1069     if ($this->activate_shadowInactive){
1070       if (!tests::is_id($this->shadowInactive)){
1071         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1072       }
1073       if (!$this->activate_shadowMax){
1074         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1075       }
1076     }
1077     if ($this->activate_shadowMin && $this->activate_shadowMax){
1078       if ($this->shadowMin > $this->shadowMax){
1079         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1080       }
1081     }
1083     return ($message);
1084   }
1087   function multiple_check()
1088   {
1089     $message = plugin::multiple_check();
1090     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1091       $message[]= msgPool::required(_("Home directory"));
1092     }
1093     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1094       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1095     }
1097     /* Check shadow settings, well I like spaghetties... */
1098     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1099       if (!tests::is_id($this->shadowMin)){
1100         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1101       }
1102     }
1103     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1104       if (!tests::is_id($this->shadowMax)){
1105         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1106       }
1107     }
1108     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1109       if (!tests::is_id($this->shadowWarning)){
1110         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1111       }
1112       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1113         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1114       }
1115       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1116         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1117       }
1118       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1119         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1120       }
1121     }
1122     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1123       if (!tests::is_id($this->shadowInactive)){
1124         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1125       }
1126       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1127         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1128       }
1129     }
1130     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1131       if ($this->shadowMin > $this->shadowMax){
1132         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1133       }
1134     }
1136     return($message);
1137   }
1140   function addGroup ($groups)
1141   {
1142     /* include global link_info */
1143     $ldap= $this->config->get_ldap_link();
1145     /* Walk through groups and add the descriptive entry if not exists */
1146     foreach ($groups as $value){
1148       if (!array_key_exists($value, $this->groupMembership)){
1149         $ldap->cat($value, array('cn', 'description', 'dn'));
1150         $attrs= $ldap->fetch();
1151         error_reporting (0);
1152         if (!isset($attrs['description'][0])){
1153           $entry= $attrs["cn"][0];
1154         } else {
1155           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1156           $entry= $attrs["cn"][0]." [$dsc]";
1157         }
1158         error_reporting (E_ALL | E_STRICT);
1160         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1161           $this->groupMembership[$attrs['dn']]= $entry;
1162           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1163             unset($this->groupMembership_some[$attrs['dn']]);
1164           }
1165         }
1166       }
1167     }
1169     /* Sort groups */
1170     asort ($this->groupMembership);
1171     reset ($this->groupMembership);
1172   }
1175   /* Del posix user from some groups */
1176   function delGroup ($groups)
1177   {
1178     $dest= array();
1179     foreach($groups as $dn_to_del){
1180       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1181         unset($this->groupMembership[$dn_to_del]);
1182       }
1183       if($this->multiple_support_active){
1184         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1185           unset($this->groupMembership_some[$dn_to_del]);
1186         }
1187       }
1188     }
1189   }
1192   /* Adapt from template, using 'dn' */
1193   function adapt_from_template($dn, $skip= array())
1194   {
1195     /* Include global link_info */
1196     $ldap= $this->config->get_ldap_link();
1198     plugin::adapt_from_template($dn, $skip);
1199     $template= $this->attrs['uid'][0];
1201     /* Adapt group membership */
1202     $ldap->cd($this->config->current['BASE']);
1203     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1205     while ($this->attrs= $ldap->fetch()){
1206       if (!isset($this->attrs["description"][0])){
1207         $entry= $this->attrs["cn"][0];
1208       } else {
1209         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1210       }
1211       $this->groupMembership[$ldap->getDN()]= $entry;
1212     }
1214     /* Fix primary group settings */
1215     $ldap->cd($this->config->current['BASE']);
1216     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1217     if ($ldap->count() != 1){
1218       $this->primaryGroup= $this->gidNumber;
1219     }
1221     $ldap->cd($this->config->current['BASE']);
1222     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1223     while($attr = $ldap->fetch()){
1224       $tmp = $attr['accessTo'];
1225       unset ($tmp['count']);
1226       $this->accessTo = $tmp;   
1227     }
1229     /* Adjust shadow checkboxes */
1230     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1231       if ($this->$val != 0){
1232         $oval= "activate_".$val;
1233         $this->$oval= "1";
1234       }
1235     }
1237     /* FIXME: NEED review of this section */
1238     /* Need to check shadowExpire separately */
1240     /* 
1241      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1242      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1243      */
1244     if(abs($this->shadowExpire - time())>86400) {
1245       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1246     }
1248     /* Only enable checkbox, if shadowExpire is in the future */
1249     if($this->shadowExpire > time()) {
1250       $this->activate_shadowExpire= "1";
1251     }
1252   }
1254   function convertToSeconds($val)
1255   {
1256     if ($val != 0){
1257       $val*= 60 * 60 * 24;
1258     } else {
1259       $date= getdate();
1260       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1261     }
1262     return($val);
1263   }
1266   function get_used_uid_numbers()
1267   {
1268     $ids= array();
1269     $ldap= $this->config->get_ldap_link();
1271     $ldap->cd ($this->config->current['BASE']);
1272     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1274     /* Get list of ids */
1275     while ($attrs= $ldap->fetch()){
1276       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1277     }
1278     return($ids);
1279   }
1282   /* Get posts from copy & paste dialog */ 
1283   function saveCopyDialog()
1284   {
1285     if(isset($_POST['homeDirectory'])){
1286       $this->homeDirectory = $_POST['homeDirectory'];
1287       if (isset ($_POST['force_ids'])){
1288         $data= 1;
1289         $this->gidNumber = $_POST['gidNumber'];
1290         $this->uidNumber = $_POST['uidNumber'];
1291       } else {
1292         $data= 0;
1293       }
1294       if ($this->force_ids != $data){
1295         $this->is_modified= TRUE;
1296       }
1297       $this->force_ids= $data;
1298       $data= $_POST['primaryGroup'];
1299       if ($this->primaryGroup != $data){
1300         $this->is_modified= TRUE;
1301       }
1302       $this->primaryGroup= $_POST['primaryGroup'];
1303     }
1304   }
1307   /* Create the posix dialog part for copy & paste */
1308   function getCopyDialog()
1309   {
1310     /* Skip dialog creation if this is not a valid account*/
1311     if(!$this->is_account) return("");
1312     if ($this->force_ids == 1){
1313       $force_ids = "checked";
1314       if (session::get('js')){
1315         $forceMode = "";
1316       }
1317     } else {
1318       if (session::get('js')){
1319         if($this->acl != "#none#")
1320           $forceMode ="disabled";
1321       }
1322       $force_ids = "";
1323     }
1325     $sta = "";
1327     /* Open group add dialog */
1328     if(isset($_POST['edit_groupmembership'])){
1329       $this->groupSelect = new groupSelect($this->config,get_userinfo());
1330       $sta = "SubDialog";
1331     }
1333     /* If the group-add dialog is closed, call execute 
1334        to ensure that the membership is updatd */
1335     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1336       $this->execute();
1337       $this->groupSelect =NULL;
1338     }
1340     if($this->groupSelect){
1341       $str = $this->execute(true);
1342       $ret = array();
1343       $ret['string'] = $str;
1344       $ret['status'] = $sta;
1345       return($ret);
1346     }
1348     /* If a group member should be deleted, simply call execute */
1349     if(isset($_POST['delete_groupmembership'])){
1350       $this->execute();
1351     }
1353     /* Assigned informations to smarty */
1354     $smarty = get_smarty();
1355     $smarty->assign("homeDirectory",$this->homeDirectory);
1356     $smarty->assign("secondaryGroups",$this->secondaryGroups);
1357     $smarty->assign("primaryGroup",$this->primaryGroup);
1359     $smarty->assign("uidNumber",$this->uidNumber);
1360     $smarty->assign("gidNumber",$this->gidNumber);
1361     $smarty->assign("forceMode",$forceMode);
1362     $smarty->assign("force_ids",$force_ids);
1363     if (!count($this->groupMembership)){
1364       $smarty->assign("groupMembership", array("&nbsp;"));
1365     } else {
1366       $smarty->assign("groupMembership", $this->groupMembership);
1367     }
1369     /* Display wars message if there are more than 16 group members */
1370     if (count($this->groupMembership) > 16){
1371       $smarty->assign("groups", "too_many_for_nfs");
1372     } else {
1373       $smarty->assign("groups", "");
1374     }
1375     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1377     $ret = array();
1378     $ret['string'] = $str;
1379     $ret['status'] = $sta;
1380     return($ret);
1381   }
1384   function PrepareForCopyPaste($source)
1385   {
1386     plugin::PrepareForCopyPaste($source);
1388     /* Avoid using the same gid/uid number as source user 
1389        empty numbers to enforce new ones. */
1390     $this->savedUidNumber = "";
1391     $this->savedGidNumber = "";
1393     /* Get group membership */
1394     $ldap = $this->config->get_ldap_link();
1395     $ldap->cd($this->config->current['BASE']);
1396     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1398     while ($attrs= $ldap->fetch()){
1399       if (!isset($attrs["description"][0])){
1400         $entry= $attrs["cn"][0];
1401       } else {
1402         $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1403       }
1404       $this->groupMembership[$ldap->getDN()]= $entry;
1405     }
1406     asort($this->groupMembership);
1407     reset($this->groupMembership);
1409     /* Fill group */
1410     if(isset($source['gidNumber'][0])){
1411       $this->primaryGroup= $source['gidNumber'][0];
1412     }
1414   }
1417   function multiple_execute()
1418   {
1419     return($this->execute());
1420   }
1423   static function plInfo()
1424   {
1425     return (array(
1426           "plDescription"     => _("POSIX account"),
1427           "plSelfModify"      => TRUE,
1428           "plDepends"         => array("user"),
1429           "plPriority"        => 2,
1430           "plSection"         => array("personal" => _("My account")),
1431           "plCategory"        => array("users"),
1432           "plOptions"         => array(),
1434           "plProvidedAcls"  => array(
1436             "homeDirectory"       =>  _("Home directory"), 
1437             "loginShell"          =>  _("Shell"),
1438             "uidNumber"           =>  _("User ID"),
1439             "gidNumber"           =>  _("Group ID"),
1441             "mustchangepassword"=>  _("Force password change on login"),
1442             "shadowMin"           =>  _("Shadow min"),
1443             "shadowMax"           =>  _("Shadow max"),
1444             "shadowWarning"       =>  _("Shadow warning"),
1445             "shadowInactive"      =>  _("Shadow inactive"),
1446             "shadowExpire"        =>  _("Shadow expire"),
1447             "sshPublickey"        =>  _("Public SSH key"),
1448             "trustModel"          =>  _("System trust model")))
1449               );
1450   }
1453   /* Return selected values for multiple edit */
1454   function get_multi_edit_values()
1455   {
1456     $ret = plugin::get_multi_edit_values();
1457     $ret['groupMembership']     = $this->groupMembership;
1458     $ret['groupMembership_some']= $this->groupMembership_some;
1460     if(in_array("primaryGroup",$this->multi_boxes)){
1461       $ret['primaryGroup'] = $this->primaryGroup;
1462     }
1463     if(in_array("trustmode",$this->multi_boxes)){
1464       $ret['trustModel'] = $this->trustModel;
1465       $ret['accessTo'] = $this->accessTo;
1466     }
1467     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1468       $active = "activate_".$entry;
1469       if(in_array($active,$this->multi_boxes)){
1470         $ret[$entry] = $this->$entry;
1471         $ret[$active] = $this->$active;
1472       }
1473     }
1474     if(in_array("mustchangepassword",$this->multi_boxes)){
1475       $ret['mustchangepassword'] = $this->mustchangepassword;
1476     }
1477     return($ret);
1478   }
1481   /* Save posts for multiple edit 
1482    */
1483   function multiple_save_object()
1484   {
1485     if(isset($_POST['posix_mulitple_edit'])){
1487       /* Backup expire value */ 
1488       $expire_tmp = $this->shadowExpire;
1490       /* Update all values */
1491       plugin::multiple_save_object();
1493       /* Get selected checkboxes */
1494       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1495         if(isset($_POST["use_".$val])){
1496           $this->multi_boxes[] = $val;
1497         }
1498       }
1500       /* Update special values, checkboxes for posixShadow */
1501       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1502         if($this->acl_is_writeable($var)){
1503           $activate_var = "activate_".$var;
1504           if(in_array($activate_var, $this->multi_boxes)){
1505             if(isset($_POST['activate_'.$var])){
1506               $this->$activate_var  = true;
1507               $this->$var      = $_POST[$var];
1508             }else{
1509               $this->$activate_var  = false;
1510               $this->$var      = 0;
1511             }
1512           }
1513         }
1514       }
1516       /* Restore shadow value, if the shadow attribute isn't used */
1517       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1518         $this->shadowExpire = $expire_tmp;
1519       }
1521       /* Force change password ? */
1522       if(isset($_POST['mustchangepassword'])){
1523         $this->mustchangepassword = TRUE;
1524       }else{
1525         $this->mustchangepassword = FALSE;
1526       }
1528       /* Trust mode - special handling */
1529       if($this->acl_is_writeable("trustModel")){
1530         if (isset($_POST['trustmode'])){
1531           $saved= $this->trustModel;
1532           if ($_POST['trustmode'] == "1"){
1533             $this->trustModel= "fullaccess";
1534           } elseif ($_POST['trustmode'] == "2"){
1535             $this->trustModel= "byhost";
1536           } else {
1537             $this->trustModel= "";
1538           }
1539           if ($this->trustModel != $saved){
1540             $this->is_modified= TRUE;
1541           }
1542         }
1543       }
1545       /* Save primary group settings */
1546       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1547         $data= $_POST['primaryGroup'];
1548         if ($this->primaryGroup != $data){
1549           $this->is_modified= TRUE;
1550         }
1551         $this->primaryGroup= $_POST['primaryGroup'];
1552       }
1553     }
1554   }
1557   /* Initialize plugin with given atribute arrays 
1558    */
1559   function init_multiple_support($attrs,$all)
1560   {
1561     plugin::init_multiple_support($attrs,$all);
1563     /* Some dummy values */
1564     $groups_some = array();
1565     $groups_all  = array();
1566     $groups_uid  = array();
1567     $uids        = array();
1568     $first       = TRUE;
1570     /* Get all groups used by currently edited users */
1571     $uid_filter="";  
1572     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1573       $uid = $this->multi_attrs_all['uid'][$i];
1574       $uids[] = $uid;
1575       $uid_filter.= "(memberUid=".$uid.")"; 
1576     }
1577     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1578     $ldap = $this->config->get_ldap_link();
1579     $ldap->cd($this->config->current['BASE']);
1580     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1581     while($group = $ldap->fetch()){
1582       $groups_some[$group['dn']] = $group['cn'][0];
1583       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1584         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1585       }
1586     }
1588     /* Create an array, containing all used groups */
1589     $groups_all = $groups_some;
1590     foreach($groups_all as $id => $group){
1591       foreach($uids as $uid){
1592         if(!in_array($uid,$groups_uid[$id])){
1593           unset($groups_all[$id]);
1594           break;
1595         }
1596       }
1597     }
1599     /* Assign group array */
1600     $this->groupMembership = $groups_all;
1602     /* Create an array of all grouops used by all users */
1603     foreach( $groups_all as $dn => $cn){
1604       if(isset($groups_some[$dn])){
1605         unset($groups_some[$dn]);
1606       }
1607     }
1608     $this->groupMembership_some = $groups_some;
1609     $this->primaryGroup = $this->gidNumber;
1611     /* Is this account a trustAccount? */
1612     if (isset($this->multi_attrs['trustModel'])){
1613       $this->trustModel= $this->multi_attrs['trustModel'][0];
1614       $this->was_trust_account= TRUE;
1615       $this->multi_boxes[] = "trustmode";
1616     } else {
1617       $this->was_trust_account= FALSE;
1618       $this->trustModel= "";
1619     }
1621     /* Create access informations */
1622     $this->accessTo = array();
1623     if (isset($this->multi_attrs['accessTo'])){
1624       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1625         $tmp= $this->multi_attrs['accessTo'][$i];
1626         $this->accessTo[$tmp]= $tmp;
1627       }
1628     }
1630     /* Adjust shadow checkboxes */
1631     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1632           "shadowExpire") as $val){
1633       if ($this->$val != 0){
1634         $oval= "activate_".$val;
1635         $this->$oval= "1";
1636       }
1637     }
1639     /* Convert to seconds */
1640     if(isset($this->multi_attrs['shadowExpire'])){
1641       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1642     }else{
1643       $this->activate_shadowExpire = FALSE;
1644     }
1645   }
1648   function set_multi_edit_values($attrs)
1649   {
1650     $groups = array();
1652     /* Update groupMembership, keep optinal group */
1653     foreach($attrs['groupMembership_some'] as $dn => $cn){
1654       if(isset($this->groupMembership[$dn])){
1655         $groups[$dn] = $cn;
1656       }
1657     }
1658     /* Update groupMembership, add forced groups */
1659     foreach($attrs['groupMembership'] as $dn => $cn){
1660       $groups[$dn] = $cn;
1661     }
1662     plugin::set_multi_edit_values($attrs);
1663     $this->groupMembership = $groups;
1664   }
1667 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1668 ?>