diff options
-rw-r--r-- | settings/application.php | 13 | ||||
-rw-r--r-- | settings/controller/userscontroller.php | 88 | ||||
-rw-r--r-- | settings/js/users/users.js | 23 | ||||
-rw-r--r-- | settings/templates/email.new_user.php | 36 | ||||
-rw-r--r-- | settings/templates/email.new_user_plain_text.php | 10 | ||||
-rw-r--r-- | settings/templates/users/main.php | 6 | ||||
-rw-r--r-- | settings/templates/users/part.createuser.php | 3 | ||||
-rw-r--r-- | tests/settings/controller/userscontrollertest.php | 70 |
8 files changed, 239 insertions, 10 deletions
diff --git a/settings/application.php b/settings/application.php index 0a80bd8b1e7..74d021c5bf3 100644 --- a/settings/application.php +++ b/settings/application.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->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(); + }); } } diff --git a/settings/controller/userscontroller.php b/settings/controller/userscontroller.php index c25989af1a9..0349a4c3d16 100644 --- a/settings/controller/userscontroller.php +++ b/settings/controller/userscontroller.php @@ -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); } } diff --git a/settings/js/users/users.js b/settings/js/users/users.js index d910e1ec129..e0eb5ff1601 100644 --- a/settings/js/users/users.js +++ b/settings/js/users/users.js @@ -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, diff --git a/settings/templates/email.new_user.php b/settings/templates/email.new_user.php new file mode 100644 index 00000000000..74149632cb8 --- /dev/null +++ b/settings/templates/email.new_user.php @@ -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> diff --git a/settings/templates/email.new_user_plain_text.php b/settings/templates/email.new_user_plain_text.php new file mode 100644 index 00000000000..79559a87020 --- /dev/null +++ b/settings/templates/email.new_user_plain_text.php @@ -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()); diff --git a/settings/templates/users/main.php b/settings/templates/users/main.php index c32c8df6809..2004c10b9ac 100644 --- a/settings/templates/users/main.php +++ b/settings/templates/users/main.php @@ -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> diff --git a/settings/templates/users/part.createuser.php b/settings/templates/users/part.createuser.php index d3ebbfb987a..9d9886f694c 100644 --- a/settings/templates/users/part.createuser.php +++ b/settings/templates/users/part.createuser.php @@ -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"> diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php index 0ac6d3f0c01..207943c4c87 100644 --- a/tests/settings/controller/userscontrollertest.php +++ b/tests/settings/controller/userscontrollertest.php @@ -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()); + } + } |