Completed
Push — 6.13.7 ( b1546d )
by
unknown
14:00
created

DoctrineDatabase::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
cc 1
nc 1
nop 1
rs 9.2

1 Method

Rating   Name   Duplication   Size   Complexity  
A DoctrineDatabase::assignRole() 0 24 3
1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase Location Gateway class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\User\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\User\Gateway;
12
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
13
use eZ\Publish\SPI\Persistence\User;
14
use eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct;
15
16
/**
17
 * User gateway implementation using the Doctrine database.
18
 */
19
class DoctrineDatabase extends Gateway
20
{
21
    /**
22
     * Database handler.
23
     *
24
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
25
     */
26
    protected $handler;
27
28
    /**
29
     * Construct from database handler.
30
     *
31
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $handler
32
     */
33
    public function __construct(DatabaseHandler $handler)
34
    {
35
        $this->handler = $handler;
36
    }
37
38
    /**
39
     * Loads user with user ID.
40
     *
41
     * @param mixed $userId
42
     *
43
     * @return array
44
     */
45 View Code Duplication
    public function load($userId)
46
    {
47
        $query = $this->handler->createSelectQuery();
48
        $query->select(
49
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
50
            $this->handler->quoteColumn('login', 'ezuser'),
51
            $this->handler->quoteColumn('email', 'ezuser'),
52
            $this->handler->quoteColumn('password_hash', 'ezuser'),
53
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
54
            $this->handler->quoteColumn('password_updated_at', 'ezuser'),
55
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
56
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
57
        )->from(
58
            $this->handler->quoteTable('ezuser')
59
        )->leftJoin(
60
            $this->handler->quoteTable('ezuser_setting'),
61
            $query->expr->eq(
62
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
63
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
64
            )
65
        )->where(
66
            $query->expr->eq(
67
                $this->handler->quoteColumn('contentobject_id', 'ezuser'),
68
                $query->bindValue($userId, null, \PDO::PARAM_INT)
69
            )
70
        );
71
72
        $statement = $query->prepare();
73
        $statement->execute();
74
75
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
76
    }
77
78
    /**
79
     * Loads user with user login.
80
     *
81
     * @param string $login
82
     *
83
     * @return array
84
     */
85 View Code Duplication
    public function loadByLogin($login)
86
    {
87
        $query = $this->handler->createSelectQuery();
88
        $query->select(
89
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
90
            $this->handler->quoteColumn('login', 'ezuser'),
91
            $this->handler->quoteColumn('email', 'ezuser'),
92
            $this->handler->quoteColumn('password_hash', 'ezuser'),
93
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
94
            $this->handler->quoteColumn('password_updated_at', 'ezuser'),
95
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
96
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
97
        )->from(
98
            $this->handler->quoteTable('ezuser')
99
        )->leftJoin(
100
            $this->handler->quoteTable('ezuser_setting'),
101
            $query->expr->eq(
102
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
103
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
104
            )
105
        )->where(
106
            $query->expr->eq(
107
                $query->expr->lower($this->handler->quoteColumn('login', 'ezuser')),
108
                // Index is case in-sensitive, on some db's lowercase, so we lowercase $login
109
                $query->bindValue(mb_strtolower($login, 'UTF-8'), null, \PDO::PARAM_STR)
110
            )
111
        );
112
113
        $statement = $query->prepare();
114
        $statement->execute();
115
116
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
117
    }
118
119
    /**
120
     * Loads user with user email.
121
     *
122
     * @param string $email
123
     *
124
     * @return array
125
     */
126 View Code Duplication
    public function loadByEmail($email)
127
    {
128
        $query = $this->handler->createSelectQuery();
129
        $query->select(
130
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
131
            $this->handler->quoteColumn('login', 'ezuser'),
132
            $this->handler->quoteColumn('email', 'ezuser'),
133
            $this->handler->quoteColumn('password_hash', 'ezuser'),
134
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
135
            $this->handler->quoteColumn('password_updated_at', 'ezuser'),
136
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
137
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
138
        )->from(
139
            $this->handler->quoteTable('ezuser')
140
        )->leftJoin(
141
            $this->handler->quoteTable('ezuser_setting'),
142
            $query->expr->eq(
143
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
144
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
145
            )
146
        )->where(
147
            $query->expr->eq(
148
                $this->handler->quoteColumn('email', 'ezuser'),
149
                $query->bindValue($email, null, \PDO::PARAM_STR)
150
            )
151
        );
152
153
        $statement = $query->prepare();
154
        $statement->execute();
155
156
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
157
    }
158
159
    /**
160
     * Loads a user with user hash key.
161
     *
162
     * @param string $hash
163
     *
164
     * @return array
165
     */
166
    public function loadUserByToken($hash)
167
    {
168
        $query = $this->handler->createSelectQuery();
169
        $query->select(
170
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
171
            $this->handler->quoteColumn('login', 'ezuser'),
172
            $this->handler->quoteColumn('email', 'ezuser'),
173
            $this->handler->quoteColumn('password_hash', 'ezuser'),
174
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
175
            $this->handler->quoteColumn('password_updated_at', 'ezuser'),
176
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
177
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
178
        )->from(
179
            $this->handler->quoteTable('ezuser')
180
        )->leftJoin(
181
            $this->handler->quoteTable('ezuser_setting'),
182
            $query->expr->eq(
183
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
184
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
185
            )
186
        )->leftJoin(
187
            $this->handler->quoteTable('ezuser_accountkey'),
188
            $query->expr->eq(
189
                $this->handler->quoteColumn('user_id', 'ezuser_accountkey'),
190
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
191
            )
192
        )->where(
193
            $query->expr->lAnd(
194
                $query->expr->eq(
195
                    $this->handler->quoteColumn('hash_key', 'ezuser_accountkey'),
196
                    $query->bindValue($hash, null, \PDO::PARAM_STR)
197
                ),
198
                $query->expr->gte(
199
                    $this->handler->quoteColumn('time', 'ezuser_accountkey'),
200
                    $query->bindValue(time(), null, \PDO::PARAM_INT)
201
                )
202
            )
203
        );
204
205
        $statement = $query->prepare();
206
        $statement->execute();
207
208
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
209
    }
210
211
    /**
212
     * Update or insert the user token information specified by the user token struct.
213
     *
214
     * @param \eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct $userTokenUpdateStruct
215
     */
216
    public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct)
