1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mafd16\User; |
4
|
|
|
|
5
|
|
|
use \Anax\Configure\ConfigureInterface; |
6
|
|
|
use \Anax\Configure\ConfigureTrait; |
7
|
|
|
use \Anax\DI\InjectionAwareInterface; |
8
|
|
|
use \Anax\Di\InjectionAwareTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* User model for Comment system. |
12
|
|
|
*/ |
13
|
|
|
class UserModel implements |
14
|
|
|
ConfigureInterface, |
15
|
|
|
InjectionAwareInterface |
16
|
|
|
{ |
17
|
|
|
use ConfigureTrait, |
18
|
|
|
InjectionAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Variables |
22
|
|
|
*/ |
23
|
|
|
//private $session; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new User. |
28
|
|
|
* The acronym is unique! |
29
|
|
|
* |
30
|
|
|
* @param object $newUser the values of the new user. |
31
|
|
|
* |
32
|
|
|
* $newUser = (object) [ |
33
|
|
|
* acronym => name, |
34
|
|
|
* password => hashing in function!, |
35
|
|
|
* email => , |
36
|
|
|
* created => timestamp in function, |
37
|
|
|
* updated => null, |
38
|
|
|
* deleted => null, |
39
|
|
|
* active => null, |
40
|
|
|
* admin => 0, |
41
|
|
|
* ]; |
42
|
|
|
* |
43
|
|
|
* @return object $user if acronym unique, false otherwise. |
44
|
|
|
*/ |
45
|
1 |
|
public function createUser($newUser) |
46
|
|
|
{ |
47
|
|
|
// Connect to db |
48
|
1 |
|
$user = new User(); |
49
|
1 |
|
$user->setDb($this->di->get("db")); |
50
|
|
|
|
51
|
|
|
// Check if acronym already exists, |
52
|
1 |
|
if ($user->find("acronym", $newUser->acronym)) { |
53
|
1 |
|
return false; |
54
|
|
|
} else { |
55
|
|
|
// else create user |
56
|
1 |
|
$user->acronym = $newUser->acronym; |
57
|
1 |
|
$user->setPassword($newUser->password); |
58
|
1 |
|
$user->email = $newUser->email; |
59
|
1 |
|
$user->created = date("Y-m-d H:i:s"); |
60
|
|
|
//$user->$updated; |
61
|
|
|
//$user->$deleted; |
62
|
|
|
//$user->$active; |
63
|
1 |
|
$user->admin = 0; |
64
|
|
|
// Save to database |
65
|
1 |
|
$user->save(); |
66
|
1 |
|
return $user; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Save a user to session. |
73
|
|
|
* |
74
|
|
|
* @param object $user the values of the user. |
75
|
|
|
* |
76
|
|
|
* $user = (object) [ |
77
|
|
|
* acronym => name, |
78
|
|
|
* password => hashed, |
79
|
|
|
* email => email, |
80
|
|
|
* created => timestamp, |
81
|
|
|
* updated => null/timestamp, |
82
|
|
|
* deleted => null/timestamp, |
83
|
|
|
* active => null/timestamp, |
84
|
|
|
* admin => 0/1, |
85
|
|
|
* ]; |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
1 |
|
public function saveToSession($user) |
90
|
|
|
{ |
91
|
1 |
|
$session = $this->di->get("session"); |
92
|
|
|
// Save user to session |
93
|
1 |
|
$session->set("my_user_id", $user->id); |
94
|
1 |
|
$session->set("my_user_name", $user->acronym); |
95
|
|
|
//$session->set("my_user_password", $user->password); |
96
|
1 |
|
$session->set("my_user_email", $user->email); |
97
|
|
|
//$session->set("my_user_created", $user->created); |
98
|
|
|
//$session->set("my_user_updated", $user->updated); |
99
|
|
|
//$session->set("my_user_deleted", $user->deleted); |
100
|
|
|
//$session->set("my_user_active", $user->active); |
101
|
1 |
|
$session->set("my_user_admin", $user->admin); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get a user from the database |
107
|
|
|
* |
108
|
|
|
* @param int $id the id of the user |
109
|
|
|
* |
110
|
|
|
* @return object $user the user object |
111
|
|
|
*/ |
112
|
1 |
|
public function getUserFromDatabase($id) |
113
|
|
|
{ |
114
|
|
|
// Connect to db |
115
|
1 |
|
$user = new User(); |
116
|
1 |
|
$user->setDb($this->di->get("db")); |
117
|
|
|
// Get the user |
118
|
1 |
|
$user->find("id", $id); |
119
|
1 |
|
return $user; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|