From 1f52edb5503aefc4bf2254e3ffcd03623e1a7fb7 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Wed, 30 May 2012 19:00:56 +0000 Subject: [PATCH] allow to send password mails on user edit page --- lam/HISTORY | 1 + lam/help/help.inc | 2 ++ lam/lib/account.inc | 60 +++++++++++++++++++++++++++++++++++- lam/lib/modules.inc | 24 +++++++++++++++ lam/templates/lib/500_lam.js | 4 ++- 5 files changed, 89 insertions(+), 2 deletions(-) diff --git a/lam/HISTORY b/lam/HISTORY index 026817e9..bf18ede0 100644 --- a/lam/HISTORY +++ b/lam/HISTORY @@ -6,6 +6,7 @@ June 2012 -> Separate group of names module for users allows to manage memberships if Unix module is not used (RFE 3504429) -> Named object module for groups (used for rfc2307bis schema) -> Password change page allows account (un)locking + -> Allow to send password mails on user edit page - fixed bugs -> Asterisk extensions with same name (3528288) diff --git a/lam/help/help.inc b/lam/help/help.inc index 3bd72517..9b4f4d36 100644 --- a/lam/help/help.inc +++ b/lam/help/help.inc @@ -169,6 +169,8 @@ $helpArray = array ( "Text" => _("The PDF structure defines what information is exported as PDF file and how the pages are structured. You can manage the PDF structures in the PDF editor (under \"Tools\").")), "406" => array ("Headline" => _("Force password change"), "Text" => _("If you set this option then the user has to change his password at the next login.")), + "407" => array ("Headline" => _("Send via mail"), + "Text" => _("Sends the password to the user via mail. Please edit your LAM server profile to setup the mail settings.")), // 500 - 599 // LAM Pro "501" => array ("Headline" => _("LDAP suffix"), diff --git a/lam/lib/account.inc b/lam/lib/account.inc index 8c1bac49..c8437b16 100644 --- a/lam/lib/account.inc +++ b/lam/lib/account.inc @@ -4,7 +4,7 @@ $Id$ This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) Copyright (C) 2003 - 2006 Tilo Lutz - 2009 - 2011 Roland Gruber + 2009 - 2012 Roland Gruber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -848,4 +848,62 @@ function extractDNSuffix($dn) { return substr($dn, strpos($dn, ',')+1); } +/** + * Sends the password mail. + * + * @param String $pwd new password + * @param array $user LDAP attributes of user + * @return array list of arrays that can be used to create status messages + */ +function sendPasswordMail($pwd, $user) { + // read mail data + $mailTo = $user['mail'][0]; + $mailFrom = $_SESSION['config']->getLamProMailFrom(); + $mailReplyTo = $_SESSION['config']->getLamProMailReplyTo(); + $mailSubject = $_SESSION['config']->getLamProMailSubject(); + $mailText = $_SESSION['config']->getLamProMailText(); + $mailIsHTML = $_SESSION['config']->getLamProMailIsHTML(); + $subject = $mailSubject; + $body = $mailText; + $body = str_replace('@@newPassword@@', $pwd, $body); + $results = array(); + $found = preg_match('/\@\@[^\@]+\@\@/', $body, $results); + while ($found == 1) { + $attr = str_replace('@', '', $results[0]); + $value = ''; + if (isset($user[strtolower($attr)][0])) { + $value = $user[strtolower($attr)][0]; + } + $body = str_replace('@@' . $attr . '@@', $value, $body); + $found = preg_match('/\@\@[^\@]+\@\@/', $body, $results); + } + $headerLines = "X-Mailer: LDAP Account Manager\r\n"; + if ($mailFrom != '') { + $headerLines .= 'From: ' . $mailFrom . "\r\n"; + } + if ($mailReplyTo != '') { + $headerLines .= 'Reply-To: ' . $mailReplyTo . "\r\n"; + } + if ($mailIsHTML == 'true') { + $headerLines .= "MIME-Version: 1.0\r\n"; + $headerLines .= "Content-type: text/html; charset=UTF-8\r\n"; + } + else { + $headerLines .= "Content-type: text; charset=UTF-8\r\n"; + } + $success = mail($mailTo, $subject, $body, $headerLines); + if ($success) { + logNewMessage(LOG_DEBUG, 'Sent password mail to ' . $mailTo); + return array( + array('INFO', sprintf(_('Mail successfully sent to %s.'), htmlspecialchars($mailTo))) + ); + } + else { + logNewMessage(LOG_ERR, 'Unable to send password mail to ' . htmlspecialchars($mailTo)); + return array( + array('ERROR', _('Unable to send mail!')) + ); + } +} + ?> diff --git a/lam/lib/modules.inc b/lam/lib/modules.inc index 5285f444..34c3e947 100644 --- a/lam/lib/modules.inc +++ b/lam/lib/modules.inc @@ -1004,6 +1004,10 @@ class accountContainer { $container->addElement(new htmlTableExtendedInputCheckbox('lamForcePasswordChange', false, _('Force password change'))); $container->addElement(new htmlHelpLink('406'), true); } + if (isLAMProVersion() && isset($this->attributes_orig['mail'][0])) { + $container->addElement(new htmlTableExtendedInputCheckbox('lamPasswordChangeSendMail', false, _('Send via mail'))); + $container->addElement(new htmlHelpLink('407'), true); + } $container->addElement(new htmlSpacer(null, '10px'), true); // password modules $moduleContainer = new htmlTable(); @@ -1067,6 +1071,10 @@ class accountContainer { if (isset($input['forcePasswordChange']) && ($input['forcePasswordChange'] == 'true')) { $forcePasswordChange = true; } + $sendMail = false; + if (isset($input['sendMail']) && ($input['sendMail'] == 'true')) { + $sendMail = true; + } $return['forcePasswordChange'] = $forcePasswordChange; if ($return['errorsOccured'] == 'false') { // set new password @@ -1090,6 +1098,22 @@ class accountContainer { } } } + if (isLAMProVersion() && $sendMail) { + $mailMessages = sendPasswordMail($password1, $this->attributes_orig); + if (sizeof($mailMessages) > 0) { + for ($i = 0; $i < sizeof($mailMessages); $i++) { + if ($mailMessages[$i][0] == 'ERROR') { + $return['errorsOccured'] = 'true'; + } + if (sizeof($mailMessages[$i]) == 2) { + $return['messages'] .= StatusMessage($mailMessages[$i][0], $mailMessages[$i][1], '', array(), true); + } + elseif (sizeof($mailMessages[$i]) == 3) { + $return['messages'] .= StatusMessage($mailMessages[$i][0], $mailMessages[$i][1], $mailMessages[$i][2], array(), true); + } + } + } + } if ($return['errorsOccured'] == 'false') { $return['messages'] .= StatusMessage('INFO', _('The new password will be stored in the directory after you save this account.'), '', array(), true); } diff --git a/lam/templates/lib/500_lam.js b/lam/templates/lib/500_lam.js index f46162f6..8de8609b 100644 --- a/lam/templates/lib/500_lam.js +++ b/lam/templates/lib/500_lam.js @@ -271,12 +271,14 @@ function passwordHandleInput(random, ajaxURL) { var pwd1 = jQuery('#passwordDialog').find('[name=newPassword1]').val(); var pwd2 = jQuery('#passwordDialog').find('[name=newPassword2]').val(); var forcePasswordChange = jQuery('input[name=lamForcePasswordChange]').attr('checked'); + var sendMail = jQuery('input[name=lamPasswordChangeSendMail]').attr('checked'); var pwdJSON = { "modules": modules, "password1": pwd1, "password2": pwd2, "random": random, - "forcePasswordChange": forcePasswordChange + "forcePasswordChange": forcePasswordChange, + "sendMail": sendMail }; // make AJAX call jQuery.post(ajaxURL, {jsonInput: pwdJSON}, function(data) {passwordHandleReply(data);}, 'json');