cerebrate/src/Model/Entity/User.php

87 lines
2.9 KiB
PHP
Raw Normal View History

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
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'];
protected $_virtual = ['user_settings_by_name', 'user_settings_by_name_with_fallback'];
protected function _getUserSettingsByName()
{
$settingsByName = [];
if (!empty($this->user_settings)) {
foreach ($this->user_settings as $i => $setting) {
$settingsByName[$setting->name] = $setting;
}
}
return $settingsByName;
}
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);
}
}
public function rearrangeForAPI(): void
{
2022-11-04 09:59:48 +01:00
if (!empty($this->tags)) {
$this->tags = $this->rearrangeTags($this->tags);
}
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;
}
2020-06-19 00:42:10 +02:00
}