Issue #51 added getSupportedDrivers()

This commit is contained in:
djmaze 2021-03-02 14:23:45 +01:00
parent 4d80671bf3
commit c0e61bb0a2

View file

@ -24,19 +24,19 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$this->addTemplate('templates/SettingsChangePassword.html'); $this->addTemplate('templates/SettingsChangePassword.html');
} }
public function Supported() : string protected function getSupportedDrivers(bool $all = false) : iterable
{ {
$oConfig = $this->Config(); // foreach (\glob(__DIR__ . '/../change-password-*', GLOB_ONLYDIR) as $file) {
foreach (\glob(__DIR__ . '/drivers/*.php') as $file) { foreach (\glob(__DIR__ . '/drivers/*.php') as $file) {
$name = \basename($file, '.php');
if ($oConfig->Get('plugin', "driver_{$name}_enabled", false)) {
require_once $file;
$class = 'ChangePasswordDriver' . $name;
$name = $class::NAME;
try try
{ {
$name = \basename($file, '.php');
if ($all || $this->Config()->Get('plugin', "driver_{$name}_enabled", false)) {
require_once $file;
$class = 'ChangePasswordDriver' . $name;
if ($class::isSupported()) { if ($class::isSupported()) {
return ''; yield $name => $class;
}
} }
} }
catch (\Throwable $oException) catch (\Throwable $oException)
@ -44,17 +44,19 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
} }
} }
} }
public function Supported() : string
{
foreach ($this->getSupportedDrivers() as $class) {
return '';
}
return 'There are no change-password drivers enabled'; return 'There are no change-password drivers enabled';
} }
public function configMapping() : array public function configMapping() : array
{ {
$result = []; $result = [];
foreach (\glob(__DIR__ . '/drivers/*.php') as $file) { foreach ($this->getSupportedDrivers(true) as $name => $class) {
require_once $file;
$name = \basename($file, '.php');
$class = 'ChangePasswordDriver' . $name;
if ($class::isSupported()) {
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled") $result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled")
->SetLabel('Enable ' . $class::NAME) ->SetLabel('Enable ' . $class::NAME)
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL) ->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
@ -62,11 +64,10 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_allowed_emails") $result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_allowed_emails")
->SetLabel('Allowed emails') ->SetLabel('Allowed emails')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net') ->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@example.net user2@example1.net *@example2.net')
->SetDefaultValue('*'); ->SetDefaultValue('*');
$result = \array_merge($result, \call_user_func("{$class}::configMapping")); $result = \array_merge($result, \call_user_func("{$class}::configMapping"));
} }
}
return $result; return $result;
} }
@ -97,17 +98,11 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$bResult = false; $bResult = false;
$oConfig = $this->Config(); $oConfig = $this->Config();
foreach (\glob(__DIR__ . '/drivers/*.php') as $file) { foreach ($this->getSupportedDrivers() as $name => $class) {
$name = \basename($file, '.php'); if (\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $oConfig->Get('plugin', "driver_{$name}_allowed_emails"))) {
if ($oConfig->Get('plugin', "driver_{$name}_enabled", false)
&& \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $oConfig->Get('plugin', "driver_{$name}_allowed_emails"))
) {
require_once $file;
$class = 'ChangePasswordDriver' . $name;
$name = $class::NAME; $name = $class::NAME;
try try
{ {
if ($class::isSupported()) {
$oDriver = new $class( $oDriver = new $class(
$oConfig(), $oConfig(),
$oActions->Logger() $oActions->Logger()
@ -120,7 +115,6 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$this->oLogger->Write("{$name} password changed for {$oAccount->Email()}"); $this->oLogger->Write("{$name} password changed for {$oAccount->Email()}");
} }
} }
}
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
if ($this->oLogger) { if ($this->oLogger) {