-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.funcs.php
314 lines (290 loc) · 9.75 KB
/
user.funcs.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
include_once("db.php");
function getUserObjectById($id) {
$pdo = getPDO();
$statement = $pdo->prepare("SELECT * FROM tbl_user WHERE u_id = :uid");
$result = $statement->execute(array('uid' => $id));
$user = $statement->fetch();
return $user;
}
function createUser($name, $email, $password, $role) {
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$pdo = getPDO();
$statement = $pdo->prepare("INSERT INTO tbl_user (u_name, u_email, u_password, u_rights)
VALUES(?, ?, ?, ?)");
$result = $statement->execute(array($name, $email, $passwordHash, $role));
}
function deleteUser($id) {
$pdo = getPDO();
$statement = $pdo->prepare("DELETE FROM tbl_user WHERE u_id=:uid");
$result = $statement->execute(array('uid' => $id));
}
function updateUser($id, $name, $role) {
$pdo = getPDO();
$statement = $pdo->prepare("UPDATE tbl_user SET u_id = :uid, u_name = :uname, u_rights = :urole WHERE u_id=:uid");
$result = $statement->execute(array('uid' => $id, 'uname' => $name, 'urole' => $role));
}
function updateUserPassword($newPassword, $userid=0) {
if($userid==0) {
$userid = $_SESSION['userid'];
}
$passwordHash = password_hash($newPassword, PASSWORD_DEFAULT);
$pdo = getPDO();
$statement = $pdo->prepare("UPDATE tbl_user SET u_password= :upass WHERE u_id=:uid");
$result = $statement->execute(array('upass' => $passwordHash , 'uid' => $userid));
}
function passwordCorrect($currentPassword) {
$user = getUserObjectById($_SESSION['userid']);
return password_verify($currentPassword, $user['u_password']);
}
function getUserRoleName($roleId) {
switch($roleId) {
case 1:
return "Benutzer*in";
case 2:
return "Manager*in";
case 4:
return "Supervisor*in";
}
}
function userHasPermission($permissinNeeded) {
if($permissinNeeded<=$_SESSION['permissions']) {
return true;
}
return false;
}
function randomPassword() {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
function useremailExists($useremail) {
$pdo = getPDO();
$statement = $pdo->prepare("SELECT COUNT(*) as existing FROM tbl_user WHERE u_email = :uemail");
$result = $statement->execute(array('uemail' => $useremail));
$user = $statement->fetch();
if($user['existing']>0) {
return true;
}
else {
return false;
}
}
function usernameExists($username) {
$pdo = getPDO();
$statement = $pdo->prepare("SELECT COUNT(*) as existing FROM tbl_user WHERE u_name = :uname");
$result = $statement->execute(array('uname' => $username));
$user = $statement->fetch();
if($user['existing']>0) {
return true;
}
else {
return false;
}
}
function printUserData($message="") {
$user = getUserObjectById($_SESSION['userid']);
echo "<h2>Meine Daten</h2>";
if($message!="") {
echo "<div class=\"error\">".$message."</div><br/>";
}
echo "<table>
<tr>
<td>ID</td>
<td>".$user["u_id"]."</td>
<td class=\"info\">Die ID wird vom System vergeben.</td>
</tr>
<tr>
<td>Name</td>
<td>".$user["u_name"]."</td>
<td class=\"info\">Dieser Name wird angezeigt.</td>
</tr>
<tr>
<td>E-Mail</td>
<td>".$user["u_email"]."</td>
<td class=\"info\">Mit dieser Adresse wird kommuniziert.</td>
</tr>
<tr>
<td>Rolle</td>
<td>".getUserRoleName($user["u_rights"])."</td>
<td class=\"info\">Dies ist ihre Benutzerrolle.</td>
</tr>
</table>
<br/>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<h2>Passwort ändern</h2>
<table>
<tr>
<td>Altes Passwort</td>
<td><input type=\"password\" name=\"current_pw\" /></td>
<td class=\"info\"></td>
</tr>
<tr>
<td>Neues Passwort</td>
<td><input type=\"password\" name=\"new_pw\" /></td>
<td class=\"info\"></td>
</tr>
<tr>
<td>Neues Passwort wiederholen</td>
<td><input type=\"password\" name=\"new_pw2\" /></td>
<td class=\"info\"></td>
</tr>
<tr>
<td></td>
<td><input type=\"submit\" name=\"confirmNewPw\" value=\"Passwort ändern\"/></td>
<td class=\"info\"></td>
</tr>
</table>
<input type=\"hidden\" name=\"func\" value=\"user\" />
<input type=\"hidden\" name=\"subfunc\" value=\"myData\" />
</form>";
}
function printUserTable() {
$pdo = getPDO();
$sth = $pdo->prepare('SELECT * FROM tbl_user ORDER BY u_name ASC');
if($sth->execute()) {
if($sth->rowCount() > 0) {
echo "<div class=\"divScroll\">
<table class=\"resultlist\">
<tr>
<th>ID</th>
<th>Name</th>
<th>E-Mail</th>
<th>Rolle</th>
<th>Optionen</th>
</tr>";
$line = 1;
while($result = $sth->fetchObject()) {
$line++;
if($line%2==0) {
$class = " class=\"evenline\"";
}
else {
$class = "";
}
//print_r($result);
echo "<tr".$class.">
<td>".$result->u_id."</td>
<td>".$result->u_name."</td>
<td>".$result->u_email."</td>
<td>".getUserRoleName($result->u_rights)."</td>
<td>".getUsrOptions($result)."</td>
</tr>";
}
echo " </table>
</div>";
} else {
echo 'there are no result';
}
}
else {
echo 'there is error';
}
}
function printEditUser($id, $message="") {
$user = getUserObjectById($_REQUEST['uid']);
echo "<h2>Benutzer*in editieren</h2>";
if($message!="") {
echo "<div class=\"error\">".$message."</div><br/>";
}
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<table>
<tr>
<td>Name</td>
<td><input type=\"text\" name=\"u_name\" value=\"".$user['u_name']."\" /></td>
<td class=\"info\">Anzeigename der Benutzer*in.</td>
</tr>
<tr>
<td>Rolle</td>
<td>".getUserRoleSelect($user['u_rights'])."</td>
<td class=\"info\">Benutzer*innen können das Korpus herunterladen, die Manager*innen dürfen Dokumente erstellen, sowie erfassen. Supervisor*innen können zudem Benutzer erstellen und Dokumente löschen.</td>
</tr>
<tr>
<td></td>
<td><input type=\"submit\" value=\"Änderungen speichern\" /></td>
<td class=\"info\">Die Benutzer*in wird nicht benachrichtigt.</td>
</tr>
</table>
<input type=\"hidden\" name=\"func\" value=\"user\" />
<input type=\"hidden\" name=\"subfunc\" value=\"showUsers\" />
<input type=\"hidden\" name=\"action\" value=\"updateUserConfirm\" />
<input type=\"hidden\" name=\"u_id\" value=\"".$id."\" />
</form>";
}
function printAddUser($request, $message="") {
if(isset($request['newUserName'])) { $username = $request['newUserName']; } else{ $username=""; }
if(isset($request['newUserEmail'])) { $useremail = $request['newUserEmail']; } else{ $useremail=""; }
if(isset($request['newUserRole'])) { $userrole = $request['newUserRole']; } else{ $userrole=""; }
echo "<h2>Neue Benutzer*in anlegen</h2>";
if($message!="") {
echo "<div class=\"error\">".$message."</div><br/>";
}
echo "<p>
Legen Sie eine Benutzer*in an und wählen Sie ihre Rolle.
Die Benutzer*in wird per E-Mail benachrichtigt um auf das Konto zuzugreifen.
</p>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<table>
<tr>
<td>Name</td>
<td><input type=\"text\" name=\"newUserName\" value=\"".$username."\" /></td>
<td class=\"info\">Anzeigename der Benutzer*in.</td>
</tr>
<tr>
<td><nobr>E-Mail</nobr></td>
<td><input type=\"text\" name=\"newUserEmail\" value=\"".$useremail."\" /></td>
<td class=\"info\">E-Mail Adresse der Benutzer*in.</td>
</tr>
<tr>
<td>Rolle</td>
<td>".getUserRoleSelect($userrole)."</td>
<td class=\"info\">Benutzer*innen können das Korpus herunterladen, die Manager*innen dürfen Dokumente erstellen, sowie erfassen. Supervisor*innen können zudem Benutzer erstellen und Dokumente löschen.</td>
</tr>
<tr>
<td></td>
<td><input type=\"submit\" name=\"newUserConfirm\" value=\"Benutzer*in anlegen\" /></td>
<td class=\"info\">Die Benutzer*in erhält eine E-Mail um ihr Konto zu aktivieren.</td>
</tr>
</table>
<input type=\"hidden\" name=\"func\" value=\"user\" />
<input type=\"hidden\" name=\"subfunc\" value=\"newUser\" />
</form>";
}
function printUserAdded() {
echo "<h2>Eine Benutzer*in wurde hinzugefügt.</h2>
Die Benutzer*in wird per E-Mail benachrichtigt. <br/>";
}
function getUserRoleSelect($selected) {
$roles = array(1 => "Benutzer*in", 2=>"Manager*in", 4=>"Supervisor*in");
$ret = "<select name=\"u_role\" class=\"searchform\">";
foreach($roles as $key => $role) {
if($selected==$key) {
$sel = "selected=\"selected\"";
}
else {
$sel = "";
}
$ret .= " <option ".$sel." value=\"".$key."\">".$role."</option>";
}
$ret .= "</select>";
return $ret;
}
function getUsrOptions($result) {
$retVal = "";
if(userHasPermission($GLOBALS['user_perm_supervisor'])) {
$retVal .= "<a href=\"?func=user&subfunc=showUsers&action=edit&uid=".$result->u_id."\">Editieren</a> ";
}
if(userHasPermission($GLOBALS['user_perm_manager'])) {
$retVal .= "<a href=\"?func=user&subfunc=showUsers&action=pwreset&uid=".$result->u_id."\">Passwort zurücksetzen</a> ";
}
if(userHasPermission($GLOBALS['user_perm_supervisor']) && $result->u_id!=$_SESSION['userid']) {
$retVal .= "<a href=\"?func=user&subfunc=showUsers&action=delete&uid=".$result->u_id."\">Löschen</a> ";
}
return $retVal;
}
?>