Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

User/UserStorage/Gateway/DoctrineStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\User\UserStorage\Gateway;
8
9 use Doctrine\DBAL\Connection;
10 use eZ\Publish\Core\FieldType\User\UserStorage\Gateway;
11 use PDO;
12
16 class DoctrineStorage extends Gateway
17 {
18 const USER_TABLE = 'ezuser';
19 const USER_SETTING_TABLE = 'ezuser_setting';
20
22 protected $connection;
23
29 protected $defaultValues = [
30 'hasStoredLogin' => false,
31 'contentId' => null,
32 'login' => null,
33 'email' => null,
34 'passwordHash' => null,
35 'passwordHashType' => null,
36 'passwordUpdatedAt' => null,
37 'enabled' => false,
38 'maxLogin' => null,
39 ];
40
41 public function __construct(Connection $connection)
42 {
43 $this->connection = $connection;
44 }
45
49 public function getFieldData($fieldId, $userId = null)
50 {
51 $userId = $userId ?: $this->fetchUserId($fieldId);
52 $userData = $this->fetchUserData($userId);
53
54 if (!isset($userData['login'])) {
55 return $this->defaultValues;
56 }
57
58 $result = array_merge(
59 $this->defaultValues,
60 [
61 'hasStoredLogin' => true,
62 ],
63 $userData,
64 $this->fetchUserSettings($userId)
65 );
66
67 return $result;
68 }
69
75 protected function getPropertyMap()
76 {
77 return [
78 'has_stored_login' => [
79 'name' => 'hasStoredlogin',
80 'cast' => function ($input) {
81 return $input == '1';
82 },
83 ],
84 'contentobject_id' => [
85 'name' => 'contentId',
86 'cast' => 'intval',
87 ],
88 'login' => [
89 'name' => 'login',
90 'cast' => 'strval',
91 ],
92 'email' => [
93 'name' => 'email',
94 'cast' => 'strval',
95 ],
96 'password_hash' => [
97 'name' => 'passwordHash',
98 'cast' => 'strval',
99 ],
100 'password_hash_type' => [
101 'name' => 'passwordHashType',
102 'cast' => 'strval',
103 ],
104 'password_updated_at' => [
105 'name' => 'passwordUpdatedAt',
106 'cast' => function ($timestamp) {
107 return $timestamp ? (int)$timestamp : null;
108 },
109 ],
110 'is_enabled' => [
111 'name' => 'enabled',
112 'cast' => function ($input) {
113 return $input == '1';
114 },
115 ],
116 'max_login' => [
117 'name' => 'maxLogin',
118 'cast' => 'intval',
119 ],
120 ];
121 }
122
130 protected function convertColumnsToProperties(array $databaseValues)
131 {
132 $propertyValues = [];
133 $propertyMap = $this->getPropertyMap();
134
135 foreach ($databaseValues as $columnName => $value) {
136 $conversionFunction = $propertyMap[$columnName]['cast'];
137
138 $propertyValues[$propertyMap[$columnName]['name']] = $conversionFunction($value);
139 }
140
141 return $propertyValues;
142 }
143
151 protected function fetchUserId($fieldId)
152 {
153 $query = $this->connection->createQueryBuilder();
154 $query
155 ->select(
156 $this->connection->quoteIdentifier('contentobject_id')
157 )
158 ->from($this->connection->quoteIdentifier('ezcontentobject_attribute'))
159 ->where(
160 $query->expr()->eq(
161 $this->connection->quoteIdentifier('id'),
162 ':fieldId'
163 )
164 )
165 ->setParameter(':fieldId', $fieldId, PDO::PARAM_INT)
166 ;
167
168 $statement = $query->execute();
169
170 return (int) $statement->fetchColumn();
171 }
172
180 protected function fetchUserData($userId)
181 {
182 $query = $this->connection->createQueryBuilder();
183 $query
184 ->select(
185 $this->connection->quoteIdentifier('usr.contentobject_id'),
186 $this->connection->quoteIdentifier('usr.login'),
187 $this->connection->quoteIdentifier('usr.email'),
188 $this->connection->quoteIdentifier('usr.password_hash'),
189 $this->connection->quoteIdentifier('usr.password_hash_type'),
190 $this->connection->quoteIdentifier('usr.password_updated_at')
191 )
192 ->from($this->connection->quoteIdentifier(self::USER_TABLE), 'usr')
193 ->where(
194 $query->expr()->eq(
195 $this->connection->quoteIdentifier('usr.contentobject_id'),
196 ':userId'
197 )
198 )
199 ->setParameter(':userId', $userId, PDO::PARAM_INT)
200 ;
201
202 $statement = $query->execute();
203
204 $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
205
206 return isset($rows[0]) ? $this->convertColumnsToProperties($rows[0]) : [];
207 }
208
216 protected function fetchUserSettings($userId)
217 {
218 $query = $this->connection->createQueryBuilder();
219 $query
220 ->select(
221 $this->connection->quoteIdentifier('s.is_enabled'),
222 $this->connection->quoteIdentifier('s.max_login')
223 )
224 ->from($this->connection->quoteIdentifier(self::USER_SETTING_TABLE), 's')
225 ->where(
226 $query->expr()->eq(
227 $this->connection->quoteIdentifier('s.user_id'),
228 ':userId'
229 )
230 )
231 ->setParameter(':userId', $userId, PDO::PARAM_INT)
232 ;
233
234 $statement = $query->execute();
235
236 $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
237
238 return isset($rows[0]) ? $this->convertColumnsToProperties($rows[0]) : [];
239 }
240 }
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage
User DoctrineStorage gateway.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:17
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\USER_SETTING_TABLE
const USER_SETTING_TABLE
Definition: User/UserStorage/Gateway/DoctrineStorage.php:19
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\fetchUserId
fetchUserId($fieldId)
Fetch user content object id for the given field id.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:151
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\convertColumnsToProperties
convertColumnsToProperties(array $databaseValues)
Convert the given database values to properties.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:130
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\fetchUserData
fetchUserData($userId)
Fetch user data.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:180
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\$connection
$connection
Definition: User/UserStorage/Gateway/DoctrineStorage.php:22
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\USER_TABLE
const USER_TABLE
Definition: User/UserStorage/Gateway/DoctrineStorage.php:18
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\__construct
__construct(Connection $connection)
Definition: User/UserStorage/Gateway/DoctrineStorage.php:41
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\fetchUserSettings
fetchUserSettings($userId)
Fetch user settings.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:216
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\$defaultValues
$defaultValues
Definition: User/UserStorage/Gateway/DoctrineStorage.php:29
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\getPropertyMap
getPropertyMap()
Map legacy database column names to property names.
Definition: User/UserStorage/Gateway/DoctrineStorage.php:75
eZ\Publish\Core\FieldType\User\UserStorage\Gateway\DoctrineStorage\getFieldData
getFieldData($fieldId, $userId=null)
{Get field data.The User storage handles the following attributes, following the user field type in e...
Definition: User/UserStorage/Gateway/DoctrineStorage.php:49
eZ\Publish\Core\FieldType\User\UserStorage\Gateway
Definition: FieldType/User/UserStorage/Gateway.php:12
eZ\Publish\Core\FieldType\User\UserStorage\Gateway
Definition: User/UserStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.