2020-06-19 00:42:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Entity;
|
|
|
|
|
|
|
|
use App\Model\Entity\AppModel;
|
|
|
|
use Cake\ORM\Entity;
|
2020-06-21 21:27:11 +02:00
|
|
|
use Authentication\PasswordHasher\DefaultPasswordHasher;
|
2020-06-19 00:42:10 +02:00
|
|
|
|
2021-10-18 13:28:26 +02:00
|
|
|
require_once(APP . 'Model' . DS . 'Table' . DS . 'SettingProviders' . DS . 'UserSettingsProvider.php');
|
|
|
|
use App\Settings\SettingsProvider\UserSettingsProvider;
|
|
|
|
|
2020-06-19 00:42:10 +02:00
|
|
|
class User extends AppModel
|
|
|
|
{
|
2022-11-04 09:59:48 +01:00
|
|
|
protected $_hidden = ['password', 'confirm_password', 'user_settings_by_name', 'user_settings_by_name_with_fallback', 'SettingsProvider', 'user_settings'];
|
2021-10-08 16:51:54 +02:00
|
|
|
|
2021-10-18 13:28:26 +02:00
|
|
|
protected $_virtual = ['user_settings_by_name', 'user_settings_by_name_with_fallback'];
|
2021-10-08 16:51:54 +02:00
|
|
|
|
|
|
|
protected function _getUserSettingsByName()
|
|
|
|
{
|
|
|
|
$settingsByName = [];
|
|
|
|
if (!empty($this->user_settings)) {
|
|
|
|
foreach ($this->user_settings as $i => $setting) {
|
|
|
|
$settingsByName[$setting->name] = $setting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $settingsByName;
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:28:26 +02:00
|
|
|
protected function _getUserSettingsByNameWithFallback()
|
|
|
|
{
|
|
|
|
if (!isset($this->SettingsProvider)) {
|
|
|
|
$this->SettingsProvider = new UserSettingsProvider();
|
|
|
|
}
|
|
|
|
$settingsByNameWithFallback = [];
|
|
|
|
if (!empty($this->user_settings)) {
|
|
|
|
foreach ($this->user_settings as $i => $setting) {
|
|
|
|
$settingsByNameWithFallback[$setting->name] = $setting->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$settingsProvider = $this->SettingsProvider->getSettingsConfiguration($settingsByNameWithFallback);
|
|
|
|
$settingsFlattened = $this->SettingsProvider->flattenSettingsConfiguration($settingsProvider);
|
|
|
|
return $settingsFlattened;
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:27:11 +02:00
|
|
|
protected function _setPassword(string $password) : ?string
|
|
|
|
{
|
|
|
|
if (strlen($password) > 0) {
|
|
|
|
return (new DefaultPasswordHasher())->hash($password);
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 13:26:44 +01:00
|
|
|
|
|
|
|
public function rearrangeForAPI(): void
|
|
|
|
{
|
2022-11-04 09:59:48 +01:00
|
|
|
if (!empty($this->tags)) {
|
|
|
|
$this->tags = $this->rearrangeTags($this->tags);
|
|
|
|
}
|
2022-10-31 13:26:44 +01:00
|
|
|
if (!empty($this->meta_fields)) {
|
|
|
|
$this->rearrangeMetaFields();
|
|
|
|
}
|
|
|
|
if (!empty($this->MetaTemplates)) {
|
|
|
|
unset($this->MetaTemplates);
|
|
|
|
}
|
2022-11-04 09:59:48 +01:00
|
|
|
if (!empty($this->user_settings_by_name)) {
|
|
|
|
$this->rearrangeUserSettings();
|
|
|
|
}
|
|
|
|
$this->rearrangeSimplify(['organisation', 'individual']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function rearrangeUserSettings()
|
|
|
|
{
|
|
|
|
$settings = [];
|
|
|
|
if (isset($this->user_settings_by_name)) {
|
|
|
|
foreach ($this->user_settings_by_name as $setting => $data) {
|
|
|
|
$settings[$setting] = $data['value'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($this->user_settings_by_name_with_fallback)) {
|
|
|
|
foreach ($this->user_settings_by_name_with_fallback as $setting => $data) {
|
|
|
|
if (!isset($settings[$setting])) {
|
|
|
|
$settings[$setting] = $data['value'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->settings = $settings;
|
2022-10-31 13:26:44 +01:00
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|