forked from userfrosting/UserFrosting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_user.php
More file actions
194 lines (170 loc) · 5.63 KB
/
Copy pathupdate_user.php
File metadata and controls
194 lines (170 loc) · 5.63 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
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
<?php
/*
UserFrosting Version: 0.1
By Alex Weissman
Copyright (c) 2014
Based on the UserCake user management system, v2.0.2.
Copyright (c) 2009-2012
UserFrosting, like UserCake, is 100% free and open-source.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
require_once("models/config.php");
set_error_handler('logAllErrors');
// Recommended admin-only access
if (!securePage($_SERVER['PHP_SELF'])){
addAlert("danger", "Whoops, looks like you don't have permission to update a user.");
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array("errors" => 1, "successes" => 0));
} else {
header("Location: " . getReferralPage());
}
exit();
}
//Check if selected user exists
if(!isset($_POST['user_id']) or !userIdExists($_POST['user_id'])){
addAlert("danger", "I'm sorry, the user id you specified is invalid!");
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array("errors" => 1, "successes" => 0));
} else {
header("Location: " . getReferralPage());
}
exit();
}
// Required: id
$id = $_POST['user_id'];
if (!isset($_POST["csrf_token"]) or !$loggedInUser->csrf_validate(trim($_POST["csrf_token"]))){
$errors[] = lang("ACCESS_DENIED");
} else {
$userdetails = fetchUserDetails(NULL, NULL, $id); //Fetch user details
$userPermissions = fetchUserPermissions($id);
//Update display name
if ($userdetails['display_name'] != $_POST['display_name']){
$displayname = trim($_POST['display_name']);
//Validate display name
if(displayNameExists($displayname))
{
$errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
}
elseif(minMaxRange(1,50,$displayname))
{
$errors[] = lang("ACCOUNT_DISPLAY_CHAR_LIMIT",array(1,50));
}
else {
if (updateDisplayName($id, $displayname)){
$successes[] = lang("ACCOUNT_DISPLAYNAME_UPDATED", array($displayname));
}
else {
$errors[] = lang("SQL_ERROR");
}
}
}
else {
$displayname = $userdetails['display_name'];
}
//Update email
if ($userdetails['email'] != $_POST['email']){
$email = trim($_POST["email"]);
//Validate email
if(!isValidEmail($email))
{
$errors[] = lang("ACCOUNT_INVALID_EMAIL");
}
elseif(emailExists($email))
{
$errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
}
else {
if (updateEmail($id, $email)){
$successes[] = lang("ACCOUNT_EMAIL_UPDATED");
}
else {
$errors[] = lang("SQL_ERROR");
}
}
}
//Update title
if ($userdetails['title'] != $_POST['title']){
$title = trim($_POST['title']);
//Validate title
if(minMaxRange(1,50,$title))
{
$errors[] = lang("ACCOUNT_TITLE_CHAR_LIMIT",array(1,50));
}
else {
if (updateTitle($id, $title)){
$successes[] = lang("ACCOUNT_TITLE_UPDATED", array ($displayname, $title));
}
else {
$errors[] = lang("SQL_ERROR");
}
}
}
//Remove permission level
if(!empty($_POST['remove_permissions'])){
// Convert string of comma-separated permission_id's into array
$remove_permissions_arr = explode(',',$_POST['remove_permissions']);
$remove = array();
// Only allow removal if the user already has this permission
foreach ($remove_permissions_arr as $permission_id){
if (isset($userPermissions[$permission_id]))
$remove[$permission_id] = $permission_id;
}
if (count($remove) > 0) {
if ($deletion_count = removePermission($remove, $id)){
$successes[] = lang("ACCOUNT_PERMISSION_REMOVED", array ($deletion_count));
}
else {
$errors[] = lang("SQL_ERROR");
}
}
}
// Add permission levels
if(!empty($_POST['add_permissions'])){
// Convert string of comma-separated permission_id's into array
$add_permissions_arr = explode(',',$_POST['add_permissions']);
$add = array();
// Only allow adding if the user does NOT already have this permission
foreach ($add_permissions_arr as $permission_id){
if (!isset($userPermissions[$permission_id]))
$add[$permission_id] = $permission_id;
}
if (count($add) > 0) {
if ($addition_count = addPermission($add, $id)){
$successes[] = lang("ACCOUNT_PERMISSION_ADDED", array ($addition_count));
}
else {
$errors[] = lang("SQL_ERROR");
}
}
}
}
restore_error_handler();
foreach ($errors as $error){
addAlert("danger", $error);
}
foreach ($successes as $success){
addAlert("success", $success);
}
if (isset($_POST['ajaxMode']) and $_POST['ajaxMode'] == "true" ){
echo json_encode(array(
"errors" => count($errors),
"successes" => count($successes)));
} else {
header('Location: ' . getReferralPage());
exit();
}
?>