Resolve Issue #51

This commit is contained in:
djmaze 2021-03-08 12:21:58 +01:00
parent eb0d1ecccb
commit 6d0fa33022
2 changed files with 17 additions and 12 deletions

View file

@ -13,10 +13,11 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
DESCRIPTION = 'This plugin allows you to change passwords of email accounts'; DESCRIPTION = 'This plugin allows you to change passwords of email accounts';
// \RainLoop\Notifications\ // \RainLoop\Notifications\
const CouldNotSaveNewPassword = 130; const
const CurrentPasswordIncorrect = 131; CouldNotSaveNewPassword = 130,
const NewPasswordShort = 132; CurrentPasswordIncorrect = 131,
const NewPasswordWeak = 133; NewPasswordShort = 132,
NewPasswordWeak = 133;
public function Init() : void public function Init() : void
{ {
@ -33,11 +34,11 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$phar = new \Phar($phar_file, \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME); $phar = new \Phar($phar_file, \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME);
foreach (new \RecursiveIteratorIterator($phar) as $file) { foreach (new \RecursiveIteratorIterator($phar) as $file) {
if (\preg_match('#/drivers/([a-z]+)\\.php$#Di', $file, $m)) { if (\preg_match('#/drivers/([a-z]+)\\.php$#Di', $file, $m)) {
$class = 'ChangePasswordDriver' . $m[1];
try try
{ {
if ($all || $this->Config()->Get('plugin', "driver_{$m[1]}_enabled", false)) { if ($all || $this->Config()->Get('plugin', "driver_{$m[1]}_enabled", false)) {
require_once $file; require_once $file;
$class = 'ChangePasswordDriver' . $m[1];
if ($class::isSupported()) { if ($class::isSupported()) {
yield $m[1] => $class; yield $m[1] => $class;
} }
@ -45,18 +46,19 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
\trigger_error("ERROR {$class}: " . $oException->getMessage());
} }
} }
} }
} else { } else {
// foreach (\glob(__DIR__ . '/../change-password-*', GLOB_ONLYDIR) as $file) { // 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');
$class = 'ChangePasswordDriver' . $name;
try try
{ {
$name = \basename($file, '.php');
if ($all || $this->Config()->Get('plugin', "driver_{$name}_enabled", false)) { if ($all || $this->Config()->Get('plugin', "driver_{$name}_enabled", false)) {
require_once $file; require_once $file;
$class = 'ChangePasswordDriver' . $name;
if ($class::isSupported()) { if ($class::isSupported()) {
yield $name => $class; yield $name => $class;
} }
@ -64,6 +66,7 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
\trigger_error("ERROR {$class}: " . $oException->getMessage());
} }
} }
} }
@ -112,6 +115,7 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
$oAccount = $oActions->GetAccount(); $oAccount = $oActions->GetAccount();
if (!$oAccount->Email()) { if (!$oAccount->Email()) {
\trigger_error('ChangePassword failed: empty email address');
throw new ClientException(static::CouldNotSaveNewPassword); throw new ClientException(static::CouldNotSaveNewPassword);
} }
@ -152,6 +156,7 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
\trigger_error("{$class} failed: {$oException->getMessage()}");
if ($oLogger) { if ($oLogger) {
$oLogger->Write("ERROR: {$name} password change for {$oAccount->Email()} failed"); $oLogger->Write("ERROR: {$name} password change for {$oAccount->Email()} failed");
$oLogger->WriteException($oException); $oLogger->WriteException($oException);
@ -162,14 +167,14 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
} }
if (!$bResult) { if (!$bResult) {
\trigger_error("ChangePassword failed");
throw new ClientException(static::CouldNotSaveNewPassword); throw new ClientException(static::CouldNotSaveNewPassword);
} }
$oAccount->SetPassword($sNewPassword); $oAccount->SetPassword($sNewPassword);
$oActions->SetAuthToken($oAccount); $oActions->SetAuthToken($oAccount);
return $oActions->GetSpecAuthToken(); return $this->jsonResponse(__FUNCTION__, $oActions->GetSpecAuthToken());
// return $this->jsonResponse(__FUNCTION__, $oActions->GetSpecAuthToken());
} }
public static function encrypt(string $algo, string $password) public static function encrypt(string $algo, string $password)

View file

@ -236,15 +236,15 @@ abstract class AbstractPlugin
/** /**
* @return mixed false|string|array * @return mixed false|string|array
*/ */
protected function jsonResponse(string $sFunctionName, array $aData) protected function jsonResponse(string $sFunctionName, $mData)
{ {
if ($this->oPluginManager) if ($this->oPluginManager)
{ {
return $this->oPluginManager->JsonResponseHelper( return $this->oPluginManager->JsonResponseHelper(
$this->oPluginManager->convertPluginFolderNameToClassName($this->Name()).'::'.$sFunctionName, $aData); $this->oPluginManager->convertPluginFolderNameToClassName($this->Name()).'::'.$sFunctionName, $mData);
} }
return \json_encode($aData); return \json_encode($mData);
} }
/** /**