add user instructions and regex option

Signed-off-by: Achim Blanarsch <hallo@pxlfrk.de>
Signed-off-by: Achim <39946364+pxlfrk@users.noreply.github.com>
This commit is contained in:
Achim 2020-12-10 10:49:17 +01:00 committed by Joas Schilling
parent e4ba662086
commit 378f4e78fc
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
8 changed files with 99 additions and 0 deletions

View File

@ -33,6 +33,10 @@ input[type="submit"] {
text-decoration: underline; text-decoration: underline;
} }
.error {
margin-bottom: 15px;
}
.groupofone { .groupofone {
position: relative; position: relative;
} }

View File

@ -223,11 +223,14 @@ class RegisterController extends Controller {
return $this->validateSecretAndTokenErrorPage(); return $this->validateSecretAndTokenErrorPage();
} }
$additional_hint = $this->config->getAppValue('registration', 'additional_hint');
return new TemplateResponse('registration', 'form/user', [ return new TemplateResponse('registration', 'form/user', [
'email' => $registration->getEmail(), 'email' => $registration->getEmail(),
'email_is_login' => $this->config->getAppValue('registration', 'email_is_login', 'no') === 'yes', 'email_is_login' => $this->config->getAppValue('registration', 'email_is_login', 'no') === 'yes',
'username' => $username, 'username' => $username,
'message' => $message, 'message' => $message,
'additional_hint' => $additional_hint,
], 'guest'); ], 'guest');
} }

View File

