From 651439c233288bd2a91a146410b54e9dc834f749 Mon Sep 17 00:00:00 2001
From: Neraste
Date: Sat, 21 Oct 2017 14:02:38 +0200
Subject: [PATCH] Option for disabling new registered users
---
controller/settingscontroller.php | 19 +++++++++++++++++--
service/mailservice.php | 22 +++++++++++++++++-----
service/registrationservice.php | 9 ++++++---
templates/admin.php | 5 +++++
4 files changed, 45 insertions(+), 10 deletions(-)
diff --git a/controller/settingscontroller.php b/controller/settingscontroller.php
index f01eadc..0252317 100644
--- a/controller/settingscontroller.php
+++ b/controller/settingscontroller.php
@@ -47,14 +47,21 @@ class SettingsController extends Controller {
*
* @param string $registered_user_group all newly registered user will be put in this group
* @param string $allowed_domains Registrations are only allowed for E-Mailadresses with these domains
+ * @param bool $admin_approval_required newly registered users have to be validated by an admin
* @return DataResponse
*/
- public function admin($registered_user_group, $allowed_domains) {
+ public function admin($registered_user_group, $allowed_domains, $admin_approval_required) {
+ // handle domains
if ( ( $allowed_domains==='' ) || ( $allowed_domains === NULL ) ){
$this->config->deleteAppValue($this->appName, 'allowed_domains');
}else{
$this->config->setAppValue($this->appName, 'allowed_domains', $allowed_domains);
}
+
+ // handle admin validation
+ $this->config->setAppValue($this->appName, 'admin_approval_required', $admin_approval_required ? "yes" : "no");
+
+ // handle groups
$groups = $this->groupmanager->search('');
$group_id_list = array();
foreach ( $groups as $group ) {
@@ -92,17 +99,25 @@ class SettingsController extends Controller {
* @return TemplateResponse
*/
public function displayPanel() {
+ // handle groups
$groups = $this->groupmanager->search('');
$group_id_list = [];
foreach ( $groups as $group ) {
$group_id_list[] = $group->getGid();
}
$current_value = $this->config->getAppValue($this->appName, 'registered_user_group', 'none');
+
+ // handle domains
$allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
+
+ // handle admin validation
+ $admin_approval_required = $this->config->getAppValue($this->appName, 'admin_approval_required', "no");
+
return new TemplateResponse('registration', 'admin', [
'groups' => $group_id_list,
'current' => $current_value,
- 'allowed' => $allowed_domains
+ 'allowed' => $allowed_domains,
+ 'approval_required' => $admin_approval_required
], '');
}
}
diff --git a/service/mailservice.php b/service/mailservice.php
index 47acb75..39172a1 100644
--- a/service/mailservice.php
+++ b/service/mailservice.php
@@ -106,8 +106,9 @@ class MailService {
/**
* @param string $userId
+ * @param bool $userIsEnabled
*/
- public function notifyAdmins($userId) {
+ public function notifyAdmins($userId, $userIsEnabled) {
// Notify admin
$admin_users = $this->groupManager->get('admin')->getUsers();
$to_arr = array();
@@ -118,7 +119,7 @@ class MailService {
}
}
try {
- $this->sendNewUserNotifEmail($to_arr, $userId);
+ $this->sendNewUserNotifEmail($to_arr, $userId, $userIsEnabled);
} catch (\Exception $e) {
$this->logger->error('Sending admin notification email failed: '. $e->getMessage());
}
@@ -128,16 +129,27 @@ class MailService {
* Sends new user notification email to admin
* @param array $to
* @param string $username the new user
+ * @param bool $userIsEnabled the new user account is enabled
* @throws \Exception
*/
- private function sendNewUserNotifEmail(array $to, $username) {
+ private function sendNewUserNotifEmail(array $to, $username, $userIsEnabled) {
$template_var = [
'user' => $username,
'sitename' => $this->defaults->getName()
];
- $html_template = new TemplateResponse('registration', 'email.newuser.disabled_html', $template_var, 'blank');
+
+ // handle user enableness
+ if ($userIsEnabled) {
+ $html_template_file = 'email.newuser_html';
+ $plaintext_template_file = 'email.newuser_plaintext';
+ } else {
+ $html_template_file = 'email.newuser.disabled_html';
+ $plaintext_template_file = 'email.newuser.disabled_plaintext';
+ }
+
+ $html_template = new TemplateResponse('registration', $html_template_file, $template_var, 'blank');
$html_part = $html_template->render();
- $plaintext_template = new TemplateResponse('registration', 'email.newuser.disabled_plaintext', $template_var, 'blank');
+ $plaintext_template = new TemplateResponse('registration', $plaintext_template_file, $template_var, 'blank');
$plaintext_part = $plaintext_template->render();
$subject = $this->l10n->t('A new user "%s" has created an account on %s', [$username, $this->defaults->getName()]);
diff --git a/service/registrationservice.php b/service/registrationservice.php
index 37a004f..83a4d57 100644
--- a/service/registrationservice.php
+++ b/service/registrationservice.php
@@ -293,8 +293,11 @@ class RegistrationService {
}
}
- // Disable user unconditionaly
- $user->setEnabled(false);
+ // disable user if this is requested by config
+ $admin_approval_required = $this->config->getAppValue($this->appName, 'admin_approval_required', "no");
+ if ($admin_approval_required) {
+ $user->setEnabled(false);
+ }
// Delete pending registration if no client secret is stored
if($registration->getClientSecret() === null) {
@@ -304,7 +307,7 @@ class RegistrationService {
}
}
- $this->mailService->notifyAdmins($userId);
+ $this->mailService->notifyAdmins($userId, $user->isEnabled());
return $user;
}
diff --git a/templates/admin.php b/templates/admin.php
index 8695603..8641035 100644
--- a/templates/admin.php
+++ b/templates/admin.php
@@ -23,4 +23,9 @@ foreach ( $_['groups'] as $group ) {
t('Enter a semicolon-separated list of allowed domains. Example: owncloud.com;github.com'));?>
+
+
+