Code

Usermng free from SELECT_
[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       del_lock ($this->usertab->dn);
128       unset ($this->usertab);
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         }
409         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
410       }
412     }
414     /* Continue template editing */
415     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid'])){
416       $this->sn= $_POST['sn'];
417       $this->givenName= $_POST['givenName'];
419       /* Check for requred values */
420       $message= array();
421       if ($this->sn == "") {
422         $message[]= _("The required field 'Name' is not set.");
423       }
424       if ($this->givenName == "") {
425         $message[]= _("The required field 'Given name' is not set.");
426       }
428       /* Check if dn is used */
429       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
430       $ldap= $this->config->get_ldap_link();
431       $ldap->cd ($dn);
432       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
433       if ($ldap->count () != 0){
434         $message[]= _("A person with the choosen name is already used in this tree.");
435       }
437       /* Show error message / continue editing */
438       if (count($message) > 0){
439         show_errors ($message);
440       } else {
441         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
442         if (isset($this->config->current['IDGEN']) &&
443             $this->config->current['IDGEN'] != ""){
444           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
445           if (count($uids)){
446             $smarty->assign("edit_uid", "false");
447             $smarty->assign("uids", $uids);
448             $this->uid= current($uids);
449           }
450         } else {
451           $this->uid= "";
452         }
453         $this->got_uid= true;
454       }
456       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
457         $smarty->assign("$attr", $this->$attr);
458       }
459       if (isset($_POST['template'])){
460         $smarty->assign("template", $_POST['template']);
461       }
462       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
463     }
465     /* No template. Ok. Lets fill data into the normal user dialog */
466     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
467       foreach(array("sn", "givenName", "uid") as $attr){
468         if (isset($_POST[$attr])){
469           $this->usertab->by_object['user']->$attr= $_POST[$attr];
470         }
471       }
472     }
474     /* Finish template preamble */
475     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
477       /* Might not be filled if IDGEN is unset */
478       $this->sn= $_POST['sn'];
479       $this->givenName= $_POST['givenName'];
481       /* Move user supplied data to sub plugins */
482       $this->uid= $_POST['uid'];
483       $this->usertab->uid= $this->uid;
484       $this->usertab->sn= $this->sn;
485       $this->usertab->givenName= $this->givenName;
486       $template_dn= $_POST['template'];
487       $this->usertab->adapt_from_template($template_dn);
488       $template_base= preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
489       $this->usertab->by_object['user']->base= $template_base;
491       /* Set up the users ACL's for this 'dn' */
492       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
493       $this->usertab->set_acl($acl);
494     }
496     /* Show tab dialog if object is present */
497     if ($this->usertab){
498       $display= $this->usertab->execute();
500       /* Don't show buttons if tab dialog requests this */
501       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
502         $display.= "<p style=\"text-align:right\">\n";
503         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
504         $display.= "&nbsp;\n";
505         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
506         $display.= "</p>";
507       }
508       return ($display);
509     }
511     $smarty= get_smarty();
513     /* Check sorting variable */
514     if (!isset($this->usertab) &&
515         !isset($_POST['new_user']) &&
516         !isset($_POST['new_template']) &&
517         !isset($_POST['delete_user']) &&
518         !isset($_POST['setpass_user']) &&
519         !isset($_POST['select_user'])){
520       $this->reload();
521     }
523     /* Check for exeeded sizelimit */
524     if (($message= check_sizelimit()) != ""){
525       return($message);
526     }
528     /* Show main page */
529     $smarty->assign("userlist", $this->userlist);
530     }
531     $smarty->assign("search_image", get_template_path('images/search.png'));
532     $smarty->assign("tree_image", get_template_path('images/tree.png'));
533     $smarty->assign("infoimage", get_template_path('images/info.png'));
534     $smarty->assign("launchimage", get_template_path('images/launch.png'));
535     $smarty->assign("deplist", $this->config->idepartments);
536     foreach( array("depselect", "regex", "functionalusers", "unixusers",
537           "mailusers", "sambausers", "proxyusers", "ftpusers",
538           "faxusers", "templates", "subsearch") as $type){
540       $smarty->assign("$type", $userfilter[$type]);
541     }
543     /* Extend if we are not using javascript */
544     $smarty->assign("apply", apply_filter());
545     $smarty->assign("alphabet", generate_alphabet());
546     $smarty->assign("hint", print_sizelimit_warning());
548     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
549   }
552   function reload()
553   {
554     /* Get config */
555     $userfilter= get_global('userfilter');
557     /* Set base for all searches */
558     $base= $userfilter['depselect'];
560     /* Regex filter? */
561     if ($userfilter['regex'] != ""){
562       $regex= $userfilter['regex'];
563     } else {
564       $regex= "*";
565     }
567     /* Set filter depending on selection */
568     if ($this->config->current['SAMBAVERSION'] == 3){
569       $samba= "sambaSamAccount";
570     } else {
571       $samba= "sambaAccount";
572     }
574     $filter="";
575     if ($userfilter['functionalusers'] == "checked"){
576       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=PureFTPdUser)(objectClass=$samba)(objectClass=goFaxAccount)(objectClass=gosaProxyAccount))))";
577     }
578     if ($userfilter['unixusers'] == "checked"){
579       $filter.= "(objectClass=posixAccount)";
580     }
581     if ($userfilter['mailusers'] == "checked"){
582       $filter.= "(objectClass=gosaMailAccount)";
583     }
584     if ($userfilter['sambausers'] == "checked"){
585       $filter.= "(objectClass=$samba)";
586     }
587     if ($userfilter['proxyusers'] == "checked"){
588       $filter.= "(objectClass=gosaProxyAccount)";
589     }
590     if ($userfilter['ftpusers'] == "checked"){
591       $filter.= "(objectClass=PureFTPdUser)";
592     }
593     if ($userfilter['faxusers'] == "checked"){
594       $filter.= "(objectClass=goFaxAccount)";
595     }
597     if ($userfilter['templates'] == "checked"){
598       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
599     } else {
600       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
601     }
602     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
604     /* Generate userlist */
605     $ldap= $this->config->get_ldap_link(TRUE);
606     if ($userfilter['subsearch'] == "checked"){
607       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
608     } else {
609       $base= get_people_ou().$base;
610       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
611     }
613     $this->userlist= array();
615     foreach ($this->list as $value){
616       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
617         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
618           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
619           continue;
620         }
621         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
622           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
623             $value["givenName"][0].
624             " [".$value["uid"][0]."]";
625         } else {
626           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
627         }
628       }
629     }
630     natcasesort ($this->userlist);
631     reset ($this->userlist);
632   }
634   function remove_from_parent()
635   {
636   }
639   /* Check values */
640   function check()
641   {
642   }
645   /* Save to LDAP */
646   function save()
647   {
648   }
650   function adapt_from_template($dn)
651   {
652   }
654   function password_change_needed()
655   {
656   }
658   function show_header($button_text, $text, $disabled= FALSE)
659   {
660   }
662   function remove_lock()
663   {
664     if (isset($this->usertab->dn)){
665       del_lock ($this->usertab->dn);
666     }
667   }
671 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
672 ?>