summaryrefslogtreecommitdiffstats
path: root/settings
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2014-12-16 09:08:38 +0100
committerMorris Jobke <hey@morrisjobke.de>2014-12-16 09:10:22 +0100
commit3b61f76ca0eb9ad9487fa2ab64dda97e51f9df57 (patch)
tree2eede9268660f3b32646ae375e37cd542d4f36a2 /settings
parent5b9c453071fe900529cd26b88fbc681d8b153b43 (diff)
downloadnextcloud-server-3b61f76ca0eb9ad9487fa2ab64dda97e51f9df57.tar.gz
nextcloud-server-3b61f76ca0eb9ad9487fa2ab64dda97e51f9df57.zip
Send mail for new users
* supply mail address * send mail with username and URL to that mail address * option to temporary enable this feature
Diffstat (limited to 'settings')
-rw-r--r--settings/application.php7
-rw-r--r--settings/controller/userscontroller.php88
-rw-r--r--settings/js/users/users.js23
-rw-r--r--settings/templates/email.new_user.php36
-rw-r--r--settings/templates/email.new_user_plain_text.php10
-rw-r--r--settings/templates/users/main.php6
-rw-r--r--settings/templates/users/part.createuser.php3
7 files changed, 163 insertions, 10 deletions
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 @@
+<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>
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">