@ -44,6 +44,9 @@ class SettingsController extends Controller {
* *
* @param string $registered_user_group all newly registered user will be put in this group * @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 string $allowed_domains Registrations are only allowed for E-Mailadresses with these domains
* @param string $additional_hint show Text at user-creation form
* @param string $email_verification_hint if filled embed Text in Verification mail send to user
* @param string $username_policy_regex optional regex to check usernames against a pattern
* @param bool|null $admin_approval_required newly registered users have to be validated by an admin * @param bool|null $admin_approval_required newly registered users have to be validated by an admin
* @param bool|null $email_is_login email address is forced as user id * @param bool|null $email_is_login email address is forced as user id
* @param bool|null $domains_is_blocklist is the domain list an allow or block list * @param bool|null $domains_is_blocklist is the domain list an allow or block list
@ -52,6 +55,9 @@ class SettingsController extends Controller {
*/ */
public function admin(string $registered_user_group, public function admin(string $registered_user_group,
string $allowed_domains, string $allowed_domains,
string $additional_hint,
string $email_verification_hint,
string $username_policy_regex,
?bool $admin_approval_required, ?bool $admin_approval_required,
?bool $email_is_login, ?bool $email_is_login,
?bool $domains_is_blocklist, ?bool $domains_is_blocklist,
@ -64,6 +70,34 @@ class SettingsController extends Controller {
$this->config->setAppValue($this->appName, 'allowed_domains', $allowed_domains); $this->config->setAppValue($this->appName, 'allowed_domains', $allowed_domains);
} }
// handle hints
if (($additional_hint === '') || ($additional_hint === null)) {
$this->config->deleteAppValue($this->appName, 'additional_hint');
} else {
$this->config->setAppValue($this->appName, 'additional_hint', $additional_hint);
}
if (($email_verification_hint === '') || ($email_verification_hint === null)) {
$this->config->deleteAppValue($this->appName, 'email_verification_hint');
} else {
$this->config->setAppValue($this->appName, 'email_verification_hint', $email_verification_hint);
}
//handle regex
if (($username_policy_regex === '') || ($username_policy_regex === null)) {
$this->config->deleteAppValue($this->appName, 'username_policy_regex');
} elseif ((@preg_match($username_policy_regex, null) === false)) {
// validate regex
return new DataResponse([
'data' => [
'message' => $this->l10n->t('Invalid username policy regex'),
],
'status' => 'error',
], Http::STATUS_BAD_REQUEST);
} else {
$this->config->setAppValue($this->appName, 'username_policy_regex', $username_policy_regex);
}
$this->config->setAppValue($this->appName, 'admin_approval_required', $admin_approval_required ? 'yes' : 'no'); $this->config->setAppValue($this->appName, 'admin_approval_required', $admin_approval_required ? 'yes' : 'no');
$this->config->setAppValue($this->appName, 'email_is_login', $email_is_login ? 'yes' : 'no'); $this->config->setAppValue($this->appName, 'email_is_login', $email_is_login ? 'yes' : 'no');
$this->config->setAppValue($this->appName, 'domains_is_blocklist', $domains_is_blocklist ? 'yes' : 'no'); $this->config->setAppValue($this->appName, 'domains_is_blocklist', $domains_is_blocklist ? 'yes' : 'no');

View File

@ -36,6 +36,7 @@ use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\Mail\IMailer; use OCP\Mail\IMailer;
use OCP\Util; use OCP\Util;
use OCP\IConfig;
class MailService { class MailService {
@ -51,15 +52,19 @@ class MailService {
private $groupManager; private $groupManager;
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var IConfig */
private $config;
public function __construct(IURLGenerator $urlGenerator, public function __construct(IURLGenerator $urlGenerator,
IMailer $mailer, IMailer $mailer,
Defaults $defaults, Defaults $defaults,
IL10N $l10n, IL10N $l10n,
IGroupManager $groupManager, IGroupManager $groupManager,
IConfig $config,
ILogger $logger) { ILogger $logger) {
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->config = $config;
$this->defaults = $defaults; $this->defaults = $defaults;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
@ -103,6 +108,12 @@ class MailService {
$body $body
); );
// if the parameter is set through the settings panel add to body text
$email_verification_hint = $this->config->getAppValue('registration', 'email_verification_hint');
if (!empty($email_verification_hint)) {
$template->addBodyText($email_verification_hint);
};
$template->addBodyText( $template->addBodyText(
$this->l10n->t('Verification code: %s', $registration->getToken()) $this->l10n->t('Verification code: %s', $registration->getToken())
); );

View File

@ -233,6 +233,13 @@ class RegistrationService {
throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); throw new RegistrationException($this->l10n->t('Please provide a valid user name.'));
} }
$regex = $this->config->getAppValue($this->appName, 'username_policy_regex', '');
if (!($regex === '')) {
if (preg_match($regex, $username) === 0) {
throw new RegistrationException($this->l10n->t('Please provide a valid user name.'));
}
}
if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { if ($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) {
throw new RegistrationException($this->l10n->t('The username you have chosen already exists.')); throw new RegistrationException($this->l10n->t('The username you have chosen already exists.'));
} }

View File

@ -56,9 +56,14 @@ class RegistrationSettings implements ISettings {
} }
$assignedGroups = $this->config->getAppValue($this->appName, 'registered_user_group', 'none'); $assignedGroups = $this->config->getAppValue($this->appName, 'registered_user_group', 'none');
// handle additional hint
$additional_hint = $this->config->getAppValue($this->appName, 'additional_hint', '');
$email_verification_hint = $this->config->getAppValue($this->appName, 'email_verification_hint', '');
// handle domains // handle domains
$allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); $allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
$username_policy_regex = $this->config->getAppValue($this->appName, 'username_policy_regex', '');
$adminApprovalRequired = $this->config->getAppValue($this->appName, 'admin_approval_required', 'no'); $adminApprovalRequired = $this->config->getAppValue($this->appName, 'admin_approval_required', 'no');
$emailIsLogin = $this->config->getAppValue($this->appName, 'email_is_login', 'no'); $emailIsLogin = $this->config->getAppValue($this->appName, 'email_is_login', 'no');
$domainsIsBlocklist = $this->config->getAppValue($this->appName, 'domains_is_blocklist', 'no'); $domainsIsBlocklist = $this->config->getAppValue($this->appName, 'domains_is_blocklist', 'no');
@ -68,6 +73,9 @@ class RegistrationSettings implements ISettings {
return new TemplateResponse('registration', 'admin', [ return new TemplateResponse('registration', 'admin', [
'groups' => $groupIds, 'groups' => $groupIds,
'current' => $assignedGroups, 'current' => $assignedGroups,
'additional_hint' => $additional_hint,
'email_verification_hint' => $email_verification_hint,
'username_policy_regex' => $username_policy_regex,
'allowed' => $allowedDomains, 'allowed' => $allowedDomains,
'approval_required' => $adminApprovalRequired, 'approval_required' => $adminApprovalRequired,
'email_is_login' => $emailIsLogin, 'email_is_login' => $emailIsLogin,

View File

@ -59,6 +59,30 @@ foreach ($_['groups'] as $group) {
<label for="email_is_login"><?php p($l->t('Force email as login name')); ?></label> <label for="email_is_login"><?php p($l->t('Force email as login name')); ?></label>
</p> </p>
<h3><?php p($l->t('Username policy')); ?></h3>
<p>
<label>
<input type="text" id="username_policy_regex" name="username_policy_regex" value="<?php p($_['username_policy_regex']);?>" placeholder="E.g.: /^[a-z-]+\.[a-z-]+$/">
</label>
</p>
<em><?php p($l->t('If configured usernames will be validated through the regular expression. If the validation fails the user is prompted with a generic error. Make sure your regex is working correctly.'));?></em>
<h3><?php p($l->t('User instructions')); ?></h3>
<em><?php p($l->t('Caution: The user instructions will not be translated and will therefore be displayed as configured below for all users regardless of their actual language.'));?></em>
<p>
<label>
<input type="text" id="additional_hint" name="additional_hint" value="<?php p($_['additional_hint']);?>" placeholder="Please create your username following the scheme 'firstname.lastname'.">
</label>
</p>
<em><?php p($l->t('Add additional user instructions (e.g. for choosing their usernames). If configured the text is displayed in the account creation step of the registration process.'));?></em>
<p>
<label>
<input type="text" id="email_verification_hint" name="email_verification_hint" value="<?php p($_['email_verification_hint']);?>" placeholder="Please create your username following the scheme 'firstname.lastname'.">
</label>
</p>
<em><?php p($l->t('Add additional user instructions (e.g. for choosing their usernames). If configured the text is embedded in the the verification-Email.'));?></em>
<h3><?php p($l->t('Admin approval')); ?></h3> <h3><?php p($l->t('Admin approval')); ?></h3>
<p> <p>
<input type="checkbox" id="admin_approval_required" class="checkbox" name="admin_approval_required" <?php if ($_['approval_required'] === 'yes') { <input type="checkbox" id="admin_approval_required" class="checkbox" name="admin_approval_required" <?php if ($_['approval_required'] === 'yes') {

View File

@ -15,6 +15,14 @@ script('registration', 'form');
<li><?php p($l->t('Welcome, you can create your account below.'));?></li> <li><?php p($l->t('Welcome, you can create your account below.'));?></li>
</ul> </ul>
<?php } ?> <?php } ?>
<?php if (!empty($_['additional_hint'])): ?>
<ul class="msg">
<li><?php p($_['additional_hint']); ?></li>
</ul>
<?php endif; ?>
<p class="grouptop"> <p class="grouptop">
<input type="email" name="email" id="email" value="<?php p($_['email']); ?>" disabled /> <input type="email" name="email" id="email" value="<?php p($_['email']); ?>" disabled />
<label for="email" class="infield"><?php p($_['email']); ?></label> <label for="email" class="infield"><?php p($_['email']); ?></label>