217
    {
218
        $query = $this->handler->createSelectQuery();
219
        $query->select(
220
            $this->handler->quoteColumn('id', 'ezuser_accountkey')
221
        )->from(
222
            $this->handler->quoteTable('ezuser_accountkey')
223
        )->where(
224
            $query->expr->eq(
225
                $this->handler->quoteColumn('user_id', 'ezuser_accountkey'),
226
                $query->bindValue($userTokenUpdateStruct->userId, null, \PDO::PARAM_INT)
227
            )
228
        );
229
230
        $statement = $query->prepare();
231
        $statement->execute();
232
233
        if (empty($statement->fetchAll(\PDO::FETCH_ASSOC))) {
234
            $query = $this->handler->createInsertQuery();
235
            $query
236
                ->insertInto($this->handler->quoteTable('ezuser_accountkey'))
237
                ->set(
238
                    $this->handler->quoteColumn('hash_key'),
239
                    $query->bindValue($userTokenUpdateStruct->hashKey)
240
                )->set(
241
                    $this->handler->quoteColumn('time'),
242
                    $query->bindValue($userTokenUpdateStruct->time)
243
                )->set(
244
                    $this->handler->quoteColumn('user_id'),
245
                    $query->bindValue($userTokenUpdateStruct->userId)
246
                );
247
248
            $query->prepare()->execute();
249
        } else {
250
            $query = $this->handler->createUpdateQuery();
251
            $query
252
                ->update($this->handler->quoteTable('ezuser_accountkey'))
253
                ->set(
254
                    $this->handler->quoteColumn('hash_key'),
255
                    $query->bindValue($userTokenUpdateStruct->hashKey)
256
                )->set(
257
                    $this->handler->quoteColumn('time'),
258
                    $query->bindValue($userTokenUpdateStruct->time)
259
                )->where(
260
                    $query->expr->eq(
261
                        $this->handler->quoteColumn('user_id'),
262
                        $query->bindValue($userTokenUpdateStruct->userId, null, \PDO::PARAM_INT)
263
                    )
264
                );
265
            $query->prepare()->execute();
266
        }
267
    }
