Code

Fixed one remaining "not assigned" error
[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           "faxusers"        => "checked",
52           "functionalusers" => "checked",
53           "depselect"       => $base,
54           "regex"           => "*");
55       register_global("userfilter", $userfilter);
56     }
57   }
59   function execute()
60   {
61     $smarty= get_smarty();
62     $userfilter= get_global("userfilter");
64     /* Save filter data if we are in the headpage */
65     if (!isset($this->usertab)){
66       foreach( array("depselect", "regex") as $type){
67         if (isset($_POST[$type])){
68           $userfilter[$type]= $_POST[$type];
69         }
70       }
71       if (isset($_POST['depselect'])){
72         foreach( array("functionalusers", "unixusers", "mailusers",
73               "sambausers", "proxyusers", "faxusers", "templates", "subsearch") as $type){
75           if (isset($_POST[$type])) {
76             $userfilter[$type]= "checked";
77           } else {
78             $userfilter[$type]= "";
79           }
80         }
81       }
82       if (isset($_GET['search'])){
83         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
84         if ($s == "**"){
85           $s= "*";
86         }
87         $userfilter['regex']= $s;
88       }
89       register_global("userfilter", $userfilter);
90     }
92     /* React on user interaction here */
93     if ($_SERVER["REQUEST_METHOD"] == "POST"){
95       /* User wants to edit data? */
96       if ( (isset($_POST['select_user']) || (isset($_POST['edit_helper']) && $_POST['edit_helper'] == "1")) && isset($_POST['userlist']) && $_POST['userlist'] != ""){
98         /* Get 'dn' from posted 'uid', must be unique */
99         $this->dn= trim($_POST['userlist']);
101         /* Check locking, save current plugin in 'back_plugin', so
102            the dialog knows where to return. */
103         if (($user= get_lock($this->dn)) != ""){
104           return(gen_locked_message ($user, $this->dn));
105         }
107         /* Lock the current entry, so everyone will get the
108            above dialog */
109         add_lock ($this->dn, $this->ui->dn);
111         /* Set up the users ACL's for this 'dn' */
112         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
114         /* Register usertab to trigger edit dialog */
115         $this->usertab= new usertabs($this->config, 
116             $this->config->data['TABS']['USERTABS'], $this->dn);
117         $this->usertab->set_acl($acl);
119         $_SESSION['objectinfo']= $this->dn;
120       }
121     }
123     /* Reset requested? */
124     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
125       if (isset($this->usertab)){
126         del_lock ($this->usertab->dn);
127         unset ($this->usertab);
128       }
129       $this->usertab= NULL;
130       $this->lognames= array();;
131       $this->sn= "";
132       $this->givenName= "";
133       $this->uid= "";
134       unset ($_SESSION['objectinfo']);
135     }
137     /* Password change requested */
138     if (isset($_POST['setpass_user']) && isset($_POST['userlist'])){
140       /* Get 'dn' from posted 'uid' */
141       $this->dn= trim($_POST['userlist']);
143       /* Load permissions for selected 'dn' and check if
144          we're allowed to remove this 'dn' */
145       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
146       $acl= get_module_permission($acl, "user", $this->dn);
147       if (chkacl($acl, "password") == ""){
149         /* User is allowed to change passwords, save 'dn' and 'acl' for next
150            dialog. */
151         $this->acl= $acl;
152         $_SESSION['objectinfo']= $this->dn;
153         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
155       } else {
156         /* User is not allowed. Show message and cancel. */
157         print_red (_("You are not allowed to set this users password!"));
158       }
159     }
161     /* Perform password change */
162     if (isset($_POST['password_finish'])){
164       /* For security reasons, check if user is allowed to set password again */
165       if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
167         /* Check input and feed errors into 'message' */
168         $message= array();
170         /* Sanity checks... */
171         if ($_POST['new_password'] != $_POST['repeated_password']){
173           /* Matching passwords in new and repeated? */
174           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
175         } else {
177           /* Empty password is not permitted by default. */
178           if ($_POST['new_password'] == ""){
179             $message[]= _("The password you've entered as 'New password' is empty.");
180           }
181         }
183         /* Errors, or change password? */
184         if (count($message) != 0){
186           /* Show error message and continue editing */
187           show_errors ($message);
188           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
189         }
191         /* Set password, perform required steps */
192         if ($this->usertab){
193           if ($this->usertab->password_change_needed()){
194             $obj= $this->usertab->by_object['user'];
195             change_password ($this->usertab->dn, $_POST['new_password'],
196                 0, $obj->pw_storage);
197             gosa_log ("Password for '".$this->usertab->dn."' has been changed");
198             unset($this->usertab);
199             $this->usertab= NULL;
200           }
201         } else {
202           change_password ($this->dn, $_POST['new_password']);
203           gosa_log ("Password for '".$this->dn."' has been changed");
204         }
205       } else {
207         /* Missing permissions, show message */
208         print_red (_("You are not allowed to set this users password!"));
209       }
211       /* Clean session, delete lock */
212       del_lock ($this->dn);
213       $this->reload();
214       unset ($this->usertab);
215       $this->usertab= NULL;
216       $this->lognames= array();;
217       $this->sn= "";
218       $this->givenName= "";
219       $this->uid= "";
220       unset ($_SESSION['objectinfo']);
221     }
224     /* Remove user was requested */
225     if (isset($_POST['delete_user']) && isset($_POST['userlist'])){
227       /* Get 'dn' from posted 'uid' */
228       $this->dn= trim($_POST['userlist']);
230       /* Load permissions for selected 'dn' and check if
231          we're allowed to remove this 'dn' */
232       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
233       $this->acl= get_module_permission($acl, "user", $this->dn);
234       if (chkacl($this->acl, "delete") == ""){
236         /* Check locking, save current plugin in 'back_plugin', so
237            the dialog knows where to return. */
238         if (($user= get_lock($this->dn)) != ""){
239           return(gen_locked_message ($user, $this->dn));
240         }
242         /* Lock the current entry, so nobody will edit it during deletion */
243         add_lock ($this->dn, $this->ui->dn);
244         $smarty->assign("info", sprintf(_("You're about to delete the user %s."), $this->dn));
245         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
246       } else {
248         /* Obviously the user isn't allowed to delete. Show message and
249            clean session. */
250         print_red (_("You are not allowed to delete this user!"));
251       }
252     }
256     /* Confirmation for deletion has been passed. User should be deleted. */
257     if (isset($_POST['delete_user_confirm'])){
259       /* Some nice guy may send this as POST, so we've to check
260          for the permissions again. */
261       if (chkacl($this->acl, "delete") == ""){
263         /* Delete request is permitted, perform LDAP action */
264         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
265             $this->dn);
266         $this->usertab->set_acl(array($this->acl));
267         $this->usertab->delete ();
268         gosa_log ("User object '".$this->dn."' has been removed");
269         unset ($this->usertab);
270         $this->usertab= NULL;
272         /* User list has changed, reload it. */
273         $this->reload ();
274       } else {
276         /* Normally this shouldn't be reached, send some extra
277            logs to notify the administrator */
278         print_red (_("You are not allowed to delete this user!"));
280         if(isset($this->ui->uid)){
281           gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
282         }
283       }
285       /* Remove lock file after successfull deletion */
286       del_lock ($this->dn);
287     }
290     /* Delete user canceled? */
291     if (isset($_POST['delete_cancel'])){
292       del_lock ($this->dn);
293     }
296     /* Finish user edit is triggered by the tabulator dialog, so
297        the user wants to save edited data. Check and save at this
298        point. */
299     if (isset($_POST['edit_finish'])){
301       /* Check tabs, will feed message array */
302       $this->usertab->last= $this->usertab->current;
303       $this->usertab->save_object();
304       $message= $this->usertab->check();
306       /* Save, or display error message? */
307       if (count($message) == 0){
309         /* No errors. Go ahead and prepare to ask for a password
310            in case we're creating a new user. 'dn' will be 'new'
311            in this case. It is set to the correct value later. */
312         if ($this->dn == "new"){
313           $set_pass= 1;
314         } else {
315           $set_pass= 0;
316         }
318         /* Save user data to ldap */
319         if($this->usertab->save() == 1){
320           gosa_log ("User object '".$this->dn."' saving failed.");
321           return;
322         }
323         gosa_log ("User object '".$this->dn."' has been saved");
325         /* User has been saved successfully, remove lock from
326            LDAP. */
327         if ($this->dn != "new"){
328           del_lock ($this->dn);
329         }
331         /* In case of new users, ask for a password */
332         if (($set_pass || $this->usertab->password_change_needed()) &&
333             !$this->is_template){
335           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
336         }
338         /* There's no page reload so we have to read new users at
339            this point. */
340         $this->reload ();
341         unset ($this->usertab);
342         $this->usertab= NULL;
343         unset ($_SESSION['objectinfo']);
345       } else {
346         /* Ok. There seem to be errors regarding to the tab data,
347            show message and continue as usual. */
348         show_errors($message);
349       }
350     }
352     /* Generate template list */
353     if (isset($_POST['new_user'])){
354       $this->templates= array();
355       $ldap= $this->config->get_ldap_link();
356       foreach ($this->config->departments as $key => $value){
357         $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
358         $acl= get_module_permission($acl, "user", get_people_ou().$value);
359         if (chkacl($acl, "create") == ""){
360           $ldap->cd (get_people_ou().$value);
361           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
363           if ($ldap->count() != 0){
364             while ($attrs= $ldap->fetch()){
365               $this->templates[$ldap->getDN()]=
366                 $attrs['uid'][0]." - $key";
367             }
368             $this->templates['none']= _("none");
369           }
370         }
371       }
372       natcasesort ($this->templates);
373       reset ($this->templates);
374     }
376     /* New user/template request */
377     if (isset($_POST['new_user']) || isset($_POST['new_template'])){
379       /* By default we set 'dn' to 'new', all relevant plugins will
380          react on this. */
381       $this->dn= "new";
382       if (isset($this->config->current['IDGEN'])){
383         $this->got_uid= false;
384       } else {
385         $this->got_uid= true;
386       }
388       /* Create new usertab object */
389       $this->usertab= new usertabs($this->config,
390           $this->config->data['TABS']['USERTABS'], $this->dn);
391       $this->usertab->set_acl(array(':all'));
393       /* Take care about templates */
394       if (isset($_POST['new_template'])){
395         $this->is_template= TRUE;
396         $this->usertab->set_template_mode ();
397       } else {
398         $this->is_template= FALSE;
399       }
401       /* Use template if there are any of them */
402       if (count($this->templates) && !isset($_POST['new_template'])){
403         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
404           $smarty->assign("$attr", $this->$attr);
405         }
406         if (isset($_POST['template'])){
407           $smarty->assign("template", $_POST['template']);
408         } else {
409           $smarty->assign("template", "");
410         }
411         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
412       }
414     }
416     /* Continue template editing */
417     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid'])){
418       $this->sn= $_POST['sn'];
419       $this->givenName= $_POST['givenName'];
421       /* Check for requred values */
422       $message= array();
423       if ($this->sn == "") {
424         $message[]= _("The required field 'Name' is not set.");
425       }
426       if ($this->givenName == "") {
427         $message[]= _("The required field 'Given name' is not set.");
428       }
430       /* Check if dn is used */
431       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
432       $ldap= $this->config->get_ldap_link();
433       $ldap->cd ($dn);
434       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
435       if ($ldap->count () != 0){
436         $message[]= _("A person with the choosen name is already used in this tree.");
437       }
439       /* Show error message / continue editing */
440       if (count($message) > 0){
441         show_errors ($message);
442       } else {
443         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
444         if (isset($this->config->current['IDGEN']) &&
445             $this->config->current['IDGEN'] != ""){
446           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
447           if (count($uids)){
448             $smarty->assign("edit_uid", "false");
449             $smarty->assign("uids", $uids);
450             $this->uid= current($uids);
451           }
452         } else {
453             $smarty->assign("edit_uid", "");
454           $this->uid= "";
455         }
456         $this->got_uid= true;
457       }
459       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
460         $smarty->assign("$attr", $this->$attr);
461       }
462       if (isset($_POST['template'])){
463         $smarty->assign("template", $_POST['template']);
464       }
465       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
466     }
468     /* No template. Ok. Lets fill data into the normal user dialog */
469     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
470       foreach(array("sn", "givenName", "uid") as $attr){
471         if (isset($_POST[$attr])){
472           $this->usertab->by_object['user']->$attr= $_POST[$attr];
473         }
474       }
475     }
477     /* Finish template preamble */
478     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
480       /* Might not be filled if IDGEN is unset */
481       $this->sn= $_POST['sn'];
482       $this->givenName= $_POST['givenName'];
484       /* Move user supplied data to sub plugins */
485       $this->uid= $_POST['uid'];
486       $this->usertab->uid= $this->uid;
487       $this->usertab->sn= $this->sn;
488       $this->usertab->givenName= $this->givenName;
489       $template_dn= $_POST['template'];
490       $this->usertab->adapt_from_template($template_dn);
491       $template_base= preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
492       $this->usertab->by_object['user']->base= $template_base;
494       /* Set up the users ACL's for this 'dn' */
495       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
496       $this->usertab->set_acl($acl);
497     }
499     /* Show tab dialog if object is present */
500     if ($this->usertab){
501       $display= $this->usertab->execute();
503       /* Don't show buttons if tab dialog requests this */
504       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
505         $display.= "<p style=\"text-align:right\">\n";
506         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
507         $display.= "&nbsp;\n";
508         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
509         $display.= "</p>";
510       }
511       return ($display);
512     }
514     $smarty= get_smarty();
516     /* Check sorting variable */
517     if (!isset($this->usertab) &&
518         !isset($_POST['new_user']) &&
519         !isset($_POST['new_template']) &&
520         !isset($_POST['delete_user']) &&
521         !isset($_POST['setpass_user']) &&
522         !isset($_POST['select_user'])){
523       $this->reload();
524     }
526     /* Check for exeeded sizelimit */
527     if (($message= check_sizelimit()) != ""){
528       return($message);
529     }
531     /* Show main page */
532     $smarty->assign("userlist", $this->userlist);
533     $smarty->assign("search_image", get_template_path('images/search.png'));
534     $smarty->assign("tree_image", get_template_path('images/tree.png'));
535     $smarty->assign("infoimage", get_template_path('images/info.png'));
536     $smarty->assign("launchimage", get_template_path('images/launch.png'));
537     $smarty->assign("deplist", $this->config->idepartments);
538     foreach( array("depselect", "regex", "functionalusers", "unixusers",
539           "mailusers", "sambausers", "proxyusers", 
540           "faxusers", "templates", "subsearch") as $type){
542       $smarty->assign("$type", $userfilter[$type]);
543     }
545     /* Extend if we are not using javascript */
546     $smarty->assign("apply", apply_filter());
547     $smarty->assign("alphabet", generate_alphabet());
548     $smarty->assign("hint", print_sizelimit_warning());
550     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
551   }
554   function reload()
555   {
556     /* Get config */
557     $userfilter= get_global('userfilter');
559     /* Set base for all searches */
560     $base= $userfilter['depselect'];
562     /* Regex filter? */
563     if ($userfilter['regex'] != ""){
564       $regex= $userfilter['regex'];
565     } else {
566       $regex= "*";
567     }
569     /* Set filter depending on selection */
570     if ($this->config->current['SAMBAVERSION'] == 3){
571       $samba= "sambaSamAccount";
572     } else {
573       $samba= "sambaAccount";
574     }
576     $filter="";
577     if ($userfilter['functionalusers'] == "checked"){
578       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=$samba)(objectClass=goFaxAccount)(objectClass=gosaProxyAccount))))";
579     }
580     if ($userfilter['unixusers'] == "checked"){
581       $filter.= "(objectClass=posixAccount)";
582     }
583     if ($userfilter['mailusers'] == "checked"){
584       $filter.= "(objectClass=gosaMailAccount)";
585     }
586     if ($userfilter['sambausers'] == "checked"){
587       $filter.= "(objectClass=$samba)";
588     }
589     if ($userfilter['proxyusers'] == "checked"){
590       $filter.= "(objectClass=gosaProxyAccount)";
591     }
592     if ($userfilter['faxusers'] == "checked"){
593       $filter.= "(objectClass=goFaxAccount)";
594     }
596     if ($userfilter['templates'] == "checked"){
597       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
598     } else {
599       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
600     }
601     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
603     /* Generate userlist */
604     $ldap= $this->config->get_ldap_link(TRUE);
605     if ($userfilter['subsearch'] == "checked"){
606       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
607     } else {
608       $base= get_people_ou().$base;
609       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
610     }
612     $this->userlist= array();
614     foreach ($this->list as $value){
615       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
616         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
617           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
618           continue;
619         }
620         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
621           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
622             $value["givenName"][0].
623             " [".$value["uid"][0]."]";
624         } else {
625           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
626         }
627       }
628     }
629     natcasesort ($this->userlist);
630     reset ($this->userlist);
631   }
633   function remove_from_parent()
634   {
635   }
638   /* Check values */
639   function check()
640   {
641   }
644   /* Save to LDAP */
645   function save()
646   {
647   }
649   function adapt_from_template($dn)
650   {
651   }
653   function password_change_needed()
654   {
655   }
657   function show_header($button_text, $text, $disabled= FALSE)
658   {
659   }
661   function remove_lock()
662   {
663     if (isset($this->usertab->dn)){
664       del_lock ($this->usertab->dn);
665     }
666   }
670 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
671 ?>