|
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)) |
|
|
|
|
|
|
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
|
|
|
} |