-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
135 lines (88 loc) · 2.71 KB
/
functions.php
File metadata and controls
135 lines (88 loc) · 2.71 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
<?php
/*********************************
* Get Saved Password by User
* -----------------------------
*
* @param: email -> string
**********************************/
function getStoredPassword($email){
require "database.php";
$stmt = $pdo->prepare("SELECT password FROM users WHERE email=:email");
$stmt->execute(['email' => $email]);
$result = $stmt->fetch();
return $result;
}
/*****************************
* Registration Function
* ---------------------
*
* @param: $entered_email -> string, $entered_username -> string, $entered_password -> string
*****************************/
function registerUser($entered_username, $entered_email, $hashed_password, $user_role){
require "database.php";
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, user_role) VALUES(:username, :email, :password, :user_role)");
$stmt->bindParam('username', $entered_username, PDO::PARAM_STR);
$stmt->bindParam('email', $entered_email, PDO::PARAM_STR);
$stmt->bindParam('password', $hashed_password, PDO::PARAM_STR);
$stmt->bindParam('user_role', $user_role, PDO::PARAM_STR);
$stmt->execute();
}
/*****************
* Get All Users
* -------------
*
*****************/
function getAllUsers(){
require "database.php";
$stmt = $pdo->query("SELECT * FROM users");
$result = $stmt->fetch();
return $result;
}
/******************
* Add Role
* ---------
*
* @param: $role_name, $permissions, $user_type
*******************/
function addRole($role_name, $permissions, $user_type){
require "database.php";
$stmt = $pdo->prepare("INSERT INTO user_roles (role_name, permissions, user_type) VALUES(:role_name, :permissions, :user_type)");
$stmt->bindParam('role_name', $role_name, PDO::PARAM_STR);
$stmt->bindParam('permissions', $permissions, PDO::PARAM_STR);
$stmt->bindParam('user_type', $user_type, PDO::PARAM_STR);
$stmt->execute();
}
/*************************************
* Update Password
* ---------------
*
* @param: $email, $new_password_hashed
**************************************/
function updatePassword($email, $new_password_hashed){
require "database.php";
$sql = "UPDATE users SET password = ? WHERE email = ?";
$pdo->prepare($sql)->execute([$new_password_hashed, $email]);
}
/*************************************
* Delete User
* ---------------
*
* @param: $id
**************************************/
function deleteUser($id){
require "database.php";
$stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
$stmt->execute([$id]);
echo "Deleted!";
}
/*****************
* Get All Roles
* -------------
*
*****************/
function getAllRoles(){
require "database.php";
$stmt = $pdo->query("SELECT * FROM user_roles");
$result = $stmt->fetch();
return $result;
}