UserManager.inc
17.6 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
require_once("$default->owl_fs_root/lib/authentication/class.AuthLdap.php");
/**
* $Id$
*
* Performs user administration tasks- this includes create, remove, update
* as well as addUserToGroup and removeUserFromGroup ..etc
*
* @version $Revision$
* @author Mukhtar Dharsey
* @package lib.administration
*/
// uses function from groupamanager class
require_once ("$default->owl_fs_root/lib/administration/GroupManager.inc");
class UserManager
{
/**
* Handle to the ldap util class
*/
var $ldap;
// user management
/**
* Searches the LDAP directory for users matching the supplied search string.
*
* @param $userNameSearch
* the username to search for
* @return array
* returns an array containing the users found
*/
function listLdapUsers($userNameSearch)
{
global $default;
// user attributes to search for
$attributes = array ("dn", "uid", "givenname", "sn", "mail", "mobile");
// initialise the ldap connection
$ldap = new AuthLdap();
$server[0] = $default->ldapServer;
$ldap->server = $server;
$ldap->dn = $default->ldapRootDn;
if ( $ldap->connect())
{
// search for the users
// append and prepend wildcards
$userArray = $ldap->getUsers("*" . $userNameSearch . "*", $attributes);
if ($userArray)
{
// return the array
return $userArray;
}
else
{
// the search failed, bail
return false;
}
}
else
{
// $_SESSION["errorMessage"] = "LDAP error: (" . $ldap->ldapErrorCode ") " . $ldap->ldapErrorText;
return false;
}
}
/**
*
* Adds a user to the unit.
*
* @param unitID
* the ID of the unit to add the user to
* @param userDetails
* an array containing the details of the user
* @return boolean
* true if the addition was successful, else false.
*/
//-----------------------------------------------------------------
function createUser($userDetails) {
global $default;
$sql = new Owl_DB;
// check that the username is unique
$query = "SELECT username FROM $default->owl_users_table WHERE username = '" . $userDetails['username'] . "'";
$sql->query($query);
$rows = $sql->num_rows($sql);
if ($rows > 0)
{
// duplicate username
$default->errorMessage = "UserManager::The username " . $userDetails['username'] . " is already in use, please choose another one";
$default->log->debug($default->errorMessage);
return false;
}
// insert the user
$query = "INSERT INTO $default->owl_users_table (username, name, password, quota_max,quota_current, email, mobile, email_notification, sms_notification, ldap_dn,language,max_sessions) " .
"VALUES ( '" . $userDetails['username'] . "'," .
"'" . $userDetails['name'] . "'," .
"'" . $userDetails['password'] . "'," .
"'" . $userDetails['quota_max'] . "'," .
"'" . $userDetails['quota_current'] . "'," .
"'" . $userDetails['email'] . "'," .
"'" . $userDetails['mobile'] . "'," .
"'" . $userDetails['email_notification'] . "'," .
"'" . $userDetails['sms_notification'] . "'," .
"'" . $userDetails['ldap_dn'] . "'," .
"'" . $userDetails['language'] . "'," .
"'" . $userDetails['max_sessions'] . "'" .
")";
$result = $sql->query($query);
if(!'result')
{
$default->log->debug( "UserManager::Addition Unsuccessful!<br>");
return false;
}
else
{
$default->log->debug ("UserManager::User added Successfully!<br>");
}
return true;
/*
// TODO: insert into group table
//TODO: must check that username is unique!
//retrieve the generated id for insert into the user unit mapping table
$query = "select * from $default->owl_users_table where username = '" . $userDetails['username'] . "'";
$sql->query($query);
$numrows = $sql->num_rows($sql);
if ($numrows == "1") {
while($sql->next_record()) {
$userID = $sql->f("id");
$default->log->debug "read userID=$userID from db<br>";
}
} else {
select failed, bail
// FIXME: need a rollback here
$default->log->debug "id select failed<br>";
return false;
}
// now insert into the user-unit mapping table
$query = "insert into $default->owl_user_unit_table (user_id, unit_id) values ($userID, $unitID)";
$result = $sql->query($query);
if (!'result') {
// FIXME: rollback!
$default->log->debug "insert into user-unit table failed<br>";
return false;
}
else
{
$default->log->debug "insert into user-unit table worked!<br>";
}*/
}
/**
* Removes a user from the users table...since a user does not exist anymore..
* deletion from all its groups is also required
*
* @param unitID
* The ID of the unit to add the user to
* @param userID
* The Id of the User that must be deleted
* @return boolean
* True if the deletion was successful, else false if not or nonexistant.
*/
function removeUser($userID)
{
global $default;
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
// check if result was found..0 if not
if($row == 0)
{
$default->log->debug("UserManager::User does not exist in the database<br>");
return false;
}
//if user id exists delete it from the users table
$query = "DELETE FROM $default->owl_users_table WHERE id = $userID";
$result = $sql->query($query);
if(!'result')
{
$default->log->debug("UserManager::Deletion unsuccessful<br>");
return false;
}
else
{
$default->log->debug ("UserManager::Deletion from user table Successful<br>");
//check if belongs to group
$result= $this->removeUserFromAllGroups($userID);
return true;
}
}
/**
* Updates a users details
*
* @param userID
* the ID of the unit to add the user to
* @param userDetails
* an array containing the details of the user
* @return boolean
* true if the addition was successful, else false.
*/
function updateUser($userID, $userDetails)
{
global $default;
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
//if row = 0 ...then no entry was found..so return false
if($row == 0)
{
$default->log->debug("UserManager::User does not exist in the database<br>");
return false;
}
//if user id exists update all info into the users table
$query = "UPDATE $default->owl_users_table SET " .
" username = '" . $userDetails['username'] . "'" .
", name = '" . $userDetails['name'] . "'" .
", password = '" . $userDetails['password'] . "'" .
", quota_max = '" . $userDetails['quota_max'] ."'" .
", quota_current = '" . $userDetails['quota_current'] ."'" .
", email = '" . $userDetails['email'] . "'" .
", mobile = '" . $userDetails['mobile'] . "'" .
", email_notification = '" . $userDetails['email_notification'] . "'" .
", sms_notification = '" . $userDetails['sms_notification'] . "'" .
" WHERE id = $userID " ;
$result = $sql->query($query);
// error checking to see if success
if(!'result')
{
$default->log->debug("UserManager::Not Updated");
return false;
}
else
{
$default->log->debug("UserManager::Update Successful<br>");
return true;
}
}
/**
* Returns an array of all the usernames
*
* @return array
* An array of usernames
*/
function listUsers()
{
global $default;
$users = array ();
$i = 0;
// create a connection
$sql = new Owl_DB;
//Get list of all the usernames
$query = "SELECT username FROM $default->owl_users_table";
$result = $sql->query($query);
$row = $sql->num_rows($result);
if($row == 0)
{
$default->log->debug("UserManager::No users in table");
return false;
}
while($sql->next_record())
{
$users["$i"] = array("username" => $sql->f("username"));
$i++;
}
//return an array of the usernames
return $users;
}
/**
* Function getUserDetails($userID)
*
* Returns an array of all the details for a specified user.
*
* @return array
* An array of details of a specified user
*/
function getUserDetails($userID)
{
global $default;
$details = array();
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
if($row == 0)
{
$default->log->debug("UserManager::User does not exist in the database<br>");
return false;
}
while($sql->next_record())
{
$details[$sql->f("id")] = array("id" => $sql->f("id"),
"username" => $sql->f("username"),
"name" => $sql->f("name"),
"password" => $sql->f("password"),
"quota_max" => $sql->f("quota_max"),
"quota_current" => $sql->f("quota_current"),
"email" => $sql->f("email"),
"mobile" => $sql->f("mobile"),
"email_notification" => $sql->f("email_notification"),
"sms_notification" => $sql->f("sms_notification"));
}
//return an array of the usernames
return $details;
}
/**
*
* Adds a user to the group.
*
* @param group
* The ID of the group to add the user to
* @param userID
* The Id of the User that must be deleted
* @return boolean
* True if the addition was successful, else false if not or nonexistant.
*/
function addUserToGroup($userID,$groupID)
{
global $default;
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
if($row >= 1)
{
$default->log->debug("UserManager::User already belongs to group<br>");
return false;
}
//add user to the table
$query = "INSERT INTO $default->owl_users_groups_table (user_id, group_id) VALUES($userID, $groupID)" ;
$result = $sql->query($query);
if(!'result')
{
$default->log->debug("UserManager::Insertion into user_group table unsuccessful<br>");
return false;
}
else
{
$default->log->debug("UserManager::Insertion into user_group table Successful<br>");
return true;
}
}
/**
* Removes a user from a group
*
* @param groupID
* The ID of the group to remove the user from
* @param userID
* The Id of the User that must be removed from the table
* @return boolean
* True if the deletion was successful, else false if not or nonexistant.
*/
function removeUserFromGroup($userID,$groupID)
{
global $default;
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
// check if result was found..0 if not
if($row == 0)
{
$default->log->debug("UserManager::User does not exist in the database<br>");
return false;
}
//if user id exists delete it from the users table
$query = "DELETE FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
$result = $sql->query($query);
if(!'result')
{
$default->log->debug ("UserManager::Deletion unsuccessful<br>");
return false;
}
else
{
$default->log->debug("UserManager::Deletion from user_group_link table Successful<br>");
return true;
}
}
/**
* Removes a user from ALL groups it belongs to
*
* @param userID
* The Id of the User that must be removed from the table
* @return boolean
* True if the deletion was successful, else false if not or nonexistant.
*/
function removeUserFromAllGroups($userID)
{
global $default;
// create a connection
$sql = new Owl_DB;
//do validation that userid exists
$query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID";
$result = $sql->query($query);
$row = $sql->num_rows($result);
// check if result was found..0 if not
if($row == 0)
{
$default->log->debug("UserManager::User does not exist in the database<br>");
return false;
}
//if user id exists delete it from the users table
$query = "DELETE FROM $default->owl_users_groups_table WHERE user_id = $userID";
$result = $sql->query($query);
if(!'result')
{
$default->log->debug ("UserManager::Deletion unsuccessful<br>");
return false;
}
else
{
$default->log->debug ("UserManager::Deletion from user_group_link table Successful<br>");
return true;
}
}
/*
* Function getUserID($username)
*
* gets the id of a user using their username
*
* @param $username
* The username for which we want its ID
* @return Integer
* The username's Id
*/
function getUserID($username)
{
global $default;
$id = lookupID($default->owl_users_table, "name", $username);
return $id;
}
/*
* Function getGroups($userID)
*
* Gets the group that the user belongs to
*
* @param $userID
* The ID of the user
* @return Array
* array of groupID's and name
*/
function getGroups($userID)
{
global $default;
$groups = array();
$sql = new Owl_DB;
$groupName = new GroupManager;
// check that username exists if it does'nt return false
$query = "SELECT group_id FROM $default->owl_users_groups_table WHERE user_id = '" . $userID . "'";
$sql->query($query);
$rows = $sql->num_rows($sql);
// if no entry..user does not belong to any groups
if ($rows == 0)
{
// duplicate username
$default->errorMessage = "UserManager::The user does not belong to any groups<br>";
$default->log->debug($default->errorMessage);
return false;
}
$i =0;
while($sql->next_record())
{
$groups[$i] = array("id" => $sql->f("group_id"),
"name" => $groupName->getGroupName($sql->f("group_id"))
);
$i++;
}
return $groups;
}
}
?>