Code

Removed undefined index message
[gosa.git] / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabs_user.inc";
22 class userManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Users";
26   var $plDescription= "This does something";
28   /* Dialog attributes */
29   var $usertab= NULL;
30   var $userlist= array();
31   var $ui= NULL;
32   var $acl= "";
33   var $templates= array();
34   var $got_uid= false;
36   function userManagement ($config, $ui)
37   {
38     /* Save configuration for internal use */
39     $this->config= $config;
40     $this->ui= $ui;
42     /* Get global filter config */
43     if (!is_global("userfilter")){
44       $base= get_base_from_people($ui->dn);
45       $userfilter= array( "mailusers"       => "checked",
46           "unixusers"       => "checked",
47           "templates"       => "",
48           "subsearch"       => "",
49           "proxyusers"      => "checked",
50           "sambausers"      => "checked",
51           "ftpusers"        => "checked",
52           "faxusers"        => "checked",
53           "functionalusers" => "checked",
54           "depselect"       => $base,
55           "regex"           => "*");
56       register_global("userfilter", $userfilter);
57     }
58   }
60   function execute()
61   {
62     $smarty= get_smarty();
63     $userfilter= get_global("userfilter");
65     /* Save filter data if we are in the headpage */
66     if (!isset($this->usertab)){
67       foreach( array("depselect", "regex") as $type){
68         if (isset($_POST[$type])){
69           $userfilter[$type]= $_POST[$type];
70         }
71       }
72       if (isset($_POST['depselect'])){
73         foreach( array("functionalusers", "unixusers", "mailusers",
74               "sambausers", "proxyusers", "ftpusers",
75               "faxusers", "templates", "subsearch") as $type){
77           if (isset($_POST[$type])) {
78             $userfilter[$type]= "checked";
79           } else {
80             $userfilter[$type]= "";
81           }
82         }
83       }
84       if (isset($_GET['search'])){
85         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
86         if ($s == "**"){
87           $s= "*";
88         }
89         $userfilter['regex']= $s;
90       }
91       register_global("userfilter", $userfilter);
92     }
94     /* React on user interaction here */
95     if ($_SERVER["REQUEST_METHOD"] == "POST"){
97       /* User wants to edit data? */
98       if ( (isset($_POST['select_user']) || (isset($_POST['edit_helper']) && $_POST['edit_helper'] == "1")) && isset($_POST['userlist']) && $_POST['userlist'] != ""){
100         /* Get 'dn' from posted 'uid', must be unique */
101         $this->dn= trim($_POST['userlist']);
103         /* Check locking, save current plugin in 'back_plugin', so
104            the dialog knows where to return. */
105         if (($user= get_lock($this->dn)) != ""){
106           return(gen_locked_message ($user, $this->dn));
107         }
109         /* Lock the current entry, so everyone will get the
110            above dialog */
111         add_lock ($this->dn, $this->ui->dn);
113         /* Set up the users ACL's for this 'dn' */
114         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
116         /* Register usertab to trigger edit dialog */
117         $this->usertab= new usertabs($this->config, 
118             $this->config->data['TABS']['USERTABS'], $this->dn);
119         $this->usertab->set_acl($acl);
121         $_SESSION['objectinfo']= $this->dn;
122       }
123     }
125     /* Reset requested? */
126     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
127       if (isset($this->usertab)){
128         del_lock ($this->usertab->dn);
129         unset ($this->usertab);
130       }
131       $this->usertab= NULL;
132       $this->lognames= array();;
133       $this->sn= "";
134       $this->givenName= "";
135       $this->uid= "";
136       unset ($_SESSION['objectinfo']);
137     }
139     /* Password change requested */
140     if (isset($_POST['setpass_user']) && isset($_POST['userlist'])){
142       /* Get 'dn' from posted 'uid' */
143       $this->dn= trim($_POST['userlist']);
145       /* Load permissions for selected 'dn' and check if
146          we're allowed to remove this 'dn' */
147       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
148       $acl= get_module_permission($acl, "user", $this->dn);
149       if (chkacl($acl, "password") == ""){
151         /* User is allowed to change passwords, save 'dn' and 'acl' for next
152            dialog. */
153         $this->acl= $acl;
154         $_SESSION['objectinfo']= $this->dn;
155         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
157       } else {
158         /* User is not allowed. Show message and cancel. */
159         print_red (_("You are not allowed to set this users password!"));
160       }
161     }
163     /* Perform password change */
164     if (isset($_POST['password_finish'])){
166       /* For security reasons, check if user is allowed to set password again */
167       if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
169         /* Check input and feed errors into 'message' */
170         $message= array();
172         /* Sanity checks... */
173         if ($_POST['new_password'] != $_POST['repeated_password']){
175           /* Matching passwords in new and repeated? */
176           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
177         } else {
179           /* Empty password is not permitted by default. */
180           if ($_POST['new_password'] == ""){
181             $message[]= _("The password you've entered as 'New password' is empty.");
182           }
183         }
185         /* Errors, or change password? */
186         if (count($message) != 0){
188           /* Show error message and continue editing */
189           show_errors ($message);
190           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
191         }
193         /* Set password, perform required steps */
194         if ($this->usertab){
195           if ($this->usertab->password_change_needed()){
196             $obj= $this->usertab->by_object['user'];
197             change_password ($this->usertab->dn, $_POST['new_password'],
198                 0, $obj->pw_storage);
199             gosa_log ("Password for '".$this->usertab->dn."' has been changed");
200             unset($this->usertab);
201             $this->usertab= NULL;
202           }
203         } else {
204           change_password ($this->dn, $_POST['new_password']);
205           gosa_log ("Password for '".$this->dn."' has been changed");
206         }
207       } else {
209         /* Missing permissions, show message */
210         print_red (_("You are not allowed to set this users password!"));
211       }
213       /* Clean session, delete lock */
214       del_lock ($this->dn);
215       $this->reload();
216       unset ($this->usertab);
217       $this->usertab= NULL;
218       $this->lognames= array();;
219       $this->sn= "";
220       $this->givenName= "";
221       $this->uid= "";
222       unset ($_SESSION['objectinfo']);
223     }
226     /* Remove user was requested */
227     if (isset($_POST['delete_user']) && isset($_POST['userlist'])){
229       /* Get 'dn' from posted 'uid' */
230       $this->dn= trim($_POST['userlist']);
232       /* Load permissions for selected 'dn' and check if
233          we're allowed to remove this 'dn' */
234       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
235       $this->acl= get_module_permission($acl, "user", $this->dn);
236       if (chkacl($this->acl, "delete") == ""){
238         /* Check locking, save current plugin in 'back_plugin', so
239            the dialog knows where to return. */
240         if (($user= get_lock($this->dn)) != ""){
241           return(gen_locked_message ($user, $this->dn));
242         }
244         /* Lock the current entry, so nobody will edit it during deletion */
245         add_lock ($this->dn, $this->ui->dn);
246         $smarty->assign("info", sprintf(_("You're about to delete the user %s."), $this->dn));
247         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
248       } else {
250         /* Obviously the user isn't allowed to delete. Show message and
251            clean session. */
252         print_red (_("You are not allowed to delete this user!"));
253       }
254     }
258     /* Confirmation for deletion has been passed. User should be deleted. */
259     if (isset($_POST['delete_user_confirm'])){
261       /* Some nice guy may send this as POST, so we've to check
262          for the permissions again. */
263       if (chkacl($this->acl, "delete") == ""){
265         /* Delete request is permitted, perform LDAP action */
266         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
267             $this->dn);
268         $this->usertab->set_acl(array($this->acl));
269         $this->usertab->delete ();
270         gosa_log ("User object '".$this->dn."' has been removed");
271         unset ($this->usertab);
272         $this->usertab= NULL;
274         /* User list has changed, reload it. */
275         $this->reload ();
276       } else {
278         /* Normally this shouldn't be reached, send some extra
279            logs to notify the administrator */
280         print_red (_("You are not allowed to delete this user!"));
282         if(isset($this->ui->uid)){
283           gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
284         }
285       }
287       /* Remove lock file after successfull deletion */
288       del_lock ($this->dn);
289     }
292     /* Delete user canceled? */
293     if (isset($_POST['delete_cancel'])){
294       del_lock ($this->dn);
295     }
298     /* Finish user edit is triggered by the tabulator dialog, so
299        the user wants to save edited data. Check and save at this
300        point. */
301     if (isset($_POST['edit_finish'])){
303       /* Check tabs, will feed message array */
304       $this->usertab->last= $this->usertab->current;
305       $this->usertab->save_object();
306       $message= $this->usertab->check();
308       /* Save, or display error message? */
309       if (count($message) == 0){
311         /* No errors. Go ahead and prepare to ask for a password
312            in case we're creating a new user. 'dn' will be 'new'
313            in this case. It is set to the correct value later. */
314         if ($this->dn == "new"){
315           $set_pass= 1;
316         } else {
317           $set_pass= 0;
318         }
320         /* Save user data to ldap */
321         if($this->usertab->save() == 1){
322           gosa_log ("User object '".$this->dn."' saving failed.");
323           return;
324         }
325         gosa_log ("User object '".$this->dn."' has been saved");
327         /* User has been saved successfully, remove lock from
328            LDAP. */
329         if ($this->dn != "new"){
330           del_lock ($this->dn);
331         }
333         /* In case of new users, ask for a password */
334         if (($set_pass || $this->usertab->password_change_needed()) &&
335             !$this->is_template){
337           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
338         }
340         /* There's no page reload so we have to read new users at
341            this point. */
342         $this->reload ();
343         unset ($this->usertab);
344         $this->usertab= NULL;
345         unset ($_SESSION['objectinfo']);
347       } else {
348         /* Ok. There seem to be errors regarding to the tab data,
349            show message and continue as usual. */
350         show_errors($message);
351       }
352     }
354     /* Generate template list */
355     if (isset($_POST['new_user'])){
356       $this->templates= array();
357       $ldap= $this->config->get_ldap_link();
358       foreach ($this->config->departments as $key => $value){
359         $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
360         $acl= get_module_permission($acl, "user", get_people_ou().$value);
361         if (chkacl($acl, "create") == ""){
362           $ldap->cd (get_people_ou().$value);
363           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
365           if ($ldap->count() != 0){
366             while ($attrs= $ldap->fetch()){
367               $this->templates[$ldap->getDN()]=
368                 $attrs['uid'][0]." - $key";
369             }
370             $this->templates['none']= _("none");
371           }
372         }
373       }
374       natcasesort ($this->templates);
375       reset ($this->templates);
376     }
378     /* New user/template request */
379     if (isset($_POST['new_user']) || isset($_POST['new_template'])){
381       /* By default we set 'dn' to 'new', all relevant plugins will
382          react on this. */
383       $this->dn= "new";
384       if (isset($this->config->current['IDGEN'])){
385         $this->got_uid= false;
386       } else {
387         $this->got_uid= true;
388       }
390       /* Create new usertab object */
391       $this->usertab= new usertabs($this->config,
392           $this->config->data['TABS']['USERTABS'], $this->dn);
393       $this->usertab->set_acl(array(':all'));
395       /* Take care about templates */
396       if (isset($_POST['new_template'])){
397         $this->is_template= TRUE;
398         $this->usertab->set_template_mode ();
399       } else {
400         $this->is_template= FALSE;
401       }
403       /* Use template if there are any of them */
404       if (count($this->templates) && !isset($_POST['new_template'])){
405         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
406           $smarty->assign("$attr", $this->$attr);
407         }
408         if (isset($_POST['template'])){
409           $smarty->assign("template", $_POST['template']);
410         } else {
411           $smarty->assign("template", "");
412         }
413         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
414       }
416     }
418     /* Continue template editing */
419     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid'])){
420       $this->sn= $_POST['sn'];
421       $this->givenName= $_POST['givenName'];
423       /* Check for requred values */
424       $message= array();
425       if ($this->sn == "") {
426         $message[]= _("The required field 'Name' is not set.");
427       }
428       if ($this->givenName == "") {
429         $message[]= _("The required field 'Given name' is not set.");
430       }
432       /* Check if dn is used */
433       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
434       $ldap= $this->config->get_ldap_link();
435       $ldap->cd ($dn);
436       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
437       if ($ldap->count () != 0){
438         $message[]= _("A person with the choosen name is already used in this tree.");
439       }
441       /* Show error message / continue editing */
442       if (count($message) > 0){
443         show_errors ($message);
444       } else {
445         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
446         if (isset($this->config->current['IDGEN']) &&
447             $this->config->current['IDGEN'] != ""){
448           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
449           if (count($uids)){
450             $smarty->assign("edit_uid", "false");
451             $smarty->assign("uids", $uids);
452             $this->uid= current($uids);
453           }
454         } else {
455           $this->uid= "";
456         }
457         $this->got_uid= true;
458       }
460       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
461         $smarty->assign("$attr", $this->$attr);
462       }
463       if (isset($_POST['template'])){
464         $smarty->assign("template", $_POST['template']);
465       }
466       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
467     }
469     /* No template. Ok. Lets fill data into the normal user dialog */
470     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
471       foreach(array("sn", "givenName", "uid") as $attr){
472         if (isset($_POST[$attr])){
473           $this->usertab->by_object['user']->$attr= $_POST[$attr];
474         }
475       }
476     }
478     /* Finish template preamble */
479     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
481       /* Might not be filled if IDGEN is unset */
482       $this->sn= $_POST['sn'];
483       $this->givenName= $_POST['givenName'];
485       /* Move user supplied data to sub plugins */
486       $this->uid= $_POST['uid'];
487       $this->usertab->uid= $this->uid;
488       $this->usertab->sn= $this->sn;
489       $this->usertab->givenName= $this->givenName;
490       $template_dn= $_POST['template'];
491       $this->usertab->adapt_from_template($template_dn);
492       $template_base= preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
493       $this->usertab->by_object['user']->base= $template_base;
495       /* Set up the users ACL's for this 'dn' */
496       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
497       $this->usertab->set_acl($acl);
498     }
500     /* Show tab dialog if object is present */
501     if ($this->usertab){
502       $display= $this->usertab->execute();
504       /* Don't show buttons if tab dialog requests this */
505       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
506         $display.= "<p style=\"text-align:right\">\n";
507         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
508         $display.= "&nbsp;\n";
509         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
510         $display.= "</p>";
511       }
512       return ($display);
513     }
515     $smarty= get_smarty();
517     /* Check sorting variable */
518     if (!isset($this->usertab) &&
519         !isset($_POST['new_user']) &&
520         !isset($_POST['new_template']) &&
521         !isset($_POST['delete_user']) &&
522         !isset($_POST['setpass_user']) &&
523         !isset($_POST['select_user'])){
524       $this->reload();
525     }
527     /* Check for exeeded sizelimit */
528     if (($message= check_sizelimit()) != ""){
529       return($message);
530     }
532     /* Show main page */
533     $smarty->assign("userlist", $this->userlist);
534     $smarty->assign("search_image", get_template_path('images/search.png'));
535     $smarty->assign("tree_image", get_template_path('images/tree.png'));
536     $smarty->assign("infoimage", get_template_path('images/info.png'));
537     $smarty->assign("launchimage", get_template_path('images/launch.png'));
538     $smarty->assign("deplist", $this->config->idepartments);
539     foreach( array("depselect", "regex", "functionalusers", "unixusers",
540           "mailusers", "sambausers", "proxyusers", "ftpusers",
541           "faxusers", "templates", "subsearch") as $type){
543       $smarty->assign("$type", $userfilter[$type]);
544     }
546     /* Extend if we are not using javascript */
547     $smarty->assign("apply", apply_filter());
548     $smarty->assign("alphabet", generate_alphabet());
549     $smarty->assign("hint", print_sizelimit_warning());
551     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
552   }
555   function reload()
556   {
557     /* Get config */
558     $userfilter= get_global('userfilter');
560     /* Set base for all searches */
561     $base= $userfilter['depselect'];
563     /* Regex filter? */
564     if ($userfilter['regex'] != ""){
565       $regex= $userfilter['regex'];
566     } else {
567       $regex= "*";
568     }
570     /* Set filter depending on selection */
571     if ($this->config->current['SAMBAVERSION'] == 3){
572       $samba= "sambaSamAccount";
573     } else {
574       $samba= "sambaAccount";
575     }
577     $filter="";
578     if ($userfilter['functionalusers'] == "checked"){
579       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=PureFTPdUser)(objectClass=$samba)(objectClass=goFaxAccount)(objectClass=gosaProxyAccount))))";
580     }
581     if ($userfilter['unixusers'] == "checked"){
582       $filter.= "(objectClass=posixAccount)";
583     }
584     if ($userfilter['mailusers'] == "checked"){
585       $filter.= "(objectClass=gosaMailAccount)";
586     }
587     if ($userfilter['sambausers'] == "checked"){
588       $filter.= "(objectClass=$samba)";
589     }
590     if ($userfilter['proxyusers'] == "checked"){
591       $filter.= "(objectClass=gosaProxyAccount)";
592     }
593     if ($userfilter['ftpusers'] == "checked"){
594       $filter.= "(objectClass=PureFTPdUser)";
595     }
596     if ($userfilter['faxusers'] == "checked"){
597       $filter.= "(objectClass=goFaxAccount)";
598     }
600     if ($userfilter['templates'] == "checked"){
601       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
602     } else {
603       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
604     }
605     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
607     /* Generate userlist */
608     $ldap= $this->config->get_ldap_link(TRUE);
609     if ($userfilter['subsearch'] == "checked"){
610       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
611     } else {
612       $base= get_people_ou().$base;
613       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
614     }
616     $this->userlist= array();
618     foreach ($this->list as $value){
619       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
620         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
621           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
622           continue;
623         }
624         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
625           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
626             $value["givenName"][0].
627             " [".$value["uid"][0]."]";
628         } else {
629           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
630         }
631       }
632     }
633     natcasesort ($this->userlist);
634     reset ($this->userlist);
635   }
637   function remove_from_parent()
638   {
639   }
642   /* Check values */
643   function check()
644   {
645   }
648   /* Save to LDAP */
649   function save()
650   {
651   }
653   function adapt_from_template($dn)
654   {
655   }
657   function password_change_needed()
658   {
659   }
661   function show_header($button_text, $text, $disabled= FALSE)
662   {
663   }
665   function remove_lock()
666   {
667     if (isset($this->usertab->dn)){
668       del_lock ($this->usertab->dn);
669     }
670   }
674 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
675 ?>