Merge pull request #12870 from owncloud/send-mail-new-user
Send mail for new users
This commit is contained in:
commit
5327b80430
|
@ -83,7 +83,12 @@ class Application extends App {
|
|||
$c->query('UserSession'),
|
||||
$c->query('Config'),
|
||||
$c->query('IsAdmin'),
|
||||
$c->query('L10N')
|
||||
$c->query('L10N'),
|
||||
$c->query('Logger'),
|
||||
$c->query('Defaults'),
|
||||
$c->query('Mail'),
|
||||
$c->query('DefaultMailAddress'),
|
||||
$c->query('URLGenerator')
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -134,5 +139,11 @@ class Application extends App {
|
|||
$container->registerService('DefaultMailAddress', function(IContainer $c) {
|
||||
return Util::getDefaultEmailAddress('no-reply');
|
||||
});
|
||||
$container->registerService('Logger', function(IContainer $c) {
|
||||
return $c->query('ServerContainer')->getLogger();
|
||||
});
|
||||
$container->registerService('URLGenerator', function(IContainer $c) {
|
||||
return $c->query('ServerContainer')->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,6 +233,7 @@ class UsersController extends Controller {
|
|||
}
|
||||
|
||||
if($user instanceof User) {
|
||||
if($groups !== null) {
|
||||
foreach( $groups as $groupName ) {
|
||||
$group = $this->groupManager->get($groupName);
|
||||
|
||||
|
@ -204,6 +243,41 @@ class UsersController extends Controller {
|
|||
$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()]);
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new DataResponse(
|
||||
array(
|
||||
|
|
|
@ -695,6 +695,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'),
|
||||
|
@ -707,14 +708,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) {
|
||||
|
@ -774,6 +785,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();
|
||||
}
|
||||
});
|
||||
|
||||
// calculate initial limit of users to load
|
||||
var initialUserCountLimit = 20,
|
||||
|
|
|
@ -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"> </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"> </td></tr>
|
||||
<tr>
|
||||
<td width="20px"> </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"> </td></tr>
|
||||
<tr>
|
||||
<td width="20px"> </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"> </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">
|
||||
|
|
|
@ -45,6 +45,16 @@ class UsersControllerTest extends \Test\TestCase {
|
|||
->will($this->returnCallback(function($text, $parameters = array()) {
|
||||
return vsprintf($text, $parameters);
|
||||
}));
|
||||
$this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->container['Mail'] = $this->getMockBuilder('\OC_Mail')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->container['DefaultMailAddress'] = 'no-reply@owncloud.com';
|
||||
$this->container['Logger'] = $this->getMockBuilder('\OCP\ILogger')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator')
|
||||
->disableOriginalConstructor()->getMock();
|
||||
|
||||
$this->usersController = $this->container['UsersController'];
|
||||
|
||||
}
|
||||
|
@ -473,4 +483,64 @@ class UsersControllerTest extends \Test\TestCase {
|
|||
$this->assertEquals($expectedResponse, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* test if an invalid mail result in a failure response
|
||||
*/
|
||||
public function testCreateUnsuccessfulWithInvalidEMail() {
|
||||
/**
|
||||
* FIXME: Disabled due to missing DI on mail class.
|
||||
* TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged.
|
||||
*/
|
||||
$this->markTestSkipped('Disable test until OC_Mail is rewritten.');
|
||||
|
||||
$this->container['Mail']
|
||||
->expects($this->once())
|
||||
->method('validateAddress')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$expectedResponse = new DataResponse(
|
||||
array(
|
||||
'message' => 'Invalid mail address'
|
||||
),
|
||||
Http::STATUS_UNPROCESSABLE_ENTITY
|
||||
);
|
||||
$response = $this->usersController->create('foo', 'password', array(), 'invalidMailAdress');
|
||||
$this->assertEquals($expectedResponse, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* test if a valid mail result in a successful mail send
|
||||
*/
|
||||
public function testCreateSuccessfulWithValidEMail() {
|
||||
/**
|
||||
* FIXME: Disabled due to missing DI on mail class.
|
||||
* TODO: Re-enable when https://github.com/owncloud/core/pull/12085 is merged.
|
||||
*/
|
||||
$this->markTestSkipped('Disable test until OC_Mail is rewritten.');
|
||||
|
||||
$this->container['Mail']
|
||||
->expects($this->once())
|
||||
->method('validateAddress')
|
||||
->will($this->returnValue(true));
|
||||
$this->container['Mail']
|
||||
->expects($this->once())
|
||||
->method('send')
|
||||
->with(
|
||||
$this->equalTo('validMail@Adre.ss'),
|
||||
$this->equalTo('foo'),
|
||||
$this->anything(),
|
||||
$this->anything(),
|
||||
$this->anything(),
|
||||
$this->equalTo('no-reply@owncloud.com'),
|
||||
$this->equalTo(1),
|
||||
$this->anything()
|
||||
);
|
||||
$this->container['Logger']
|
||||
->expects($this->never())
|
||||
->method('error');
|
||||
|
||||
$response = $this->usersController->create('foo', 'password', array(), 'validMail@Adre.ss');
|
||||
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue