Repository navigation
Expand file tree
/
Copy pathUser.php
More file actions
170 lines (144 loc) · 4.86 KB
/
Copy pathUser.php
File metadata and controls
170 lines (144 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Models;
use Core\Model;
use PDO;
/**
* @property int UserID
* @property string PasswordHash
* @property string Email
* @property string Type
* @property string Name
* @property string FirstName
* @property string LastName
* @property string Password
* @author Bram Bos <brambos27@gmail.com>
*/
class User extends Model
{
public $errors = [];
public function __construct($data = [])
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
/**
* Authenticate user by email and Password.
*
* @param string $email
* @param string $password
* @return mixed User object if successful or false if email and Password don't match
*/
public static function authenticate($email, $password)
{
$user = self::find_by_email($email);
if ($user){
if (password_verify($password, $user->PasswordHash)){
return $user;
}
}
return false;
}
/**
* Updates user table with $token
* @param int $token
*/
public function store_token($token)
{
//TODO: Function store_token
// $sql = 'UPDATE users SET UserTokenHash = ? WHERE UserID = ?';
// $parameters = [$token, $this->UserID];
// self::execute_edit_query($sql, $parameters);
}
/**
* @param string $email
* @return mixed User model if successful, FALSE if unsuccessful
*/
public static function find_by_email($email)
{
$sql = 'SELECT * FROM users WHERE Email = ?'; //TODO Bepaal of ik wel alles uit de users tabel wil halen (*)
$stmt = self::execute_select_query($sql, PDO::FETCH_CLASS ,[$email]);
return $stmt->fetch();
}
public static function get_by_id($id)
{
$sql = 'SELECT * FROM users WHERE UserID = ?';
$stmt = self::execute_select_query($sql, PDO::FETCH_CLASS ,[$id]);
return $stmt->fetch();
}
public static function get_All_Users()
{
$sql = 'SELECT UserID, Email, Type, FirstName, LastName FROM users'; //TODO Bepaal of ik wel alles uit de users tabel wil halen (*)
$stmt = self::execute_select_query($sql, PDO::FETCH_CLASS);
return $stmt->fetchAll();
}
public static function get_quantity()
{
$sql = "SELECT COUNT(UserID) AS 'Number' FROM users";
$stmt = self::execute_select_query($sql, PDO::FETCH_CLASS);
return $stmt->fetch();
}
public static function edit_User($Type, $id){
$sql = 'UPDATE users SET Type = ? WHERE UserID = ?';
self::execute_edit_query($sql, [$Type, $id]);
}
public static function delete_User($id){
$sql = 'DELETE FROM users WHERE UserID = ?';
self::execute_edit_query($sql, [$id]);
}
//TODO Kijken hoe ik dit kan gebruiken
// public function profileLink()
// {
// return sprintf('<a href="/profile/%s">%s</a>',$this->id,$this->username);
// }
/**
* If properties are valid stores user model in database
*
* @return void
*/
public function register_user()
{
$this->validate();
if (empty($this->errors)){
$fullNameArray = $this->split_name(trim($this->Name));
$firstName = $fullNameArray[0];
$lastName = $fullNameArray[1];
$password_hash = password_hash($this->Password, PASSWORD_DEFAULT);
//TODO Activation Token
if (empty($this->Type)){
$this->Type = 'customer';
}
//TODO UserTokenHash
$sql = 'INSERT INTO users (Email, Type, PasswordHash, FirstName, LastName) VALUES (?, ?, ?, ?, ?)';
$parameters = [$this->Email, $this->Type, $password_hash, $firstName, $lastName];
self::execute_edit_query($sql, $parameters);
}
}
/**
* @param string $name Full name
* @return array An array with the first name at index 0 and last name at index 1
*/
private function split_name($name) {
$name = trim($name);
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
$first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) );
return array($first_name, $last_name);
}
/**
* Validate property values, adding error messages to the errors array property
* @return void
*/
private function validate()
{
if (strlen($this->Password) < 6)
$this->errors[] = 'Please enter at least 6 characters for the Password';
if (preg_match('/.*[a-z]+.*/i', $this->Password) == 0)
$this->errors[] = 'Password needs at least one letter';
if (preg_match('/.*\d+.*/i', $this->Password) == 0)
$this->errors[] = 'Password needs at least one number';
}
public function send_activation_email()
{
//TODO
}
}