Send mail for new users

* supply mail address
* send mail with username and URL to that mail address
* option to temporary enable this feature
remotes/origin/fix-10825
Morris Jobke 10 years ago
parent 5b9c453071
commit 3b61f76ca0
  1. 7
      settings/application.php
  2. 88
      settings/controller/userscontroller.php
  3. 23
      settings/js/users/users.js
  4. 36
      settings/templates/email.new_user.php
  5. 10
      settings/templates/email.new_user_plain_text.php
  6. 6
      settings/templates/users/main.php
  7. 3
      settings/templates/users/part.createuser.php

@ -83,7 +83,12 @@ class Application extends App {
$c->query('UserSession'),
$c->query('Config'),
$c->query('IsAdmin'),
$c->query('L10N')
$c->query('L10N'),
$c->getServer()->getLogger(),
$c->query('Defaults'),
$c->query('Mail'),
$c->query('DefaultMailAddress'),
$c->getServer()->getURLGenerator()
);
});

@ -15,10 +15,13 @@ use OC\User\Manager;
use OC\User\User;
use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
@ -39,6 +42,16 @@ class UsersController extends Controller {
private $groupManager;
/** @var IConfig */
private $config;
/** @var ILogger */
private $log;
/** @var \OC_Defaults */
private $defaults;
/** @var \OC_Mail */
private $mail;
/** @var string */
private $fromMailAddress;
/** @var IURLGenerator */
private $urlGenerator;
/**
* @param string $appName
@ -49,6 +62,10 @@ class UsersController extends Controller {
* @param IConfig $config
* @param bool $isAdmin
* @param IL10N $l10n
* @param ILogger $log
* @param \OC_Defaults $defaults
* @param \OC_Mail $mail
* @param string $fromMailAddress
*/
public function __construct($appName,
IRequest $request,
@ -57,7 +74,12 @@ class UsersController extends Controller {
IUserSession $userSession,
IConfig $config,
$isAdmin,
IL10N $l10n) {
IL10N $l10n,
ILogger $log,
\OC_Defaults $defaults,
\OC_Mail $mail,
$fromMailAddress,
IURLGenerator $urlGenerator) {
parent::__construct($appName, $request);
$this->userManager = $userManager;
$this->groupManager = $groupManager;
@ -65,6 +87,11 @@ class UsersController extends Controller {
$this->config = $config;
$this->isAdmin = $isAdmin;
$this->l10n = $l10n;
$this->log = $log;
$this->defaults = $defaults;
$this->mail = $mail;
$this->fromMailAddress = $fromMailAddress;
$this->urlGenerator = $urlGenerator;
}
/**
@ -164,12 +191,23 @@ class UsersController extends Controller {
* @param string $username
* @param string $password
* @param array $groups
* @param string $email
* @return DataResponse
*
* TODO: Tidy up and write unit tests - code is mainly static method calls
*/
public function create($username, $password, array $groups) {
public function create($username, $password, array $groups=array(), $email='') {
if($email !== '' && !$this->mail->validateAddress($email)) {
return new DataResponse(
array(
'message' => (string)$this->l10n->t('Invalid mail address')
),
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
// TODO FIXME get rid of the static calls to OC_Subadmin
if (!$this->isAdmin) {
if (!empty($groups)) {
foreach ($groups as $key => $group) {
@ -195,13 +233,49 @@ class UsersController extends Controller {
}
if($user instanceof User) {
foreach( $groups as $groupName ) {
$group = $this->groupManager->get($groupName);
if($groups !== null) {
foreach( $groups as $groupName ) {
$group = $this->groupManager->get($groupName);
if(empty($group)) {
$group = $this->groupManager->createGroup($groupName);
}
$group->addUser($user);
}
}
/**
* Send new user mail only if a mail is set
*/
if($email !== '') {
$this->config->setUserValue($username, 'settings', 'email', $email);
// data for the mail template
$mailData = array(
'username' => $username,
'url' => $this->urlGenerator->getAbsoluteURL('/')
);
$mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
$mailContent = $mail->render();
$mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
$plainTextMailContent = $mail->render();
$subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]);
if(empty($group)) {
$group = $this->groupManager->createGroup($groupName);
try {
$this->mail->send(
$email,
$username,
$subject,
$mailContent,
$this->fromMailAddress,
$this->defaults->getName(),
1,
$plainTextMailContent);
} catch(\Exception $e) {
$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
}
$group->addUser($user);
}
}

@ -690,6 +690,7 @@ $(document).ready(function () {
event.preventDefault();
var username = $('#newusername').val();
var password = $('#newuserpassword').val();
var email = $('#newemail').val();
if ($.trim(username) === '') {
OC.dialogs.alert(
t('settings', 'A valid username must be provided'),
@ -702,14 +703,24 @@ $(document).ready(function () {
t('settings', 'Error creating user'));
return false;
}
var groups = $('#newusergroups').val();
if(!$('#CheckboxMailOnUserCreate').is(':checked')) {
email = '';
}
if ($('#CheckboxMailOnUserCreate').is(':checked') && $.trim(email) === '') {
OC.dialogs.alert(
t('settings', 'A valid email must be provided'),
t('settings', 'Error creating user'));
return false;
}
var groups = $('#newusergroups').val() || [];
$('#newuser').get(0).reset();
$.post(
OC.generateUrl('/settings/users/users'),
{
username: username,
password: password,
groups: groups
groups: groups,
email: email
},
function (result) {
if (result.groups) {
@ -769,6 +780,14 @@ $(document).ready(function () {
$("#userlist .userBackend").hide();
}
});
// Option to display/hide the "E-Mail" input field
$('#CheckboxMailOnUserCreate').click(function() {
if ($('#CheckboxMailOnUserCreate').is(':checked')) {
$("#newemail").show();
} else {
$("#newemail").hide();
}
});
// trigger loading of users on startup
UserList.update(UserList.currentGid);

@ -0,0 +1,36 @@
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr><td>
<table cellspacing="0" cellpadding="0" border="0" width="600px">
<tr>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>" width="20px">&nbsp;</td>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>">
<img src="<?php p(OC_Helper::makeURLAbsolute(image_path('', 'logo-mail.gif'))); ?>" alt="<?php p($theme->getName()); ?>"/>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<?php
print_unescaped($l->t('Hey there,<br><br>just letting you know that you now have an %s account.<br><br>Your username: %s<br>Access it: <a href="%s">%s</a><br><br>', array($theme->getName(), $_['username'], $_['url'], $_['url'])));
// TRANSLATORS term at the end of a mail
p($l->t('Cheers!'));
?>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">--<br>
<?php p($theme->getName()); ?> -
<?php p($theme->getSlogan()); ?>
<br><a href="<?php p($theme->getBaseUrl()); ?>"><?php p($theme->getBaseUrl());?></a>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</td></tr>
</table>

@ -0,0 +1,10 @@
<?php
print_unescaped($l->t("Hey there,\n\njust letting you know that you now have an %s account.\n\nYour username: %s\nAccess it: %s\n\n", array($theme->getName(), $_['username'], $_['url'])));
// TRANSLATORS term at the end of a mail
p($l->t("Cheers!"));
?>
--
<?php p($theme->getName() . ' - ' . $theme->getSlogan()); ?>
<?php print_unescaped("\n".$theme->getBaseUrl());

@ -62,6 +62,12 @@ translation('settings');
<?php p($l->t('Show user backend')) ?>
</label>
</p>
<p>
<input type="checkbox" name="MailOnUserCreate" value="MailOnUserCreate" id="CheckboxMailOnUserCreate">
<label for="CheckboxMailOnUserCreate">
<?php p($l->t('Send mail to new user')) ?>
</label>
</p>
</div>
</div>
</div>

@ -7,6 +7,9 @@
type="password" id="newuserpassword"
placeholder="<?php p($l->t('Password'))?>"
autocomplete="off" autocapitalize="off" autocorrect="off" />
<input id="newemail" type="text" style="display:none"
placeholder="<?php p($l->t('E-Mail'))?>"
autocomplete="off" autocapitalize="off" autocorrect="off" />
<select
class="groupsselect" id="newusergroups" data-placeholder="groups"
title="<?php p($l->t('Groups'))?>" multiple="multiple">

Loading…
Cancel
Save