|
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
|
|
|
|
|
13
|
|
|
public function __construct(Container $container) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($container); |
|
16
|
|
|
$this->userTbl = $this->getTablePrefix("users"); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get all the data about a user for posts (Author). |
|
21
|
|
|
* @param int $authorId |
|
22
|
|
|
* @return array |
|
23
|
|
|
* @throws \ReflectionException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getAuthorDetails(int $authorId) |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->getRowById($authorId); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* check if the email is present in the database |
|
32
|
|
|
* @param string $email |
|
33
|
|
|
* @return bool |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
*/ |
|
36
|
|
|
public function isEmailUsed(string $email) |
|
37
|
|
|
{ |
|
38
|
|
|
$sql = " |
|
39
|
|
|
SELECT * FROM $this->userTbl WHERE email = :email |
|
40
|
|
|
"; |
|
41
|
|
|
$this->query($sql); |
|
42
|
|
|
$this->bind(':email', $email); |
|
43
|
|
|
$this->execute(); |
|
44
|
|
|
|
|
45
|
|
|
return $this->stmt->rowCount() > 0; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function registerUser(\stdClass $userData) : int |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
$passwordHash = password_hash($userData->password, PASSWORD_DEFAULT ); |
|
52
|
|
|
|
|
53
|
|
|
$sql = " |
|
54
|
|
|
INSERT INTO $this->userTbl (username, email, password, surname, name, creation_date, last_update, roles_idroles, locked_out, bad_login_tries) |
|
55
|
|
|
VALUES (:username, :email, :password, :surname, :name, NOW(), NOW(), :roles_idroles, 1, 0) |
|
56
|
|
|
"; |
|
57
|
|
|
$this->query($sql); |
|
58
|
|
|
$this->bind(':username', $userData->username); |
|
59
|
|
|
$this->bind(':email', $userData->email); |
|
60
|
|
|
$this->bind(':password', $passwordHash); |
|
61
|
|
|
$this->bind(':surname', $userData->surname); |
|
62
|
|
|
$this->bind(':name', $userData->name); |
|
63
|
|
|
$this->bind(':roles_idroles', 1); |
|
64
|
|
|
$this->execute(); |
|
65
|
|
|
|
|
66
|
|
|
return (int)$this->dbh->lastInsertId(); |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |