Code

Fixed o
[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;
63   /* attribute list for save action */
64   var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
65       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
66       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
67   var $objectclasses= array("posixAccount", "shadowAccount");
70   /* constructor, if 'dn' is set, the node loads the given
71      'dn' from LDAP */
72   function posixAccount ($config, $dn= NULL)
73   {
74     /* Configuration is fine, allways */
75     $this->config= $config;
77     /* Load bases attributes */
78     plugin::plugin($config, $dn);
80     $ldap= $this->config->get_ldap_link();
82     if ($dn != NULL){
84       /* Correct is_account. shadowAccount is not required. */
85       if (isset($this->attrs['objectClass']) &&
86           in_array ('posixAccount', $this->attrs['objectClass'])){
88         $this->is_account= TRUE;
89       }
91       /* Is this account a trustAccount? */
92       if ($this->is_account && isset($this->attrs['trustModel'])){
93         $this->trustModel= $this->attrs['trustModel'][0];
94         $this->was_trust_account= TRUE;
95       } else {
96         $this->was_trust_account= FALSE;
97         $this->trustModel= "";
98       }
99          
100           $this->accessTo = array(); 
101       if ($this->is_account && isset($this->attrs['accessTo'])){
102         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
103           $tmp= $this->attrs['accessTo'][$i];
104           $this->accessTo[$tmp]= $tmp;
105         }
106       }
107       $this->initially_was_account= $this->is_account;
109       /* Fill group */
110       $this->primaryGroup= $this->gidNumber;
112       /* Generate status text */
113       $current= date("U");
114       if (($current >= $this->shadowExpire) && $this->shadowExpire){
115         $this->status= "expired";
116         if (($this->shadowExpire - $current) < $this->shadowInactive){
117           $this->status.= ", grace time active";
118         }
119       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
120         $this->status= "active, password not changable";
121       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
122         $this->status= "active, password expired";
123       } else {
124         $this->status= "active";
125       }
127       /* Get group membership */
128       $ldap->cd($this->config->current['BASE']);
129       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
131       while ($this->attrs= $ldap->fetch()){
132         if (!isset($this->attrs["description"][0])){
133           $entry= $this->attrs["cn"][0];
134         } else {
135           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
136           $entry= $this->attrs["cn"][0]." [$dsc]";
137         }
138         $this->groupMembership[$ldap->getDN()]= $entry;
139       }
140       asort($this->groupMembership);
141       reset($this->groupMembership);
142       $this->savedGroupMembership= $this->groupMembership;
143       $this->savedUidNumber= $this->uidNumber;
144       $this->savedGidNumber= $this->gidNumber;
145     }
147     /* Adjust shadow checkboxes */
148     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
149           "shadowExpire") as $val){
151       if ($this->$val != 0){
152         $oval= "use_".$val;
153         $this->$oval= "1";
154       }
155     }
157     /* Convert to seconds */
158     if ($this->shadowExpire != 0){
159       $this->shadowExpire*= 60 * 60 * 24;
160     } else {
161       $date= getdate();
162       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
163     }
165     /* Generate shell list from /etc/gosa/shells */
166     if (file_exists('/etc/gosa/shells')){
167       $shells = file ('/etc/gosa/shells');
168       foreach ($shells as $line){
169         if (!preg_match ("/^#/", $line)){
170           $this->loginShellList[]= trim($line);
171         }
172       }
173     } else {
174       if ($this->loginShell == ""){
175         $this->loginShellList[]= _("unconfigured");
176       }
177     }
179     /* Insert possibly missing loginShell */
180     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
181       $this->loginShellList[]= $this->loginShell;
182     }
184     /* Generate printer list */
185     if (isset($this->config->data['SERVERS']['CUPS'])){
186       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
187       asort($this->printerList);
188     }
190     /* Generate group list */
191     $ldap->cd($this->config->current['BASE']);
192     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
193     $this->secondaryGroups[]= "- "._("automatic")." -";
194     while ($attrs= $ldap->fetch()){
195       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
196     }
197     asort ($this->secondaryGroups);
199     /* Get global filter config */
200     if (!is_global("sysfilter")){
201       $ui= get_userinfo();
202       $base= get_base_from_people($ui->dn);
203       $sysfilter= array( "depselect"       => $base,
204           "regex"           => "*");
205       register_global("sysfilter", $sysfilter);
206     }
207   }
210   /* execute generates the html output for this node */
211   function execute()
212   {
213         /* Call parent execute */
214         plugin::execute();
216     /* Do we need to flip is_account state? */
217     if (isset($_POST['modify_state'])){
218       $this->is_account= !$this->is_account;
219     }
221     /* Do we represent a valid posixAccount? */
222     if (!$this->is_account && $this->parent == NULL ){
223       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
224         _("This account has no unix extensions.")."</b>";
225       $display.= back_to_main();
226       return ($display);
227     }
229     $display= "";
231     /* Show tab dialog headers */
232     if ($this->parent != NULL){
233       if ($this->is_account){
234         if (isset($this->parent->by_object['sambaAccount'])){
235           $obj= $this->parent->by_object['sambaAccount'];
236         }
237         if (isset($obj) && $obj->is_account == TRUE &&
238              ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
239                         ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
241           /* Samba3 dependency on posix accounts are enabled
242              in the moment, because I need to rely on unique
243              uidNumbers. There'll be a better solution later
244              on. */
245           $display= $this->show_header(_("Remove posix account"),
246               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
247         } else {
248           $display= $this->show_header(_("Remove posix account"),
249               _("This account has posix features enabled. You can disable them by clicking below."));
250         }
251       } else {
252         $display= $this->show_header(_("Create posix account"),
253             _("This account has posix features disabled. You can enable them by clicking below."));
254         return($display);
255       }
256     }
258     /* Trigger group edit? */
259     if (isset($_POST['edit_groupmembership'])){
260       $this->group_dialog= TRUE;
261       $this->dialog= TRUE;
262     }
264     /* Cancel group edit? */
265     if (isset($_POST['add_groups_cancel']) ||
266         isset($_POST['add_groups_finish'])){
267       $this->group_dialog= FALSE;
268       $this->dialog= FALSE;
269     }
271     /* Add selected groups */
272     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
273         count($_POST['groups'])){
275       if (chkacl ($this->acl, "memberUid") == ""){
276         $this->addGroup ($_POST['groups']);
277         $this->is_modified= TRUE;
278       }
279     }
281     /* Delete selected groups */
282     if (isset($_POST['delete_groupmembership']) && 
283         isset($_POST['group_list']) && count($_POST['group_list'])){
285       if (chkacl ($this->acl, "memberUid") == ""){
286         $this->delGroup ($_POST['group_list']);
287         $this->is_modified= TRUE;
288       }
289     }
291     /* Add user workstation? */
292     if (isset($_POST["add_ws"])){
293       $this->show_ws_dialog= TRUE;
294       $this->dialog= TRUE;
295     }
297     /* Add user workstation? */
298     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
299       foreach($_POST['wslist'] as $ws){
300         $this->accessTo[$ws]= $ws;
301       }
302       ksort($this->accessTo);
303       $this->is_modified= TRUE;
304     }
306     /* Remove user workstations? */
307     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
308       foreach($_POST['workstation_list'] as $name){
309         unset ($this->accessTo[$name]);
310       }
311       $this->is_modified= TRUE;
312     }
314     /* Add user workstation finished? */
315     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
316       $this->show_ws_dialog= FALSE;
317       $this->dialog= FALSE;
318     }
320     /* Templates now! */
321     $smarty= get_smarty();
323     /* Show ws dialog */
324     if ($this->show_ws_dialog){
325       /* Save data */
326       $sysfilter= get_global("sysfilter");
327       foreach( array("depselect", "regex") as $type){
328         if (isset($_POST[$type])){
329           $sysfilter[$type]= $_POST[$type];
330         }
331       }
332       if (isset($_GET['search'])){
333         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
334         if ($s == "**"){
335           $s= "*";
336         }
337         $sysfilter['regex']= $s;
338       }
339       register_global("sysfilter", $sysfilter);
341       /* Get workstation list */
342       $exclude= "";
343       foreach($this->accessTo as $ws){
344         $exclude.= "(cn=$ws)";
345       }
346       if ($exclude != ""){
347         $exclude= "(!(|$exclude))";
348       }
349       $acl= array($this->config->current['BASE'] => ":all");
350       $regex= $sysfilter['regex'];
351       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
352       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
353       $wslist= array();
354       foreach ($res as $attrs){
355         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
356       }
357       asort($wslist);
358       $smarty->assign("search_image", get_template_path('images/search.png'));
359       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
360       $smarty->assign("tree_image", get_template_path('images/tree.png'));
361       $smarty->assign("deplist", $this->config->idepartments);
362       $smarty->assign("alphabet", generate_alphabet());
363       foreach( array("depselect", "regex") as $type){
364         $smarty->assign("$type", $sysfilter[$type]);
365       }
366       $smarty->assign("hint", print_sizelimit_warning());
367       $smarty->assign("wslist", $wslist);
368       $smarty->assign("apply", apply_filter());
369       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
370       return ($display);
371     }
373     /* Manage group add dialog */
374     if ($this->group_dialog){
375       $gd= new groupManagement($this->config, get_userinfo());
377       /* Save data */
378       $groupfilter= get_global("groupfilter");
379       foreach( array("depselect", "guser", "regex") as $type){
380         if (isset($_POST[$type])){
381           $groupfilter[$type]= $_POST[$type];
382         }
383       }
384       if (isset($_POST['depselect'])){
385         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
386               "functionalgroups") as $type){
388           if (isset($_POST[$type])) {
389             $groupfilter[$type]= "checked";
390           } else {
391             $groupfilter[$type]= "";
392           }
393         }
394       }
395       if (isset($_GET['search'])){
396         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
397         if ($s == "**"){
398           $s= "*";
399         }
400         $groupfilter['regex']= $s;
401       }
402       register_global("groupfilter", $groupfilter);
404       /* Calculate actual groups */
405       $gd->reload(true);
406       $glist= array();
407       foreach ($gd->grouplist as $key => $value){
408         if (!isset($this->groupMembership[$key])){
409           $glist[$key]= $value;
410         }
411       }
413       $smarty->assign("groups", $glist);
414       $smarty->assign("search_image", get_template_path('images/search.png'));
415       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
416       $smarty->assign("tree_image", get_template_path('images/tree.png'));
417       $smarty->assign("deplist", $this->config->idepartments);
418       $smarty->assign("alphabet", generate_alphabet());
419       foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
420             "appgroups", "sambagroups", "functionalgroups") as $type){
421         $smarty->assign("$type", $groupfilter[$type]);
422       }
423       $smarty->assign("hint", print_sizelimit_warning());
425       $smarty->assign("apply", apply_filter());
426       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
427       return ($display);
428     }
430     /* Show main page */
431     $smarty= get_smarty();
433     /* Depending on pwmode, currently hardcoded because there are no other methods */
434     if ( 1 == 1 ){
435       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
436       $shadowMinACL= chkacl($this->acl, "shadowMin");
437       $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."\">"));
438       $shadowMaxACL= chkacl($this->acl, "shadowMax");
439       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
440       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
441       $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."\">"));
442       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
443       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
444       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
445             "use_shadowExpire", "use_shadowInactive",
446             "use_shadowWarning") as $val){
447         if ($this->$val == 1){
448           $smarty->assign("$val", "checked");
449         } else {
450           $smarty->assign("$val", "");
451         }
452         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
453       }
454     }
456     /* Fill calendar */
457     $date= getdate($this->shadowExpire);
459     $days= array();
460     for($d= 1; $d<32; $d++){
461       $days[$d]= $d;
462     }
463     $years= array();
464     for($y= $date['year']-10; $y<$date['year']+10; $y++){
465       $years[]= $y;
466     }
467     $months= array(_("January"), _("February"), _("March"), _("April"),
468         _("May"), _("June"), _("July"), _("August"), _("September"),
469         _("October"), _("November"), _("December"));
470     $smarty->assign("day", $date["mday"]);
471     $smarty->assign("days", $days);
472     $smarty->assign("months", $months);
473     $smarty->assign("month", $date["mon"]-1);
474     $smarty->assign("years", $years);
475     $smarty->assign("year", $date["year"]);
477     /* Fill arrays */
478     $smarty->assign("shells", $this->loginShellList);
479     $smarty->assign("secondaryGroups", $this->secondaryGroups);
480     $smarty->assign("primaryGroup", $this->primaryGroup);
481     if (!count($this->groupMembership)){
482       $smarty->assign("groupMembership", array("&nbsp;"));
483     } else {
484       $smarty->assign("groupMembership", $this->groupMembership);
485     }
486     if (count($this->groupMembership) > 16){
487       $smarty->assign("groups", "too_many_for_nfs");
488     } else {
489       $smarty->assign("groups", "");
490     }
491     $smarty->assign("printerList", $this->printerList);
492     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
494         /* Avoid "Undefined index: forceMode" */
495     $smarty->assign("forceMode", "");
497     /* Checkboxes */
498     if ($this->force_ids == 1){
499       $smarty->assign("force_ids", "checked");
500       if ($_SESSION['js']){
501         $smarty->assign("forceMode", "");
502       }
503     } else {
504       if ($_SESSION['js']){
505         $smarty->assign("forceMode", "disabled");
506       }
507       $smarty->assign("force_ids", "");
508     }
509     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
511     /* Load attributes and acl's */
512     foreach($this->attributes as $val){
513       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
514         {
515           $smarty->assign("$val"."ACL","");
516           $smarty->assign("$val", $this->$val);
517           continue;
518         }
519       $smarty->assign("$val", $this->$val);
520       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
521     }
522     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
523     $smarty->assign("status", $this->status);
525     /* Work on trust modes */
526     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
527     if ($this->trustModel == "fullaccess"){
528       $trustmode= 1;
529       // pervent double disable tag in html code, this will disturb our clean w3c html
530     
531     if(chkacl($this->acl, "trustmode")==""){
532           $smarty->assign("trusthide", "disabled");
533       }else{
534           $smarty->assign("trusthide", "");
535       }
537     } elseif ($this->trustModel == "byhost"){
538       $trustmode= 2;
539       $smarty->assign("trusthide", "");
540     } else {
541       // pervent double disable tag in html code, this will disturb our clean w3c html
542       if(chkacl($this->acl, "trustmode")==""){
543           $smarty->assign("trusthide", "disabled");
544       }else{
545           $smarty->assign("trusthide", "");
546       }
547       $trustmode= 0;
548     }
549     $smarty->assign("trustmode", $trustmode);
550     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
551           2 => _("allow access to these hosts")));
555     if((count($this->accessTo))==0)
556       $smarty->assign("emptyArrAccess",true);
557     else
558       $smarty->assign("emptyArrAccess",false);
559     
562     $smarty->assign("workstations", $this->accessTo);
564     $smarty->assign("apply", apply_filter());
565     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
566     return($display);
567   }
570   /* remove object from parent */
571   function remove_from_parent()
572   {
573     /* Cancel if there's nothing to do here */
574     if (!$this->initially_was_account){
575       return;
576     }
578     /* include global link_info */
579     $ldap= $this->config->get_ldap_link();
580     
581         /* Remove and write to LDAP */
582     plugin::remove_from_parent();
584     /* Zero out array */
585     $this->attrs['gosaHostACL']= array();
587     /* Keep uid, because we need it for authentification! */
588     unset($this->attrs['uid']);
589     unset($this->attrs['trustModel']);
591     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
592         $this->attributes, "Save");
593     $ldap->cd($this->dn);
594     $ldap->modify($this->attrs);
595     show_ldap_error($ldap->get_error());
597     /* Delete group only if cn is uid and there are no other
598        members inside */
599     $ldap->cd ($this->config->current['BASE']);
600     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
601     if ($ldap->count() != 0){
602       $attrs= $ldap->fetch();
603       if ($attrs['cn'][0] == $this->uid &&
604           !isset($this->attrs['memberUid'])){
606         $ldap->rmDir($ldap->getDN());
607       }
608     }
610     /* Optionally execute a command after we're done */
611     $this->handle_post_events("remove");
612   }
615   function save_object()
616   {
617     if (isset($_POST['posixTab'])){
618       /* Save values to object */
619       plugin::save_object();
621       /* Save force GID attribute */
622       if (chkacl ($this->acl, "force_ids") == ""){
623         if (isset ($_POST['force_ids'])){
624           $data= 1;
625         } else {
626           $data= 0;
627         }
628         if ($this->force_ids != $data){
629           $this->is_modified= TRUE;
630         }
631         $this->force_ids= $data;
633         $data= $_POST['primaryGroup'];
634         if ($this->primaryGroup != $data){
635           $this->is_modified= TRUE;
636         }
637         $this->primaryGroup= $_POST['primaryGroup'];
638       }
640       /* Save pwmode dependent attributes, curently hardcoded because there're
641          no alternatives */
642       if (1 == 1){
643         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
644               "use_shadowExpire", "use_shadowInactive",
645               "use_shadowWarning") as $val){
646           if (chkacl($this->acl, "$val") == ""){
647             if (isset ($_POST[$val])){
648               $data= 1;
649             } else {
650               $data= 0;
651             }
652             if ($data != $this->$val){
653               $this->is_modified= TRUE;
654             }
655             $this->$val= $data;
656           }
657         }
658       }
660       /* Trust mode - special handling */
661       if (isset($_POST['trustmode'])){
662         $saved= $this->trustModel;
663         if ($_POST['trustmode'] == "1"){
664           $this->trustModel= "fullaccess";
665         } elseif ($_POST['trustmode'] == "2"){
666           $this->trustModel= "byhost";
667         } else {
668           $this->trustModel= "";
669         }
670         if ($this->trustModel != $saved){
671           $this->is_modified= TRUE;
672         }
673       }
674     }
675   }
678   /* Save data to LDAP, depending on is_account we save or delete */
679   function save()
680   {
681         
682     /* include global link_info */
683     $ldap= $this->config->get_ldap_link();
685     /* Adapt shadow values */
686     if (!$this->use_shadowExpire){
687       $this->shadowExpire= "0";
688     } else {
689       /* Transform seconds to days here */
690       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
691     }
692     if (!$this->use_shadowMax){
693       $this->shadowMax= "0";
694     }
695     if ($this->must_change_password){
696       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
697     } else {
698       $this->shadowLastChange= (int)(date("U") / 86400);
699     }
700     if (!$this->use_shadowWarning){
701       $this->shadowWarning= "0";
702     }
704     /* Check what to do with ID's */
705     if ($this->force_ids == 0){
707       /* Use id's that are already set */
708       if ($this->savedUidNumber != ""){
709         $this->uidNumber= $this->savedUidNumber;
710         $this->gidNumber= $this->savedGidNumber;
711       } else {
713         /* Calculate new id's. We need to place a lock before calling get_next_id
714            to get real unique values. */
715         $wait= 10;
716         while (get_lock("uidnumber") != ""){
717           sleep (1);
719           /* Oups - timed out */
720           if ($wait-- == 0){
721             print_red (_("Failed: overriding lock"));
722             break;
723           }
724         }
726         add_lock ("uidnumber", "gosa");
727         $this->uidNumber= $this->get_next_id("uidNumber");
728         if ($this->savedGidNumber != ""){
729           $this->gidNumber= $this->savedGidNumber;
730         } else {
731           $this->gidNumber= $this->get_next_id("gidNumber");
732         }
733       }
735       if ($this->primaryGroup != 0){
736         $this->gidNumber= $this->primaryGroup;
737       }
738     }
740     if ($this->use_shadowMin != "1" ) {
741       $this->shadowMin = "";
742     }
744     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
745       $this->shadowMax = "";
746     }
748     if ($this->use_shadowWarning != "1" ) {
749       $this->shadowWarning = "";
750     }
752     if ($this->use_shadowInactive != "1" ) {
753       $this->shadowInactive = "";
754     }
756     if ($this->use_shadowExpire != "1" ) {
757       $this->shadowExpire = "";
758     }
760     /* Fill gecos */
761     if (isset($this->parent) && $this->parent != NULL){
762       $this->gecos= rewrite($this->parent->by_object['user']->cn);
763           if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
764                   $this->gecos= "";
765           }
766     }
768     /* Call parents save to prepare $this->attrs */
769     plugin::save();
771     /* Trust accounts */
772     $objectclasses= array();
773     foreach ($this->attrs['objectClass'] as $key => $class){
774       if (preg_match('/trustAccount/i', $class)){
775         continue;
776       }
777       $objectclasses[]= $this->attrs['objectClass'][$key];
778     }
779     $this->attrs['objectClass']= $objectclasses;
780     if ($this->trustModel != ""){
781       $this->attrs['objectClass'][]= "trustAccount";
782       $this->attrs['trustModel']= $this->trustModel;
783       $this->attrs['accessTo']= array();
784       if ($this->trustModel == "byhost"){
785         foreach ($this->accessTo as $host){
786           $this->attrs['accessTo'][]= $host;
787         }
788       }
789     } else {
790       if ($this->was_trust_account){
791         $this->attrs['accessTo']= array();
792         $this->attrs['trustModel']= array();
793       }
794     }
796     if(empty($this->attrs['gosaDefaultPrinter'])){
797       $thid->attrs['gosaDefaultPrinter']=array();
798     }
800         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive") as $attr){
801                 $this->attrs[$attr] = (int) $this->attrs[$attr];
802         }
804     /* Save data to LDAP */
805     $ldap->cd($this->dn);
806     $ldap->modify($this->attrs);
807     show_ldap_error($ldap->get_error());
809     /* Remove lock needed for unique id generation */
810     del_lock ("uidnumber");
813     /* Posix accounts have group interrelationship, take care about these here. */
814     if ($this->force_ids == 0 && $this->primaryGroup == 0){
815       $ldap->cd($this->config->current['BASE']);
816       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
818       /* Create group if it doesn't exist */
819       if ($ldap->count() == 0){
820         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
822         $g= new group($this->config, $groupdn);
823         $g->cn= $this->uid;
824         $g->force_gid= 1;
825         $g->gidNumber= $this->gidNumber;
826         $g->description= "Group of user ".$this->givenName." ".$this->sn;
827         $g->save ();
828       }
829     }
831     /* Take care about groupMembership values: add to groups */
832     foreach ($this->groupMembership as $key => $value){
833       $g= new group($this->config, $key);
834       $g->addUser ($this->uid);
835       $g->save();
837       /* May need to save the mail part, too */
838       if ($g->has_mailAccount){
839         $m= new mailgroup($this->config, $key);
840         $m->save();
841       }
842     }
844     /* Remove from groups not listed in groupMembership */
845     foreach ($this->savedGroupMembership as $key => $value){
846       if (!array_key_exists ($key, $this->groupMembership)){
847         $g= new group($this->config, $key);
848         $g->removeUser ($this->uid);
849         $g->save();
851         /* May need to save the mail part, too */
852         if ($g->has_mailAccount){
853           $m= new mailgroup($this->config, $key);
854           $m->save();
855         }
856       }
857     }
859     /* Optionally execute a command after we're done */
860     if ($this->initially_was_account == $this->is_account){
861       if ($this->is_modified){
862         $this->handle_post_events("mofify");
863       }
864     } else {
865       $this->handle_post_events("add");
866     }
867   }
869   /* Check formular input */
870   function check()
871   {
872     /* Include global link_info */
873     $ldap= $this->config->get_ldap_link();
875     /* Reset message array */
876     $message= array();
878     /* must: homeDirectory */
879     if ($this->homeDirectory == ""){
880       $message[]= _("The required field 'Home directory' is not set.");
881     }
882     if (!is_path($this->homeDirectory)){
883       $message[]= _("Please enter a valid path in 'Home directory' field.");
884     }
886     /* Check ID's if they are forced by user */
887     if ($this->force_ids == "1"){
889       /* Valid uid/gid? */
890       if (!is_id($this->uidNumber)){
891         $message[]= _("Value specified as 'UID' is not valid.");
892       } else {
893         if ($this->uidNumber < $this->config->current['MINID']){
894           $message[]= _("Value specified as 'UID' is too small.");
895         }
896       }
897       if (!is_id($this->gidNumber)){
898         $message[]= _("Value specified as 'GID' is not valid.");
899       } else {
900         if ($this->gidNumber < $this->config->current['MINID']){
901           $message[]= _("Value specified as 'GID' is too small.");
902         }
903       }
904     }
906     /* Check shadow settings, well I like spaghetties... */
907     if ($this->use_shadowMin){
908       if (!is_id($this->shadowMin)){
909         $message[]= _("Value specified as 'shadowMin' is not valid.");
910       }
911     }
912     if ($this->use_shadowMax){
913       if (!is_id($this->shadowMax)){
914         $message[]= _("Value specified as 'shadowMax' is not valid.");
915       }
916     }
917     if ($this->use_shadowWarning){
918       if (!is_id($this->shadowWarning)){
919         $message[]= _("Value specified as 'shadowWarning' is not valid.");
920       }
921       if (!$this->use_shadowMax){
922         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
923       }
924       if ($this->shadowWarning > $this->shadowMax){
925         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
926       }
927       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
928         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
929       }
930     }
931     if ($this->use_shadowInactive){
932       if (!is_id($this->shadowInactive)){
933         $message[]= _("Value specified as 'shadowInactive' is not valid.");
934       }
935       if (!$this->use_shadowMax){
936         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
937       }
938     }
939     if ($this->use_shadowMin && $this->use_shadowMax){
940       if ($this->shadowMin > $this->shadowMax){
941         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
942       }
943     }
945   //  if(empty($this->gosaDefaultPrinter)){
946   //    $message[]= _("You need to specify a valid default printer.");
947   //  }
949     return ($message);
950   }
952   function addGroup ($groups)
953   {
954     /* include global link_info */
955     $ldap= $this->config->get_ldap_link();
957     /* Walk through groups and add the descriptive entry if not exists */
958     foreach ($groups as $value){
959       if (!array_key_exists($value, $this->groupMembership)){
960         $ldap->cat($value);
961         $attrs= $ldap->fetch();
962         error_reporting (0);
963         if (!isset($attrs['description'][0])){
964           $entry= $attrs["cn"][0];
965         } else {
966           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
967           $entry= $attrs["cn"][0]." [$dsc]";
968         }
969         error_reporting (E_ALL);
970         $this->groupMembership[$ldap->getDN()]= $entry;
971       }
972     }
974     /* Sort groups */
975     asort ($this->groupMembership);
976     reset ($this->groupMembership);
977   }
980   /* Del posix user from some groups */
981   function delGroup ($groups)
982   {
983     $dest= array();
985     foreach ($this->groupMembership as $key => $value){
986       if (!in_array($key, $groups)){
987         $dest[$key]= $value;
988       }
989     }
990     $this->groupMembership= $dest;
991   }
993   /* Adapt from template, using 'dn' */
994   function adapt_from_template($dn)
995   {
996     /* Include global link_info */
997     $ldap= $this->config->get_ldap_link();
999     plugin::adapt_from_template($dn);
1000     $template= $this->attrs['uid'][0];
1002     /* Adapt group membership */
1003     $ldap->cd($this->config->current['BASE']);
1004     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1006     while ($this->attrs= $ldap->fetch()){
1007       if (!isset($this->attrs["description"][0])){
1008         $entry= $this->attrs["cn"][0];
1009       } else {
1010         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1011       }
1012       $this->groupMembership[$ldap->getDN()]= $entry;
1013     }
1015     /* Fix primary group settings */
1016     $ldap->cd($this->config->current['BASE']);
1017     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1018     if ($ldap->count() != 1){
1019       $this->primaryGroup= $this->gidNumber;
1020     }
1022         $ldap->cd($this->config->current['BASE']);
1023     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1024         while($attr = $ldap->fetch()){
1025                 $tmp = $attr['accessTo'];
1026                 unset ($tmp['count']);
1027                 $this->accessTo = $tmp; 
1028         }
1029         
1030     /* Adjust shadow checkboxes */
1031     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1032           "shadowExpire") as $val){
1034       if ($this->$val != 0){
1035         $oval= "use_".$val;
1036         $this->$oval= "1";
1037       }
1038     }
1039   }
1041   function get_next_id($attrib)
1042   {
1043     $ids= array();
1044     $ldap= $this->config->get_ldap_link();
1046     $ldap->cd ($this->config->current['BASE']);
1047     $ldap->search ("($attrib=*)", array("$attrib"));
1049     /* Get list of ids */
1050     while ($attrs= $ldap->fetch()){
1051       $ids[]= (int)$attrs["$attrib"][0];
1052     }
1054     /* Find out next free id near to UID_BASE */
1055     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1056       if (!in_array($id, $ids)){
1057         return ($id);
1058       }
1059     }
1061     /* Should not happen */
1062     if ($id == 65000){
1063       print_red(_("Too many users, can't allocate a free ID!"));
1064       exit;
1065     }
1067   }
1073 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1079 ?>