From d4b6bfc753d50467be397eaaccb86afb4cf708d2 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 5 Mar 2007 04:37:29 +0000 Subject: [PATCH] r1427: More work on the authentication system: - Adding a list of fields that will be synced between Bugdar and the auth db - Authentication::_createBugdarUser() is now implemented in the abstract class because we have a sync-fields list - Authentication::_syncBugdarUser() is responsible for syncing the already-created Bugdar user with the user in the auth db --- includes/auth/auth.php | 79 +++++++++++++++++++++++++++----- includes/auth/auth_default.php | 3 ++ includes/auth/auth_vbulletin.php | 45 ++++++------------ 3 files changed, 83 insertions(+), 44 deletions(-) diff --git a/includes/auth/auth.php b/includes/auth/auth.php index d6c4476..5484da0 100644 --- a/includes/auth/auth.php +++ b/includes/auth/auth.php @@ -65,6 +65,17 @@ class Authentication */ var $bugdarUser; + /** + * Mapping of Bugdar fields to authentication database fields; these will be synced between databases upon login + * @var array + */ + var $fieldMap = array( + 'authid' => null, + 'displayname' => null, + 'email' => null, + 'password' => null, + ); + // ################################################################### /** * Constructor @@ -76,6 +87,8 @@ class Authentication $this->registry =& $bugsys; $this->db =& $bugsys->db; + $this->registry->debug('authentication system: ' . get_class($this)); + $this->_setupDatabase(); } @@ -152,6 +165,7 @@ class Authentication if ($this->_verifyCookieData()) { $this->_setCookies(true); + $this->_syncBugdarUser(); $this->bugdarUser = $this->_fetchBugdarUserFromAuthUser(); return true; } @@ -199,6 +213,7 @@ class Authentication if ($this->_verifyLoginUser($password)) { $this->_setCookies($sticky); + $this->_syncBugdarUser(); $this->bugdarUser = $this->_fetchBugdarUserFromAuthUser(); return true; } @@ -224,20 +239,60 @@ class Authentication * Creates a Bugdar user with the authentication details specified in * the auth array and returns it. You need to call this in * _fetchBugdarUserFromAuthUser() and use the UserAPI to create the user. - * Example for this function is this (all of these fields are required): - * - * $user = new UserAPI($this->registry); // do not change this - * $user->set('email', $this->authUser['email']); - * $user->set('displayname', $this->authUser['name']); - * $user->set('password', $this->authUser['password']); // some random value that won't be used for authentication - * $user->set('usergroupid', 2); // default "Registered Users" group - * $user->set('authid', $this->authUser['userid']); // This must be a COMPLETELY STATIC key that is found in the auth db that will permanently link Bugdar to the auth user - * $user->insert(); // saves the user - * - * return $user->values; // returns the newly created user array + * This will create a new user in Bugdar with the data from the authentication DB + * with the fields specified in fieldMap. + */ + function _createBugdarUser() + { + $user = new UserAPI($this->registry); + + // if the email already exists in the DB, it must be the same person so just hook up the authid + if ($check = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->db->escape_string($this->authUser[ $this->fieldMap['email'] ]) . "'")) + { + $user->set('userid', $check['userid']); + $user->set_condition(); + $user->set('authid', $this->authUser[ $this->fieldMap['authid'] ]); + $user->update(); + $user->fetch(); + + return $user->objdata; + } + else + { + $user = new UserAPI($this->registry); + foreach ($this->fieldMap AS $bugdar => $authdb) + { + $user->set($bugdar, $this->authUser["$authDb"]); + } + $user->set('usergroupid', 2); + $user->insert(); + + return $user->values; + } + } + + // ################################################################### + /** + * Syncs a Bugdar user's fieldMap'ed values to the authentication DB's + * values. This allows the users to stay mostly-in-sync for the most + * basic of information (like email, timezone, etc.). Passwords are + * NOT synced. */ - function _createBugdarUser() {} + function _syncBugdarUser() + { + $fields = $this->fieldMap; + unset($fields['authid']); + unset($fields['password']); + $user = new UserAPI($this->registry); + $user->set('userid', $this->authUser[ $this->fieldMap['authid'] ]); + foreach ($fields AS $bugdar => $authdb) + { + $user->set($bugdar, $this->authUser["$authdb"]); + } + $user->update(); + } + // ################################################################### /** * Responsible for unsetting all authentication cookies because they diff --git a/includes/auth/auth_default.php b/includes/auth/auth_default.php index acfcb3d..da0a295 100644 --- a/includes/auth/auth_default.php +++ b/includes/auth/auth_default.php @@ -95,6 +95,9 @@ class AuthenticationDefault extends Authentication { return ($this->authUser['password'] == md5(md5($password) . md5($this->authUser['salt']))); } + + // ################################################################### + function _syncBugdarUser() {} } /*=====================================================================*\ diff --git a/includes/auth/auth_vbulletin.php b/includes/auth/auth_vbulletin.php index e0e5d8c..cd24b29 100644 --- a/includes/auth/auth_vbulletin.php +++ b/includes/auth/auth_vbulletin.php @@ -48,6 +48,18 @@ class AuthenticationVbulletin extends Authentication */ var $vBTablePrefix = ''; + /** + * Fields that map Bugdar fields to vBulletin fields + * @var string + */ + var $fieldMap = array( + 'authid' => 'userid', + 'email' => 'email', + 'timezone' => 'timezoneoffset', + 'password' => 'passworrd', + 'displayname' => 'username' + ); + // ################################################################### function _setupDatabase() { @@ -103,38 +115,7 @@ class AuthenticationVbulletin extends Authentication } return $user; } - - // ################################################################### - function _createBugdarUser() - { - $user = new UserAPI($this->registry); - - // if the email already exists in the DB, it must be the same person so just hook up the authid - if ($check = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->db->escape_string($this->authUser['email']) . "'")) - { - $user->set('userid', $check['userid']); - $user->set_condition(); - $user->set('authid', $this->authUser['userid']); - $user->update(); - $user->fetch(); - - return $user->objdata; - } - else - { - $user = new UserAPI($this->registry); - $user->set('email', $this->authUser['email']); - $user->set('displayname', $this->authUser['username']); - $user->set('password', $this->authUser['password']); - $user->set('usergroupid', 2); - $user->set('authid', $this->authUser['userid']); - $user->set('timezone', $this->authUser['timezoneoffset']); - $user->insert(); - - return $user->values; - } - } - + // ################################################################### function _fetchUserWithIdentifier($username) { -- 2.43.5