Code

Updated posix Account
[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 $accessTo= array();
54   var $glist=array();
55   var $status= "";
56   var $loginShellList= array();
57   var $groupMembership= array();
58   var $savedGroupMembership= array();
59   var $savedUidNumber= "";
60   var $savedGidNumber= "";
61   var $activate_shadowMin= "0";
62   var $activate_shadowMax= "0";
63   var $activate_shadowWarning= "0";
64   var $activate_shadowInactive= "0";
65   var $activate_shadowExpire= "0";
66   var $mustchangepassword= "0";
67   var $force_ids= 0;
68   var $gotoLastSystemLogin= "";
69   var $groupSelect= FALSE;
70   var $secondaryGroups= array();
71   var $primaryGroup= 0;
72   var $memberGroup= array();
73   var $grouplist= array();
74   var $ui= array();
75   var $ssh= null;
76   var $sshAcl= "";
78   var $GroupRegex= "*";
79   var $GroupUserRegex= "*";
80   var $SubSearch= false;
82   var $view_logged= false;
84   /* attribute list for save action */
85   var $CopyPasteVars  = 
86     array("grouplist","groupMembership","activate_shadowMin",
87         "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
88         "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
90   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
91       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
92       "shadowExpire", "uid", "gotoLastSystemLogin");
94   var $objectclasses= array("posixAccount", "shadowAccount");
96   var $uid= "";
97   var $multiple_support = TRUE;
98   var $groupMembership_some = array();
100   /* constructor, if 'dn' is set, the node loads the given
101      'dn' from LDAP */
102   function posixAccount (&$config, $dn= NULL, $parent =NULL)
103   {
104     global $class_mapping;
106     /* Configuration is fine, allways */
107     $this->config= $config;
109     /* Load bases attributes */
110     plugin::plugin($config, $dn, $parent);
112     $this->trustModeDialog = new trustModeDialog($this->config, $this->dn, $parent, 'users/posixAccount');
113     $this->trustModeDialog->setAcl('users/posixAccount');
115     /*  If gotoLastSystemLogin is available read it from ldap and create a readable
116         date time string, fallback to sambaLogonTime if available.
117      */
118     if(isset($this->attrs['gotoLastSystemLogin'][0]) && preg_match("/^[0-9]*$/",$this->attrs['gotoLastSystemLogin'][0])){
119       $this->gotoLastSystemLogin = date("d.m.Y H:i:s", strtotime($this->attrs['gotoLastSystemLogin'][0]));
120     } else if(isset($this->attrs['sambaLogonTime'][0]) && preg_match("/^[0-9]*$/",$this->attrs['sambaLogonTime'][0])){
121       $this->gotoLastSystemLogin = date("d.m.Y H:i:s", $this->attrs['sambaLogonTime'][0]);
122     }
124     /* Setting uid to default */
125     if(isset($this->attrs['uid'][0])){
126       $this->uid = $this->attrs['uid'][0];
127     }
129     $ldap= $this->config->get_ldap_link();
131     if ($dn !== NULL){
133       /* Correct is_account. shadowAccount is not required. */
134       if (isset($this->attrs['objectClass']) &&
135           in_array ('posixAccount', $this->attrs['objectClass'])){
137         $this->is_account= TRUE;
138       }
139       $this->initially_was_account= $this->is_account;
141       /* Fill group */
142       $this->primaryGroup= $this->gidNumber;
144       /* Generate status text */
145       $current= date("U");
147       $current= floor($current / 60 /60 / 24);
149       if (($current >= $this->shadowExpire) && $this->shadowExpire){
150         $this->status= _("expired");
151         if (($current - $this->shadowExpire) < $this->shadowInactive){
152           $this->status.= ", "._("grace time active");
153         }
154       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
155         $this->status= _("active").", "._("password not changeable");
156       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
157         $this->status= _("active").", "._("password expired");
158       } else {
159         $this->status= _("active");
160       }
162       /* Get group membership */
163       $ldap->cd($this->config->current['BASE']);
164       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
166       while ($attrs= $ldap->fetch()){
167         if (!isset($attrs["description"][0])){
168           $entry= $attrs["cn"][0];
169         } else {
170           $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
171         }
172         $this->groupMembership[$ldap->getDN()]= $entry;
173       }
174       asort($this->groupMembership);
175       reset($this->groupMembership);
176       $this->savedGroupMembership= $this->groupMembership;
177       $this->savedUidNumber= $this->uidNumber;
178       $this->savedGidNumber= $this->gidNumber;
180       // Instanciate SSH object if available
181       if (isset($class_mapping["sshPublicKey"])){
182         if (empty($this->acl_base)){
183           $this->acl_base= $config->current['BASE'];
184         }
186         $this->sshAcl= $this->getacl("sshPublicKey");
187         $this->ssh= new sshPublicKey($this->config, $this->dn, $this->sshAcl);
188       }
189     }
191     /* Adjust shadow checkboxes */
192     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
193           "shadowExpire") as $val){
195       if ($this->$val != 0){
196         $oval= "activate_".$val;
197         $this->$oval= "1";
198       }
199     }
201     /* Convert shadowExpire for usage */
202     if ($this->shadowExpire == 0){
203       $this->shadowExpire= "";
204     } else {
205       $this->shadowExpire= date('d.m.Y', $this->shadowExpire * 60 * 60 * 24);
206     }
209     /* Generate shell list from CONFIG_DIR./shells */
210     if (file_exists(CONFIG_DIR.'/shells')){
211       $shells = file (CONFIG_DIR.'/shells');
212       foreach ($shells as $line){
213         if (!preg_match ("/^#/", $line)){
214           $this->loginShellList[]= trim($line);
215         }
216       }
217     } else {
218       if ($this->loginShell == ""){
219         $this->loginShellList[]= _("unconfigured");
220       }
221     }
223     /* Insert possibly missing loginShell */
224     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
225       $this->loginShellList[]= $this->loginShell;
226     }
228     /* Generate group list */
229     $this->ui = get_userinfo(); 
230     $this->secondaryGroups[]= "- "._("automatic")." -";
231     $ldap->cd($this->config->current['BASE']);
232     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
233     while($attrs = $ldap->fetch()){
234       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
235     }
236     asort ($this->secondaryGroups);
238     $this->ui = get_userinfo();
239   }
242   /* execute generates the html output for this node */
243   function execute($isCopyPaste = false)
244   {
245     /* Call parent execute */
246     plugin::execute();
247     $display= "";
249     /* Log view */
250     if($this->is_account && !$this->view_logged){
251       $this->view_logged = TRUE;
252       new log("view","users/".get_class($this),$this->dn);
253     }
255     if($this->multiple_support_active){
256       $this->is_account = TRUE;
257     }
259     if(!$isCopyPaste && ! $this->multiple_support_active){
261       /* Do we need to flip is_account state? */
262       if(isset($_POST['modify_state'])){
263         if($this->is_account && $this->acl_is_removeable()){
264           $this->is_account= FALSE;
265         }elseif(!$this->is_account && $this->acl_is_createable()){
266           $this->is_account= TRUE;
267         }
268       }
270       /* Do we represent a valid posixAccount? */
271       if (!$this->is_account && $this->parent === NULL ){
272         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
273           msgPool::noValidExtension(_("POSIX"))."</b>";
274         $display.= back_to_main();
275         return ($display);
276       }
279       /* Show tab dialog headers */
280       if ($this->parent !== NULL){
281         if ($this->is_account){
282           if (isset($this->parent->by_object['sambaAccount'])){
283             $obj= $this->parent->by_object['sambaAccount'];
284           }
285           if (isset($obj) && $obj->is_account == TRUE &&
286               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
287               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
289             /* Samba3 dependency on posix accounts are enabled
290                in the moment, because I need to rely on unique
291                uidNumbers. There'll be a better solution later
292                on. */
293             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
294           } else {
295             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
296           }
297         } else {
298           $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
299           return($display);
300         }
301       }
302     }
304     // Display dialog to allow selection of groups
305     if (isset($_POST['edit_groupmembership'])){
306       $this->groupSelect = new groupSelect($this->config,get_userinfo());
307       $this->dialog= TRUE;
308     }
310     // Cancel group dialog
311     if (isset($_POST['add_groups_cancel'])){
312       $this->groupSelect= NULL;
313       $this->dialog= FALSE;
314     }
316     // Add groups selected in groupSelect dialog to ours.
317     if (isset($_POST['add_groups_finish']) && $this->groupSelect){
318       $groups = $this->groupSelect->detectPostActions();
319       if(isset($groups['targets'])){
320         $this->addGroup ($groups['targets']);
321         $this->is_modified= TRUE;
322       }
323       $this->groupSelect= NULL;
324       $this->dialog= FALSE;
325     }
327     // Remove groups from currently selected groups.
328     if (isset($_POST['delete_groupmembership']) && 
329         isset($_POST['group_list']) && count($_POST['group_list'])){
331       $this->delGroup ($_POST['group_list']);
332     }
334     /* Templates now! */
335     $smarty= get_smarty();
336     $smarty->assign("usePrototype", "true");
339     // Handle trust mode dialog
340     $trustModeDialog = $this->trustModeDialog->execute();
341     if($this->trustModeDialog->trustSelect){
342         $this->dialog = TRUE;
343         return($trustModeDialog);
344         
345     }
346     $this->dialog = FALSE;
347     $smarty->assign("trustModeDialog" , $trustModeDialog);
350     /* Manage group add dialog */
351     if ($this->groupSelect){
353       // Build up blocklist
354       session::set('filterBlacklist', array('dn' => array_keys($this->groupMembership)));
355       return($this->groupSelect->execute());
356     }
358     // Handle ssh dialog?
359     if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
360       $smarty->assign("usePrototype", "false");
361       if ($result= $this->ssh->execute()) {
362         $this->dialog= true;
363         pathNavigator::registerPlugin("SSH keys");
364         return $result;
365       }
366       $this->dialog= false;
367     }
370     /* Show main page */
371     $smarty= get_smarty();
372     $smarty->assign("usePrototype", "true");
374     $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey"));
376     /* Depending on pwmode, currently hardcoded because there are no other methods */
377     if ( 1 == 1 ){
378       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
380       $shadowMinACL     =  $this->getacl("shadowMin");
381       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
382             "<input type='text' name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
384       $shadowMaxACL     =  $this->getacl("shadowMax");
385       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
386             "<input type='text' name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
388       $shadowInactiveACL=  $this->getacl("shadowInactive");
389       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"), 
390             "<input type='text' name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
392       $shadowWarningACL =  $this->getacl("shadowWarning");
393       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"), 
394             "<input type='text' name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
396       foreach( array("activate_shadowMin", "activate_shadowMax",
397             "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
398         if ($this->$val == 1){
399           $smarty->assign("$val", "checked");
400         } else {
401           $smarty->assign("$val", "");
402         }
403         $smarty->assign("$val"."ACL", $this->getacl(preg_replace("/^.*_/","",$val)));
404       }
406       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword"));
407     }
409     // Set last system login
410     $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
412     /* Fill arrays */
413     $smarty->assign("shells", $this->loginShellList);
414     $smarty->assign("secondaryGroups", $this->secondaryGroups);
415     $smarty->assign("primaryGroup", $this->primaryGroup);
416     if(!$this->multiple_support_active){
417       if (!count($this->groupMembership)){
418         $smarty->assign("groupMembership", array("&nbsp;"));
419       } else {
420         $smarty->assign("groupMembership", $this->groupMembership);
421       }
422     }else{
423       $smarty->assign("groupMembership", $this->groupMembership);
424       $smarty->assign("groupMembership_some", $this->groupMembership_some);
425     }
426     if (count($this->groupMembership) > 16){
427       $smarty->assign("groups", "too_many_for_nfs");
428     } else {
429       $smarty->assign("groups", "");
430     }
432     /* Avoid "Undefined index: forceMode" */
433     $smarty->assign("forceMode", "");
435     /* Checkboxes */
436     if ($this->force_ids == 1){
437       $smarty->assign("force_ids", "checked");
438       if (session::get('js')){
439         $smarty->assign("forceMode", "");
440       }
441     } else {
442       if (session::get('js')){
443         $smarty->assign("forceMode", "disabled");
444       }
445       $smarty->assign("force_ids", "");
446     }
448     /* Create onClick="" action string for the "Force UID/GID" option 
449      */
450     $onClickIDS ="";
451     if(preg_match("/w/",$this->getacl("uidNumber"))){
452       $onClickIDS .= "changeState('uidNumber');";
453     }
454     if(preg_match("/w/",$this->getacl("gidNumber"))){
455       $onClickIDS .= "changeState('gidNumber');";
456     }
457     $smarty->assign("onClickIDS", $onClickIDS);
458     $smarty->assign("force_idsACL", $this->getacl("uidNumber").$this->getacl("gidNumber"));
460     foreach(array("primaryGroup","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
461       if(in_array($val,$this->multi_boxes)){
462         $smarty->assign("use_".$val,TRUE);
463       }else{
464         $smarty->assign("use_".$val,FALSE);
465       }
466     }
469     /* Load attributes and acl's */
470     foreach($this->attributes as $val){
471       if(in_array($val,$this->multi_boxes)){
472         $smarty->assign("use_".$val,TRUE);
473       }else{
474         $smarty->assign("use_".$val,FALSE);
475       }
476       $smarty->assign("$val", $this->$val);
477     }
479     $tmp = $this->plInfo();                                                                                    
480     foreach($tmp['plProvidedAcls'] as $val => $desc){                                                          
481       $smarty->assign("$val"."ACL", $this->getacl($val));                                                      
482     }   
484     if($this->read_only){
485       $smarty->assign("groupMembershipACL","r");
486     }else{
487       $smarty->assign("groupMembershipACL","rw");
488     }
489     $smarty->assign("status", $this->status);
491     if($this->mustchangepassword){
492       $smarty->assign("mustchangepassword", " checked ");
493     } else {
494       $smarty->assign("mustchangepassword", "");
495     }
497     // Add SSH button if available
498     $smarty->assign("sshPublicKey", $this->ssh?1:0);
500     $smarty->assign("apply", apply_filter());
501     $smarty->assign("multiple_support" , $this->multiple_support_active);
502     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
503     return($display);
504   }
507   /* remove object from parent */
508   function remove_from_parent()
509   {
510     /* Cancel if there's nothing to do here */
511     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
512       return;
513     }
516     /* Remove and write to LDAP */
517     plugin::remove_from_parent();
519     /* Zero out array */
520     $this->attrs['gosaHostACL']= array();
522     /* Keep uid, because we need it for authentification! */
523     unset($this->attrs['uid']);
525     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
526         /* include global link_info */
527         $this->attributes, "Save");
528     $ldap= $this->config->get_ldap_link();
529     $ldap->cd($this->dn);
530     $this->cleanup();
531     $ldap->modify ($this->attrs); 
533     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
535     if (!$ldap->success()){
536       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
537     }
539     /* Delete group only if cn is uid and there are no other
540        members inside */
541     $ldap->cd ($this->config->current['BASE']);
542     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
543     if ($ldap->count() != 0){
544       $attrs= $ldap->fetch();
545       if ($attrs['cn'][0] == $this->uid &&
546           !isset($this->attrs['memberUid'])){
548         $ldap->rmDir($ldap->getDN());
549       }
550     }
552     /* Optionally execute a command after we're done */
553     $this->handle_post_events("remove",array("uid" => $this->uid));
554   }
557   function save_object()
558   {
559     if (isset($_POST['posixTab'])){
560       /* Save values to object */
561       plugin::save_object();
562       $this->trustModeDialog->save_object();
564       /* Save force GID checkbox */
565       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
566         if (isset ($_POST['force_ids'])){
567           $data= 1;
568         } else {
569           $data= 0;
570         }
571         if ($this->force_ids != $data){
572           $this->is_modified= TRUE;
573         }
574         $this->force_ids= $data;
575       }
577       /*Save primary group settings */
578       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
579         $data= $_POST['primaryGroup'];
580         if ($this->primaryGroup != $data){
581           $this->is_modified= TRUE;
582         }
583         $this->primaryGroup= $_POST['primaryGroup'];
584       }
586       /* Get seelcted shadow checkboxes */
587       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
588         if($this->acl_is_writeable($var)){
589           $activate_var = "activate_".$var;
590           if(isset($_POST['activate_'.$var])){
591             $this->$activate_var  = true;
592             $this->$var      = $_POST[$var];
593           }else{
594             $this->$activate_var  = false;
595             if ($var != "shadowExpire") {
596               $this->$var      = 0;
597             }
598           }
599         }
600       }
602       /* Force change password ? */
603       if(isset($_POST['mustchangepassword'])){
604         $this->mustchangepassword = TRUE;
605       }else{
606         $this->mustchangepassword = FALSE;
607       }
608     }
610     /* Get regex from alphabet */
611     if(isset($_GET['search'])){
612       $this->GroupRegex = $_GET['search']."*";
613     }
615     /* Check checkboxes and regexes */
616     if(isset($_POST["PosixGroupDialogPosted"])){
618       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
619         $this->SubSearch = true;
620       }else{
621         $this->SubSearch = false;
622       }
623       if(isset($_POST['guser'])){
624         $this->GroupUserRegex = $_POST['guser'];
625       }
626       if(isset($_POST['regex'])){
627         $this->GroupRegex = $_POST['regex'];
628       }
629     }
630     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
631     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
632   }
635   /* Save data to LDAP, depending on is_account we save or delete */
636   function save()
637   {
639     /* Adapt shadow values */
640     if (!$this->activate_shadowExpire){
641       $this->shadowExpire= "0";
642     } else {
643       /* Transform date to days since the beginning */
644       list($day, $month, $year)= explode('.', $this->shadowExpire, 3);
645       $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
646     }
647     if (!$this->activate_shadowMax){
648       $this->shadowMax= "0";
649     }
650     if ($this->mustchangepassword){
651       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
652     } else {
653       $this->shadowLastChange= (int)(date("U") / 86400);
654     }
655     if (!$this->activate_shadowWarning){
656       $this->shadowWarning= "0";
657     }
659     /* Check what to do with ID's 
660        Nothing forced, so we may have to generate our own IDs, if not done already.
661      */
662     if ($this->force_ids == 0){
664       /* Handle uidNumber. 
665        * - use existing number if possible
666        * - if not, try to create a new uniqe one.
667        * */
668       if ($this->savedUidNumber != ""){
669         $this->uidNumber= $this->savedUidNumber;
670       } else {
672         /* Calculate new id's. We need to place a lock before calling get_next_id
673            to get real unique values. 
674          */
675         $wait= 10;
676         while (get_lock("uidnumber") != ""){
677           sleep (1);
679           /* Oups - timed out */
680           if ($wait-- == 0){
681             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
682             break;
683           }
684         }
685         add_lock ("uidnumber", "gosa");
686         $this->uidNumber= get_next_id("uidNumber", $this->dn);
687       }
688     }
691     /* Handle gidNumber 
692      * - If we do not have a primary group selected (automatic), we will check if there 
693      *    is already a group  with the same name and use this as primary.
694      * - .. if we couldn't find a group with the same name, we will create a new one, 
695      *    using the users uid as cn and a generated uniqe gidNumber. 
696      * */
697     if ($this->primaryGroup == 0 || $this->force_ids){
699       /* Search for existing group */
700       $ldap = $this->config->get_ldap_link();
701       $ldap->cd($this->config->current['BASE']);
703       /* Are we forced to use a special gidNumber? */ 
704       if($this->force_ids){
705         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
706       }else{
707         $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
708       }
710       /* No primary group found, create a new one */
711       if ($ldap->count() == 0){
713         $groupcn = $this->uid;
714         $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
715         $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
716             'cn='.$groupcn.','.get_groups_ou(), $this->dn);
718         /* Request a new and uniqe gidNumber, if required */
719         if(!$this->force_ids){
720           $this->gidNumber= get_next_id("gidNumber", $this->dn);
721         }
723         /* If forced gidNumber could not be found, then check if the given group name already exists 
724            we do not want to modify the gidNumber of an existing group.
725          */
726         $cnt= 0; 
727         while($ldap->dn_exists($groupdn) && ($cnt < 100)){
728           $cnt ++;
729           $groupcn = $this->uid."_".$cnt;
730           $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
731               'cn='.$groupcn.','.get_groups_ou(), $this->dn);
732         }
734         /* Create new primary group and enforce the new gidNumber */
735         $g= new group($this->config, $groupdn);
736         $g->cn= $groupcn;
737         $g->force_gid= 1;
738         $g->gidNumber= $this->gidNumber;
739         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
740         $g->save ();
742         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
743             sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
744       }else{
745         $attrs = $ldap->fetch();
746         $this->gidNumber = $attrs['gidNumber'][0];
747         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
748             "Found and used: <i>".$attrs['dn']."</i>",
749             sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
750       }
751     }else{
753       /* Primary group was selected by user
754        */
755       $this->gidNumber = $this->primaryGroup;
756       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
757           sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
758     }
760     if ($this->activate_shadowMin != "1" ) {
761       $this->shadowMin = "";
762     }
764     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
765       $this->shadowMax = "";
766     }
768     if ($this->activate_shadowWarning != "1" ) {
769       $this->shadowWarning = "";
770     }
772     if ($this->activate_shadowInactive != "1" ) {
773       $this->shadowInactive = "";
774     }
776     if ($this->activate_shadowExpire != "1" ) {
777       $this->shadowExpire = "";
778     }
780     /* Fill gecos */
781     if (isset($this->parent) && $this->parent !== NULL){
782       $this->gecos= rewrite($this->parent->by_object['user']->cn);
783       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
784         $this->gecos= "";
785       }
786     }
788     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
789       $this->$attr = (int) $this->$attr;
790     }
791     /* Call parents save to prepare $this->attrs */
792     plugin::save();
793     $this->trustModeDialog->dn = $this->dn;
794     $this->trustModeDialog->save();
796     /* include global link_info */
797     $this->cleanup();
799     /* This is just a test, we have had duplicated ids 
800        in the past when copy & paste was used. 
801        Normaly this should not happen.
802      */ 
803     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
804       $used = $this->get_used_uid_numbers();
805       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
806         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);
807       }
808     }
810     $ldap= $this->config->get_ldap_link();
811     $ldap->cd($this->dn);
812     unset($this->attrs['uid']);
813     $ldap->modify ($this->attrs); 
815     /* Log last action */ 
816     if($this->initially_was_account){
817       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
818     }else{
819       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
820     }
822     if (!$ldap->success()){
823       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
824     }
826     /* Remove lock needed for unique id generation */
827     del_lock ("uidnumber");
829     // Save ssh stuff if needed
830     if ($this->ssh) {
831       $this->ssh->setDN($this->dn);
832       $this->ssh->save();
833     }
835     /* Take care about groupMembership values: add to groups */
836     foreach ($this->groupMembership as $key => $value){
837       if (!isset($this->savedGroupMembership[$key])){
838         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
839         $g->set_acl_base($key);
840         $g->by_object['group']->addUser($this->uid);
841         $g->save();
842       }
843     }
845     /* Remove groups not listed in groupMembership */
846     foreach ($this->savedGroupMembership as $key => $value){
847       if (!isset($this->groupMembership[$key])){
848         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
849         $g->set_acl_base($key);
850         $g->by_object['group']->removeUser ($this->uid);
851         $g->save();
852       }
853     }
855     /* Optionally execute a command after we're done */
856     if ($this->initially_was_account == $this->is_account){
857       if ($this->is_modified){
858         $this->handle_post_events("modify",array("uid" => $this->uid));
859       }
860     } else {
861       $this->handle_post_events("add" ,array("uid"=> $this->uid));
862     }
863   }
866   /* Check formular input */
867   function check()
868   {
869     /* Include global link_info */
870     $ldap= $this->config->get_ldap_link();
872     /* Append groups as memberGroup: to check hook 
873      */
874     $tmp_attributes  = $this->attributes;    
875     $this->attributes[] = "memberGroup";
876     $this->memberGroup = array();
877     foreach($this->groupMembership as $dn => $name){
878       $this->memberGroup[] = $name;
879     }
881     /* Call common method to give check the hook */
882     $message= plugin::check();
883     $this->attributes = $tmp_attributes;
885     /* must: homeDirectory */
886     if ($this->homeDirectory == ""){
887       $message[]= msgPool::required(_("Home directory"));
888     }
889     if (!tests::is_path($this->homeDirectory)){
890       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
891     }
893     /* Check ID's if they are forced by user */
894     if ($this->force_ids == "1"){
896       /* Valid uid/gid? */
897       if (!tests::is_id($this->uidNumber)){
898         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
899       } else {
900         if ($this->uidNumber < $this->config->get_cfg_value("minId")){
901           $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
902         }
903       }
904       if (!tests::is_id($this->gidNumber)){
905         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
906       } else {
907         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
908           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
909         }
910       }
911     }
913     /* Check dates */
914     if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
915       $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
916     }
918     /* Check shadow settings, well I like spaghetties... */
919     if ($this->activate_shadowMin){
920       if (!tests::is_id($this->shadowMin)){
921         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
922       }
923     }
924     if ($this->activate_shadowMax){
925       if (!tests::is_id($this->shadowMax)){
926         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
927       }
928     }
929     if ($this->activate_shadowWarning){
930       if (!tests::is_id($this->shadowWarning)){
931         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
932       }
933       if (!$this->activate_shadowMax){
934         $message[]= msgPool::depends("shadowWarning", "shadowMax");
935       }
936       if ($this->shadowWarning > $this->shadowMax){
937         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
938       }
939       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
940         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
941       }
942     }
943     if ($this->activate_shadowInactive){
944       if (!tests::is_id($this->shadowInactive)){
945         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
946       }
947       if (!$this->activate_shadowMax){
948         $message[]= msgPool::depends("shadowInactive", "shadowMax");
949       }
950     }
951     if ($this->activate_shadowMin && $this->activate_shadowMax){
952       if ($this->shadowMin > $this->shadowMax){
953         $message[]= msgPool::toobig("shadowMin", "shadowMax");
954       }
955     }
957     return ($message);
958   }
961   function multiple_check()
962   {
963     $message = plugin::multiple_check();
964     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
965       $message[]= msgPool::required(_("Home directory"));
966     }
967     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
968       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
969     }
971     /* Check shadow settings, well I like spaghetties... */
972     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
973       if (!tests::is_id($this->shadowMin)){
974         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
975       }
976     }
977     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
978       if (!tests::is_id($this->shadowMax)){
979         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
980       }
981     }
982     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
983       if (!tests::is_id($this->shadowWarning)){
984         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
985       }
986       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
987         $message[]= msgPool::depends("shadowWarning", "shadowMax");
988       }
989       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
990         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
991       }
992       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
993         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
994       }
995     }
996     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
997       if (!tests::is_id($this->shadowInactive)){
998         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
999       }
1000       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1001         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1002       }
1003     }
1004     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1005       if ($this->shadowMin > $this->shadowMax){
1006         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1007       }
1008     }
1010     return($message);
1011   }
1014   function addGroup ($groups)
1015   {
1016     /* include global link_info */
1017     $ldap= $this->config->get_ldap_link();
1019     /* Walk through groups and add the descriptive entry if not exists */
1020     foreach ($groups as $value){
1022       if (!array_key_exists($value, $this->groupMembership)){
1023         $ldap->cat($value, array('cn', 'description', 'dn'));
1024         $attrs= $ldap->fetch();
1025         error_reporting (0);
1026         if (!isset($attrs['description'][0])){
1027           $entry= $attrs["cn"][0];
1028         } else {
1029           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1030           $entry= $attrs["cn"][0]." [$dsc]";
1031         }
1032         error_reporting (E_ALL | E_STRICT);
1034         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1035           $this->groupMembership[$attrs['dn']]= $entry;
1036           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1037             unset($this->groupMembership_some[$attrs['dn']]);
1038           }
1039         }
1040       }
1041     }
1043     /* Sort groups */
1044     asort ($this->groupMembership);
1045     reset ($this->groupMembership);
1046   }
1049   /* Del posix user from some groups */
1050   function delGroup ($groups)
1051   {
1052     $dest= array();
1053     foreach($groups as $dn_to_del){
1054       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1055         unset($this->groupMembership[$dn_to_del]);
1056       }
1057       if($this->multiple_support_active){
1058         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1059           unset($this->groupMembership_some[$dn_to_del]);
1060         }
1061       }
1062     }
1063   }
1066   /* Adapt from template, using 'dn' */
1067   function adapt_from_template($dn, $skip= array())
1068   {
1069     /* Include global link_info */
1070     $ldap= $this->config->get_ldap_link();
1072     plugin::adapt_from_template($dn, $skip);
1073     $template= $this->attrs['uid'][0];
1075     /* Adapt group membership */
1076     $ldap->cd($this->config->current['BASE']);
1077     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1079     while ($this->attrs= $ldap->fetch()){
1080       if (!isset($this->attrs["description"][0])){
1081         $entry= $this->attrs["cn"][0];
1082       } else {
1083         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1084       }
1085       $this->groupMembership[$ldap->getDN()]= $entry;
1086     }
1088     /* Fix primary group settings */
1089     $ldap->cd($this->config->current['BASE']);
1090     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1091     if ($ldap->count() != 1){
1092       $this->primaryGroup= $this->gidNumber;
1093     }
1095     $ldap->cd($this->config->current['BASE']);
1096     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1097     while($attr = $ldap->fetch()){
1098       $tmp = $attr['accessTo'];
1099       unset ($tmp['count']);
1100       $this->accessTo = $tmp;   
1101     }
1103     /* Adjust shadow checkboxes */
1104     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1105       if ($this->$val != 0){
1106         $oval= "activate_".$val;
1107         $this->$oval= "1";
1108       }
1109     }
1111     /* FIXME: NEED review of this section */
1112     /* Need to check shadowExpire separately */
1114     /* 
1115      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1116      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1117      */
1118     if(abs($this->shadowExpire - time())>86400) {
1119       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1120     }
1122     /* Only enable checkbox, if shadowExpire is in the future */
1123     if($this->shadowExpire > time()) {
1124       $this->activate_shadowExpire= "1";
1125     }
1126   }
1128   function convertToSeconds($val)
1129   {
1130     if ($val != 0){
1131       $val*= 60 * 60 * 24;
1132     } else {
1133       $date= getdate();
1134       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1135     }
1136     return($val);
1137   }
1140   function get_used_uid_numbers()
1141   {
1142     $ids= array();
1143     $ldap= $this->config->get_ldap_link();
1145     $ldap->cd ($this->config->current['BASE']);
1146     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1148     /* Get list of ids */
1149     while ($attrs= $ldap->fetch()){
1150       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1151     }
1152     return($ids);
1153   }
1156   /* Get posts from copy & paste dialog */ 
1157   function saveCopyDialog()
1158   {
1159     if(isset($_POST['homeDirectory'])){
1160       $this->homeDirectory = $_POST['homeDirectory'];
1161       if (isset ($_POST['force_ids'])){
1162         $data= 1;
1163         $this->gidNumber = $_POST['gidNumber'];
1164         $this->uidNumber = $_POST['uidNumber'];
1165       } else {
1166         $data= 0;
1167       }
1168       if ($this->force_ids != $data){
1169         $this->is_modified= TRUE;
1170       }
1171       $this->force_ids= $data;
1172       $data= $_POST['primaryGroup'];
1173       if ($this->primaryGroup != $data){
1174         $this->is_modified= TRUE;
1175       }
1176       $this->primaryGroup= $_POST['primaryGroup'];
1177     }
1178   }
1181   /* Create the posix dialog part for copy & paste */
1182   function getCopyDialog()
1183   {
1184     /* Skip dialog creation if this is not a valid account*/
1185     if(!$this->is_account) return("");
1186     if ($this->force_ids == 1){
1187       $force_ids = "checked";
1188       if (session::get('js')){
1189         $forceMode = "";
1190       }
1191     } else {
1192       if (session::get('js')){
1193         if($this->acl != "#none#")
1194           $forceMode ="disabled";
1195       }
1196       $force_ids = "";
1197     }
1199     $sta = "";
1201     /* Open group add dialog */
1202     if(isset($_POST['edit_groupmembership'])){
1203       $this->groupSelect = new groupSelect($this->config,get_userinfo());
1204       $sta = "SubDialog";
1205     }
1207     /* If the group-add dialog is closed, call execute 
1208        to ensure that the membership is updatd */
1209     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1210       $this->execute();
1211       $this->groupSelect =NULL;
1212     }
1214     if($this->groupSelect){
1215       $str = $this->execute(true);
1216       $ret = array();
1217       $ret['string'] = $str;
1218       $ret['status'] = $sta;
1219       return($ret);
1220     }
1222     /* If a group member should be deleted, simply call execute */
1223     if(isset($_POST['delete_groupmembership'])){
1224       $this->execute();
1225     }
1227     /* Assigned informations to smarty */
1228     $smarty = get_smarty();
1229     $smarty->assign("homeDirectory",$this->homeDirectory);
1230     $smarty->assign("secondaryGroups",$this->secondaryGroups);
1231     $smarty->assign("primaryGroup",$this->primaryGroup);
1233     $smarty->assign("uidNumber",$this->uidNumber);
1234     $smarty->assign("gidNumber",$this->gidNumber);
1235     $smarty->assign("forceMode",$forceMode);
1236     $smarty->assign("force_ids",$force_ids);
1237     if (!count($this->groupMembership)){
1238       $smarty->assign("groupMembership", array("&nbsp;"));
1239     } else {
1240       $smarty->assign("groupMembership", $this->groupMembership);
1241     }
1243     /* Display wars message if there are more than 16 group members */
1244     if (count($this->groupMembership) > 16){
1245       $smarty->assign("groups", "too_many_for_nfs");
1246     } else {
1247       $smarty->assign("groups", "");
1248     }
1249     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1251     $ret = array();
1252     $ret['string'] = $str;
1253     $ret['status'] = $sta;
1254     return($ret);
1255   }
1258   function PrepareForCopyPaste($source)
1259   {
1260     plugin::PrepareForCopyPaste($source);
1262     /* Avoid using the same gid/uid number as source user 
1263        empty numbers to enforce new ones. */
1264     $this->savedUidNumber = "";
1265     $this->savedGidNumber = "";
1267     /* Get group membership */
1268     $ldap = $this->config->get_ldap_link();
1269     $ldap->cd($this->config->current['BASE']);
1270     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1272     while ($attrs= $ldap->fetch()){
1273       if (!isset($attrs["description"][0])){
1274         $entry= $attrs["cn"][0];
1275       } else {
1276         $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1277       }
1278       $this->groupMembership[$ldap->getDN()]= $entry;
1279     }
1280     asort($this->groupMembership);
1281     reset($this->groupMembership);
1283     /* Fill group */
1284     if(isset($source['gidNumber'][0])){
1285       $this->primaryGroup= $source['gidNumber'][0];
1286     }
1288   }
1291   function multiple_execute()
1292   {
1293     return($this->execute());
1294   }
1297   static function plInfo()
1298   {
1299     return (array(
1300           "plDescription"     => _("POSIX account"),
1301           "plSelfModify"      => TRUE,
1302           "plDepends"         => array("user"),
1303           "plPriority"        => 2,
1304           "plSection"         => array("personal" => _("My account")),
1305           "plCategory"        => array("users"),
1306           "plOptions"         => array(),
1308           "plProvidedAcls"  => array(
1310             "homeDirectory"       =>  _("Home directory"), 
1311             "primaryGroup"        =>  _("Primary group"), 
1312             "loginShell"          =>  _("Shell"),
1313             "uidNumber"           =>  _("User ID"),
1314             "gidNumber"           =>  _("Group ID"),
1316             "shadowLastChange"    =>  _("Shadow last changed"),
1317             "gotoLastSystemLogin"    =>  _("Last login"),
1319             "mustchangepassword"=>  _("Force password change on login"),
1320             "shadowMin"           =>  _("Shadow min"),
1321             "shadowMax"           =>  _("Shadow max"),
1322             "shadowWarning"       =>  _("Shadow warning"),
1323             "shadowInactive"      =>  _("Shadow inactive"),
1324             "shadowExpire"        =>  _("Shadow expire"),
1325             "sshPublicKey"        =>  _("Public SSH key"),
1326             "accessTo"            =>  _("System trust model")))
1327               );
1328   }
1331   /* Return selected values for multiple edit */
1332   function get_multi_edit_values()
1333   {
1334     $ret = plugin::get_multi_edit_values();
1335     $ret = array_merge($ret,$this->trustModeDialog->get_multi_edit_values());
1336     $ret['groupMembership']     = $this->groupMembership;
1337     $ret['groupMembership_some']= $this->groupMembership_some;
1339     if(in_array("primaryGroup",$this->multi_boxes)){
1340       $ret['primaryGroup'] = $this->primaryGroup;
1341     }
1342     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1343       $active = "activate_".$entry;
1344       if(in_array($active,$this->multi_boxes)){
1345         $ret[$entry] = $this->$entry;
1346         $ret[$active] = $this->$active;
1347       }
1348     }
1349     if(in_array("mustchangepassword",$this->multi_boxes)){
1350       $ret['mustchangepassword'] = $this->mustchangepassword;
1351     }
1352     return($ret);
1353   }
1356   /* Save posts for multiple edit 
1357    */
1358   function multiple_save_object()
1359   {
1360     if(isset($_POST['posix_mulitple_edit'])){
1362       /* Backup expire value */ 
1363       $expire_tmp = $this->shadowExpire;
1365       /* Update all values */
1366       plugin::multiple_save_object();
1368       $this->trustModeDialog->multiple_save_object();
1370       /* Get selected checkboxes */
1371       foreach(array("primaryGroup","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1372         if(isset($_POST["use_".$val])){
1373           $this->multi_boxes[] = $val;
1374         }
1375       }
1377       /* Update special values, checkboxes for posixShadow */
1378       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1379         if($this->acl_is_writeable($var)){
1380           $activate_var = "activate_".$var;
1381           if(in_array($activate_var, $this->multi_boxes)){
1382             if(isset($_POST['activate_'.$var])){
1383               $this->$activate_var  = true;
1384               $this->$var      = $_POST[$var];
1385             }else{
1386               $this->$activate_var  = false;
1387               $this->$var      = 0;
1388             }
1389           }
1390         }
1391       }
1393       /* Restore shadow value, if the shadow attribute isn't used */
1394       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1395         $this->shadowExpire = $expire_tmp;
1396       }
1398       /* Force change password ? */
1399       if(isset($_POST['mustchangepassword'])){
1400         $this->mustchangepassword = TRUE;
1401       }else{
1402         $this->mustchangepassword = FALSE;
1403       }
1405       /* Save primary group settings */
1406       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1407         $data= $_POST['primaryGroup'];
1408         if ($this->primaryGroup != $data){
1409           $this->is_modified= TRUE;
1410         }
1411         $this->primaryGroup= $_POST['primaryGroup'];
1412       }
1413     }
1414   }
1417   /* Initialize plugin with given atribute arrays 
1418    */
1419   function init_multiple_support($attrs,$all)
1420   {
1421     plugin::init_multiple_support($attrs,$all);
1422     $this->trustModeDialog->init_multiple_support($attrs,$all);
1424     /* Some dummy values */
1425     $groups_some = array();
1426     $groups_all  = array();
1427     $groups_uid  = array();
1428     $uids        = array();
1429     $first       = TRUE;
1431     /* Get all groups used by currently edited users */
1432     $uid_filter="";  
1433     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1434       $uid = $this->multi_attrs_all['uid'][$i];
1435       $uids[] = $uid;
1436       $uid_filter.= "(memberUid=".$uid.")"; 
1437     }
1438     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1439     $ldap = $this->config->get_ldap_link();
1440     $ldap->cd($this->config->current['BASE']);
1441     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1442     while($group = $ldap->fetch()){
1443       $groups_some[$group['dn']] = $group['cn'][0];
1444       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1445         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1446       }
1447     }
1449     /* Create an array, containing all used groups */
1450     $groups_all = $groups_some;
1451     foreach($groups_all as $id => $group){
1452       foreach($uids as $uid){
1453         if(!in_array($uid,$groups_uid[$id])){
1454           unset($groups_all[$id]);
1455           break;
1456         }
1457       }
1458     }
1460     /* Assign group array */
1461     $this->groupMembership = $groups_all;
1463     /* Create an array of all grouops used by all users */
1464     foreach( $groups_all as $dn => $cn){
1465       if(isset($groups_some[$dn])){
1466         unset($groups_some[$dn]);
1467       }
1468     }
1469     $this->groupMembership_some = $groups_some;
1470     $this->primaryGroup = $this->gidNumber;
1472     /* Adjust shadow checkboxes */
1473     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1474           "shadowExpire") as $val){
1475       if ($this->$val != 0){
1476         $oval= "activate_".$val;
1477         $this->$oval= "1";
1478       }
1479     }
1481     /* Convert to seconds */
1482     if(isset($this->multi_attrs['shadowExpire'])){
1483       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1484     }else{
1485       $this->activate_shadowExpire = FALSE;
1486     }
1487   }
1490   function set_multi_edit_values($attrs)
1491   {
1492     $groups = array();
1494     /* Update groupMembership, keep optinal group */
1495     foreach($attrs['groupMembership_some'] as $dn => $cn){
1496       if(isset($this->groupMembership[$dn])){
1497         $groups[$dn] = $cn;
1498       }
1499     }
1500     /* Update groupMembership, add forced groups */
1501     foreach($attrs['groupMembership'] as $dn => $cn){
1502       $groups[$dn] = $cn;
1503     }
1504     plugin::set_multi_edit_values($attrs);
1505     $this->trustModeDialog->set_multi_edit_values($attrs);
1506     $this->groupMembership = $groups;
1507   }
1509   function set_acl_base($base)
1510   {
1511     @DEBUG (DEBUG_ACL, __LINE__, __FUNCTION__, __FILE__,"<b>".$base."</b>","<b>ACL-Base:</b> ");
1512     $this->acl_base= $base;
1513     $this->trustModeDialog->set_acl_base($base);
1514   }
1516   /*! \brief  Enables multiple support for this plugin
1517    */
1518   function enable_multiple_support()
1519   {
1520     plugin::enable_multiple_support();
1521     $this->trustModeDialog->enable_multiple_support();
1522   }
1526 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1527 ?>