Commit 6a12010c7da6914948dab0efa91781b4c942e9ee

Authored by rob
1 parent 0aa81c92

Replaced by user class in /lib/users/User.inc


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@416 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/administration/UserManager.inc deleted
1   -<?php
2   -
3   -require_once("$default->owl_fs_root/lib/authentication/class.AuthLdap.php");
4   -
5   -/**
6   - * $Id$
7   - *
8   - * Performs user administration tasks- this includes create, remove, update
9   - * as well as addUserToGroup and removeUserFromGroup ..etc
10   - *
11   - * @version $Revision$
12   - * @author Mukhtar Dharsey
13   - * @package lib.administration
14   - */
15   -
16   -// uses function from groupamanager class
17   -require_once ("$default->owl_fs_root/lib/administration/GroupManager.inc");
18   -
19   -class UserManager
20   - {
21   -
22   - /**
23   - * Handle to the ldap util class
24   - */
25   - var $ldap;
26   -
27   - // user management
28   -
29   - /**
30   - * Searches the LDAP directory for users matching the supplied search string.
31   - *
32   - * @param $userNameSearch
33   - * the username to search for
34   - * @return array
35   - * returns an array containing the users found
36   - */
37   - function listLdapUsers($userNameSearch)
38   - {
39   - global $default;
40   -
41   - // user attributes to search for
42   - $attributes = array ("dn", "uid", "givenname", "sn", "mail", "mobile");
43   - // initialise the ldap connection
44   - $ldap = new AuthLdap();
45   - $server[0] = $default->ldapServer;
46   - $ldap->server = $server;
47   - $ldap->dn = $default->ldapRootDn;
48   -
49   - if ( $ldap->connect())
50   - {
51   - // search for the users
52   - // append and prepend wildcards
53   - $userArray = $ldap->getUsers("*" . $userNameSearch . "*", $attributes);
54   - if ($userArray)
55   - {
56   - // return the array
57   - return $userArray;
58   - }
59   - else
60   - {
61   - // the search failed, bail
62   - return false;
63   - }
64   - }
65   - else
66   - {
67   - // $_SESSION["errorMessage"] = "LDAP error: (" . $ldap->ldapErrorCode ") " . $ldap->ldapErrorText;
68   - return false;
69   - }
70   - }
71   -
72   - /**
73   - *
74   - * Adds a user to the unit.
75   - *
76   - * @param unitID
77   - * the ID of the unit to add the user to
78   - * @param userDetails
79   - * an array containing the details of the user
80   - * @return boolean
81   - * true if the addition was successful, else false.
82   - */
83   - //-----------------------------------------------------------------
84   - function createUser($userDetails) {
85   - global $default;
86   -
87   - $sql = new Owl_DB;
88   -
89   -
90   - // check that the username is unique
91   - $query = "SELECT username FROM $default->owl_users_table WHERE username = '" . $userDetails['username'] . "'";
92   - $sql->query($query);
93   - $rows = $sql->num_rows($sql);
94   -
95   - if ($rows > 0)
96   - {
97   - // duplicate username
98   - $default->errorMessage = "UserManager::The username " . $userDetails['username'] . " is already in use, please choose another one";
99   - $default->log->debug($default->errorMessage);
100   - return false;
101   - }
102   - // insert the user
103   - $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) " .
104   - "VALUES ( '" . $userDetails['username'] . "'," .
105   - "'" . $userDetails['name'] . "'," .
106   - "'" . $userDetails['password'] . "'," .
107   - "'" . $userDetails['quota_max'] . "'," .
108   - "'" . $userDetails['quota_current'] . "'," .
109   - "'" . $userDetails['email'] . "'," .
110   - "'" . $userDetails['mobile'] . "'," .
111   - "'" . $userDetails['email_notification'] . "'," .
112   - "'" . $userDetails['sms_notification'] . "'," .
113   - "'" . $userDetails['ldap_dn'] . "'," .
114   - "'" . $userDetails['language'] . "'," .
115   - "'" . $userDetails['max_sessions'] . "'" .
116   - ")";
117   -
118   - $result = $sql->query($query);
119   -
120   - if(!'result')
121   - {
122   - $default->log->debug( "UserManager::Addition Unsuccessful!<br>");
123   - return false;
124   - }
125   - else
126   - {
127   - $default->log->debug ("UserManager::User added Successfully!<br>");
128   - }
129   - return true;
130   -
131   -
132   - /*
133   - // TODO: insert into group table
134   -
135   - //TODO: must check that username is unique!
136   - //retrieve the generated id for insert into the user unit mapping table
137   - $query = "select * from $default->owl_users_table where username = '" . $userDetails['username'] . "'";
138   - $sql->query($query);
139   - $numrows = $sql->num_rows($sql);
140   -
141   - if ($numrows == "1") {
142   - while($sql->next_record()) {
143   - $userID = $sql->f("id");
144   - $default->log->debug "read userID=$userID from db<br>";
145   - }
146   - } else {
147   - select failed, bail
148   - // FIXME: need a rollback here
149   - $default->log->debug "id select failed<br>";
150   - return false;
151   - }
152   -
153   - // now insert into the user-unit mapping table
154   - $query = "insert into $default->owl_user_unit_table (user_id, unit_id) values ($userID, $unitID)";
155   - $result = $sql->query($query);
156   - if (!'result') {
157   - // FIXME: rollback!
158   - $default->log->debug "insert into user-unit table failed<br>";
159   - return false;
160   - }
161   - else
162   - {
163   - $default->log->debug "insert into user-unit table worked!<br>";
164   - }*/
165   -
166   -
167   - }
168   -
169   - /**
170   - * Removes a user from the users table...since a user does not exist anymore..
171   - * deletion from all its groups is also required
172   - *
173   - * @param unitID
174   - * The ID of the unit to add the user to
175   - * @param userID
176   - * The Id of the User that must be deleted
177   - * @return boolean
178   - * True if the deletion was successful, else false if not or nonexistant.
179   - */
180   - function removeUser($userID)
181   - {
182   - global $default;
183   - // create a connection
184   - $sql = new Owl_DB;
185   -
186   - //do validation that userid exists
187   - $query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
188   - $result = $sql->query($query);
189   - $row = $sql->num_rows($result);
190   -
191   - // check if result was found..0 if not
192   - if($row == 0)
193   - {
194   - $default->log->debug("UserManager::User does not exist in the database<br>");
195   - return false;
196   - }
197   -
198   - //if user id exists delete it from the users table
199   - $query = "DELETE FROM $default->owl_users_table WHERE id = $userID";
200   - $result = $sql->query($query);
201   -
202   - if(!'result')
203   - {
204   - $default->log->debug("UserManager::Deletion unsuccessful<br>");
205   - return false;
206   - }
207   - else
208   - {
209   - $default->log->debug ("UserManager::Deletion from user table Successful<br>");
210   - //check if belongs to group
211   - $result= $this->removeUserFromAllGroups($userID);
212   - return true;
213   - }
214   -
215   -
216   -
217   - }
218   -
219   - /**
220   - * Updates a users details
221   - *
222   - * @param userID
223   - * the ID of the unit to add the user to
224   - * @param userDetails
225   - * an array containing the details of the user
226   - * @return boolean
227   - * true if the addition was successful, else false.
228   - */
229   - function updateUser($userID, $userDetails)
230   - {
231   - global $default;
232   - // create a connection
233   - $sql = new Owl_DB;
234   -
235   - //do validation that userid exists
236   - $query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
237   - $result = $sql->query($query);
238   - $row = $sql->num_rows($result);
239   -
240   - //if row = 0 ...then no entry was found..so return false
241   - if($row == 0)
242   - {
243   - $default->log->debug("UserManager::User does not exist in the database<br>");
244   - return false;
245   - }
246   -
247   - //if user id exists update all info into the users table
248   - $query = "UPDATE $default->owl_users_table SET " .
249   - " username = '" . $userDetails['username'] . "'" .
250   - ", name = '" . $userDetails['name'] . "'" .
251   - ", password = '" . $userDetails['password'] . "'" .
252   - ", quota_max = '" . $userDetails['quota_max'] ."'" .
253   - ", quota_current = '" . $userDetails['quota_current'] ."'" .
254   - ", email = '" . $userDetails['email'] . "'" .
255   - ", mobile = '" . $userDetails['mobile'] . "'" .
256   - ", email_notification = '" . $userDetails['email_notification'] . "'" .
257   - ", sms_notification = '" . $userDetails['sms_notification'] . "'" .
258   - " WHERE id = $userID " ;
259   -
260   - $result = $sql->query($query);
261   -
262   -
263   - // error checking to see if success
264   - if(!'result')
265   - {
266   - $default->log->debug("UserManager::Not Updated");
267   - return false;
268   - }
269   - else
270   - {
271   - $default->log->debug("UserManager::Update Successful<br>");
272   - return true;
273   - }
274   - }
275   -
276   - /**
277   - * Returns an array of all the usernames
278   - *
279   - * @return array
280   - * An array of usernames
281   - */
282   - function listUsers()
283   - {
284   -
285   - global $default;
286   - $users = array ();
287   - $i = 0;
288   - // create a connection
289   - $sql = new Owl_DB;
290   -
291   - //Get list of all the usernames
292   - $query = "SELECT username FROM $default->owl_users_table";
293   - $result = $sql->query($query);
294   - $row = $sql->num_rows($result);
295   -
296   - if($row == 0)
297   - {
298   - $default->log->debug("UserManager::No users in table");
299   - return false;
300   - }
301   -
302   - while($sql->next_record())
303   - {
304   - $users["$i"] = array("username" => $sql->f("username"));
305   - $i++;
306   - }
307   - //return an array of the usernames
308   - return $users;
309   -
310   - }
311   -
312   - /**
313   - * Function getUserDetails($userID)
314   - *
315   - * Returns an array of all the details for a specified user.
316   - *
317   - * @return array
318   - * An array of details of a specified user
319   - */
320   - function getUserDetails($userID)
321   - {
322   -
323   - global $default;
324   - $details = array();
325   - // create a connection
326   - $sql = new Owl_DB;
327   -
328   - //do validation that userid exists
329   - $query = "SELECT * FROM $default->owl_users_table WHERE id = $userID";
330   - $result = $sql->query($query);
331   - $row = $sql->num_rows($result);
332   -
333   - if($row == 0)
334   - {
335   - $default->log->debug("UserManager::User does not exist in the database<br>");
336   - return false;
337   - }
338   -
339   - while($sql->next_record())
340   - {
341   - $details[$sql->f("id")] = array("id" => $sql->f("id"),
342   - "username" => $sql->f("username"),
343   - "name" => $sql->f("name"),
344   - "password" => $sql->f("password"),
345   - "quota_max" => $sql->f("quota_max"),
346   - "quota_current" => $sql->f("quota_current"),
347   - "email" => $sql->f("email"),
348   - "mobile" => $sql->f("mobile"),
349   - "email_notification" => $sql->f("email_notification"),
350   - "sms_notification" => $sql->f("sms_notification"));
351   - }
352   -
353   - //return an array of the usernames
354   - return $details;
355   -
356   - }
357   -
358   - /**
359   - *
360   - * Adds a user to the group.
361   - *
362   - * @param group
363   - * The ID of the group to add the user to
364   - * @param userID
365   - * The Id of the User that must be deleted
366   - * @return boolean
367   - * True if the addition was successful, else false if not or nonexistant.
368   - */
369   - function addUserToGroup($userID,$groupID)
370   - {
371   - global $default;
372   - // create a connection
373   - $sql = new Owl_DB;
374   -
375   - //do validation that userid exists
376   - $query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
377   - $result = $sql->query($query);
378   - $row = $sql->num_rows($result);
379   -
380   - if($row >= 1)
381   - {
382   - $default->log->debug("UserManager::User already belongs to group<br>");
383   - return false;
384   - }
385   -
386   - //add user to the table
387   - $query = "INSERT INTO $default->owl_users_groups_table (user_id, group_id) VALUES($userID, $groupID)" ;
388   - $result = $sql->query($query);
389   -
390   - if(!'result')
391   - {
392   - $default->log->debug("UserManager::Insertion into user_group table unsuccessful<br>");
393   - return false;
394   - }
395   - else
396   - {
397   - $default->log->debug("UserManager::Insertion into user_group table Successful<br>");
398   - return true;
399   - }
400   -
401   - }
402   -
403   -
404   - /**
405   - * Removes a user from a group
406   - *
407   - * @param groupID
408   - * The ID of the group to remove the user from
409   - * @param userID
410   - * The Id of the User that must be removed from the table
411   - * @return boolean
412   - * True if the deletion was successful, else false if not or nonexistant.
413   - */
414   - function removeUserFromGroup($userID,$groupID)
415   - {
416   - global $default;
417   - // create a connection
418   - $sql = new Owl_DB;
419   -
420   - //do validation that userid exists
421   - $query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
422   - $result = $sql->query($query);
423   - $row = $sql->num_rows($result);
424   -
425   - // check if result was found..0 if not
426   - if($row == 0)
427   - {
428   - $default->log->debug("UserManager::User does not exist in the database<br>");
429   - return false;
430   - }
431   -
432   - //if user id exists delete it from the users table
433   - $query = "DELETE FROM $default->owl_users_groups_table WHERE user_id = $userID AND group_id = $groupID";
434   - $result = $sql->query($query);
435   -
436   - if(!'result')
437   - {
438   - $default->log->debug ("UserManager::Deletion unsuccessful<br>");
439   - return false;
440   - }
441   - else
442   - {
443   - $default->log->debug("UserManager::Deletion from user_group_link table Successful<br>");
444   - return true;
445   - }
446   -
447   - }
448   -
449   - /**
450   - * Removes a user from ALL groups it belongs to
451   - *
452   - * @param userID
453   - * The Id of the User that must be removed from the table
454   - * @return boolean
455   - * True if the deletion was successful, else false if not or nonexistant.
456   - */
457   - function removeUserFromAllGroups($userID)
458   - {
459   - global $default;
460   - // create a connection
461   - $sql = new Owl_DB;
462   -
463   - //do validation that userid exists
464   - $query = "SELECT * FROM $default->owl_users_groups_table WHERE user_id = $userID";
465   - $result = $sql->query($query);
466   - $row = $sql->num_rows($result);
467   -
468   - // check if result was found..0 if not
469   - if($row == 0)
470   - {
471   - $default->log->debug("UserManager::User does not exist in the database<br>");
472   - return false;
473   - }
474   -
475   - //if user id exists delete it from the users table
476   - $query = "DELETE FROM $default->owl_users_groups_table WHERE user_id = $userID";
477   - $result = $sql->query($query);
478   -
479   - if(!'result')
480   - {
481   - $default->log->debug ("UserManager::Deletion unsuccessful<br>");
482   - return false;
483   - }
484   - else
485   - {
486   - $default->log->debug ("UserManager::Deletion from user_group_link table Successful<br>");
487   - return true;
488   - }
489   -
490   - }
491   -
492   -
493   - /*
494   - * Function getUserID($username)
495   - *
496   - * gets the id of a user using their username
497   - *
498   - * @param $username
499   - * The username for which we want its ID
500   - * @return Integer
501   - * The username's Id
502   - */
503   -
504   - function getUserID($username)
505   - {
506   - global $default;
507   -
508   - $id = lookupID($default->owl_users_table, "name", $username);
509   -
510   - return $id;
511   - }
512   -
513   - /*
514   - * Function getGroups($userID)
515   - *
516   - * Gets the group that the user belongs to
517   - *
518   - * @param $userID
519   - * The ID of the user
520   - * @return Array
521   - * array of groupID's and name
522   - */
523   - function getGroups($userID)
524   - {
525   - global $default;
526   - $groups = array();
527   - $sql = new Owl_DB;
528   - $groupName = new GroupManager;
529   -
530   -
531   - // check that username exists if it does'nt return false
532   - $query = "SELECT group_id FROM $default->owl_users_groups_table WHERE user_id = '" . $userID . "'";
533   - $sql->query($query);
534   - $rows = $sql->num_rows($sql);
535   -
536   - // if no entry..user does not belong to any groups
537   - if ($rows == 0)
538   - {
539   - // duplicate username
540   - $default->errorMessage = "UserManager::The user does not belong to any groups<br>";
541   - $default->log->debug($default->errorMessage);
542   - return false;
543   - }
544   -
545   - $i =0;
546   -
547   - while($sql->next_record())
548   - {
549   - $groups[$i] = array("id" => $sql->f("group_id"),
550   - "name" => $groupName->getGroupName($sql->f("group_id"))
551   - );
552   - $i++;
553   - }
554   -
555   - return $groups;
556   - }
557   -
558   -
559   -
560   -}
561   -?>