-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
140 lines (128 loc) · 3.76 KB
/
User.php
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
<?php
use Luracast\Restler\RestException;
class User
{
/**
* @url POST register
* @url POST
*/
function postRegister(
$email, $password, $firstname, $lastname, $nickname, $phone, $birthdate, $school, $province, $level, $purpose, $avatarId)
{
//Hash password
$hash = password_hash($password, PASSWORD_DEFAULT);
$serial = sha1(uniqid());
$statement = "
INSERT INTO user
(email, role, hash, serial, firstname, lastname, nickname, phone, birthdate, school, province, level, purpose, avatarId)
VALUE
(:email, :role, :hash, :serial, :firstname, :lastname, :nickname, :phone, :birthdate, :school, :province, :level, :purpose, :avatarId)
";
$bind = array (
'email' => $email,
'role' => 'student',
'hash' => $hash,
'serial' => $serial,
'firstname' => $firstname,
'lastname' => $lastname,
'nickname' => $nickname,
'phone' => $phone,
'birthdate' => $birthdate,
'school' => $school,
'province' => $province,
'level' => $level,
'purpose' => $purpose,
'avatarId' => $avatarId
);
$count = \Db::execute($statement, $bind);
$userId = \Db::getLastInsertId();
\TTOMail::createAndSendAdmin('A new user registered', json_encode($bind));
\TTOMail::createAndSend(ADMINEMAIL, \TTO::getEmail(), 'You have registered to Too Tutor Online', 'Your serial number : ' + $serial);
$response = new \stdClass();
$response->count = $count;
$response->userId = $last_insert_id;
return $response;
}
/**
* @url GET profile/{userId}
* @url GET {userId}
*/
protected function getProfile($userId)
{
if ($userId == \TTO::getUserId() || \TTO::getRole() == 'admin') {
$statement = '
SELECT firstname, lastname, nickname, phone, birthdate, school, province, level, purpose, avatarId, coin, point, role, status
FROM user WHERE userId = :userId
';
$bind = array('userId' => $userId);
return \Db::getRow($statement, $bind);
} else {
throw new RestException(401, 'No Authorize or Invalid request !!!');
}
}
/**
* @url POST profile/{userId}
* @url PUT {userId}
*/
protected function postProfile(
$userId, $firstname, $lastname, $nickname, $phone, $birthdate, $school, $province, $level, $purpose, $avatarId)
{
if ($userId == \TTO::getUserId()) {
$statement = '
UPDATE user SET
firstname = :firstname,
lastname = :lastname,
nickname = :nickname,
phone = :phone,
birthdate = :birthdate,
school = :school,
province = :province,
level = :level,
purpose = :purpose,
avatarId = :avatarId
WHERE userId = :userId
';
$bind = array(
'firstname' => $firstname,
'lastname' => $lastname,
'nickname' => $nickname,
'phone' => $phone,
'birthdate' => $birthdate,
'school' => $school,
'province' => $province,
'level' => $level,
'purpose' => $purpose,
'avatarId' => $avatarId,
'userId' => $userId
);
$row_update = \Db::execute($statement, $bind);
\TTOMail::createAndSendAdmin('A user updated profile', json_encode($bind));
$response = new \stdClass();
$response->row_update = $row_update;
return $response;
} else {
throw new RestException(401, 'No Authorize or Invalid request !!!');
}
}
/**
* @url GET alluser
* @url GET
*/
protected function getAllUser()
{
if (\TTO::getRole() == 'admin') {
$statement = 'SELECT * FROM user';
return \Db::getResult($statement);
} else {
throw new RestException(401, 'No Authorize or Invalid request !!!');
}
}
/**
* @url GET allAvatar
*/
function getAllAvatar()
{
$statement = 'SELECT * FROM avatar';
return \Db::getResult($statement);
}
}