268
269
    /**
270
     * Expires user token with user hash.
271
     *
272
     * @param string $hash
273
     */
274 View Code Duplication
    public function expireUserToken($hash)
275
    {
276
        $query = $this->handler->createUpdateQuery();
277
        $query
278
            ->update($this->handler->quoteTable('ezuser_accountkey'))
279
            ->set(
280
                $this->handler->quoteColumn('time'),
281
                $query->bindValue(0)
282
            )->where(
283
                $query->expr->eq(
284
                    $this->handler->quoteColumn('hash_key'),
285
                    $query->bindValue($hash, null, \PDO::PARAM_STR)
286
                )
287
            );
288
        $query->prepare()->execute();
289
    }
290
291
    /**
292
     * Assigns role to user with given limitation.
293
     *
294
     * @param mixed $contentId
295
     * @param mixed $roleId
296
     * @param array $limitation
297
     */
298
    public function assignRole($contentId, $roleId, array $limitation)
299
    {
300
        foreach ($limitation as $identifier => $values) {
301
            foreach ($values as $value) {
302
                $query = $this->handler->createInsertQuery();
303
                $query
304
                    ->insertInto($this->handler->quoteTable('ezuser_role'))
305
                    ->set(
306
                        $this->handler->quoteColumn('contentobject_id'),
307
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
308
                    )->set(
309
                        $this->handler->quoteColumn('role_id'),
310
                        $query->bindValue($roleId, null, \PDO::PARAM_INT)
311
                    )->set(
312
                        $this->handler->quoteColumn('limit_identifier'),
313
                        $query->bindValue($identifier)
314
                    )->set(
315
                        $this->handler->quoteColumn('limit_value'),
316
                        $query->bindValue($value)
317
                    );
318
                $query->prepare()->execute();
319
            }
320
        }
321
    }
322
323
    /**
324
     * Remove role from user or user group.
325
     *
326
     * @param mixed $contentId
327
     * @param mixed $roleId
328
     */
329 View Code Duplication
    public function removeRole($contentId, $roleId)
330
    {
331
        $query = $this->handler->createDeleteQuery();
332
        $query
333
            ->deleteFrom($this->handler->quoteTable('ezuser_role'))
334
            ->where(
335
                $query->expr->lAnd(
336
                    $query->expr->eq(
337
                        $this->handler->quoteColumn('contentobject_id'),
338
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
339
                    ),
340
                    $query->expr->eq(
341
                        $this->handler->quoteColumn('role_id'),
342
                        $query->bindValue($roleId, null, \PDO::PARAM_INT)
343
                    )
344
                )
345
            );
346
        $query->prepare()->execute();
347
    }
348
349
    /**
350
     * Remove role from user or user group, by assignment ID.
351
     *
352
     * @param mixed $roleAssignmentId
353
     */
354
    public function removeRoleAssignmentById($roleAssignmentId)
355
    {
356
        $query = $this->handler->createDeleteQuery();
357
        $query
358
            ->deleteFrom($this->handler->quoteTable('ezuser_role'))
359
            ->where(
360
                $query->expr->eq(
361
                    $this->handler->quoteColumn('id'),
362
                    $query->bindValue($roleAssignmentId, null, \PDO::PARAM_INT)
363
                )
364
            );
365
        $query->prepare()->execute();
366
    }
367
}
368