Code

Removed old and ununsed code
[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     /* 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     // Display dialog to allow selection of groups
336     if (isset($_POST['edit_groupmembership'])){
337       $this->groupSelect = new groupSelect($this->config,get_userinfo());
338       $this->dialog= TRUE;
339     }
341     // Allow to select trusted machines from a list
342     if (isset($_POST["add_ws"])){
343       $this->trustSelect= new trustSelect($this->config,get_userinfo());
344       $this->dialog= TRUE;
345     }
347     // Cancel trust and group dialog
348     if (isset($_POST['add_groups_cancel']) || isset($_POST['add_ws_cancel'])){
349       $this->groupSelect= NULL;
350       $this->trustSelect= NULL;
351       $this->dialog= FALSE;
352     }
354     // Add groups selected in groupSelect dialog to ours.
355     if (isset($_POST['add_groups_finish']) && $this->groupSelect){
356       $groups = $this->groupSelect->detectPostActions();
357       if(isset($groups['targets'])){
358         $this->addGroup ($groups['targets']);
359         $this->is_modified= TRUE;
360       }
361       $this->groupSelect= NULL;
362       $this->dialog= FALSE;
363     }
365     // Remove groups from currently selected groups.
366     if (isset($_POST['delete_groupmembership']) && 
367         isset($_POST['group_list']) && count($_POST['group_list'])){
369       $this->delGroup ($_POST['group_list']);
370     }
372     // Add selected machines to trusted ones.
373     if (isset($_POST["add_ws_finish"]) &&  $this->trustSelect){
374       $trusts = $this->trustSelect->detectPostActions();
375       if(isset($trusts['targets'])){
377         $headpage = $this->trustSelect->getHeadpage();              
378         foreach($trusts['targets'] as $id){
379           $attrs = $headpage->getEntry($id);
380           $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
381         }
382         ksort($this->accessTo);
383         $this->is_modified= TRUE;
384       }
385       $this->trustSelect= NULL;
386       $this->dialog= FALSE;
387     }
389     // Remove machine from trusted ones.
390     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
391       foreach($_POST['workstation_list'] as $name){
392         unset ($this->accessTo[$name]);
393       }
394       $this->is_modified= TRUE;
395     }
399     /* Templates now! */
400     $smarty= get_smarty();
401     $smarty->assign("usePrototype", "true");
403     /* Show ws dialog */
404     if ($this->trustSelect){
405       return($this->trustSelect->execute());
406     }
408     /* Manage group add dialog */
409     if ($this->groupSelect){
410       return($this->groupSelect->execute());
411     }
413     // Handle ssh dialog?
414     if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
415         $smarty->assign("usePrototype", "false");
416        if ($result= $this->ssh->execute()) {
417          $this->dialog= true;
418          return $result;
419        }
420        $this->dialog= false;
421     }
424     /* Show main page */
425     $smarty= get_smarty();
426     $smarty->assign("usePrototype", "true");
428     /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */ 
429     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
431     $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey", $SkipWrite));
433     /* Depending on pwmode, currently hardcoded because there are no other methods */
434     if ( 1 == 1 ){
435       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
437       $shadowMinACL     =  $this->getacl("shadowMin",$SkipWrite);
438       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
439                                               "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
441       $shadowMaxACL     =  $this->getacl("shadowMax",$SkipWrite);
442       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
443                                               "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
445       $shadowInactiveACL=  $this->getacl("shadowInactive",$SkipWrite);
446       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"), 
447                                               "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
449       $shadowWarningACL =  $this->getacl("shadowWarning",$SkipWrite);
450       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"), 
451                                               "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
453       foreach( array("activate_shadowMin", "activate_shadowMax",
454                      "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
455         if ($this->$val == 1){
456           $smarty->assign("$val", "checked");
457         } else {
458           $smarty->assign("$val", "");
459         }
460         $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
461       }
463       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
464     }
466     // Set last system login
467     $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
469     /* Fill arrays */
470     $smarty->assign("shells", $this->loginShellList);
471     $smarty->assign("secondaryGroups", $this->secondaryGroups);
472     $smarty->assign("primaryGroup", $this->primaryGroup);
473     if(!$this->multiple_support_active){
474       if (!count($this->groupMembership)){
475         $smarty->assign("groupMembership", array("&nbsp;"));
476       } else {
477         $smarty->assign("groupMembership", $this->groupMembership);
478       }
479     }else{
480       $smarty->assign("groupMembership", $this->groupMembership);
481       $smarty->assign("groupMembership_some", $this->groupMembership_some);
482     }
483     if (count($this->groupMembership) > 16){
484       $smarty->assign("groups", "too_many_for_nfs");
485     } else {
486       $smarty->assign("groups", "");
487     }
489     /* Avoid "Undefined index: forceMode" */
490     $smarty->assign("forceMode", "");
492     /* Checkboxes */
493     if ($this->force_ids == 1){
494       $smarty->assign("force_ids", "checked");
495       if (session::get('js')){
496         $smarty->assign("forceMode", "");
497       }
498     } else {
499       if (session::get('js')){
500         $smarty->assign("forceMode", "disabled");
501       }
502       $smarty->assign("force_ids", "");
503     }
505     /* Create onClick="" action string for the "Force UID/GID" option 
506      */
507     $onClickIDS ="";
508     if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
509       $onClickIDS .= "changeState('uidNumber');";
510     }
511     if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
512       $onClickIDS .= "changeState('gidNumber');";
513     }
514     $smarty->assign("onClickIDS", $onClickIDS);
515     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
517     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
518       if(in_array($val,$this->multi_boxes)){
519         $smarty->assign("use_".$val,TRUE);
520       }else{
521         $smarty->assign("use_".$val,FALSE);
522       }
523     }
526     /* Load attributes and acl's */
527     foreach($this->attributes as $val){
528       if(in_array($val,$this->multi_boxes)){
529         $smarty->assign("use_".$val,TRUE);
530       }else{
531         $smarty->assign("use_".$val,FALSE);
532       }
534       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
535       {
536         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
537         $smarty->assign("$val", $this->$val);
538         continue;
539       }
540       $smarty->assign("$val", $this->$val);
541       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
542     }
543     if($SkipWrite){
544       $smarty->assign("groupMembershipACL","r");
545     }else{
546       $smarty->assign("groupMembershipACL","rw");
547     }
548     $smarty->assign("status", $this->status);
550     /* Work on trust modes */
551     $smarty->assign("trusthide", " disabled ");
552     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
553     if ($this->trustModel == "fullaccess"){
554       $trustmode= 1;
555       // pervent double disable tag in html code, this will disturb our clean w3c html
556       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
558     } elseif ($this->trustModel == "byhost"){
559       $trustmode= 2;
560       $smarty->assign("trusthide", "");
561     } else {
562       // pervent double disable tag in html code, this will disturb our clean w3c html
563       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
564       $trustmode= 0;
565     }
566     $smarty->assign("trustmode", $trustmode);
567     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
568           2 => _("allow access to these hosts")));
572     if((count($this->accessTo))==0)
573       $smarty->assign("emptyArrAccess",true);
574     else
575       $smarty->assign("emptyArrAccess",false);
577       if($this->mustchangepassword){
578         $smarty->assign("mustchangepassword", " checked ");
579       } else {
580         $smarty->assign("mustchangepassword", "");
581       }
583     $smarty->assign("workstations", $this->accessTo);
585     // Add SSH button if available
586     $smarty->assign("sshPublicKey", $this->ssh?1:0);
588     $smarty->assign("apply", apply_filter());
589     $smarty->assign("multiple_support" , $this->multiple_support_active);
590     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
591     return($display);
592   }
595   /* remove object from parent */
596   function remove_from_parent()
597   {
598     /* Cancel if there's nothing to do here */
599     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
600       return;
601     }
604     /* Remove and write to LDAP */
605     plugin::remove_from_parent();
607     /* Zero out array */
608     $this->attrs['gosaHostACL']= array();
610     /* Keep uid, because we need it for authentification! */
611     unset($this->attrs['uid']);
612     unset($this->attrs['trustModel']);
614     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
615     /* include global link_info */
616      $this->attributes, "Save");
617     $ldap= $this->config->get_ldap_link();
618     $ldap->cd($this->dn);
619     $this->cleanup();
620     $ldap->modify ($this->attrs); 
622     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
624     if (!$ldap->success()){
625       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
626     }
628     /* Delete group only if cn is uid and there are no other
629        members inside */
630     $ldap->cd ($this->config->current['BASE']);
631     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
632     if ($ldap->count() != 0){
633       $attrs= $ldap->fetch();
634       if ($attrs['cn'][0] == $this->uid &&
635           !isset($this->attrs['memberUid'])){
637         $ldap->rmDir($ldap->getDN());
638       }
639     }
641     /* Optionally execute a command after we're done */
642     $this->handle_post_events("remove",array("uid" => $this->uid));
643   }
646   function save_object()
647   {
648     if (isset($_POST['posixTab'])){
649       /* Save values to object */
650       plugin::save_object();
653       /* Save force GID checkbox */
654       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
655         if (isset ($_POST['force_ids'])){
656           $data= 1;
657         } else {
658           $data= 0;
659         }
660         if ($this->force_ids != $data){
661           $this->is_modified= TRUE;
662         }
663         $this->force_ids= $data;
664       }
666       /*Save primary group settings */
667       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
668         $data= $_POST['primaryGroup'];
669         if ($this->primaryGroup != $data){
670           $this->is_modified= TRUE;
671         }
672         $this->primaryGroup= $_POST['primaryGroup'];
673       }
675       /* Get seelcted shadow checkboxes */
676       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
677         if($this->acl_is_writeable($var)){
678           $activate_var = "activate_".$var;
679           if(isset($_POST['activate_'.$var])){
680             $this->$activate_var  = true;
681             $this->$var      = $_POST[$var];
682           }else{
683             $this->$activate_var  = false;
684             if ($var != "shadowExpire") {
685               $this->$var      = 0;
686             }
687           }
688         }
689       }
691       /* Force change password ? */
692       if(isset($_POST['mustchangepassword'])){
693         $this->mustchangepassword = TRUE;
694       }else{
695         $this->mustchangepassword = FALSE;
696       }
698       /* Trust mode - special handling */
699       if($this->acl_is_writeable("trustModel")){
700         if (isset($_POST['trustmode'])){
701           $saved= $this->trustModel;
702           if ($_POST['trustmode'] == "1"){
703             $this->trustModel= "fullaccess";
704           } elseif ($_POST['trustmode'] == "2"){
705             $this->trustModel= "byhost";
706           } else {
707             $this->trustModel= "";
708           }
709           if ($this->trustModel != $saved){
710             $this->is_modified= TRUE;
711           }
712         }
713       }
714     }
716     /* Get regex from alphabet */
717     if(isset($_GET['search'])){
718       $this->GroupRegex = $_GET['search']."*";
719     }
721     /* Check checkboxes and regexes */
722     if(isset($_POST["PosixGroupDialogPosted"])){
724       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
725         $this->SubSearch = true;
726       }else{
727         $this->SubSearch = false;
728       }
729       if(isset($_POST['guser'])){
730         $this->GroupUserRegex = $_POST['guser'];
731       }
732       if(isset($_POST['regex'])){
733         $this->GroupRegex = $_POST['regex'];
734       }
735     }
736     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
737     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
738   }
741   /* Save data to LDAP, depending on is_account we save or delete */
742   function save()
743   {
745     /* Adapt shadow values */
746     if (!$this->activate_shadowExpire){
747       $this->shadowExpire= "0";
748     } else {
749       /* Transform date to days since the beginning */
750       list($day, $month, $year)= explode('.', $this->shadowExpire, 3);
751       $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
752     }
753     if (!$this->activate_shadowMax){
754       $this->shadowMax= "0";
755     }
756     if ($this->mustchangepassword){
757       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
758     } else {
759       $this->shadowLastChange= (int)(date("U") / 86400);
760     }
761     if (!$this->activate_shadowWarning){
762       $this->shadowWarning= "0";
763     }
765     /* Check what to do with ID's 
766         Nothing forced, so we may have to generate our own IDs, if not done already.
767      */
768     if ($this->force_ids == 0){
770       /* Handle uidNumber. 
771        * - use existing number if possible
772        * - if not, try to create a new uniqe one.
773        * */
774       if ($this->savedUidNumber != ""){
775         $this->uidNumber= $this->savedUidNumber;
776       } else {
778         /* Calculate new id's. We need to place a lock before calling get_next_id
779            to get real unique values. 
780          */
781         $wait= 10;
782         while (get_lock("uidnumber") != ""){
783           sleep (1);
785           /* Oups - timed out */
786           if ($wait-- == 0){
787             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
788             break;
789           }
790         }
791         add_lock ("uidnumber", "gosa");
792         $this->uidNumber= get_next_id("uidNumber", $this->dn);
793       }
794     }
797     /* Handle gidNumber 
798      * - If we do not have a primary group selected (automatic), we will check if there 
799      *    is already a group  with the same name and use this as primary.
800      * - .. if we couldn't find a group with the same name, we will create a new one, 
801      *    using the users uid as cn and a generated uniqe gidNumber. 
802      * */
803     if ($this->primaryGroup == 0 || $this->force_ids){
805       /* Search for existing group */
806       $ldap = $this->config->get_ldap_link();
807       $ldap->cd($this->config->current['BASE']);
809       /* Are we forced to use a special gidNumber? */ 
810       if($this->force_ids){
811         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
812       }else{
813         $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
814       }
816       /* No primary group found, create a new one */
817       if ($ldap->count() == 0){
819         $groupcn = $this->uid;
820         $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
821         $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
822             'cn='.$groupcn.','.get_groups_ou(), $this->dn);
824         /* Request a new and uniqe gidNumber, if required */
825         if(!$this->force_ids){
826           $this->gidNumber= get_next_id("gidNumber", $this->dn);
827         }
829         /* If forced gidNumber could not be found, then check if the given group name already exists 
830            we do not want to modify the gidNumber of an existing group.
831          */
832         $cnt= 0; 
833         while($ldap->dn_exists($groupdn) && ($cnt < 100)){
834           $cnt ++;
835           $groupcn = $this->uid."_".$cnt;
836           $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
837               'cn='.$groupcn.','.get_groups_ou(), $this->dn);
838         }
840         /* Create new primary group and enforce the new gidNumber */
841         $g= new group($this->config, $groupdn);
842         $g->cn= $groupcn;
843         $g->force_gid= 1;
844         $g->gidNumber= $this->gidNumber;
845         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
846         $g->save ();
848         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
849             sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
850       }else{
851         $attrs = $ldap->fetch();
852         $this->gidNumber = $attrs['gidNumber'][0];
853         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
854             "Found and used: <i>".$attrs['dn']."</i>",
855             sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
856       }
857     }else{
859       /* Primary group was selected by user
860        */
861       $this->gidNumber = $this->primaryGroup;
862       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
863           sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
864     }
866     if ($this->activate_shadowMin != "1" ) {
867       $this->shadowMin = "";
868     }
870     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
871       $this->shadowMax = "";
872     }
874     if ($this->activate_shadowWarning != "1" ) {
875       $this->shadowWarning = "";
876     }
878     if ($this->activate_shadowInactive != "1" ) {
879       $this->shadowInactive = "";
880     }
882     if ($this->activate_shadowExpire != "1" ) {
883       $this->shadowExpire = "";
884     }
886     /* Fill gecos */
887     if (isset($this->parent) && $this->parent !== NULL){
888       $this->gecos= rewrite($this->parent->by_object['user']->cn);
889       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
890         $this->gecos= "";
891       }
892     }
894     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
895       $this->$attr = (int) $this->$attr;
896     }
897     /* Call parents save to prepare $this->attrs */
898     plugin::save();
900     /* Trust accounts */
901     $objectclasses= array();
902     foreach ($this->attrs['objectClass'] as $key => $class){
903       if (preg_match('/trustAccount/i', $class)){
904         continue;
905       }
906       $objectclasses[]= $this->attrs['objectClass'][$key];
907     }
908     $this->attrs['objectClass']= $objectclasses;
909     if ($this->trustModel != ""){
910       $this->attrs['objectClass'][]= "trustAccount";
911       $this->attrs['trustModel']= $this->trustModel;
912       $this->attrs['accessTo']= array();
913       if ($this->trustModel == "byhost"){
914         foreach ($this->accessTo as $host){
915           $this->attrs['accessTo'][]= $host;
916         }
917       }
918     } else {
919       if ($this->was_trust_account){
920         $this->attrs['accessTo']= array();
921         $this->attrs['trustModel']= array();
922       }
923     }
925     if(empty($this->attrs['gosaDefaultPrinter'])){
926       $thid->attrs['gosaDefaultPrinter']=array();
927     }
930     /* include global link_info */
931     $this->cleanup();
932  
933     /* This is just a test, we have had duplicated ids 
934         in the past when copy & paste was used. 
935        Normaly this should not happen.
936      */ 
937     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
938       $used = $this->get_used_uid_numbers();
939       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
940         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);
941       }
942     }
944     $ldap= $this->config->get_ldap_link();
945     $ldap->cd($this->dn);
946     unset($this->attrs['uid']);
947     $ldap->modify ($this->attrs); 
949     /* Log last action */ 
950     if($this->initially_was_account){
951       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
952     }else{
953       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
954     }
956     if (!$ldap->success()){
957       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
958     }
960     /* Remove lock needed for unique id generation */
961     del_lock ("uidnumber");
963     // Save ssh stuff if needed
964     if ($this->ssh) {
965       $this->ssh->setDN($this->dn);
966       $this->ssh->save();
967     }
969     /* Take care about groupMembership values: add to groups */
970     foreach ($this->groupMembership as $key => $value){
971       if (!isset($this->savedGroupMembership[$key])){
972         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
973         $g->set_acl_base($key);
974         $g->by_object['group']->addUser($this->uid);
975         $g->save();
976       }
977     }
979     /* Remove groups not listed in groupMembership */
980     foreach ($this->savedGroupMembership as $key => $value){
981       if (!isset($this->groupMembership[$key])){
982         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
983         $g->set_acl_base($key);
984         $g->by_object['group']->removeUser ($this->uid);
985         $g->save();
986       }
987     }
989     /* Optionally execute a command after we're done */
990     if ($this->initially_was_account == $this->is_account){
991       if ($this->is_modified){
992         $this->handle_post_events("modify",array("uid" => $this->uid));
993       }
994     } else {
995       $this->handle_post_events("add" ,array("uid"=> $this->uid));
996     }
997   }
1000   /* Check formular input */
1001   function check()
1002   {
1003     /* Include global link_info */
1004     $ldap= $this->config->get_ldap_link();
1006     /* Append groups as memberGroup: to check hook 
1007      */
1008     $tmp_attributes  = $this->attributes;    
1009     $this->attributes[] = "memberGroup";
1010     $this->memberGroup = array();
1011     foreach($this->groupMembership as $dn => $name){
1012       $this->memberGroup[] = $name;
1013     }
1015     /* Call common method to give check the hook */
1016     $message= plugin::check();
1017     $this->attributes = $tmp_attributes;
1019     /* must: homeDirectory */
1020     if ($this->homeDirectory == ""){
1021       $message[]= msgPool::required(_("Home directory"));
1022     }
1023     if (!tests::is_path($this->homeDirectory)){
1024       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1025     }
1027     /* Check ID's if they are forced by user */
1028     if ($this->force_ids == "1"){
1030       /* Valid uid/gid? */
1031       if (!tests::is_id($this->uidNumber)){
1032         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1033       } else {
1034         if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1035           $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1036         }
1037       }
1038       if (!tests::is_id($this->gidNumber)){
1039         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1040       } else {
1041         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1042           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1043         }
1044       }
1045     }
1047     /* Check dates */
1048     if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
1049       $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
1050     }
1052     /* Check shadow settings, well I like spaghetties... */
1053     if ($this->activate_shadowMin){
1054       if (!tests::is_id($this->shadowMin)){
1055         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1056       }
1057     }
1058     if ($this->activate_shadowMax){
1059       if (!tests::is_id($this->shadowMax)){
1060         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1061       }
1062     }
1063     if ($this->activate_shadowWarning){
1064       if (!tests::is_id($this->shadowWarning)){
1065         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1066       }
1067       if (!$this->activate_shadowMax){
1068         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1069       }
1070       if ($this->shadowWarning > $this->shadowMax){
1071         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1072       }
1073       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1074         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1075       }
1076     }
1077     if ($this->activate_shadowInactive){
1078       if (!tests::is_id($this->shadowInactive)){
1079         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1080       }
1081       if (!$this->activate_shadowMax){
1082         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1083       }
1084     }
1085     if ($this->activate_shadowMin && $this->activate_shadowMax){
1086       if ($this->shadowMin > $this->shadowMax){
1087         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1088       }
1089     }
1091     return ($message);
1092   }
1095   function multiple_check()
1096   {
1097     $message = plugin::multiple_check();
1098     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1099       $message[]= msgPool::required(_("Home directory"));
1100     }
1101     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1102       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1103     }
1105     /* Check shadow settings, well I like spaghetties... */
1106     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1107       if (!tests::is_id($this->shadowMin)){
1108         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1109       }
1110     }
1111     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1112       if (!tests::is_id($this->shadowMax)){
1113         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1114       }
1115     }
1116     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1117       if (!tests::is_id($this->shadowWarning)){
1118         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1119       }
1120       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1121         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1122       }
1123       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1124         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1125       }
1126       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1127         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1128       }
1129     }
1130     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1131       if (!tests::is_id($this->shadowInactive)){
1132         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1133       }
1134       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1135         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1136       }
1137     }
1138     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1139       if ($this->shadowMin > $this->shadowMax){
1140         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1141       }
1142     }
1144     return($message);
1145   }
1148   function addGroup ($groups)
1149   {
1150     /* include global link_info */
1151     $ldap= $this->config->get_ldap_link();
1153     /* Walk through groups and add the descriptive entry if not exists */
1154     foreach ($groups as $value){
1156       if (!array_key_exists($value, $this->groupMembership)){
1157         $ldap->cat($value, array('cn', 'description', 'dn'));
1158         $attrs= $ldap->fetch();
1159         error_reporting (0);
1160         if (!isset($attrs['description'][0])){
1161           $entry= $attrs["cn"][0];
1162         } else {
1163           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1164           $entry= $attrs["cn"][0]." [$dsc]";
1165         }
1166         error_reporting (E_ALL | E_STRICT);
1168         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1169           $this->groupMembership[$attrs['dn']]= $entry;
1170           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1171             unset($this->groupMembership_some[$attrs['dn']]);
1172           }
1173         }
1174       }
1175     }
1177     /* Sort groups */
1178     asort ($this->groupMembership);
1179     reset ($this->groupMembership);
1180   }
1183   /* Del posix user from some groups */
1184   function delGroup ($groups)
1185   {
1186     $dest= array();
1187     foreach($groups as $dn_to_del){
1188       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1189         unset($this->groupMembership[$dn_to_del]);
1190       }
1191       if($this->multiple_support_active){
1192         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1193           unset($this->groupMembership_some[$dn_to_del]);
1194         }
1195       }
1196     }
1197   }
1200   /* Adapt from template, using 'dn' */
1201   function adapt_from_template($dn, $skip= array())
1202   {
1203     /* Include global link_info */
1204     $ldap= $this->config->get_ldap_link();
1206     plugin::adapt_from_template($dn, $skip);
1207     $template= $this->attrs['uid'][0];
1209     /* Adapt group membership */
1210     $ldap->cd($this->config->current['BASE']);
1211     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1213     while ($this->attrs= $ldap->fetch()){
1214       if (!isset($this->attrs["description"][0])){
1215         $entry= $this->attrs["cn"][0];
1216       } else {
1217         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1218       }
1219       $this->groupMembership[$ldap->getDN()]= $entry;
1220     }
1222     /* Fix primary group settings */
1223     $ldap->cd($this->config->current['BASE']);
1224     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1225     if ($ldap->count() != 1){
1226       $this->primaryGroup= $this->gidNumber;
1227     }
1229     $ldap->cd($this->config->current['BASE']);
1230     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1231     while($attr = $ldap->fetch()){
1232       $tmp = $attr['accessTo'];
1233       unset ($tmp['count']);
1234       $this->accessTo = $tmp;   
1235     }
1237     /* Adjust shadow checkboxes */
1238     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1239       if ($this->$val != 0){
1240         $oval= "activate_".$val;
1241         $this->$oval= "1";
1242       }
1243     }
1245     /* FIXME: NEED review of this section */
1246     /* Need to check shadowExpire separately */
1248     /* 
1249      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1250      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1251      */
1252     if(abs($this->shadowExpire - time())>86400) {
1253       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1254     }
1255     
1256     /* Only enable checkbox, if shadowExpire is in the future */
1257     if($this->shadowExpire > time()) {
1258       $this->activate_shadowExpire= "1";
1259     }
1260   }
1262   function convertToSeconds($val)
1263   {
1264     if ($val != 0){
1265       $val*= 60 * 60 * 24;
1266     } else {
1267       $date= getdate();
1268       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1269     }
1270     return($val);
1271   }
1274   function get_used_uid_numbers()
1275   {
1276     $ids= array();
1277     $ldap= $this->config->get_ldap_link();
1279     $ldap->cd ($this->config->current['BASE']);
1280     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1282     /* Get list of ids */
1283     while ($attrs= $ldap->fetch()){
1284       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1285     }
1286     return($ids);
1287   }
1290   /* Get posts from copy & paste dialog */ 
1291   function saveCopyDialog()
1292   {
1293     if(isset($_POST['homeDirectory'])){
1294       $this->homeDirectory = $_POST['homeDirectory'];
1295       if (isset ($_POST['force_ids'])){
1296         $data= 1;
1297         $this->gidNumber = $_POST['gidNumber'];
1298         $this->uidNumber = $_POST['uidNumber'];
1299       } else {
1300         $data= 0;
1301       }
1302       if ($this->force_ids != $data){
1303         $this->is_modified= TRUE;
1304       }
1305       $this->force_ids= $data;
1306       $data= $_POST['primaryGroup'];
1307       if ($this->primaryGroup != $data){
1308         $this->is_modified= TRUE;
1309       }
1310       $this->primaryGroup= $_POST['primaryGroup'];
1311     }
1312   }
1313  
1315   /* Create the posix dialog part for copy & paste */
1316   function getCopyDialog()
1317   {
1318     /* Skip dialog creation if this is not a valid account*/
1319     if(!$this->is_account) return("");
1320     if ($this->force_ids == 1){
1321       $force_ids = "checked";
1322       if (session::get('js')){
1323         $forceMode = "";
1324       }
1325     } else {
1326       if (session::get('js')){
1327         if($this->acl != "#none#")
1328           $forceMode ="disabled";
1329       }
1330       $force_ids = "";
1331     }
1333     $sta = "";
1335     /* Open group add dialog */
1336     if(isset($_POST['edit_groupmembership'])){
1337       $this->groupSelect = new groupSelect($this->config,get_userinfo());
1338       $sta = "SubDialog";
1339     }
1341     /* If the group-add dialog is closed, call execute 
1342        to ensure that the membership is updatd */
1343     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1344       $this->execute();
1345       $this->groupSelect =NULL;
1346     }
1348     if($this->groupSelect){
1349       $str = $this->execute(true);
1350       $ret = array();
1351       $ret['string'] = $str;
1352       $ret['status'] = $sta;
1353       return($ret);
1354     }
1356     /* If a group member should be deleted, simply call execute */
1357     if(isset($_POST['delete_groupmembership'])){
1358       $this->execute();
1359     }
1361     /* Assigned informations to smarty */
1362     $smarty = get_smarty();
1363     $smarty->assign("homeDirectory",$this->homeDirectory);
1364     $smarty->assign("secondaryGroups",$this->secondaryGroups);
1365     $smarty->assign("primaryGroup",$this->primaryGroup);
1366  
1367     $smarty->assign("uidNumber",$this->uidNumber);
1368     $smarty->assign("gidNumber",$this->gidNumber);
1369     $smarty->assign("forceMode",$forceMode);
1370     $smarty->assign("force_ids",$force_ids);
1371     if (!count($this->groupMembership)){
1372       $smarty->assign("groupMembership", array("&nbsp;"));
1373     } else {
1374       $smarty->assign("groupMembership", $this->groupMembership);
1375     }
1377     /* Display wars message if there are more than 16 group members */
1378     if (count($this->groupMembership) > 16){
1379       $smarty->assign("groups", "too_many_for_nfs");
1380     } else {
1381       $smarty->assign("groups", "");
1382     }
1383     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1385     $ret = array();
1386     $ret['string'] = $str;
1387     $ret['status'] = $sta;
1388     return($ret);
1389   }
1392   function PrepareForCopyPaste($source)
1393   {
1394     plugin::PrepareForCopyPaste($source);
1396     /* Avoid using the same gid/uid number as source user 
1397         empty numbers to enforce new ones. */
1398     $this->savedUidNumber = "";
1399     $this->savedGidNumber = "";
1401     /* Get group membership */
1402     $ldap = $this->config->get_ldap_link();
1403     $ldap->cd($this->config->current['BASE']);
1404     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1406     while ($attrs= $ldap->fetch()){
1407       if (!isset($attrs["description"][0])){
1408         $entry= $attrs["cn"][0];
1409       } else {
1410         $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1411       }
1412       $this->groupMembership[$ldap->getDN()]= $entry;
1413     }
1414     asort($this->groupMembership);
1415     reset($this->groupMembership);
1417     /* Fill group */
1418     if(isset($source['gidNumber'][0])){
1419       $this->primaryGroup= $source['gidNumber'][0];
1420     }
1422   }
1425   function multiple_execute()
1426   {
1427     return($this->execute());
1428   }
1431   static function plInfo()
1432   {
1433     return (array(
1434           "plDescription"     => _("POSIX account"),
1435           "plSelfModify"      => TRUE,
1436           "plDepends"         => array("user"),
1437           "plPriority"        => 2,
1438           "plSection"         => array("personal" => _("My account")),
1439           "plCategory"        => array("users"),
1440           "plOptions"         => array(),
1442           "plProvidedAcls"  => array(
1444             "homeDirectory"       =>  _("Home directory"), 
1445             "loginShell"          =>  _("Shell"),
1446             "uidNumber"           =>  _("User ID"),
1447             "gidNumber"           =>  _("Group ID"),
1449             "mustchangepassword"=>  _("Force password change on login"),
1450             "shadowMin"           =>  _("Shadow min"),
1451             "shadowMax"           =>  _("Shadow max"),
1452             "shadowWarning"       =>  _("Shadow warning"),
1453             "shadowInactive"      =>  _("Shadow inactive"),
1454             "shadowExpire"        =>  _("Shadow expire"),
1455             "sshPublickey"        =>  _("Public SSH key"),
1456             "trustModel"          =>  _("System trust model")))
1457             );
1458   }
1461   /* Return selected values for multiple edit */
1462   function get_multi_edit_values()
1463   {
1464     $ret = plugin::get_multi_edit_values();
1465     $ret['groupMembership']     = $this->groupMembership;
1466     $ret['groupMembership_some']= $this->groupMembership_some;
1468     if(in_array("primaryGroup",$this->multi_boxes)){
1469       $ret['primaryGroup'] = $this->primaryGroup;
1470     }
1471     if(in_array("trustmode",$this->multi_boxes)){
1472       $ret['trustModel'] = $this->trustModel;
1473       $ret['accessTo'] = $this->accessTo;
1474     }
1475     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1476       $active = "activate_".$entry;
1477       if(in_array($active,$this->multi_boxes)){
1478         $ret[$entry] = $this->$entry;
1479         $ret[$active] = $this->$active;
1480       }
1481     }
1482     if(in_array("mustchangepassword",$this->multi_boxes)){
1483       $ret['mustchangepassword'] = $this->mustchangepassword;
1484     }
1485     return($ret);
1486   }
1489   /* Save posts for multiple edit 
1490    */
1491   function multiple_save_object()
1492   {
1493     if(isset($_POST['posix_mulitple_edit'])){
1494  
1495       /* Backup expire value */ 
1496       $expire_tmp = $this->shadowExpire;
1497   
1498       /* Update all values */
1499       plugin::multiple_save_object();
1501       /* Get selected checkboxes */
1502       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1503         if(isset($_POST["use_".$val])){
1504           $this->multi_boxes[] = $val;
1505         }
1506       }
1508       /* Update special values, checkboxes for posixShadow */
1509       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1510         if($this->acl_is_writeable($var)){
1511           $activate_var = "activate_".$var;
1512           if(in_array($activate_var, $this->multi_boxes)){
1513             if(isset($_POST['activate_'.$var])){
1514               $this->$activate_var  = true;
1515               $this->$var      = $_POST[$var];
1516             }else{
1517               $this->$activate_var  = false;
1518               $this->$var      = 0;
1519             }
1520           }
1521         }
1522       }
1524       /* Restore shadow value, if the shadow attribute isn't used */
1525       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1526         $this->shadowExpire = $expire_tmp;
1527       }
1529       /* Force change password ? */
1530       if(isset($_POST['mustchangepassword'])){
1531         $this->mustchangepassword = TRUE;
1532       }else{
1533         $this->mustchangepassword = FALSE;
1534       }
1536       /* Trust mode - special handling */
1537       if($this->acl_is_writeable("trustModel")){
1538         if (isset($_POST['trustmode'])){
1539           $saved= $this->trustModel;
1540           if ($_POST['trustmode'] == "1"){
1541             $this->trustModel= "fullaccess";
1542           } elseif ($_POST['trustmode'] == "2"){
1543             $this->trustModel= "byhost";
1544           } else {
1545             $this->trustModel= "";
1546           }
1547           if ($this->trustModel != $saved){
1548             $this->is_modified= TRUE;
1549           }
1550         }
1551       }
1553       /* Save primary group settings */
1554       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1555         $data= $_POST['primaryGroup'];
1556         if ($this->primaryGroup != $data){
1557           $this->is_modified= TRUE;
1558         }
1559         $this->primaryGroup= $_POST['primaryGroup'];
1560       }
1561     }
1562   }
1564   
1565   /* Initialize plugin with given atribute arrays 
1566    */
1567   function init_multiple_support($attrs,$all)
1568   {
1569     plugin::init_multiple_support($attrs,$all);
1571     /* Some dummy values */
1572     $groups_some = array();
1573     $groups_all  = array();
1574     $groups_uid  = array();
1575     $uids        = array();
1576     $first       = TRUE;
1578     /* Get all groups used by currently edited users */
1579     $uid_filter="";  
1580     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1581       $uid = $this->multi_attrs_all['uid'][$i];
1582       $uids[] = $uid;
1583       $uid_filter.= "(memberUid=".$uid.")"; 
1584     }
1585     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1586     $ldap = $this->config->get_ldap_link();
1587     $ldap->cd($this->config->current['BASE']);
1588     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1589     while($group = $ldap->fetch()){
1590       $groups_some[$group['dn']] = $group['cn'][0];
1591       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1592         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1593       }
1594     }
1596     /* Create an array, containing all used groups */
1597     $groups_all = $groups_some;
1598     foreach($groups_all as $id => $group){
1599       foreach($uids as $uid){
1600         if(!in_array($uid,$groups_uid[$id])){
1601           unset($groups_all[$id]);
1602           break;
1603         }
1604       }
1605     }
1607     /* Assign group array */
1608     $this->groupMembership = $groups_all;
1610     /* Create an array of all grouops used by all users */
1611     foreach( $groups_all as $dn => $cn){
1612       if(isset($groups_some[$dn])){
1613         unset($groups_some[$dn]);
1614       }
1615     }
1616     $this->groupMembership_some = $groups_some;
1617     $this->primaryGroup = $this->gidNumber;
1619     /* Is this account a trustAccount? */
1620     if (isset($this->multi_attrs['trustModel'])){
1621       $this->trustModel= $this->multi_attrs['trustModel'][0];
1622       $this->was_trust_account= TRUE;
1623       $this->multi_boxes[] = "trustmode";
1624     } else {
1625       $this->was_trust_account= FALSE;
1626       $this->trustModel= "";
1627     }
1629     /* Create access informations */
1630     $this->accessTo = array();
1631     if (isset($this->multi_attrs['accessTo'])){
1632       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1633         $tmp= $this->multi_attrs['accessTo'][$i];
1634         $this->accessTo[$tmp]= $tmp;
1635       }
1636     }
1638     /* Adjust shadow checkboxes */
1639     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1640           "shadowExpire") as $val){
1641       if ($this->$val != 0){
1642         $oval= "activate_".$val;
1643         $this->$oval= "1";
1644       }
1645     }
1647     /* Convert to seconds */
1648     if(isset($this->multi_attrs['shadowExpire'])){
1649       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1650     }else{
1651       $this->activate_shadowExpire = FALSE;
1652     }
1653   }
1656   function set_multi_edit_values($attrs)
1657   {
1658     $groups = array();
1660     /* Update groupMembership, keep optinal group */
1661     foreach($attrs['groupMembership_some'] as $dn => $cn){
1662       if(isset($this->groupMembership[$dn])){
1663         $groups[$dn] = $cn;
1664       }
1665     }
1666     /* Update groupMembership, add forced groups */
1667     foreach($attrs['groupMembership'] as $dn => $cn){
1668       $groups[$dn] = $cn;
1669     }
1670     plugin::set_multi_edit_values($attrs);
1671     $this->groupMembership = $groups;
1672   }
1675 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1676 ?>