/
BaseUser.php
146 lines (122 loc) · 2.9 KB
/
BaseUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace UniteCMS\CoreBundle\Security\User;
use UniteCMS\CoreBundle\Content\BaseContent;
use UniteCMS\CoreBundle\Content\ContentInterface;
use UniteCMS\CoreBundle\Content\FieldData;
use UniteCMS\CoreBundle\Content\SensitiveFieldData;
abstract class BaseUser extends BaseContent implements UserInterface
{
/**
* @var string
*/
protected $username = '';
/**
* @var FieldData[]
*/
protected $sensitiveData = [];
/**
* @var array
*/
protected $tokens = [];
/**
* @var bool $fullyAuthenticated
*/
protected $fullyAuthenticated = false;
/**
* {@inheritDoc}
*/
public function setFullyAuthenticated(bool $fullyAuthenticated = true): void
{
$this->fullyAuthenticated = $fullyAuthenticated;
}
/**
* {@inheritDoc}
*/
public function isFullyAuthenticated(): bool
{
return $this->fullyAuthenticated;
}
/**
* {@inheritDoc}
*/
public function getData(): array
{
if(!is_array($this->sensitiveData)) {
$this->sensitiveData = [];
}
return (parent::getData() + $this->sensitiveData + ['username' => new FieldData($this->getUsername())]);
}
/**
* @param array $data
* @return self
*/
public function setData(array $data) : ContentInterface
{
$this->data = [];
$this->sensitiveData = [];
foreach($data as $name => $value) {
if($name === 'username') {
$this->username = $value;
}
else if ($value instanceof SensitiveFieldData) {
$this->sensitiveData[$name] = $value;
}
else {
$this->data[$name] = $value;
}
}
return $this;
}
/**
* {@inheritDoc}
*/
public function getFieldData(string $fieldName): ?FieldData
{
return $fieldName === 'username' ?
new FieldData($this->getUsername()) :
(isset($this->getData()[$fieldName]) ? $this->getData()[$fieldName] : null);
}
/**
* {@inheritDoc}
*/
public function getUsername() : string {
return $this->username;
}
/**
* {@inheritDoc}
*/
public function getRoles()
{
return [sprintf('ROLE_%s', strtoupper($this->getType()))];
}
/**
* {@inheritDoc}
*/
public function getPassword()
{
return null;
}
/**
* {@inheritDoc}
*/
public function getSalt()
{
return null;
}
/**
* {@inheritDoc}
*/
public function eraseCredentials() {}
/**
* {@inheritDoc}
*/
public function getToken(string $key) : ?string {
return $this->tokens[$key] ?? null;
}
/**
* {@inheritDoc}
*/
public function setToken(string $key, ?string $token = null) : void {
$this->tokens[$key] = $token;
}
}