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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
<?php
namespace app\models;
use \Registry;
use app\extends\App_manager;
use app\models\History_model;
class Access_model
{
## User methods
## -------------------------------------------------------------------------
/** check user (by index key)
*
* @param $indexKey (string) : key for user identification
* @param $value (string)
*
* @param $email (string): user's email
*/
public static function checkUser($email)
{
$user = Registry::use('database')->query(
"SELECT * FROM user WHERE email = :email AND active = 1",
[ ':email' => $email ]
)->getFirst();
// if no user, return false
if ($user === false) return false;
return $user;
}
/** getUserByInvitation
*
* @param string $invitation
*/
public static function getUserByInvitation($invitation)
{
return Registry::use('database')->query(
"SELECT * FROM user WHERE invitation = :invitation",
['invitation' => $invitation]
)->getFirst();
}
/** create user
*
* creates user record;
* assigns privileged (usualy defaults);
* creates activation_code
*
* @param $data (array): Request->POST array
* @param $password (string): secure hashed password
*
* @return $activation_code
*
*/
public static function registerUser($data, $password, $privileges = DEFAULT_PRIVILEGES)
{
$required_fields = [
'name',
'surname',
'email',
'password'
];
// check required fields
$isOK = true;
foreach($required_fields as $fi) {
if (empty($data[$fi])) $isOK = false;
}
// if empty required fields exists ... return false
if (!$isOK) {
return [ "success" => false, 'error' => EMPTY_REQUIRED_FIELDS ];
}
// create an activation code
$activation_code = md5($data['email'].time().rand(0, 10000));
// if isOK go on and...
// create user record
$new_user_id = Registry::use('database')->query(
"INSERT INTO user
(`first_name`, `last_name`, `email`, `password`, `active`, `activation`)
VALUES
(:nam, :surname, :email, :pass, :act, :actcode)",
[
':nam' => $data['name'],
':surname' => $data['surname'],
':email' => $data['email'],
':pass' => $password,
':act' => 0, // needs email confirmation to be activated ...
':actcode' => $activation_code // ... with the activation code
]
)->lastInsertID();
// set default privileges
self::set_user_privileges($new_user_id, $privileges);
// set reader role
self::set_user_role($new_user_id, 5);
// update history
History_model::trackUserAccess($new_user_id, TRACK_ACCOUNT, 'Create User Account');
// return success and user id
return [
"success" => true,
'id' => $new_user_id ,
'activation' => $activation_code
];
}
/** activate_set_password
*
* activate user and set password
*
* @param $ticket (hex/MD5): activation code;
*
*/
public static function activate_set_password($post, $password)
{
// Update and set activate = true
$rowCount = Registry::use('database')->query(
"UPDATE user
SET first_name = :first_name,
last_name = :last_name,
email = :email,
father_name = :father_name,
registration_number = :registration_number,
sector = :sector,
belonging_school = :belonging_school,
position = :position,
phone = :phone,
`password` = :password,
invitation = NULL,
active = :active
WHERE id = :id AND invitation = :invitation",
[
':first_name' => $post['first_name'],
':last_name' => $post['last_name'],
':email' => $post['email'],
':father_name' => $post['father_name'],
':registration_number' => $post['registration_number'],
':sector' => $post['sector'],
':belonging_school' => $post['belonging_school'],
':position' => $post['position'],
':phone' => $post['phone'],
':password' => $password,
':active' => 1,
':id' => $post['id'],
':invitation' => $post['invitation']
]
)->rowCount();
// update history
History_model::trackUserAccess($post['id'], TRACK_ACCOUNT, 'User Account Activated');
return $rowCount;
}
## Set permission methods
## -------------------------------------------------------------------------
/** set_user_role
*
* user, role are (int) IDs
*/
public static function set_user_role($user, $role)
{
Registry::use('database')->runQuery(
"INSERT INTO user_role (user_id, role_id) VALUES (:user, :role)",
[ ':user' => $user, ":role" => $role ]
);
return true;
}
}
|