Completed
Push — master ( 59965c...4339a8 )
by
unknown
114:08 queued 95:41
created

DoctrineDatabase::updateUserToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 41
nc 2
nop 1
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Create user.
40
     *
41
     * @param user $user
42
     *
43
     * @return mixed
44
     */
45
    public function createUser(User $user)
46
    {
47
        $query = $this->handler->createInsertQuery();
48
        $query
49
            ->insertInto($this->handler->quoteTable('ezuser'))
50
            ->set(
51
                $this->handler->quoteColumn('contentobject_id'),
52
                $query->bindValue($user->id, null, \PDO::PARAM_INT)
53
            )->set(
54
                $this->handler->quoteColumn('login'),
55
                $query->bindValue($user->login)
56
            )->set(
57
                $this->handler->quoteColumn('email'),
58
                $query->bindValue($user->email)
59
            )->set(
60
                $this->handler->quoteColumn('password_hash'),
61
                $query->bindValue($user->passwordHash)
62
            )->set(
63
                $this->handler->quoteColumn('password_hash_type'),
64
                $query->bindValue($user->hashAlgorithm, null, \PDO::PARAM_INT)
65
            );
66
        $query->prepare()->execute();
67
68
        $query = $this->handler->createInsertQuery();
69
        $query
70
            ->insertInto($this->handler->quoteTable('ezuser_setting'))
71
            ->set(
72
                $this->handler->quoteColumn('user_id'),
73
                $query->bindValue($user->id, null, \PDO::PARAM_INT)
74
            )->set(
75
                $this->handler->quoteColumn('is_enabled'),
76
                $query->bindValue($user->isEnabled, null, \PDO::PARAM_INT)
77
            )->set(
78
                $this->handler->quoteColumn('max_login'),
79
                $query->bindValue($user->maxLogin, null, \PDO::PARAM_INT)
80
            );
81
        $query->prepare()->execute();
82
    }
83
84
    /**
85
     * Delete user with the given ID.
86
     *
87
     * @param mixed $userId
88
     */
89
    public function deleteUser($userId)
90
    {
91
        $query = $this->handler->createDeleteQuery();
92
        $query
93
            ->deleteFrom($this->handler->quoteTable('ezuser_setting'))
94
            ->where(
95
                $query->expr->eq(
96
                    $this->handler->quoteColumn('user_id'),
97
                    $query->bindValue($userId, null, \PDO::PARAM_INT)
98
                )
99
            );
100
        $query->prepare()->execute();
101
102
        $query = $this->handler->createDeleteQuery();
103
        $query
104
            ->deleteFrom($this->handler->quoteTable('ezuser'))
105
            ->where(
106
                $query->expr->eq(
107
                    $this->handler->quoteColumn('contentobject_id'),
108
                    $query->bindValue($userId, null, \PDO::PARAM_INT)
109
                )
110
            );
111
        $query->prepare()->execute();
112
    }
113
114
    /**
115
     * Loads user with user ID.
116
     *
117
     * @param mixed $userId
118
     *
119
     * @return array
120
     */
121 View Code Duplication
    public function load($userId)
122
    {
123
        $query = $this->handler->createSelectQuery();
124
        $query->select(
125
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
126
            $this->handler->quoteColumn('login', 'ezuser'),
127
            $this->handler->quoteColumn('email', 'ezuser'),
128
            $this->handler->quoteColumn('password_hash', 'ezuser'),
129
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
130
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
131
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
132
        )->from(
133
            $this->handler->quoteTable('ezuser')
134
        )->leftJoin(
135
            $this->handler->quoteTable('ezuser_setting'),
136
            $query->expr->eq(
137
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
138
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
139
            )
140
        )->where(
141
            $query->expr->eq(
142
                $this->handler->quoteColumn('contentobject_id', 'ezuser'),
143
                $query->bindValue($userId, null, \PDO::PARAM_INT)
144
            )
145
        );
146
147
        $statement = $query->prepare();
148
        $statement->execute();
149
150
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
151
    }
