Passed
Push — Auth ( f3bfe9...e2d976 )
by Stone
02:37
created

UserModel::authenticateUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Container;
6
use Core\Model;
7
8
class UserModel extends Model
9
{
10
11
    private $userTbl;
12
    private $roleTbl;
13
14
    public function __construct(Container $container)
15
    {
16
        parent::__construct($container);
17
        $this->userTbl = $this->getTablePrefix("users");
18
        $this->roleTbl = $this->getTablePrefix("roles");
19
    }
20
21
    /**
22
     * get the password from the user email. mainly for login purposes
23
     * @param string $email
24
     * @return string
25
     * @throws \Exception
26
     */
27
    private function getUserPassword(string $email): string
28
    {
29
        if (!$this->isEmailUsed($email)) {
30
            throw new \Exception("Email not present in Database");
31
        }
32
        $sql = "SELECT password FROM $this->userTbl WHERE email = :email";
33
        $this->query($sql);
34
        $this->bind(':email', $email);
35
        $this->execute();
36
        return $this->stmt->fetchColumn();
37
    }
38
39
    /**
40
     * Get all the useful data about a user from his ID
41
     * @param int $userId
42
     * @return mixed
43
     * @throws \Exception
44
     */
45
    public function getUserDetailsById(int $userId)
46
    {
47
        $sql = "
48
            SELECT idusers, username, avatar, email, surname, name, creation_date, last_update, locked_out, role_name, role_level
49
            FROM $this->userTbl
50
            INNER JOIN $this->roleTbl ON $this->userTbl.roles_idroles = $this->roleTbl.idroles
51
            WHERE idusers = :userId
52
        ";
53
        $this->query($sql);
54
        $this->bind(':userId', $userId);
55
        $this->execute();
56
        return $this->fetch();
57
    }
58
59
    /**
60
     * Get all the useful data about a user from his mail
61
     * @param string $email
62
     * @return mixed
63
     * @throws \Exception
64
     */
65
    public function getUserDetailsByEmail(string $email)
66
    {
67
        //check if email is valid for sanity
68
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
69
            $email = htmlspecialchars($email);
70
            throw new \Exception("invalid email " . $email);
71
        }
72
        $sql = "
73
            SELECT idusers, username, avatar, email, surname, name, creation_date, last_update, locked_out, role_name, role_level
74
            FROM $this->userTbl
75
            INNER JOIN $this->roleTbl ON $this->userTbl.roles_idroles = $this->roleTbl.idroles
76
            WHERE email = :email
77
        ";
78
        $this->query($sql);
79
        $this->bind(':email', $email);
80
        $this->execute();
81
        return $this->fetch();
82
    }
83
84
    public function authenticateUser(string $email, string $password)
85
    {
86
        $user = $this->getUserDetailsByEmail($email);
87
        if ($user !== false) {
88
            if (password_verify($password, $this->getUserPassword($email))) {
89
                return $user;
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * check if the email is present in the database
98
     * @param string $email
99
     * @return bool
100
     * @throws \Exception
101
     */
102
    public function isEmailUsed(string $email)
103
    {
104
        return $this->getUserDetailsByEmail($email) !== false;
105
    }
106
107
    public function registerUser(\stdClass $userData): int
108
    {
109
110
        //TODO need to get the default user role. Config ??
111
        $passwordHash = password_hash($userData->password, PASSWORD_DEFAULT);
112
113
        $sql = "
114
            INSERT INTO $this->userTbl (username, email, password, surname, name, creation_date, last_update, roles_idroles, locked_out, bad_login_tries)
115
            VALUES (:username, :email, :password, :surname, :name, NOW(), NOW(), :roles_idroles, 0, 0)
116
        ";
117
        $this->query($sql);
118
        $this->bind(':username', $userData->username);
119
        $this->bind(':email', $userData->email);
120
        $this->bind(':password', $passwordHash);
121
        $this->bind(':surname', $userData->surname);
122
        $this->bind(':name', $userData->name);
123
        $this->bind(':roles_idroles', 1);
124
        $this->execute();
125
126
        return (int)$this->dbh->lastInsertId();
127
128
    }
129
}