From: <jhe...@us...> - 2002-11-28 14:22:17
|
Update of /cvsroot/upcase-project/UpCase/lib In directory sc8-pr-cvs1:/tmp/cvs-serv2954 Modified Files: uc_accounts.php Added Files: uc_user.php uc_group.php Log Message: use of an account manager objectto handle user and group objects --- NEW FILE: uc_user.php --- <?php include_once("lib/uc_sql.php"); class UcUser { var $name; var $uid; var $gid; var $secGids; var $firstName; var $lastName; var $email; var $lang; var $accountMgr; var $newUser; function UcUser(&$accountMgr) { $this->accountMgr = $accountMgr; } function getSecondaryGroups() { return $this->secGids; } function setPassword($passwd) { $this->accountMgr->setUserPassword($this, $password); $password = ''; } function setLanguage($lang) { $this->lang = $lang; } function setPrimaryGroup($gid) { $this->gid = $gid; } function setSecondaryGroups($gids) { $this->secGids = $gids; } function setFirstName($name) { $this->firstName = $name; } function setLastName($name) { $this->lastName = $name; } function setEmail($email) { $this->email = $email; } } ?> --- NEW FILE: uc_group.php --- <?php class UcGroup { var $name; var $gid; var $accountMgr; function UcGroup(&$accountMgr, $name) { $this->name = $name; $this->accountMgr = $accountMgr; $this->gid = -1; } } ?> Index: uc_accounts.php =================================================================== RCS file: /cvsroot/upcase-project/UpCase/lib/uc_accounts.php,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** uc_accounts.php 8 Aug 2002 13:50:17 -0000 1.4 --- uc_accounts.php 28 Nov 2002 14:22:14 -0000 1.5 *************** *** 1,179 **** <?php ! include_once("lib/uc_sql.php"); ! ! class UcUser { - var $name; - var $uid; - var $gid; - var $lang; - var $groups; var $db; ! var $firstName; ! var $lastName; ! var $email; ! function UcUser() ! { ! $this->groups = array(); ! $this->db = new UcSql(); } ! function getSecondaryGroups() { ! global $ucsql_usergroups; ! $query = sprintf($ucsql_usergroups, $this->uid); ! ! $res = $this->db->Execute($query) or ! die("Unable to get groups: " . $db->ErrorMsg()); ! ! unset($this->groups); ! $this->groups = array(); ! ! while ($o = $res->FetchNextObject(true)) ! $this->groups[] = $o->GID; ! return $this->groups; } ! function setPassword($passwd) { ! global $ucsql_usersetpw; ! $query = sprintf($ucsql_usersetpw, $passwd, $this->uid); ! $this->db->Execute($query) or die("Unable to set user password: " ! . $this->db->ErrorMsg()); } ! function setLanguage($lang) { ! global $ucsql_usersetlang; ! $query = sprintf($ucsql_usersetlang, $lang, $this->uid); ! $this->db->Execute($query) or die("Unable to set user lang: " ! . $this->db->ErrorMsg()); ! $this->lang = $lang; } ! function setPrimaryGroup($gid) { ! global $ucsql_usersetgid; ! $query = sprintf($ucsql_usersetgid, $gid, $this->uid); ! $res = $this->db->Execute($query) ! or die("Unable to set user primary group: " ! . $this->db->ErrorMsg()); ! $this->gid = $gid; } ! function setSecondaryGroups($gids) { ! global $ucsql_memberrm; ! global $ucsql_memberadd; ! ! $query = sprintf($ucsql_memberrm, $this->uid); ! $this->db->Execute($query) or die("Unable to reset user memberships: " ! . $this->db->ErrorMsg()); ! foreach ($gids as $gid) ! { ! $query = sprintf($ucsql_memberadd, $gid,$this->uid); ! $this->db->Execute($query) or die("Unable to add membership: " ! . $thid->db->ErrorMsg()); ! } ! ! $this->groups = $gids; } ! function setFirstName($name) { ! global $ucsql_userfirstname; ! $query = sprintf($ucsql_userfirstname, $name, $this->uid); ! $this->db->Execute($query) or die("Unable to set user's first name: " ! . $this->db->ErrorMsg()); ! $this->firstName = $name; } ! ! function setLastName($name) { ! global $ucsql_userlastname; ! $query = sprintf($ucsql_userlastname, $name, $this->uid); ! $this->db->Execute($query) or die("Unable to set user's last name : " ! . $this->db->ErrorMsg()); ! $this->lastName = $name; } ! ! function setEmail($email) { ! global $ucsql_useremail; ! $query = sprintf($ucsql_useremail, $email, $this->uid); ! $this->db->Execute($query) or die("Unable to set user's email: " ! . $this->db->ErrorMsg()); ! $this->email = $email; } - } - ! // Fill a user object with data retrieved from the database ! function getUser($username, $uid) ! { ! global $ucsql_userget; ! ! $db = new UcSQL(); ! ! // retrieving the user ! $query = sprintf($ucsql_userget, $username, $uid); ! $res = $db->Execute($query) or die("Unable to get user $username, $uid: " ! . $db->ErrorMsg()); ! if ($res->RecordCount() != 1) { ! print("FOUND: " . $res->RecordCount() . "<br>"); ! die("Problem with your database: more than one user with same uid"); } ! $o = $res->FetchNextObject(TRUE); ! ! $user = new UcUser(); ! $user->name = $o->USERNAME; ! $user->uid = $o->UID; ! $user->gid = $o->GROUPID; ! $user->lang = $o->PREFERRED_LANG; ! $user->groups = $user->getSecondaryGroups(); ! $user->firstName = $o->FIRSTNAME; ! $user->lastName = $o->LASTNAME; ! $user->email = $o->EMAIL; ! return $user; ! } ! // Create a new user in the database ! function createUser($username) ! { ! global $ucsql_useradd; ! global $ucsql_userget; ! $db = new UcSql(); ! $query = sprintf($ucsql_useradd, $username); ! $db->Execute($query) or die("Unable to create new user: " ! . $db->ErrorMsg()); ! ! return getUser($username, ''); ! } ! ! ! function getAllGroups() ! { ! global $ucsql_allgroups; ! $db = new UcSql(); ! $query = $ucsql_allgroups; ! $res = $db->Execute($query) or die("Unable to get group list: " ! . $db->ErrorMsg()); ! $groups = array(); ! while ($o = $res->FetchNextObject(TRUE)) { ! $groups[$o->GID] = $o->GROUPNAME; } - return $groups; } --- 1,246 ---- <?php + include_once("uc_sql.php"); + include_once("uc_user.php"); + include_once("uc_group.php"); ! class UcAccountManager { var $db; ! var $cfg; ! ! function UcAccountManager(&$db) ! { ! $this->cfg = $GLOBALS["ucConfig"]; ! if ($db == null) ! $this->db = new UcSql(); ! else ! $this->db = $db; ! } ! // Users account methods ! ! // Factory method : Create a new user ! function createUser($username) ! { ! $user = new UcUser($this); ! $user->name = $username; ! $user->gid = $this->cfg->defaultGroup; ! $user->lang = $this->cfg->defaultLang; ! $user->secGids = array(); ! $user->lastName = ''; ! $user->firstName = ''; ! $user->email = ''; ! $user->uid = -1; ! $user->newUser = true; ! return $user; } ! function anonymousUser() { ! $user = new UcUser($this); ! $user->name = "anonymous"; ! $user->uid = -1; ! $user->gid = -1; ! $user->lang = $this->cfg->defaultLang; ! $user->groups = array(); ! return $user; } ! ! function setSecondaryGroups(&$user) { ! if ($user->uid == -1) ! $u = $this->getUser($user->name); ! else ! $u =& $user; ! $this->removeMember($u); ! if (count($u->secGids) > 0) ! { ! $ar = array(); ! foreach ($u->secGids as $gid) ! $ar[] = "('" . $gid . "', '" . $u->uid . "')"; ! $gidsStr = join(",", $ar); ! $query = $GLOBALS["ucsql_memberadd"] . $gidsStr; ! $this->db->Execute($query) ! or die("Unable to set memberships for user: " ! . $this->db->ErrorMsg()); ! } ! } ! function getSecondaryGroups(&$user) ! { ! $query = sprintf($GLOBALS["ucsql_memberget"], $user->uid); ! $res = $this->db->Execute($query) ! or die("Unable to get user memberships: " ! . $this->db->ErrorMsg()); ! $gids = array(); ! while (($o = $res->FetchNextObject(TRUE))) ! $gids[] = $o->GID; ! return $gids; } ! // add a user to the database ! // return a new user object with data retrieve from the db ! function addUser($user) { ! $query = sprintf($GLOBALS["ucsql_useradd"], ! $user->name, $user->gid, $user->lastName, $user->firstName, ! $user->lang, $user->email); ! $this->db->Execute($query) ! or die("Unable to add user: " . $this->db->ErrorMsg()); ! $this->setSecondaryGroups($user); ! return $this->getUser($user->name); ! } ! // update user data in the database ! // return a new user object with data retrieved from the database ! function updateUser($user) ! { ! $query = sprintf($GLOBALS["ucsql_userupdate"], ! $user->name, $user->gid, $user->lastName, $user->firstName, ! $user->lang, $user->email, $user->uid); ! print("<B>QUERY: $query</B><br>"); ! $this->db->Execute($query) ! or die("Unable to update user: " . $this->db->ErrorMsg()); ! $this->setSecondaryGroups($user); ! return $this->getUser($user->name); ! } ! // Fill a user object with data retrieved from the database ! function getUser($username, $uid = '') ! { ! // retrieving the user ! $query = sprintf($GLOBALS["ucsql_userget"], $username, $uid); ! $res = $this->db->Execute($query) ! or die("Unable to get user $username, $uid: " ! . $this->db->ErrorMsg()); ! if ($res->RecordCount() != 1) ! die("Problem with your database: more than one user with same uid"); ! ! $o = $res->FetchNextObject(TRUE); ! ! $user = new UcUser($this); ! $user->name = $o->USERNAME; ! $user->uid = $o->UID; ! $user->gid = $o->GROUPID; ! $user->lang = $o->PREFERRED_LANG; ! $user->firstName = $o->FIRSTNAME; ! $user->lastName = $o->LASTNAME; ! $user->email = $o->EMAIL; ! $user->secGids = $this->getSecondaryGroups($user); ! $user->newUser = false; ! return $user; } ! function deleteUser(&$user) { ! $this->removeMember($user); ! $query = sprintf($GLOBALS["ucsql_userdel"], $user->uid); ! $this->db->Execute($query) ! or die("Unable to remove user: " . $this->db->ErrorMsg()); ! } ! function listUsers() ! { ! $query = $GLOBALS["ucsql_allusers"]; ! $res = $this->db->Execute($query) ! or die("Unable to list users: " . $this->db->ErrorMsg()); ! $ret = array(); ! while (($o = $res->FetchNextObject(true))) ! $ret[] = $o->USERNAME; ! return $ret; } ! function setUserPassword($user, $password) { ! $query = sprintf($GLOBALS["ucsql_usersetpw"], $password, $user->uid); ! $password = ''; ! $this->db->Execute($query) ! or die("Unable to set password: " . $this->db->ErrorMsg()); } ! function checkUserPassword($username, $password) { ! $query = sprintf($GLOBALS["ucsql_usercheckpw"], $username, $password); ! $res = $this->db->Execute($query) ! or die("Unable to check password: " . $this->db->ErrorMsg()); ! if ($res->RowCount() != 1) ! return false; ! else ! return true; } ! ! // Groups methods ! function createGroup($name) { ! $g = new UcGroup($this, $name); ! return $g; } ! ! function addGroup($group) { ! $query = sprintf($GLOBALS["ucsql_groupadd"], $group->name); ! $this->db->Execute($query) ! or die("Unable to create group $name: " . $this->db->ErrorMsg()); ! return $this->getGroup($group->name); } ! function getGroup($name, $gid = '') { ! $query = sprintf($GLOBALS["ucsql_groupget"], $name, $gid); ! $res = $this->db->Execute($query) ! or die("Unable to get group $name, $gid: " ! . $this->db->ErrorMsg()); ! if ($res->RowCount() != 1) ! die("Problem with your DB, more than one group fetched"); ! $o = $res->FetchNextObject(true); ! $g = new UcGroup($this, $o->GROUPNAME); ! $g->gid = $o->GID; ! return $g; } ! function removeMember(&$user) ! { ! $query = sprintf($GLOBALS["ucsql_memberrm"], $user->uid); ! $this->db->Execute($query) ! or die("Unable to remove memberships: " . $this->db->ErrorMsg()); ! } ! function isPrimaryGroup($group) ! { ! $query = sprintf($GLOBALS["ucsql_primarygroup"], $group->name); ! $res = $this->db->Execute($query) ! or die("Unable to check if group $group->name is primary group: " ! . $this->db->ErrorMsg()); ! if ($res->RowCount() == 0) ! return false; ! else ! return true; ! } ! function deleteGroup($group) ! { ! if ($this->isPrimaryGroup($group)) ! return false; ! $query = sprintf($GLOBALS["ucsql_groupdel"], $group->gid); ! $this->db->Execute($query) ! or die("Unable to delete group $group->name: " ! . $this->db->ErrorMsg()); ! return true; ! } ! function listGroups() { ! global $ucsql_allgroups; ! $db = new UcSql(); ! $query = $ucsql_allgroups; ! $res = $db->Execute($query) or die("Unable to get group list: " ! . $db->ErrorMsg()); ! $groups = array(); ! while ($o = $res->FetchNextObject(TRUE)) ! $groups[$o->GID] = $o->GROUPNAME; ! return $groups; } } |