152
153
    /**
154
     * Loads user with user login.
155
     *
156
     * @param string $login
157
     *
158
     * @return array
159
     */
160 View Code Duplication
    public function loadByLogin($login)
161
    {
162
        $query = $this->handler->createSelectQuery();
163
        $query->select(
164
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
165
            $this->handler->quoteColumn('login', 'ezuser'),
166
            $this->handler->quoteColumn('email', 'ezuser'),
167
            $this->handler->quoteColumn('password_hash', 'ezuser'),
168
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
169
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
170
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
171
        )->from(
172
            $this->handler->quoteTable('ezuser')
173
        )->leftJoin(
174
            $this->handler->quoteTable('ezuser_setting'),
175
            $query->expr->eq(
176
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
177
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
178
            )
179
        )->where(
180
            $query->expr->eq(
181
                $query->expr->lower($this->handler->quoteColumn('login', 'ezuser')),
182
                // Index is case in-sensitive, on some db's lowercase, so we lowercase $login
183
                $query->bindValue(mb_strtolower($login, 'UTF-8'), null, \PDO::PARAM_STR)
184
            )
185
        );
186
187
        $statement = $query->prepare();
188
        $statement->execute();
189
190
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
191
    }
192
193
    /**
194
     * Loads user with user email.
195
     *
196
     * @param string $email
197
     *
198
     * @return array
199
     */
200 View Code Duplication
    public function loadByEmail($email)
201
    {
202
        $query = $this->handler->createSelectQuery();
203
        $query->select(
204
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
205
            $this->handler->quoteColumn('login', 'ezuser'),
206
            $this->handler->quoteColumn('email', 'ezuser'),
207
            $this->handler->quoteColumn('password_hash', 'ezuser'),
208
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
209
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
210
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
211
        )->from(
212
            $this->handler->quoteTable('ezuser')
213
        )->leftJoin(
214
            $this->handler->quoteTable('ezuser_setting'),
215
            $query->expr->eq(
216
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
217
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
218
            )
219
        )->where(
220
            $query->expr->eq(
221
                $this->handler->quoteColumn('email', 'ezuser'),
222
                $query->bindValue($email, null, \PDO::PARAM_STR)
223
            )
224
        );
225
226
        $statement = $query->prepare();
227
        $statement->execute();
228
229
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
230
    }
231
232
    /**
233
     * Loads a user with user hash key.
234
     *
235
     * @param string $hash
236
     *
237
     * @return array
238
     */
239
    public function loadUserByToken($hash)
240
    {
241
        $query = $this->handler->createSelectQuery();
242
        $query->select(
243
            $this->handler->quoteColumn('contentobject_id', 'ezuser'),
244
            $this->handler->quoteColumn('login', 'ezuser'),
245
            $this->handler->quoteColumn('email', 'ezuser'),
246
            $this->handler->quoteColumn('password_hash', 'ezuser'),
247
            $this->handler->quoteColumn('password_hash_type', 'ezuser'),
248
            $this->handler->quoteColumn('is_enabled', 'ezuser_setting'),
249
            $this->handler->quoteColumn('max_login', 'ezuser_setting')
250
        )->from(
251
            $this->handler->quoteTable('ezuser')
252
        )->leftJoin(
253
            $this->handler->quoteTable('ezuser_setting'),
254
            $query->expr->eq(
255
                $this->handler->quoteColumn('user_id', 'ezuser_setting'),
256
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
257
            )
258
        )->leftJoin(
259
            $this->handler->quoteTable('ezuser_accountkey'),
260
            $query->expr->eq(
261
                $this->handler->quoteColumn('user_id', 'ezuser_accountkey'),
262
                $this->handler->quoteColumn('contentobject_id', 'ezuser')
263
            )
264
        )->where(
265
            $query->expr->lAnd(
266
                $query->expr->eq(
267
                    $this->handler->quoteColumn('hash_key', 'ezuser_accountkey'),
268
                    $query->bindValue($hash, null, \PDO::PARAM_STR)
269
                ),
270
                $query->expr->gte(
271
                    $this->handler->quoteColumn('time', 'ezuser_accountkey'),
272
                    $query->bindValue(time(), null, \PDO::PARAM_INT)
273
                )
274
            )
275
        );
276
277
        $statement = $query->prepare();
278
        $statement->execute();
279
280
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
281
    }
