diff --git a/settings/application.php b/settings/application.php index 0a80bd8b1e7..b088c2f937b 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->getServer()->getLogger(), + $c->query('Defaults'), + $c->query('Mail'), + $c->query('DefaultMailAddress'), + $c->getServer()->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 62e18d1be04..91bc34bb754 100644 --- a/settings/js/users/users.js +++ b/settings/js/users/users.js @@ -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); 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 @@ + + +
+ + + + + + + + + + + + + + + + + + +
  + <?php p($theme->getName()); ?> +
 
  + t('Hey there,

just letting you know that you now have an %s account.

Your username: %s
Access it: %s

', array($theme->getName(), $_['username'], $_['url'], $_['url']))); + + // TRANSLATORS term at the end of a mail + p($l->t('Cheers!')); + ?> +
 
 --
+ getName()); ?> - + getSlogan()); ?> +
getBaseUrl());?> +
 
+
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 @@ +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!")); +?> + + -- +getName() . ' - ' . $theme->getSlogan()); ?> +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'); t('Show user backend')) ?>

+

+ + +

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="t('Password'))?>" autocomplete="off" autocapitalize="off" autocorrect="off" /> +