From 007e4b25ebbae78f5568679f1e2f3c55ae387228 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 26 Feb 2024 02:47:56 +0100 Subject: [PATCH] Improved Settings handling to prevent bugs in outer code --- .../libraries/RainLoop/Actions/Folders.php | 4 ++-- .../app/libraries/RainLoop/Actions/Themes.php | 2 +- .../app/libraries/RainLoop/Actions/User.php | 11 ++++----- .../libraries/RainLoop/Plugins/Manager.php | 2 +- .../RainLoop/Providers/AbstractProvider.php | 10 -------- .../libraries/RainLoop/Providers/Settings.php | 18 +++++++-------- .../Providers/Settings/DefaultSettings.php | 23 ++++++++++--------- .../Storage/Enumerations/StorageType.php | 6 +++++ .../app/libraries/RainLoop/ServiceActions.php | 2 +- .../0.0.0/app/libraries/RainLoop/Settings.php | 16 +++++++++++-- 10 files changed, 50 insertions(+), 44 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php index be90a63b0..6ca21093b 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php @@ -192,7 +192,7 @@ trait Folders $oSettingsLocal->SetConf('CheckableFolder', \json_encode(\array_unique($aCheckableFolder))); - return $this->DefaultResponse($this->SettingsProvider(true)->Save($oAccount, $oSettingsLocal)); + return $this->DefaultResponse($oSettingsLocal->save()); } /** @@ -320,7 +320,7 @@ trait Folders $oSettingsLocal->SetConf('TrashFolder', $this->GetActionParam('trash', '')); $oSettingsLocal->SetConf('ArchiveFolder', $this->GetActionParam('archive', '')); - return $this->DefaultResponse($this->SettingsProvider(true)->Save($oAccount, $oSettingsLocal)); + return $this->DefaultResponse($oSettingsLocal->save()); } public function DoFolderACL() : array diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Themes.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Themes.php index 3fa64a8f1..89a60d884 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Themes.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Themes.php @@ -187,7 +187,7 @@ trait Themes $oSettings->SetConf('UserBackgroundName', $sName); $oSettings->SetConf('UserBackgroundHash', $sHash); - $this->SettingsProvider()->Save($oAccount, $oSettings); + $oSettings->save(); } } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php index 1c4d04ea6..07db0edfd 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php @@ -61,7 +61,7 @@ trait User if ($sCurrentLanguage !== $sLanguage) { $oSettings->SetConf('language', $sLanguage); - $this->SettingsProvider()->Save($oAccount, $oSettings); + $oSettings->save(); } } } @@ -219,8 +219,7 @@ trait User $this->setSettingsFromParams($oSettingsLocal, 'ShowUnreadCount', 'bool'); $this->setSettingsFromParams($oSettingsLocal, 'CheckMailInterval', 'int'); - return $this->DefaultResponse($this->SettingsProvider()->Save($oAccount, $oSettings) && - $this->SettingsProvider(true)->Save($oAccount, $oSettingsLocal)); + return $this->DefaultResponse($oSettings->save() && $oSettingsLocal->save()); } public function DoQuota() : array @@ -263,12 +262,11 @@ trait User public function DoClearUserBackground() : array { - $oAccount = $this->getAccountFromToken(); - if (!$this->GetCapa(Capa::USER_BACKGROUND)) { return $this->FalseResponse(); } + $oAccount = $this->getAccountFromToken(); $oSettings = $this->SettingsProvider()->Load($oAccount); if ($oAccount && $oSettings) { $this->StorageProvider()->Clear($oAccount, @@ -280,8 +278,7 @@ trait User $oSettings->SetConf('UserBackgroundHash', ''); } - return $this->DefaultResponse($oAccount && $oSettings ? - $this->SettingsProvider()->Save($oAccount, $oSettings) : false); + return $this->DefaultResponse($oAccount && $oSettings ? $oSettings->save() : false); } private function setSettingsFromParams(\RainLoop\Settings $oSettings, string $sConfigName, string $sType = 'string', ?callable $cCallback = null) : void diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Manager.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Manager.php index ef7ebcb79..5fe62aaab 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Manager.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Manager.php @@ -419,7 +419,7 @@ class Manager $aData[$sPluginName] = $aPluginSettings; $oSettings->SetConf('Plugins',$aData); - return $this->oActions->SettingsProvider()->Save($oAccount, $oSettings); + return $oSettings->save(); } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AbstractProvider.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AbstractProvider.php index f499c712e..9c1e2c5a5 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AbstractProvider.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/AbstractProvider.php @@ -6,18 +6,8 @@ abstract class AbstractProvider { use \MailSo\Log\Inherit; - /** - * @var \RainLoop\Model\Account - */ - protected $oAccount; - public function IsActive() : bool { return false; } - - public function SetAccount(\RainLoop\Model\Account $oAccount) - { - $this->oAccount = $oAccount; - } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings.php index b10a50307..66123e4bf 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings.php @@ -2,30 +2,30 @@ namespace RainLoop\Providers; +use RainLoop\Model\Account; +use RainLoop\Providers\Settings\ISettings; + class Settings extends \RainLoop\Providers\AbstractProvider { - /** - * @var \RainLoop\Providers\Settings\ISettings - */ - private $oDriver; + private ISettings $oDriver; - public function __construct(\RainLoop\Providers\Settings\ISettings $oDriver) + public function __construct(ISettings $oDriver) { $this->oDriver = $oDriver; } - public function Load(\RainLoop\Model\Account $oAccount) : \RainLoop\Settings + public function Load(Account $oAccount) : \RainLoop\Settings { - return new \RainLoop\Settings($this->oDriver->Load($oAccount)); + return new \RainLoop\Settings($this, $oAccount, $this->oDriver->Load($oAccount)); } - public function Save(\RainLoop\Model\Account $oAccount, \RainLoop\Settings $oSettings) : bool + public function Save(Account $oAccount, \RainLoop\Settings $oSettings) : bool { return $this->oDriver->Save($oAccount, $oSettings); } public function IsActive() : bool { - return $this->oDriver instanceof \RainLoop\Providers\Settings\ISettings; + return true; } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings/DefaultSettings.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings/DefaultSettings.php index 1ea00d7a2..63df9be23 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings/DefaultSettings.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Settings/DefaultSettings.php @@ -2,25 +2,26 @@ namespace RainLoop\Providers\Settings; +use RainLoop\Model\Account; +use RainLoop\Providers\Storage; +use RainLoop\Providers\Storage\Enumerations\StorageType; + class DefaultSettings implements ISettings { const FILE_NAME = 'settings'; const FILE_NAME_LOCAL = 'settings_local'; - /** - * @var \RainLoop\Providers\Storage - */ - private $oStorageProvider; + private Storage $oStorageProvider; - public function __construct(\RainLoop\Providers\Storage $oStorageProvider) + public function __construct(Storage $oStorageProvider) { $this->oStorageProvider = $oStorageProvider; } - public function Load(\RainLoop\Model\Account $oAccount) : array + public function Load(Account $oAccount) : array { $sValue = $this->oStorageProvider->Get($oAccount, - \RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, + StorageType::CONFIG, $this->oStorageProvider->IsLocal() ? self::FILE_NAME_LOCAL : self::FILE_NAME @@ -36,20 +37,20 @@ class DefaultSettings implements ISettings return array(); } - public function Save(\RainLoop\Model\Account $oAccount, \RainLoop\Settings $oSettings) : bool + public function Save(Account $oAccount, \RainLoop\Settings $oSettings) : bool { return $this->oStorageProvider->Put($oAccount, - \RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, + StorageType::CONFIG, $this->oStorageProvider->IsLocal() ? self::FILE_NAME_LOCAL : self::FILE_NAME, \json_encode($oSettings)); } - public function Delete(\RainLoop\Model\Account $oAccount) : bool + public function Delete(Account $oAccount) : bool { return $this->oStorageProvider->Clear($oAccount, - \RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, + StorageType::CONFIG, $this->oStorageProvider->IsLocal() ? self::FILE_NAME_LOCAL : self::FILE_NAME); diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Storage/Enumerations/StorageType.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Storage/Enumerations/StorageType.php index 0b3c29cb7..969c28156 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Storage/Enumerations/StorageType.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Providers/Storage/Enumerations/StorageType.php @@ -2,6 +2,12 @@ namespace RainLoop\Providers\Storage\Enumerations; +/** + * PHP 8.1 +enum StorageType: int { + case USER = 1; +} +*/ class StorageType { const USER = 1; diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index 762bf9bf3..a4986f70f 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -588,7 +588,7 @@ class ServiceActions } if ($bNeedToSettings) { - $this->SettingsProvider()->Save($oAccount, $oSettings); + $oSettings->save(); } } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Settings.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Settings.php index b2791824b..bf0bf23ad 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Settings.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Settings.php @@ -2,11 +2,16 @@ namespace RainLoop; +use RainLoop\Model\Account; +use RainLoop\Providers\Settings as SettingsProvider; + class Settings implements \JsonSerializable { - protected array $aData = array(); + protected array $aData; + protected Account $oAccount; + protected SettingsProvider $oProvider; - public function __construct(array $aData) + public function __construct(SettingsProvider $oProvider, Account $oAccount, array $aData) { if (isset($aData['SpamFolder']) && !isset($aData['JunkFolder'])) { $aData['JunkFolder'] = $aData['SpamFolder']; @@ -19,6 +24,13 @@ class Settings implements \JsonSerializable unset($aData['Language']); } $this->aData = $aData; + $this->oAccount = $oAccount; + $this->oProvider = $oProvider; + } + + public function save() : bool + { + return $this->oProvider->Save($this->oAccount, $this); } public function toArray() : array