Passed
Push — Auth ( e088d1...64a80e )
by Stone
01:56
created

UserModel::getAuthorDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 all the useful data about a user from his ID
23
     * @param int $userId
24
     * @return mixed
25
     * @throws \Exception
26
     */
27
    public function getUserDetailsById(int $userId)
28
    {
29
        $sql = "
30
            SELECT idusers, username, avatar, email, surname, name, creation_date, last_update, locked_out, role_name, role_level
31
            FROM $this->userTbl
32
            INNER JOIN $this->roleTbl ON $this->userTbl.roles_idroles = $this->roleTbl.idroles
33
            WHERE idusers = :userId
34
        ";
35
        $this->query($sql);
36
        $this->bind(':userId', $userId);
37
        $this->execute();
38
        return $this->fetch();
39
    }
40
41
    /**
42
     * Get all the useful data about a user from his mail
43
     * @param string $email
44
     * @return mixed
45
     * @throws \Exception
46
     */
47
    public function getUserDetailsByEmail(string $email)
48
    {
49
        //check if email is valid for sanity
50
        if (!filter_var($this->user->email, FILTER_VALIDATE_EMAIL))
0 ignored issues
show
Bug introduced by
The property user does not exist on App\Models\UserModel. Did you mean userTbl?
Loading history...
51
        {
52
            $email = htmlspecialchars($email);
53
            throw new \Exception("invalid email ".$email);
54
        }
55
        $sql = "
56
            SELECT idusers, username, avatar, email, surname, name, creation_date, last_update, locked_out, role_name, role_level
57
            FROM $this->userTbl
58
            INNER JOIN $this->roleTbl ON $this->userTbl.roles_idroles = $this->roleTbl.idroles
59
            WHERE email = :email
60
        ";
61
        $this->query($sql);
62
        $this->bind(':email', $email);
63
        $this->execute();
64
        return $this->fetch();
65
    }
66
67
    /**
68
     * check if the email is present in the database
69
     * @param string $email
70
     * @return bool
71
     * @throws \Exception
72
     */
73
    public function isEmailUsed(string $email)
74
    {
75
        $sql = "
76
            SELECT * FROM $this->userTbl WHERE email = :email
77
        ";
78
        $this->query($sql);
79
        $this->bind(':email', $email);
80
        $this->execute();
81
82
        return $this->stmt->rowCount() > 0;
83
    }
84
85
    public function registerUser(\stdClass $userData): int
86
    {
87
88
        $passwordHash = password_hash($userData->password, PASSWORD_DEFAULT);
89
90
        $sql = "
91
            INSERT INTO $this->userTbl (username, email, password, surname, name, creation_date, last_update, roles_idroles, locked_out, bad_login_tries)
92
            VALUES (:username, :email, :password, :surname, :name, NOW(), NOW(), :roles_idroles, 1, 0)
93
        ";
94
        $this->query($sql);
95
        $this->bind(':username', $userData->username);
96
        $this->bind(':email', $userData->email);
97
        $this->bind(':password', $passwordHash);
98
        $this->bind(':surname', $userData->surname);
99
        $this->bind(':name', $userData->name);
100
        $this->bind(':roles_idroles', 1);
101
        $this->execute();
102
103
        return (int)$this->dbh->lastInsertId();
104
105
    }
106
}