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,25 +24,31 @@ 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'); try
if ($oConfig->Get('plugin', "driver_{$name}_enabled", false)) { {
require_once $file; $name = \basename($file, '.php');
$class = 'ChangePasswordDriver' . $name; if ($all || $this->Config()->Get('plugin', "driver_{$name}_enabled", false)) {
$name = $class::NAME; require_once $file;
try $class = 'ChangePasswordDriver' . $name;
{
if ($class::isSupported()) { if ($class::isSupported()) {
return ''; yield $name => $class;
} }
} }
catch (\Throwable $oException)
{
}
} }
catch (\Throwable $oException)
{
}
}
}
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';
} }
@ -50,22 +56,17 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
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; $result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled")
$name = \basename($file, '.php'); ->SetLabel('Enable ' . $class::NAME)
$class = 'ChangePasswordDriver' . $name; ->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
if ($class::isSupported()) { ->SetDescription($class::DESCRIPTION);
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled") $result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_allowed_emails")
->SetLabel('Enable ' . $class::NAME) ->SetLabel('Allowed emails')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL) ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription($class::DESCRIPTION); ->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@example.net user2@example1.net *@example2.net')
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_allowed_emails") ->SetDefaultValue('*');
->SetLabel('Allowed emails') $result = \array_merge($result, \call_user_func("{$class}::configMapping"));
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net')
->SetDefaultValue('*');
$result = \array_merge($result, \call_user_func("{$class}::configMapping"));
}
} }
return $result; return $result;
} }
@ -97,28 +98,21 @@ 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() );
); if (!$oDriver->ChangePassword($oAccount, $sPrevPassword, $sNewPassword)) {
if (!$oDriver->ChangePassword($oAccount, $sPrevPassword, $sNewPassword)) { throw new ClientException(static::CouldNotSaveNewPassword);
throw new ClientException(static::CouldNotSaveNewPassword); }
} $bResult = true;
$bResult = true; if ($this->oLogger) {
if ($this->oLogger) { $this->oLogger->Write("{$name} password changed for {$oAccount->Email()}");
$this->oLogger->Write("{$name} password changed for {$oAccount->Email()}");
}
} }
} }
catch (\Throwable $oException) catch (\Throwable $oException)