-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_user.php
77 lines (66 loc) · 2.21 KB
/
add_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
<?php
require_once 'includes/auth.php';
require_once 'includes/utilities.php';
// Get Auth instance and validate session
$auth = Auth::getInstance();
$auth->validateSession();
// Enforce 'manage_users' permission
try {
$auth->requirePermission('manage_users');
} catch (Exception $e) {
error_log("Permission denied: " . $e->getMessage());
header('Location: dashboard.php');
exit;
}
// Get PermissionManager and database connection
$permManager = $auth->getPermManager();
$conn = $auth->getConnection();
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
// Validate inputs
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$user_type = $_POST['user_type'];
// Validate email format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception("Invalid email format");
}
// Prepare SQL statement
$sql = "INSERT INTO tbl_users (name, email, password, user_type)
VALUES (:name, :email, :password, :user_type)";
$stmt = $conn->prepare($sql);
// Bind parameters and execute
if ($stmt->execute([
':name' => $name,
':email' => $email,
':password' => $password,
':user_type' => $user_type
])) {
// Notify admin about successful user addition
notifySystemError(
'User Added',
"New user added by admin {$auth->getUserEmail()}\n" .
"Name: $name\n" .
"Email: $email\n" .
"Type: $user_type",
__FILE__,
__LINE__
);
header('Location: admin_manage_account.php');
exit;
}
} catch (Exception $e) {
error_log("User addition error: " . $e->getMessage());
// Notify admin about error
notifySystemError(
'Database Error',
$e->getMessage(),
__FILE__,
__LINE__
);
die("An error occurred while adding user. Please try again later.");
}
}
?>