Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feature(Admin/Cli): send mail to user with new random pw
Browse files Browse the repository at this point in the history
... and email-server data
  • Loading branch information
pschuele committed May 12, 2021
1 parent 714a2be commit 9e615f5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
13 changes: 1 addition & 12 deletions tine20/Admin/Frontend/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ protected function _readCsv($csv, $firstColIsKey = false)
*/
protected function _setPasswordsForUsers(Zend_Console_Getopt $opts, $users, $pw = null, $sendmail = false)
{
$smtp = Tinebase_Notification_Factory::getBackend(Tinebase_Notification_Factory::SMTP);
$pwCsv = '';

foreach ($users as $userdata) {
Expand Down Expand Up @@ -524,17 +523,7 @@ protected function _setPasswordsForUsers(Zend_Console_Getopt $opts, $users, $pw
Tinebase_User::getInstance()->setPassword($user, $newPw);
if ($sendmail && ! empty($userdata[1])) {
echo "sending mail to " . $userdata[1] . "\n";
$smtp->send(
Tinebase_Core::getUser(),
new Addressbook_Model_Contact([
'email' => $userdata[1],
'n_fn' => $userdata[0],
]),
// @todo translate
'Neues Tine 2.0/E-Mail Passwort',
'Ihr neues Passwort lautet: ' . $newPw . "\r\n"
. Tinebase_Config::getInstance()->get(Tinebase_Config::TINE20_URL)
);
Tinebase_User::getInstance()->sendPasswordChangeMail($user, $newPw, $userdata[1]);
}
} else {
echo "--DRYRUN-- setting pw for user " . $userdata[0] . "\n";
Expand Down
56 changes: 55 additions & 1 deletion tine20/Tinebase/User/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @package Tinebase
* @subpackage User
*/

abstract class Tinebase_User_Abstract implements Tinebase_User_Interface
{
/**
Expand Down Expand Up @@ -782,8 +781,63 @@ abstract public function deleteUsers(array $_accountIds);
*/
abstract public function getMultiple($_id, $_accountClass = 'Tinebase_Model_User');

/**
* @return string
*/
public function getModel()
{
return Tinebase_Model_FullUser::class;
}

/**
* send welcome / password update mail to a user
*
* @param $user
* @param $newPw
* @param null $email
* @throws Tinebase_Exception_InvalidArgument
* @throws Tinebase_Exception_NotFound
* @throws Tinebase_Exception_Record_DefinitionFailure
* @throws Tinebase_Exception_Record_Validation
* @throws Zend_Mail_Protocol_Exception
*/
public function sendPasswordChangeMail($user, $newPw, $email = null)
{
$recipient = Addressbook_Controller_Contact::getInstance()->getContactByUserId($user);
if ($email) {
$recipient->email = $email;
}

// @todo translate / add to config
$subject = 'Neues Tine 2.0 / E-Mail Passwort';
// TODO add only if configured
$emailSettings = "\r\nZugangsdaten für den Zugriff auf das Mailkonto über einen anderen E-Mail-Client:\r\n"
. "IMAP: Host: {{ imap.host }} Port: {{ imap.port }}\r\n"
. "SMTP: Host: {{ smtp.hostname }} Port: {{ smtp.port }}\r\n"
. "Benutzername: Ihre E-Mail Adresse ({{ email }})\r\n";
$message = "Guten Tag!\r\n\r\nIhr neues Tine 2.0 / E-Mail-Passwort lautet: {{ password }}\r\n" .
"Bitte ändern Sie es gleich nach dem ersten Login.\r\n" .
"\r\nTine 2.0 URL: {{ tine20url }}\r\n" .
$emailSettings .
"\r\nEinen schönen Tag wünscht: Ihr Metaways Team\r\n";

$tbConfig = Tinebase_Config::getInstance();
$twig = new Twig_Environment(new Twig_Loader_Array());

$message = $twig->createTemplate($message)->render([
'tine20url' => $tbConfig->get(Tinebase_Config::TINE20_URL),
'smtp' => $tbConfig->get(Tinebase_Config::SMTP)->toArray(),
'imap' => $tbConfig->get(Tinebase_Config::IMAP)->toArray(),
'password' => $newPw,
'email' => $recipient->email,
]);

$smtp = Tinebase_Notification_Factory::getBackend(Tinebase_Notification_Factory::SMTP);
$smtp->send(
Tinebase_Core::getUser(),
$recipient,
$subject,
$message
);
}
}

0 comments on commit 9e615f5

Please sign in to comment.