Improved Settings handling to prevent bugs in outer code

This commit is contained in:
the-djmaze 2024-02-26 02:47:56 +01:00
parent c6aad25581
commit 007e4b25eb
10 changed files with 50 additions and 44 deletions

View file

@ -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

View file

@ -187,7 +187,7 @@ trait Themes
$oSettings->SetConf('UserBackgroundName', $sName);
$oSettings->SetConf('UserBackgroundHash', $sHash);
$this->SettingsProvider()->Save($oAccount, $oSettings);
$oSettings->save();
}
}
}

View file

@ -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

View file

@ -419,7 +419,7 @@ class Manager
$aData[$sPluginName] = $aPluginSettings;
$oSettings->SetConf('Plugins',$aData);
return $this->oActions->SettingsProvider()->Save($oAccount, $oSettings);
return $oSettings->save();
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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);

View file

@ -2,6 +2,12 @@
namespace RainLoop\Providers\Storage\Enumerations;
/**
* PHP 8.1
enum StorageType: int {
case USER = 1;
}
*/
class StorageType
{
const USER = 1;

View file

@ -588,7 +588,7 @@ class ServiceActions
}
if ($bNeedToSettings) {
$this->SettingsProvider()->Save($oAccount, $oSettings);
$oSettings->save();
}
}

View file

@ -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