-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-functions.php
197 lines (140 loc) · 3.88 KB
/
user-functions.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
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
189
190
191
192
193
194
195
196
197
<?php
include_once 'config.php';
if (isset($_POST['checkuser'])) {
$username = $_POST['email'];
$password = $_POST['password'];
login($username, $password, $pdo);
} elseif (isset($_POST['userlist'])) {
listUsers($pdo);
} elseif (isset($_POST['user_role'])) {
getRoles($pdo);
} elseif (isset($_POST['userSubmit'])) {
insertUser($pdo);
} elseif (isset($_GET['userId'])) {
populateUserFields($pdo);
} else {
logout();
}
function login($username, $password, $pdo)
{
session_start();
$stmt = $pdo->prepare("SELECT * FROM user_data WHERE email=:email");
$stmt->execute(['email' => $username]);
if ($stmt->rowCount() > 0) {
$user = $stmt->fetch();
$hashed_password = $user['password'];
if (password_verify($password, $hashed_password)) {
$_SESSION['user_found'] = 1;
$_SESSION['user']['user_id'] = $user['user_data_id'];
$_SESSION['user']['role'] = $user['user_role_id'];
$_SESSION['user']['first_name'] = $user['first_name'];
$_SESSION['user']['last_name'] = $user['last_name'];
$_SESSION['user']['email'] = $user['email'];
if ($_SESSION['user']['role'] == 2)
header("Location: dashboard.php");
else
header("Location: users-dashboard.php");
} else {
$_SESSION['user_found'] = 0;
header("Location: index.php");
}
} else {
$_SESSION['user_found'] = 0;
header("Location: index.php");
}
}
function logout()
{
session_start();
unset($_SESSION["user"]);
unset($_SESSION["user_found"]);
header("Location:index.php");
}
function getRoles($pdo)
{
$stmt = $pdo->prepare("SELECT user_role_id,name from user_role");
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$z = json_encode($result);
echo $z;
} else {
return '[]';
}
}
function listUsers($pdo)
{
$stmt = $pdo->prepare("SELECT
user_data_id,
first_name,
last_name,
email,
name as user_type
FROM user_data
inner join user_role
ON user_data.user_role_id=user_role.user_role_id");
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$z = json_encode($result);
echo $z;
} else {
echo '[]';
}
}
function insertUser($pdo)
{
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$user_role_id = $_POST['user_type'];
//create hashed password
$options = [
'cost' => 11
];
$hashed_password = password_hash($password, PASSWORD_BCRYPT, $options);
if ($_POST['actiontype'] == 'create') {
$sql = 'INSERT INTO user_data (first_name,last_name,email,password,user_role_id) VALUES (:first_name,:last_name,:email,:password,:user_role_id)';
} else if ($_POST['actiontype'] == 'update') {
$sql = 'UPDATE user_data set first_name=:first_name,last_name=:last_name,email=:email,password=:password,user_role_id=:user_role_id
where user_data_id =1';
//.$_SESSION['edit']['user_data_id'];
}
$stmt = $pdo->prepare($sql);
$stmt->execute([
':first_name' => $first_name,
':last_name' => $last_name,
':email' => $email,
':password' => $hashed_password,
':user_role_id' => $user_role_id
]);
$_SESSION['import_success'] = 1;
header("Location: users-dashboard.php");
}
function populateUserFields($pdo)
{
$user_data_id = $_GET['userId'];
$stmt = $pdo->prepare("SELECT
user_data_id,
first_name,
last_name,
email,
user_role_id
FROM user_data
where user_data_id=:user_data_id");
$stmt->execute(['user_data_id' => $user_data_id]);
if ($stmt->rowCount() > 0) {
$user = $stmt->fetch();
$_SESSION['edit']['user_data_id'] = $user['user_data_id'];
$_SESSION['edit']['firstname'] = $user['first_name'];
$_SESSION['edit']['lastname'] = $user['last_name'];
$_SESSION['edit']['email'] = $user['email'];
$_SESSION['edit']['user_role_id'] = $user['user_role_id'];
// $z = json_encode($result);
header("Location: user-form.php");
//return $z;
} else {
return '[]';
}
}