Commit 197d5e64cd3d7c05de9aa85abb0af72426c3be88

Authored by michael
1 parent 1c7d4a29

initial revision


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@81 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/administration/UnitManager.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 <a href="mailto:michael@jamwarehouse.com>Michael Joseph</a>, Jam Warehouse (Pty) Ltd, South Africa
  12 + * @package dmslib
  13 + */
  14 +class UnitManager {
  15 +
  16 + /**
  17 + * Handle to the ldap util class
  18 + */
  19 + var $ldap;
  20 +
  21 + // user management
  22 +
  23 + /**
  24 + * Searches the LDAP directory for users matching the supplied search string.
  25 + *
  26 + * @param $userNameSearch
  27 + * the username to search for
  28 + * @return
  29 + * returns an array containing the users found
  30 + */
  31 + function listLdapUsers($userNameSearch) {
  32 + global $default;
  33 +
  34 + // user attributes to search for
  35 + $attributes = array ("dn", "uid", "givenname", "sn", "mail", "mobile");
  36 + // initialise the ldap connection
  37 + $ldap = new AuthLdap();
  38 + $server[0] = $default->ldapServer;
  39 + $ldap->server = $server;
  40 + $ldap->dn = $default->ldapRootDn;
  41 +
  42 + if ( $ldap->connect()) {
  43 + // search for the users
  44 + // append and prepend wildcards
  45 + $userArray = $ldap->getUsers("*" . $userNameSearch . "*", $attributes);
  46 + if ($userArray) {
  47 + // return the array
  48 + return $userArray;
  49 + } else {
  50 + // the search failed, bail
  51 + return false;
  52 + }
  53 + } else {
  54 + // ldap connection failed, bail
  55 + // TODO: error handling
  56 + return false;
  57 + /*
  58 + echo "There was a problem.<br>";
  59 + echo "Error code : " . $ldap->ldapErrorCode . "<br>";
  60 + echo "Error text : " . $ldap->ldapErrorText . "<br>";
  61 + */
  62 + }
  63 + }
  64 +
  65 + /**
  66 + * Adds a user to the unit.
  67 + *
  68 + * @param unitID
  69 + * the ID of the unit to add the user to
  70 + * @param userDetails
  71 + * an array containing the details of the user
  72 + * @return
  73 + * true if the addition was successful, else false.
  74 + */
  75 + function addUser($unitID, $userDetails) {
  76 + global $default;
  77 +
  78 + $sql = new Owl_DB;
  79 +
  80 + // TODO: userDetails validation
  81 +
  82 + // check that the username is unique
  83 + $query = "select username from $default->owl_users_table where username = '" . $userDetails['username'] . "'";
  84 + $sql->query($query);
  85 + $numrows = $sql->num_rows($sql);
  86 +
  87 + if ($numrows > 0) {
  88 + // duplicate username
  89 + $default->errorMessage = "The username " . $userDetails['username'] . " is already in use, please choose another one";
  90 + return false;
  91 + }
  92 + // insert the user
  93 + $query = "insert into $default->owl_users_table (username, name, password, email, mobile, ldap_dn) " .
  94 + "values ( '" . $userDetails['username'] . "', " .
  95 + "'" . $userDetails['name'] . "', " .
  96 + "'', " .
  97 + "'" . $userDetails['email'] . "', " .
  98 + "'" . $userDetails['mobile'] . "', " .
  99 + "'" . $userDetails['ldap_dn'] . "')";
  100 + $result = $sql->query($query);
  101 + if(!'result') {
  102 + return false;
  103 + } else {
  104 + echo "insert into user table worked!<br>";
  105 + }
  106 +
  107 + // TODO: must check that username is unique!
  108 + // retrieve the generated id for insert into the user unit mapping table
  109 + $query = "select * from $default->owl_users_table where username = '" . $userDetails['username'] . "'";
  110 + $sql->query($query);
  111 + $numrows = $sql->num_rows($sql);
  112 +
  113 + if ($numrows == "1") {
  114 + while($sql->next_record()) {
  115 + $userID = $sql->f("id");
  116 + echo "read userID=$userID from db<br>";
  117 + }
  118 + } else {
  119 + // select failed, bail
  120 + // FIXME: need a rollback here
  121 + echo "id select failed<br>";
  122 + return false;
  123 + }
  124 +
  125 + // now insert into the user-unit mapping table
  126 + $query = "insert into $default->owl_user_unit_table (user_id, unit_id) values ($userID, $unitID)";
  127 + $result = $sql->query($query);
  128 + if (!'result') {
  129 + // FIXME: rollback!
  130 + echo "insert into user-unit table failed<br>";
  131 + return false;
  132 + } else {
  133 + echo "insert into user-unit table worked!<br>";
  134 + }
  135 +
  136 + return true;
  137 + }
  138 +
  139 + /**
  140 + * @param unitID
  141 + * @param userID
  142 + */
  143 + function removeUser($unitID, $userID){
  144 + return false;
  145 + }
  146 +
  147 + /**
  148 + * @param userID
  149 + * @param userDetails
  150 + */
  151 + function updateUser($userID, $userDetails){
  152 + return false;
  153 + }
  154 +
  155 + function listUser(){
  156 + //return null;
  157 + }
  158 +
  159 + // group management
  160 +
  161 + /**
  162 + * @param name
  163 + */
  164 + function createGroup($name){
  165 + return false;
  166 + }
  167 +
  168 + /**
  169 + * @param groupID
  170 + */
  171 + function removeGroup($groupID){
  172 + return false;
  173 + }
  174 +
  175 + /**
  176 + * @param groupID
  177 + * @param name
  178 + */
  179 + function updateGroup($groupID, $name){
  180 + return false;
  181 + }
  182 +
  183 + function listGroup(){
  184 + //return null;
  185 + }
  186 +
  187 + /**
  188 + * @param groupID
  189 + * @param userID
  190 + */
  191 + function addUserToGroup($groupID, $userID){
  192 + return false;
  193 + }
  194 +
  195 + /**
  196 + * @param groupID
  197 + * @param userID
  198 + */
  199 + function removeUserFromGroup($groupID, $userID){
  200 + return false;
  201 + }
  202 +
  203 + // category management
  204 +
  205 + /**
  206 + * @param name
  207 + */
  208 + function createCategory($name){
  209 + return false;
  210 + }
  211 +
  212 + /**
  213 + * @param categoryID
  214 + */
  215 + function removeCategory($categoryID){
  216 + return false;
  217 + }
  218 +
  219 + /**
  220 + * @param name
  221 + * @param categoryID
  222 + */
  223 + function updateCategory($name, $categoryID){
  224 + return false;
  225 + }
  226 +
  227 + function listCategories(){
  228 + //return null;
  229 + }
  230 +
  231 +}
  232 +?>