Refine configuration for account disabled notification.

This commit is contained in:
Richard Henkenjohann committed 2019-09-12 21:49:20 +09:00
1 parent 1496b3412d
commit 2851fa5b92
10 files changed
+108 -14

No files matched your search

+11
View File
@@ -71,6 +71,17 @@ Parameter | Purpose
the page root.
2. You can now force members to change their passwords by ticking the corresponding checkbox in the member edit-mask.
### Notifications
#### Account disabled
The bundle triggers a notification whenever an account gets disabled (only when using `invalid_attempts` options). This allows you to inform both website admin and account holder.
To create such notification, you can create a notification of type "Account disabled" in the notification center. This notification is chosen automatically for both back and front end users. If you only have one notification of this type, no further action is needed.
However, you have the possibility to select a dedicated notification:
- To use a dedicated notification for users and members, set the `nc_account_disabled` parameter in the above mentioned config tree. Just set the notification ID as parameter value.
- To use a dedicated notification for members on a certain page, you can select a notification in the page root config.
## Add your own password validator
@@ -30,6 +30,7 @@ final class Configuration implements ConfigurationInterface
->integerNode('min_length')->end()
->integerNode('max_length')->end()
->integerNode('invalid_attempts')->end()
->integerNode('nc_account_disabled')->end()
->integerNode('password_history')->end()
->integerNode('change_days')->end()
->scalarNode('other_chars')->end()
+24 -5
View File
@@ -14,7 +14,9 @@ declare(strict_types=1);
namespace Terminal42\PasswordValidationBundle\EventListener;
use Contao\BackendUser;
use Contao\FrontendUser;
use Contao\Message;
use Contao\PageModel;
use Contao\User;
use NotificationCenter\Model\Notification;
use Terminal42\PasswordValidationBundle\Validation\ValidationConfiguration;
@@ -57,10 +59,7 @@ final class InvalidAttemptsListener
Message::addError($this->translate('accountDisabled'));
/** @var Notification $notification */
// TODO add field to tl_page (root pages) to configure the message. If none found or in the backend, fall back to this one.
$notification = Notification::findOneBy('type', 'account_disabled');
if (null !== $notification) {
if (null !== $notification = $this->getNotification($user)) {
$notification->send($this->getNotificationTokens($username, $user));
}
}
@@ -94,11 +93,31 @@ final class InvalidAttemptsListener
return null;
}
private function getNotification(User $user): ?Notification
{
if (($user instanceof FrontendUser) && null !== $rootPage = PageModel::findByPk($GLOBALS['objPage']->rootId)) {
$notification = Notification::findByPk($rootPage->nc_account_disabled);
}
$configuration = $this->configuration->getConfiguration(\get_class($user));
if (null === $notification && $notificationId = $configuration['nc_account_disabled']) {
$notification = Notification::findByPk($notificationId);
}
if (null === $notification) {
$notification = Notification::findOneBy('type', 'account_disabled');
}
return $notification;
}
private function getNotificationTokens(string $username, User $user): array
{
$tokens = [];
$tokens['username'] = $username;
$tokens['user_class'] = \get_class($user);
$tokens['username'] = $username;
foreach ($user->getData() as $k => $v) {
$tokens['user_'.$k] = $v;
}
+3 -1
View File
@@ -17,7 +17,7 @@ $GLOBALS['TL_HOOKS']['addCustomRegexp'][] =
['terminal42_password_validation.listener.regexp', 'onAddCustomRegexp'];
$GLOBALS['TL_HOOKS']['setNewPassword'][] =
['terminal42_password_validation.listener.history_log', 'onSetNewPassword'];
$GLOBALS['TL_HOOKS']['getPageLayout'][] =
$GLOBALS['TL_HOOKS']['getPageLayout'][] =
['terminal42_password_validation.listener.password_change_frontend', 'onGetPageLayout'];
$GLOBALS['TL_HOOKS']['setNewPassword'][] =
['terminal42_password_validation.listener.password_change_frontend', 'onSetNewPassword'];
@@ -43,10 +43,12 @@ $GLOBALS['NOTIFICATION_CENTER']['NOTIFICATION_TYPE'] = array_merge(
],
'email_text' => [
'username',
'user_class',
'user_*',
],
'email_html' => [
'username',
'user_class',
'user_*',
],
'email_sender_name' => [
+16 -1
View File
@@ -16,7 +16,7 @@ $GLOBALS['TL_DCA']['tl_page']['config']['onload_callback'][] =
$GLOBALS['TL_DCA']['tl_page']['palettes']['root'] = str_replace(
';{publish_legend}',
';{pwChangeLegend},pwChangePage;{publish_legend}',
';{pwChangeLegend},pwChangePage,nc_account_disabled;{publish_legend}',
$GLOBALS['TL_DCA']['tl_page']['palettes']['root']
);
@@ -29,3 +29,18 @@ $GLOBALS['TL_DCA']['tl_page']['fields']['pwChangePage'] = [
'sql' => "int(10) unsigned NOT NULL default '0'",
'relation' => ['type' => 'hasOne', 'load' => 'eager'],
];
$GLOBALS['TL_DCA']['tl_page']['fields']['nc_account_disabled'] = [
'label' => &$GLOBALS['TL_LANG']['tl_page']['nc_account_disabled'],
'exclude' => true,
'inputType' => 'select',
'options_callback' => static function () {
return \Contao\System::getContainer()
->get('database_connection')
->executeQuery('SELECT id,title FROM tl_nc_notification WHERE type=\'account_disabled\' ORDER BY title')
->fetchAll(PDO::FETCH_KEY_PAIR);
},
'eval' => ['includeBlankOption' => true, 'chosen' => true, 'tl_class' => 'w50 clr'],
'sql' => "int(10) unsigned NOT NULL default '0'",
'relation' => ['type' => 'hasOne', 'load' => 'lazy', 'table' => 'tl_nc_notification'],
];
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>
<xliff version="1.1">
<file datatype="php" original="src/Resources/contao/languages/de/tl_nc_notification.php" source-language="en" target-language="de">
<body>
<trans-unit id="tl_nc_notification.type.password_validation">
<source>Password validation</source>
<target>Password-Validierung</target>
</trans-unit>
<trans-unit id="tl_nc_notification.type.account_disabled.0">
<source>Account disabled</source>
<target>Account deaktiviert</target>
</trans-unit>
</body>
</file>
</xliff>
+14 -2
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" ?>
<xliff version="1.1">
<file datatype="php" original="src/Resources/contao/languages/de/tl_page.php" source-language="en" target-language="de">
<file datatype="php" original="src/Resources/contao/languages/de/tl_page.php" source-language="en"
target-language="de">
<body>
<trans-unit id="tl_page.pwChangeLegend">
<source>Password change</source>
@@ -12,7 +13,18 @@
</trans-unit>
<trans-unit id="tl_page.pwChangePage.1">
<source>Redirect on this page to force the member to change their password.</source>
<target>Der Nutzende wird auf diese Seite weitergeleitet, wenn das Passwort geändert werden muss.</target>
<target>Der Nutzende wird auf diese Seite weitergeleitet, wenn das Passwort geändert werden muss.
</target>
</trans-unit>
<trans-unit id="tl_page.nc_account_disabled.0">
<source>Account disabled notification</source>
<target>Benachrichtigung bei deaktivertem Account</target>
</trans-unit>
<trans-unit id="tl_page.nc_account_disabled.1">
<source>Select the notification to trigger when a member account gets disabled.</source>
<target>Wählen Sie die Benachrichtigung aus, die gesendet werden soll, sobald ein Mitgliedsaccount
deaktiviert wird.
</target>
</trans-unit>
</body>
</file>
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>
<xliff version="1.1">
<file datatype="php" original="src/Resources/contao/languages/en/tl_nc_notification.php" source-language="en">
<body>
<trans-unit id="tl_nc_notification.type.password_validation">
<source>Password validation</source>
</trans-unit>
<trans-unit id="tl_nc_notification.type.account_disabled.0">
<source>Account disabled</source>
</trans-unit>
</body>
</file>
</xliff>
@@ -11,6 +11,12 @@
<trans-unit id="tl_page.pwChangePage.1">
<source>Redirect on this page to force the member to change their password.</source>
</trans-unit>
<trans-unit id="tl_page.nc_account_disabled.0">
<source>Account disabled notification</source>
</trans-unit>
<trans-unit id="tl_page.nc_account_disabled.1">
<source>Select the notification to trigger when a member account gets disabled.</source>
</trans-unit>
</body>
</file>
</xliff>
@@ -62,11 +62,11 @@ final class RequiredCharacters implements PasswordValidatorInterface
continue;
}
$errors[] = new ValidatorException(sprintf($this->translate('required.' . $category), $minimum));
$errors[] = new ValidatorException(sprintf($this->translate('required.'.$category), $minimum));
}
}
if (count($errors) > 1) {
if (\count($errors) > 1) {
throw new ValidatorException(
sprintf(
$this->translate('required.summary'),
@@ -79,7 +79,7 @@ final class RequiredCharacters implements PasswordValidatorInterface
);
}
if (count($errors) > 0) {
if (\count($errors) > 0) {
throw array_pop($errors);
}
@@ -108,7 +108,7 @@ final class RequiredCharacters implements PasswordValidatorInterface
return null;
}
return \strlen(preg_replace('/[^' . $chars . ']+/', '', $string));
return \strlen(preg_replace('/[^'.$chars.']+/', '', $string));
default:
return null;
@@ -125,7 +125,7 @@ final class RequiredCharacters implements PasswordValidatorInterface
$return = '';
foreach (array_unique(preg_split('//u', $chars, -1, PREG_SPLIT_NO_EMPTY)) as $char) {
$return .= '\\' . $char;
$return .= '\\'.$char;
}
return $return;