282
283
    /**
284
     * Update the user information specified by the user struct.
285
     *
286
     * @param User $user
287
     */
288
    public function updateUser(User $user)
289
    {
290
        $query = $this->handler->createUpdateQuery();
291
        $query
292
            ->update($this->handler->quoteTable('ezuser'))
293
            ->set(
294
                $this->handler->quoteColumn('login'),
295
                $query->bindValue($user->login)
296
            )->set(
297
                $this->handler->quoteColumn('email'),
298
                $query->bindValue($user->email)
299
            )->set(
300
                $this->handler->quoteColumn('password_hash'),
301
                $query->bindValue($user->passwordHash)
302
            )->set(
303
                $this->handler->quoteColumn('password_hash_type'),
304
                $query->bindValue($user->hashAlgorithm)
305
            )->where(
306
                $query->expr->eq(
307
                    $this->handler->quoteColumn('contentobject_id'),
308
                    $query->bindValue($user->id, null, \PDO::PARAM_INT)
309
                )
310
            );
311
        $query->prepare()->execute();
312
313
        $query = $this->handler->createUpdateQuery();
314
        $query
315
            ->update($this->handler->quoteTable('ezuser_setting'))
316
            ->set(
317
                $this->handler->quoteColumn('is_enabled'),
318
                $query->bindValue($user->isEnabled, null, \PDO::PARAM_INT)
319
            )->set(
320
                $this->handler->quoteColumn('max_login'),
321
                $query->bindValue($user->maxLogin, null, \PDO::PARAM_INT)
322
            )->where(
323
                $query->expr->eq(
324
                    $this->handler->quoteColumn('user_id'),
325
                    $query->bindValue($user->id, null, \PDO::PARAM_INT)
326
                )
327
            );
328
        $query->prepare()->execute();
329
    }
330
331
    /**
332
     * Update or insert the user token information specified by the user token struct.
333
     *
334
     * @param \eZ\Publish\SPI\Persistence\User\UserTokenUpdateStruct $userTokenUpdateStruct
335
     */
336
    public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct)
337
    {
338
        $query = $this->handler->createSelectQuery();
339
        $query->select(
340
            $this->handler->quoteColumn('id', 'ezuser_accountkey')
341
        )->from(
342
            $this->handler->quoteTable('ezuser_accountkey')
343
        )->where(
344
            $query->expr->eq(
345
                $this->handler->quoteColumn('user_id', 'ezuser_accountkey'),
346
                $query->bindValue($userTokenUpdateStruct->userId, null, \PDO::PARAM_INT)
347
            )
348
        );
349
350
        $statement = $query->prepare();
351
        $statement->execute();
352
353
        if (empty($statement->fetchAll(\PDO::FETCH_ASSOC))) {
354
            $query = $this->handler->createInsertQuery();
355
            $query
356
                ->insertInto($this->handler->quoteTable('ezuser_accountkey'))
357
                ->set(
358
                    $this->handler->quoteColumn('hash_key'),
359
                    $query->bindValue($userTokenUpdateStruct->hashKey)
360
                )->set(
361
                    $this->handler->quoteColumn('time'),
362
                    $query->bindValue($userTokenUpdateStruct->time)
363
                )->set(
364
                    $this->handler->quoteColumn('user_id'),
365
                    $query->bindValue($userTokenUpdateStruct->userId)
366
                );
367
368
            $query->prepare()->execute();
369
        } else {
370
            $query = $this->handler->createUpdateQuery();
371
            $query
372
                ->update($this->handler->quoteTable('ezuser_accountkey'))
373
                ->set(
374
                    $this->handler->quoteColumn('hash_key'),
375
                    $query->bindValue($userTokenUpdateStruct->hashKey)
376
                )->set(
377
                    $this->handler->quoteColumn('time'),
378
                    $query->bindValue($userTokenUpdateStruct->time)
379
                )->where(
380
                    $query->expr->eq(
381
                        $this->handler->quoteColumn('user_id'),
382
                        $query->bindValue($userTokenUpdateStruct->userId, null, \PDO::PARAM_INT)
383
                    )
384
                );
385
            $query->prepare()->execute();
386
        }
387
    }
