Code

Updated tag handling for several lists
[gosa.git] / plugins / personal / posix / class_posixAccount.inc
1 <?php
2 /*!
3   \brief   posixAccount plugin
4   \author  Cajus Pollmeier <pollmeier@gonicus.de>
5   \version 2.00
6   \date    24.07.2003
8   This class provides the functionality to read and write all attributes
9   relevant for posixAccounts and shadowAccounts from/to the LDAP. It
10   does syntax checking and displays the formulars required.
11  */
13 class posixAccount extends plugin
14 {
15   /* Definitions */
16   var $plHeadline= "UNIX";
17   var $plDescription= "This does something";
19   /* CLI vars */
20   var $cli_summary= "Manage users posix account";
21   var $cli_description= "Some longer text\nfor help";
22   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24   /* Plugin specific values */
25   var $homeDirectory= "";
26   var $loginShell= "/bin/bash";
27   var $uidNumber= "";
28   var $gidNumber= "";
29   var $gecos= "";
30   var $shadowMin= "0";
31   var $shadowMax= "0";
32   var $shadowWarning= "0";
33   var $shadowLastChange= "0";
34   var $shadowInactive= "0";
35   var $shadowExpire= "0";
36   var $gosaDefaultPrinter= "";
37   var $gosaDefaultLanguage= "";
38   var $accessTo= array();
39   var $trustModel= "";
41   var $glist=array();
42   var $status= "";
43   var $loginShellList= array();
44   var $groupMembership= array();
45   var $savedGroupMembership= array();
46   var $savedUidNumber= "";
47   var $savedGidNumber= "";
48   var $use_shadowMin= "0";
49   var $use_shadowMax= "0";
50   var $use_shadowWarning= "0";
51   var $use_shadowInactive= "0";
52   var $use_shadowExpire= "0";
53   var $must_change_password= "0";
54   var $force_ids= 0;
55   var $printerList= array();
56   var $group_dialog= FALSE;
57   var $show_ws_dialog= FALSE;
58   var $secondaryGroups= array();
59   var $primaryGroup= 0;
60   var $was_trust_account= FALSE;
62   var $grouplist  = array();
63   var $ui         = array();
65   var $GroupRegex       = "*";
66   var $GroupUserRegex   = "*";
67   var $SubSearch        = false;
69   /* attribute list for save action */
70   var $CopyPasteVars  = array("grouplist","groupMembership","use_shadowMin","use_shadowMax","use_shadowWarning","use_shadowInactive","use_shadowExpire","must_change_password","force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
71   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
72       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
73       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
74   var $objectclasses= array("posixAccount", "shadowAccount");
77   /* constructor, if 'dn' is set, the node loads the given
78      'dn' from LDAP */
79   function posixAccount ($config, $dn= NULL)
80   {
81     /* Configuration is fine, allways */
82     $this->config= $config;
84     /* Load bases attributes */
85     plugin::plugin($config, $dn);
87     $ldap= $this->config->get_ldap_link();
89     if ($dn != NULL){
91       /* Correct is_account. shadowAccount is not required. */
92       if (isset($this->attrs['objectClass']) &&
93           in_array ('posixAccount', $this->attrs['objectClass'])){
95         $this->is_account= TRUE;
96       }
98       /* Is this account a trustAccount? */
99       if ($this->is_account && isset($this->attrs['trustModel'])){
100         $this->trustModel= $this->attrs['trustModel'][0];
101         $this->was_trust_account= TRUE;
102       } else {
103         $this->was_trust_account= FALSE;
104         $this->trustModel= "";
105       }
106          
107           $this->accessTo = array(); 
108       if ($this->is_account && isset($this->attrs['accessTo'])){
109         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
110           $tmp= $this->attrs['accessTo'][$i];
111           $this->accessTo[$tmp]= $tmp;
112         }
113       }
114       $this->initially_was_account= $this->is_account;
116       /* Fill group */
117       $this->primaryGroup= $this->gidNumber;
119       /* Generate status text */
120       $current= date("U");
122       $current= floor($current / 60 /60 / 24);
124       if (($current >= $this->shadowExpire) && $this->shadowExpire){
125         $this->status= _("expired");
126         if (($current - $this->shadowExpire) < $this->shadowInactive){
127           $this->status.= ", "._("grace time active");
128         }
129       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
130         $this->status= _("active, password not changable");
131       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
132         $this->status= _("active, password expired");
133       } else {
134         $this->status= _("active");
135       }
137       /* Get group membership */
138       $ldap->cd($this->config->current['BASE']);
139       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
141       while ($this->attrs= $ldap->fetch()){
142         if (!isset($this->attrs["description"][0])){
143           $entry= $this->attrs["cn"][0];
144         } else {
145           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
146           $entry= $this->attrs["cn"][0]." [$dsc]";
147         }
148         $this->groupMembership[$ldap->getDN()]= $entry;
149       }
150       asort($this->groupMembership);
151       reset($this->groupMembership);
152       $this->savedGroupMembership= $this->groupMembership;
153       $this->savedUidNumber= $this->uidNumber;
154       $this->savedGidNumber= $this->gidNumber;
155     }
157     /* Adjust shadow checkboxes */
158     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
159           "shadowExpire") as $val){
161       if ($this->$val != 0){
162         $oval= "use_".$val;
163         $this->$oval= "1";
164       }
165     }
167     /* Convert to seconds */
168     if ($this->shadowExpire != 0){
169       $this->shadowExpire*= 60 * 60 * 24;
170     } else {
171       $date= getdate();
172       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
173     }
175     /* Generate shell list from /etc/gosa/shells */
176     if (file_exists('/etc/gosa/shells')){
177       $shells = file ('/etc/gosa/shells');
178       foreach ($shells as $line){
179         if (!preg_match ("/^#/", $line)){
180           $this->loginShellList[]= trim($line);
181         }
182       }
183     } else {
184       if ($this->loginShell == ""){
185         $this->loginShellList[]= _("unconfigured");
186       }
187     }
189     /* Insert possibly missing loginShell */
190     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
191       $this->loginShellList[]= $this->loginShell;
192     }
194     /* Generate printer list */
195     if (isset($this->config->data['SERVERS']['CUPS'])){
196       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
197       asort($this->printerList);
198     }
200     /* Set tag attribute if we've tagging activated */
201     $filter= "(objectClass=posixGroup)";
202     $ui= get_userinfo();
203     if ($ui->gosaUnitTag != "" && isset($config->current['STRICT_UNITS']) &&
204         preg_match('/TRUE/i', $config->current['STRICT_UNITS'])){
205       $filter= "(&(objectClass=posixGroup)(gosaUnitTag=".$ui->gosaUnitTag."))";
206     }
208     /* Generate group list */
209     $ldap->cd($this->config->current['BASE']);
210     $ldap->search("$filter", array("cn", "gidNumber"));
211     $this->secondaryGroups[]= "- "._("automatic")." -";
212     while ($attrs= $ldap->fetch()){
213       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
214     }
215     asort ($this->secondaryGroups);
217     /* Get global filter config */
218     if (!is_global("sysfilter")){
219       $ui= get_userinfo();
220       $base= get_base_from_people($ui->dn);
221       $sysfilter= array( "depselect"       => $base,
222           "regex"           => "*");
223       register_global("sysfilter", $sysfilter);
224     }
225     $this->ui = get_userinfo();
226   }
229   /* execute generates the html output for this node */
230   function execute($isCopyPaste = false)
231   {
232         /* Call parent execute */
233         plugin::execute();
234   $display= "";
236   /* Department has changed? */
237   if(isset($_POST['depselect'])){
238     $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
239   }
241   if(!$isCopyPaste){
242     /* Do we need to flip is_account state? */
243     if (isset($_POST['modify_state'])){
244       $this->is_account= !$this->is_account;
245     }
247     /* Do we represent a valid posixAccount? */
248     if (!$this->is_account && $this->parent == NULL ){
249       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
250         _("This account has no unix extensions.")."</b>";
251       $display.= back_to_main();
252       return ($display);
253     }
256     /* Show tab dialog headers */
257     if ($this->parent != NULL){
258       if ($this->is_account){
259         if (isset($this->parent->by_object['sambaAccount'])){
260           $obj= $this->parent->by_object['sambaAccount'];
261         }
262         if (isset($obj) && $obj->is_account == TRUE &&
263             ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
264             ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
266           /* Samba3 dependency on posix accounts are enabled
267              in the moment, because I need to rely on unique
268              uidNumbers. There'll be a better solution later
269              on. */
270           $display= $this->show_header(_("Remove posix account"),
271               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
272         } else {
273           $display= $this->show_header(_("Remove posix account"),
274               _("This account has posix features enabled. You can disable them by clicking below."));
275         }
276       } else {
277         $display= $this->show_header(_("Create posix account"),
278             _("This account has posix features disabled. You can enable them by clicking below."));
279         return($display);
280       }
281     }
282   }
283   /* Trigger group edit? */
284   if (isset($_POST['edit_groupmembership'])){
285     $this->group_dialog= TRUE;
286     $this->dialog= TRUE;
287   }
289   /* Cancel group edit? */
290   if (isset($_POST['add_groups_cancel']) ||
291       isset($_POST['add_groups_finish'])){
292     $this->group_dialog= FALSE;
293     $this->dialog= FALSE;
294   }
296   /* Add selected groups */
297   if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
298       count($_POST['groups'])){
300       if (chkacl ($this->acl, "memberUid") == ""){
301         $this->addGroup ($_POST['groups']);
302         $this->is_modified= TRUE;
303       }
304     }
306     /* Delete selected groups */
307     if (isset($_POST['delete_groupmembership']) && 
308         isset($_POST['group_list']) && count($_POST['group_list'])){
310       if (chkacl ($this->acl, "memberUid") == ""){
311         $this->delGroup ($_POST['group_list']);
312         $this->is_modified= TRUE;
313       }
314     }
316     /* Add user workstation? */
317     if (isset($_POST["add_ws"])){
318       $this->show_ws_dialog= TRUE;
319       $this->dialog= TRUE;
320     }
322     /* Add user workstation? */
323     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
324       foreach($_POST['wslist'] as $ws){
325         $this->accessTo[$ws]= $ws;
326       }
327       ksort($this->accessTo);
328       $this->is_modified= TRUE;
329     }
331     /* Remove user workstations? */
332     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
333       foreach($_POST['workstation_list'] as $name){
334         unset ($this->accessTo[$name]);
335       }
336       $this->is_modified= TRUE;
337     }
339     /* Add user workstation finished? */
340     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
341       $this->show_ws_dialog= FALSE;
342       $this->dialog= FALSE;
343     }
345     /* Templates now! */
346     $smarty= get_smarty();
348     /* Show ws dialog */
349     if ($this->show_ws_dialog){
350       /* Save data */
351       $sysfilter= get_global("sysfilter");
352       foreach( array("depselect", "regex") as $type){
353         if (isset($_POST[$type])){
354           $sysfilter[$type]= $_POST[$type];
355         }
356       }
357       if (isset($_GET['search'])){
358         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
359         if ($s == "**"){
360           $s= "*";
361         }
362         $sysfilter['regex']= $s;
363       }
364       register_global("sysfilter", $sysfilter);
366       /* Get workstation list */
367       $exclude= "";
368       foreach($this->accessTo as $ws){
369         $exclude.= "(cn=$ws)";
370       }
371       if ($exclude != ""){
372         $exclude= "(!(|$exclude))";
373       }
374       $acl= array($this->config->current['BASE'] => ":all");
375       $regex= $sysfilter['regex'];
376       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
377       $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
378       $wslist= array();
379       foreach ($res as $attrs){
380         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
381       }
382       asort($wslist);
383       $smarty->assign("search_image", get_template_path('images/search.png'));
384       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
385       $smarty->assign("tree_image", get_template_path('images/tree.png'));
386       $smarty->assign("deplist", $this->config->idepartments);
387       $smarty->assign("alphabet", generate_alphabet());
388       foreach( array("depselect", "regex") as $type){
389         $smarty->assign("$type", $sysfilter[$type]);
390       }
391       $smarty->assign("hint", print_sizelimit_warning());
392       $smarty->assign("wslist", $wslist);
393       $smarty->assign("apply", apply_filter());
394       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
395       return ($display);
396     }
398     /* Manage group add dialog */
399     if ($this->group_dialog){
401       /* Get global filter config */
402       $this->reload();
404       /* remove already assigned groups */
405       $glist= array();
406       foreach ($this->grouplist as $key => $value){
407         if (!isset($this->groupMembership[$key])){
408           $glist[$key]= $value;
409         }
410       }
412       if($this->SubSearch){
413         $smarty->assign("SubSearchCHK"," checked ");
414       }else{
415         $smarty->assign("SubSearchCHK","");
416       }
418       $smarty->assign("regex",$this->GroupRegex);
419       $smarty->assign("guser",$this->GroupUserRegex);
420       $smarty->assign("groups", $glist);
421       $smarty->assign("search_image", get_template_path('images/search.png'));
422       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
423       $smarty->assign("tree_image", get_template_path('images/tree.png'));
424       $smarty->assign("deplist", $this->config->idepartments);
425       $smarty->assign("alphabet", generate_alphabet());
426       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
427       $smarty->assign("hint", print_sizelimit_warning());
429       $smarty->assign("apply", apply_filter());
430       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
431       return ($display);
432     }
434     /* Show main page */
435     $smarty= get_smarty();
437     /* Depending on pwmode, currently hardcoded because there are no other methods */
438     if ( 1 == 1 ){
439       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
440       $shadowMinACL= chkacl($this->acl, "shadowMin");
441       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), "<input name=\"shadowMin\" size=3 maxlength=4 $shadowMinACL value=\"".$this->shadowMin."\">"));
442       $shadowMaxACL= chkacl($this->acl, "shadowMax");
443       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
444       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
445       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"), "<input name=\"shadowInactive\" size=3 maxlength=4 $shadowInactiveACL value=\"".$this->shadowInactive."\">"));
446       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
447       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
448       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
449             "use_shadowExpire", "use_shadowInactive",
450             "use_shadowWarning") as $val){
451         if ($this->$val == 1){
452           $smarty->assign("$val", "checked");
453         } else {
454           $smarty->assign("$val", "");
455         }
456         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
457       }
458     }
460     /* Fill calendar */
461     $date= getdate($this->shadowExpire);
463     $days= array();
464     for($d= 1; $d<32; $d++){
465       $days[$d]= $d;
466     }
467     $years= array();
468     for($y= $date['year']-10; $y<$date['year']+10; $y++){
469       $years[]= $y;
470     }
471     $months= array(_("January"), _("February"), _("March"), _("April"),
472         _("May"), _("June"), _("July"), _("August"), _("September"),
473         _("October"), _("November"), _("December"));
474     $smarty->assign("day", $date["mday"]);
475     $smarty->assign("days", $days);
476     $smarty->assign("months", $months);
477     $smarty->assign("month", $date["mon"]-1);
478     $smarty->assign("years", $years);
479     $smarty->assign("year", $date["year"]);
481     /* Fill arrays */
482     $smarty->assign("shells", $this->loginShellList);
483     $smarty->assign("secondaryGroups", $this->secondaryGroups);
484     $smarty->assign("primaryGroup", $this->primaryGroup);
485    if (!count($this->groupMembership)){
486       $smarty->assign("groupMembership", array("&nbsp;"));
487     } else {
488       $smarty->assign("groupMembership", $this->groupMembership);
489     }
490     if (count($this->groupMembership) > 16){
491       $smarty->assign("groups", "too_many_for_nfs");
492     } else {
493       $smarty->assign("groups", "");
494     }
495     $smarty->assign("printerList", $this->printerList);
496     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
498         /* Avoid "Undefined index: forceMode" */
499     $smarty->assign("forceMode", "");
501     /* Checkboxes */
502     if ($this->force_ids == 1){
503       $smarty->assign("force_ids", "checked");
504       if ($_SESSION['js']){
505         $smarty->assign("forceMode", "");
506       }
507     } else {
508       if ($_SESSION['js']){
509                 if($this->acl != "#none#")
510         $smarty->assign("forceMode", "disabled");
511       }
512       $smarty->assign("force_ids", "");
513     }
514     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
516     /* Load attributes and acl's */
517     foreach($this->attributes as $val){
518       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
519         {
520           $smarty->assign("$val"."ACL","");
521           $smarty->assign("$val", $this->$val);
522           continue;
523         }
524       $smarty->assign("$val", $this->$val);
525       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
526     }
527     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
528     $smarty->assign("status", $this->status);
530     /* Work on trust modes */
531     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
532     if ($this->trustModel == "fullaccess"){
533       $trustmode= 1;
534       // pervent double disable tag in html code, this will disturb our clean w3c html
535     
536     if(chkacl($this->acl, "trustmode")==""){
537           $smarty->assign("trusthide", "disabled");
538       }else{
539           $smarty->assign("trusthide", "");
540       }
542     } elseif ($this->trustModel == "byhost"){
543       $trustmode= 2;
544       $smarty->assign("trusthide", "");
545     } else {
546       // pervent double disable tag in html code, this will disturb our clean w3c html
547       if(chkacl($this->acl, "trustmode")==""){
548           $smarty->assign("trusthide", "disabled");
549       }else{
550           $smarty->assign("trusthide", "");
551       }
552       $trustmode= 0;
553     }
554     $smarty->assign("trustmode", $trustmode);
555     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
556           2 => _("allow access to these hosts")));
560     if((count($this->accessTo))==0)
561       $smarty->assign("emptyArrAccess",true);
562     else
563       $smarty->assign("emptyArrAccess",false);
564     
567     $smarty->assign("workstations", $this->accessTo);
569     $smarty->assign("apply", apply_filter());
570     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
571     return($display);
572   }
575   /* remove object from parent */
576   function remove_from_parent()
577   {
578     /* Cancel if there's nothing to do here */
579     if (!$this->initially_was_account){
580       return;
581     }
583     /* include global link_info */
584     $ldap= $this->config->get_ldap_link();
585     
586         /* Remove and write to LDAP */
587     plugin::remove_from_parent();
589     /* Zero out array */
590     $this->attrs['gosaHostACL']= array();
592     /* Keep uid, because we need it for authentification! */
593     unset($this->attrs['uid']);
594     unset($this->attrs['trustModel']);
596     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
597         $this->attributes, "Save");
598     $ldap->cd($this->dn);
599     $this->cleanup();
600     $ldap->modify ($this->attrs); 
602     show_ldap_error($ldap->get_error(), _("Removing UNIX account failed"));
604     /* Delete group only if cn is uid and there are no other
605        members inside */
606     $ldap->cd ($this->config->current['BASE']);
607     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
608     if ($ldap->count() != 0){
609       $attrs= $ldap->fetch();
610       if ($attrs['cn'][0] == $this->uid &&
611           !isset($this->attrs['memberUid'])){
613         $ldap->rmDir($ldap->getDN());
614       }
615     }
617     /* Optionally execute a command after we're done */
618     $this->handle_post_events("remove");
619   }
622   function save_object()
623   {
624     if (isset($_POST['posixTab'])){
625       /* Save values to object */
626       plugin::save_object();
628       /* Save force GID attribute */
629       if (chkacl ($this->acl, "force_ids") == ""){
630         if (isset ($_POST['force_ids'])){
631           $data= 1;
632         } else {
633           $data= 0;
634         }
635         if ($this->force_ids != $data){
636           $this->is_modified= TRUE;
637         }
638         $this->force_ids= $data;
640         $data= $_POST['primaryGroup'];
641         if ($this->primaryGroup != $data){
642           $this->is_modified= TRUE;
643         }
644         $this->primaryGroup= $_POST['primaryGroup'];
645       }
647       /* Save pwmode dependent attributes, curently hardcoded because there're
648          no alternatives */
649       if (1 == 1){
650         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
651               "use_shadowExpire", "use_shadowInactive",
652               "use_shadowWarning") as $val){
653           if (chkacl($this->acl, "$val") == ""){
654             if (isset ($_POST[$val])){
655               $data= 1;
656             } else {
657               $data= 0;
658             }
659             if ($data != $this->$val){
660               $this->is_modified= TRUE;
661             }
662             $this->$val= $data;
663           }
664         }
665       }
667       /* Trust mode - special handling */
668       if (isset($_POST['trustmode'])){
669         $saved= $this->trustModel;
670         if ($_POST['trustmode'] == "1"){
671           $this->trustModel= "fullaccess";
672         } elseif ($_POST['trustmode'] == "2"){
673           $this->trustModel= "byhost";
674         } else {
675           $this->trustModel= "";
676         }
677         if ($this->trustModel != $saved){
678           $this->is_modified= TRUE;
679         }
680       }
681     }
683     /* Get regex from alphabet */
684     if(isset($_GET['search'])){
685       $this->GroupRegex = $_GET['search']."*";
686     }
688     /* Check checkboxes and regexes */
689     if(isset($_POST["PosixGroupDialogPosted"])){
690       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
691         $this->SubSearch = true;
692       }else{
693         $this->SubSearch = false;
694       }
695     
696       if(isset($_POST['guser'])){
697         $this->GroupUserRegex = $_POST['guser'];
698       }
699       if(isset($_POST['regex'])){
700         $this->GroupRegex = $_POST['regex'];
701       }
702     }
703     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
704     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
705   }
708   /* Save data to LDAP, depending on is_account we save or delete */
709   function save()
710   {
711         
712     /* include global link_info */
713     $ldap= $this->config->get_ldap_link();
715     /* Adapt shadow values */
716     if (!$this->use_shadowExpire){
717       $this->shadowExpire= "0";
718     } else {
719       /* Transform seconds to days here */
720       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
721     }
722     if (!$this->use_shadowMax){
723       $this->shadowMax= "0";
724     }
725     if ($this->must_change_password){
726       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
727     } else {
728       $this->shadowLastChange= (int)(date("U") / 86400);
729     }
730     if (!$this->use_shadowWarning){
731       $this->shadowWarning= "0";
732     }
734     /* Check what to do with ID's */
735     if ($this->force_ids == 0){
737       /* Use id's that are already set */
738       if ($this->savedUidNumber != ""){
739         $this->uidNumber= $this->savedUidNumber;
740         $this->gidNumber= $this->savedGidNumber;
741       } else {
743         /* Calculate new id's. We need to place a lock before calling get_next_id
744            to get real unique values. */
745         $wait= 10;
746         while (get_lock("uidnumber") != ""){
747           sleep (1);
749           /* Oups - timed out */
750           if ($wait-- == 0){
751             print_red (_("Failed: overriding lock"));
752             break;
753           }
754         }
756         add_lock ("uidnumber", "gosa");
757         $this->uidNumber= $this->get_next_id("uidNumber");
758         if ($this->savedGidNumber != ""){
759           $this->gidNumber= $this->savedGidNumber;
760         } else {
761           $this->gidNumber= $this->get_next_id("gidNumber");
762         }
763       }
765       if ($this->primaryGroup != 0){
766         $this->gidNumber= $this->primaryGroup;
767       }
768     }
770     if ($this->use_shadowMin != "1" ) {
771       $this->shadowMin = "";
772     }
774     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
775       $this->shadowMax = "";
776     }
778     if ($this->use_shadowWarning != "1" ) {
779       $this->shadowWarning = "";
780     }
782     if ($this->use_shadowInactive != "1" ) {
783       $this->shadowInactive = "";
784     }
786     if ($this->use_shadowExpire != "1" ) {
787       $this->shadowExpire = "";
788     }
790     /* Fill gecos */
791     if (isset($this->parent) && $this->parent != NULL){
792       $this->gecos= rewrite($this->parent->by_object['user']->cn);
793       if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
794         $this->gecos= "";
795       }
796     }
798         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
799                 $this->$attr = (int) $this->$attr;
800         }
801     /* Call parents save to prepare $this->attrs */
802     plugin::save();
804     /* Trust accounts */
805     $objectclasses= array();
806     foreach ($this->attrs['objectClass'] as $key => $class){
807       if (preg_match('/trustAccount/i', $class)){
808         continue;
809       }
810       $objectclasses[]= $this->attrs['objectClass'][$key];
811     }
812     $this->attrs['objectClass']= $objectclasses;
813     if ($this->trustModel != ""){
814       $this->attrs['objectClass'][]= "trustAccount";
815       $this->attrs['trustModel']= $this->trustModel;
816       $this->attrs['accessTo']= array();
817       if ($this->trustModel == "byhost"){
818         foreach ($this->accessTo as $host){
819           $this->attrs['accessTo'][]= $host;
820         }
821       }
822     } else {
823       if ($this->was_trust_account){
824         $this->attrs['accessTo']= array();
825         $this->attrs['trustModel']= array();
826       }
827     }
829     if(empty($this->attrs['gosaDefaultPrinter'])){
830       $thid->attrs['gosaDefaultPrinter']=array();
831     }
834     /* Save data to LDAP */
835     $ldap->cd($this->dn);
836     $this->cleanup();
837     unset($this->attrs['uid']);
838     $ldap->modify ($this->attrs); 
840     show_ldap_error($ldap->get_error(), _("Saving UNIX account failed"));
842     /* Remove lock needed for unique id generation */
843     del_lock ("uidnumber");
846     /* Posix accounts have group interrelationship, take care about these here. */
847     if ($this->force_ids == 0 && $this->primaryGroup == 0){
848       $ldap->cd($this->config->current['BASE']);
849       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
851       /* Create group if it doesn't exist */
852       if ($ldap->count() == 0){
853         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
855         $g= new group($this->config, $groupdn);
856         $g->cn= $this->uid;
857         $g->force_gid= 1;
858         $g->gidNumber= $this->gidNumber;
859         $g->description= "Group of user ".$this->givenName." ".$this->sn;
860         $g->save ();
861       }
862     }
863  
864     /* Take care about groupMembership values: add to groups */
865     foreach ($this->groupMembership as $key => $value){
866       $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
867       $g->by_object['group']->addUser($this->uid);
868       $g->save();
869     }
870     
871     /* Remove from groups not listed in groupMembership */
872     foreach ($this->savedGroupMembership as $key => $value){
873       if (!isset($this->groupMembership[$key])){
874         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
875         $g->by_object['group']->removeUser ($this->uid);
876         $g->save();
877       }
878     }
880     /* Optionally execute a command after we're done */
881     if ($this->initially_was_account == $this->is_account){
882       if ($this->is_modified){
883         $this->handle_post_events("mofify");
884       }
885     } else {
886       $this->handle_post_events("add");
887     }
888   }
890   /* Check formular input */
891   function check()
892   {
893     /* Include global link_info */
894     $ldap= $this->config->get_ldap_link();
896     /* Call common method to give check the hook */
897     $message= plugin::check();
899     /* must: homeDirectory */
900     if ($this->homeDirectory == ""){
901       $message[]= _("The required field 'Home directory' is not set.");
902     }
903     if (!is_path($this->homeDirectory)){
904       $message[]= _("Please enter a valid path in 'Home directory' field.");
905     }
907     /* Check ID's if they are forced by user */
908     if ($this->force_ids == "1"){
910       /* Valid uid/gid? */
911       if (!is_id($this->uidNumber)){
912         $message[]= _("Value specified as 'UID' is not valid.");
913       } else {
914         if ($this->uidNumber < $this->config->current['MINID']){
915           $message[]= _("Value specified as 'UID' is too small.");
916         }
917       }
918       if (!is_id($this->gidNumber)){
919         $message[]= _("Value specified as 'GID' is not valid.");
920       } else {
921         if ($this->gidNumber < $this->config->current['MINID']){
922           $message[]= _("Value specified as 'GID' is too small.");
923         }
924       }
925     }
927     /* Check shadow settings, well I like spaghetties... */
928     if ($this->use_shadowMin){
929       if (!is_id($this->shadowMin)){
930         $message[]= _("Value specified as 'shadowMin' is not valid.");
931       }
932     }
933     if ($this->use_shadowMax){
934       if (!is_id($this->shadowMax)){
935         $message[]= _("Value specified as 'shadowMax' is not valid.");
936       }
937     }
938     if ($this->use_shadowWarning){
939       if (!is_id($this->shadowWarning)){
940         $message[]= _("Value specified as 'shadowWarning' is not valid.");
941       }
942       if (!$this->use_shadowMax){
943         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
944       }
945       if ($this->shadowWarning > $this->shadowMax){
946         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
947       }
948       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
949         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
950       }
951     }
952     if ($this->use_shadowInactive){
953       if (!is_id($this->shadowInactive)){
954         $message[]= _("Value specified as 'shadowInactive' is not valid.");
955       }
956       if (!$this->use_shadowMax){
957         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
958       }
959     }
960     if ($this->use_shadowMin && $this->use_shadowMax){
961       if ($this->shadowMin > $this->shadowMax){
962         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
963       }
964     }
966   //  if(empty($this->gosaDefaultPrinter)){
967   //    $message[]= _("You need to specify a valid default printer.");
968   //  }
970     return ($message);
971   }
973   function addGroup ($groups)
974   {
975     /* include global link_info */
976     $ldap= $this->config->get_ldap_link();
978     /* Walk through groups and add the descriptive entry if not exists */
979     foreach ($groups as $value){
980       if (!array_key_exists($value, $this->groupMembership)){
981         $ldap->cat($value, array('cn', 'description', 'dn'));
982         $attrs= $ldap->fetch();
983         error_reporting (0);
984         if (!isset($attrs['description'][0])){
985           $entry= $attrs["cn"][0];
986         } else {
987           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
988           $entry= $attrs["cn"][0]." [$dsc]";
989         }
990         error_reporting (E_ALL);
991         $this->groupMembership[$attrs['dn']]= $entry;
992       }
993     }
995     /* Sort groups */
996     asort ($this->groupMembership);
997     reset ($this->groupMembership);
998   }
1001   /* Del posix user from some groups */
1002   function delGroup ($groups)
1003   {
1004     $dest= array();
1006     foreach ($this->groupMembership as $key => $value){
1007       if (!in_array($key, $groups)){
1008         $dest[$key]= $value;
1009       }
1010     }
1011     $this->groupMembership= $dest;
1012   }
1014   /* Adapt from template, using 'dn' */
1015   function adapt_from_template($dn)
1016   {
1017     /* Include global link_info */
1018     $ldap= $this->config->get_ldap_link();
1020     plugin::adapt_from_template($dn);
1021     $template= $this->attrs['uid'][0];
1023     /* Adapt group membership */
1024     $ldap->cd($this->config->current['BASE']);
1025     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1027     while ($this->attrs= $ldap->fetch()){
1028       if (!isset($this->attrs["description"][0])){
1029         $entry= $this->attrs["cn"][0];
1030       } else {
1031         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1032       }
1033       $this->groupMembership[$ldap->getDN()]= $entry;
1034     }
1036     /* Fix primary group settings */
1037     $ldap->cd($this->config->current['BASE']);
1038     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1039     if ($ldap->count() != 1){
1040       $this->primaryGroup= $this->gidNumber;
1041     }
1043         $ldap->cd($this->config->current['BASE']);
1044     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1045         while($attr = $ldap->fetch()){
1046                 $tmp = $attr['accessTo'];
1047                 unset ($tmp['count']);
1048                 $this->accessTo = $tmp; 
1049         }
1050         
1051     /* Adjust shadow checkboxes */
1052     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1053           "shadowExpire") as $val){
1055       if ($this->$val != 0){
1056         $oval= "use_".$val;
1057         $this->$oval= "1";
1058       }
1059     }
1060   }
1062   function get_next_id($attrib)
1063   {
1064     $ids= array();
1065     $ldap= $this->config->get_ldap_link();
1067     $ldap->cd ($this->config->current['BASE']);
1068     if (preg_match('/gidNumber/i', $attrib)){
1069       $oc= "posixGroup";
1070     } else {
1071       $oc= "posixAccount";
1072     }
1073     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1075     /* Get list of ids */
1076     while ($attrs= $ldap->fetch()){
1077       $ids[]= (int)$attrs["$attrib"][0];
1078     }
1080     /* Find out next free id near to UID_BASE */
1081     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1082       if (!in_array($id, $ids)){
1083         return ($id);
1084       }
1085     }
1087     /* Should not happen */
1088     if ($id == 65000){
1089       print_red(_("Too many users, can't allocate a free ID!"));
1090       exit;
1091     }
1093   }
1095  function reload()
1096   {
1098     /* Set base for all searches */
1099     $base     = $_SESSION['CurrentMainBase'];
1100     $base     = $base;
1101     $ldap     = $this->config->get_ldap_link();    
1102     $attrs    =  array("cn", "description", "gidNumber");
1103     $Flags    = GL_SIZELIMIT;
1105     /* Get groups */
1106     if ($this->GroupUserRegex == '*'){
1107       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1108     } else {
1109       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1110     }
1111     if($this->SubSearch){
1112       $Flags |= GL_SUBSEARCH;
1113     }else{
1114       $base = get_groups_ou().$base;
1115     }
1118     $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1120     /* check sizelimit */
1121     if (preg_match("/size limit/i", $ldap->error)){
1122       $_SESSION['limit_exceeded']= TRUE;
1123     }
1125     /* Create a list of users */
1126     $this->grouplist = array();
1127     foreach ($res as $value){
1128         $this->grouplist[$value['gidNumber'][0]]= $value;
1129     }
1131     $tmp=array();
1132     foreach($this->grouplist as $tkey => $val ){
1133       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1134     }
1136     /* Sort index */
1137     ksort($tmp);
1139     /* Recreate index array[dn]=cn[description]*/
1140     $this->grouplist=array();
1141     foreach($tmp as $val){
1142       if(isset($val['description'])){
1143         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1144       }else{
1145         $this->grouplist[$val['dn']]=$val['cn'][0];
1146       }
1147     }
1148     reset ($this->grouplist);
1149   }
1152   /* Create the posix dialog part for copy & paste */
1153   function getCopyDialog()
1154   {
1155     /* Skip dialog creation if this is not a valid account*/
1156     if(!$this->is_account) return("");
1157     if ($this->force_ids == 1){
1158       $force_ids = "checked";
1159       if ($_SESSION['js']){
1160         $forceMode = "";
1161       }
1162     } else {
1163       if ($_SESSION['js']){
1164         if($this->acl != "#none#")
1165           $forceMode ="disabled";
1166       }
1167       $force_ids = "";
1168     }
1169    
1170     $sta = "";
1171  
1172     /* Open group add dialog */
1173     if(isset($_POST['edit_groupmembership'])){
1174       $this->group_dialog = TRUE;
1175       $sta = "SubDialog";
1176     }
1178     /* If the group-add dialog is closed, call execute 
1179         to ensure that the membership is updatd */
1180     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1181       $this->execute();
1182       $this->group_dialog =FALSE;
1183     }
1185     if($this->group_dialog){
1186       $str = $this->execute(true);
1187       $ret = array();
1188       $ret['string'] = $str;
1189       $ret['status'] = $sta;
1190       return($ret);
1191     }
1193     /* If a group member should be deleted, simply call execute */
1194     if(isset($_POST['delete_groupmembership'])){
1195       $this->execute();
1196     }
1198     /* Assigned informations to smarty */
1199     $smarty = get_smarty();
1200     $smarty->assign("homeDirectory",$this->homeDirectory);
1201     $smarty->assign("uidNumber",$this->uidNumber);
1202     $smarty->assign("gidNumber",$this->gidNumber);
1203     $smarty->assign("forceMode",$forceMode);
1204     $smarty->assign("force_ids",$force_ids);
1205     if (!count($this->groupMembership)){
1206       $smarty->assign("groupMembership", array("&nbsp;"));
1207     } else {
1208       $smarty->assign("groupMembership", $this->groupMembership);
1209     }
1211     /* Display wars message if there are more than 16 group members */
1212     if (count($this->groupMembership) > 16){
1213       $smarty->assign("groups", "too_many_for_nfs");
1214     } else {
1215       $smarty->assign("groups", "");
1216     }
1217     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1219     $ret = array();
1220     $ret['string'] = $str;
1221     $ret['status'] = $sta;
1222     return($ret);
1223   }
1227 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1228 ?>