Commit cd7393e40fc61137e0ea01cfd5f845fd38161726

Authored by mukhtar
1 parent 90818efe

UserManager.inc file added

- added all functions for create, update and remove a User
- added function to Add and Remove a user from a group
- added function to remove a user from ALL groups
   (used in conjunction with remove user)
-  added function  getID from username
- changed and modified comments


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