Test Failed
Push — develop ( ebbd0c...8a31ce )
by nguereza
03:10
created

SessionAuthentication::getUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 17
rs 9.9666
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file SessionAuthentication.php
34
 *
35
 *  The Authentication using session feature class
36
 *
37
 *  @package    Platine\Framework\Auth\Authentication
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Auth\Authentication;
49
50
use Platine\Framework\Auth\AuthenticationInterface;
51
use Platine\Framework\Auth\Exception\AccountLockedException;
52
use Platine\Framework\Auth\Exception\AccountNotFoundException;
53
use Platine\Framework\Auth\Exception\InvalidCredentialsException;
54
use Platine\Framework\Auth\Exception\MissingCredentialsException;
55
use Platine\Framework\Auth\IdentityInterface;
56
use Platine\Framework\Auth\Repository\UserRepository;
57
use Platine\Security\Hash\BcryptHash;
58
use Platine\Session\Session;
59
60
/**
61
 * class SessionAuthentication
62
 * @package Platine\Framework\Auth\Authentication
63
 */
64
class SessionAuthentication implements AuthenticationInterface
65
{
66
67
    /**
68
     * The session instance to use
69
     * @var Session
70
     */
71
    protected Session $session;
72
73
    /**
74
     * The user repository instance
75
     * @var UserRepository
76
     */
77
    protected UserRepository $userRepository;
78
79
    /**
80
     * Create new instance
81
     * @param Session $session
82
     * @param UserRepository $userRepository
83
     */
84
    public function __construct(
85
        Session $session,
86
        UserRepository $userRepository
87
    ) {
88
        $this->session = $session;
89
        $this->userRepository = $userRepository;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getUser(): IdentityInterface
96
    {
97
        if (!$this->isLogged()) {
98
            throw new AccountNotFoundException('User not logged', 401);
99
        }
100
101
        $id = $this->session->get('user.id');
102
        $user = $this->userRepository->find($id);
103
104
        if (!$user) {
105
            throw new AccountNotFoundException(
106
                'Can not find the logged user information, may be data is corrupted',
107
                401
108
            );
109
        }
110
111
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type Platine\Orm\Entity which is incompatible with the type-hinted return Platine\Framework\Auth\IdentityInterface.
Loading history...
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function isLogged(): bool
118
    {
119
        return $this->session->has('user');
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function login(array $credentials = [], bool $remeberMe = false): bool
126
    {
127
        if (!isset($credentials['username']) || !isset($credentials['password'])) {
128
            throw new MissingCredentialsException(
129
                'Missing username or password information',
130
                401
131
            );
132
        }
133
        $username = $credentials['username'];
134
        $password = $credentials['password'];
135
        $user = $this->userRepository
136
                    ->with('roles.permissions')
137
                    ->findBy(['username' => $username]);
138
        if (!$user) {
139
            throw new AccountNotFoundException('Can not find the user with the given information', 401);
140
        } elseif ($user->status == 0) {
141
            throw new AccountLockedException(
142
                'User is locked',
143
                401
144
            );
145
        }
146
147
        $hash = new BcryptHash();
148
        if (!$hash->verify($password, $user->password)) {
149
            throw new InvalidCredentialsException(
150
                'Invalid credentials',
151
                401
152
            );
153
        }
154
155
        $permissions = [];
156
157
        $roles = $user->roles;
158
        foreach ($roles as $role) {
159
            $rolePermissions = $role->permissions;
160
            foreach ($rolePermissions as $permission) {
161
                $permissions[] = $permission->code;
162
            }
163
        }
164
165
        $data = [
166
          'id' => $user->id,
167
          'username' => $user->username,
168
          'lastname' => $user->lastname,
169
          'firstname' => $user->firstname,
170
          'permissions' => array_unique($permissions),
171
        ];
172
173
        $this->session->set('user', $data);
174
175
        return $this->isLogged();
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function logout(): void
182
    {
183
        $this->session->remove('user');
184
    }
185
}
186