Completed
Push — master ( 0e1368...c48087 )
by Sam
05:46
created

UserRepository::getAccessibleSubQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\User;
8
use Cake\Chronos\Chronos;
9
10
class UserRepository extends AbstractRepository implements LimitedAccessSubQueryInterface
11
{
12
    /**
13
     * Returns the user authenticated by its email and password
14
     *
15
     * @param string $login
16
     * @param string $password
17
     *
18
     * @return null|User
19
     */
20 2
    public function getByLoginPassword(string $login, string $password): ?User
21
    {
22
        /** @var User $user */
23 2
        $user = $this->getByLogin($login);
24
25 2
        if (!$user || ($user->getActiveUntil() && $user->getActiveUntil() < new Chronos())) {
1 ignored issue
show
introduced by
$user is of type Application\Model\User, thus it always evaluated to true.
Loading history...
26 1
            return null;
27
        }
28
29 2
        $hashFromDb = $user->getPassword();
30 2
        $isMd5 = mb_strlen($hashFromDb) === 32 && ctype_xdigit($hashFromDb);
31
32
        // If we found a user and he has a correct MD5 or correct new hash, then return the user
33 2
        if (($isMd5 && md5($password) === $hashFromDb) || password_verify($password, $hashFromDb)) {
34
35
            // Update the hash in DB, if we are still MD5, or if PHP default options changed
36 2
            if ($isMd5 || password_needs_rehash($hashFromDb, PASSWORD_DEFAULT)) {
37 2
                $user->setPassword($password);
38 2
                _em()->flush();
39
            }
40
41 2
            return $user;
42
        }
43
44 1
        return null;
45
    }
46
47
    /**
48
     * Unsecured way to get a user from its ID.
49
     *
50
     * This should only be used in tests or controlled environment.
51
     *
52
     * @param int $id
53
     *
54
     * @return null|User
55
     */
56 1
    public function getOneById(int $id): ?User
57
    {
58 1
        $this->getAclFilter()->setEnabled(false);
59 1
        $user = $this->findOneById($id);
1 ignored issue
show
Bug introduced by
The method findOneById() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        /** @scrutinizer ignore-call */ 
60
        $user = $this->findOneById($id);
Loading history...
60 1
        $this->getAclFilter()->setEnabled(true);
61
62 1
        return $user;
63
    }
64
65
    /**
66
     * Unsecured way to get a user from its login.
67
     *
68
     * This should only be used in tests or controlled environment.
69
     *
70
     * @param null|string $login
71
     *
72
     * @return null|User
73
     */
74 11
    public function getByLogin(?string $login): ?User
75
    {
76 11
        $this->getAclFilter()->setEnabled(false);
77 11
        $user = $this->findOneByLogin($login);
1 ignored issue
show
Bug introduced by
The method findOneByLogin() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        /** @scrutinizer ignore-call */ 
78
        $user = $this->findOneByLogin($login);
Loading history...
78 11
        $this->getAclFilter()->setEnabled(true);
79
80 11
        return $user;
81
    }
82
83
    /**
84
     * Returns pure SQL to get ID of all objects that are accessible to given user.
85
     *
86
     * @param null|User $user
87
     *
88
     * @return string
89
     */
90 7
    public function getAccessibleSubQuery(?User $user): string
91
    {
92 7
        if ($user) {
93 6
            return $this->getAllIdsQuery();
94
        }
95
96 1
        return '-1';
97
    }
98
}
99