388
389
    /**
390
     * Expires user token with user hash.
391
     *
392
     * @param string $hash
393
     */
394 View Code Duplication
    public function expireUserToken($hash)
395
    {
396
        $query = $this->handler->createUpdateQuery();
397
        $query
398
            ->update($this->handler->quoteTable('ezuser_accountkey'))
399
            ->set(
400
                $this->handler->quoteColumn('time'),
401
                $query->bindValue(0)
402
            )->where(
403
                $query->expr->eq(
404
                    $this->handler->quoteColumn('hash_key'),
405
                    $query->bindValue($hash, null, \PDO::PARAM_STR)
406
                )
407
            );
408
        $query->prepare()->execute();
409
    }
410
411
    /**
412
     * Assigns role to user with given limitation.
413
     *
414
     * @param mixed $contentId
415
     * @param mixed $roleId
416
     * @param array $limitation
417
     */
418
    public function assignRole($contentId, $roleId, array $limitation)
419
    {
420
        foreach ($limitation as $identifier => $values) {
421
            foreach ($values as $value) {
422
                $query = $this->handler->createInsertQuery();
423
                $query
424
                    ->insertInto($this->handler->quoteTable('ezuser_role'))
425
                    ->set(
426
                        $this->handler->quoteColumn('contentobject_id'),
427
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
428
                    )->set(
429
                        $this->handler->quoteColumn('role_id'),
430
                        $query->bindValue($roleId, null, \PDO::PARAM_INT)
431
                    )->set(
432
                        $this->handler->quoteColumn('limit_identifier'),
433
                        $query->bindValue($identifier)
434
                    )->set(
435
                        $this->handler->quoteColumn('limit_value'),
436
                        $query->bindValue($value)
437
                    );
438
                $query->prepare()->execute();
439
            }
440
        }
441
    }
442
443
    /**
444
     * Remove role from user or user group.
445
     *
446
     * @param mixed $contentId
447
     * @param mixed $roleId
448
     */
449 View Code Duplication
    public function removeRole($contentId, $roleId)
450
    {
451
        $query = $this->handler->createDeleteQuery();
452
        $query
453
            ->deleteFrom($this->handler->quoteTable('ezuser_role'))
454
            ->where(
455
                $query->expr->lAnd(
456
                    $query->expr->eq(
457
                        $this->handler->quoteColumn('contentobject_id'),
458
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
459
                    ),
460
                    $query->expr->eq(
461
                        $this->handler->quoteColumn('role_id'),
462
                        $query->bindValue($roleId, null, \PDO::PARAM_INT)
463
                    )
464
                )
465
            );
466
        $query->prepare()->execute();
467
    }
468
469
    /**
470
     * Remove role from user or user group, by assignment ID.
471
     *
472
     * @param mixed $roleAssignmentId
473
     */
474
    public function removeRoleAssignmentById($roleAssignmentId)
475
    {
476
        $query = $this->handler->createDeleteQuery();
477
        $query
478
            ->deleteFrom($this->handler->quoteTable('ezuser_role'))
479
            ->where(
480
                $query->expr->eq(
481
                    $this->handler->quoteColumn('id'),
482
                    $query->bindValue($roleAssignmentId, null, \PDO::PARAM_INT)
483
                )
484
            );
485
        $query->prepare()->execute();
486
    }
487
}
488