r1427: More work on the authentication system:
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 5 Mar 2007 04:37:29 +0000 (04:37 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 5 Mar 2007 04:37:29 +0000 (04:37 +0000)
- 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
includes/auth/auth_default.php
includes/auth/auth_vbulletin.php

index d6c4476e6915c731feac22268d8a816ed518922b..5484da0b1a4f479a4e91240ae72539956c4c21ef 100644 (file)
@@ -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
index acfcb3d1e11ff817a79140786486082b237ecec7..da0a29563fbce74e4208ae12cba742365553a05f 100644 (file)
@@ -95,6 +95,9 @@ class AuthenticationDefault extends Authentication
        {
                return ($this->authUser['password'] == md5(md5($password) . md5($this->authUser['salt'])));
        }
+       
+       // ###################################################################
+       function _syncBugdarUser() {}
 }
 
 /*=====================================================================*\
index e0e5d8c632cb88d1dc7c2265e9b12124b2baa744..cd24b296aa959ce03f129b1fb5ea3fec409883e9 100644 (file)
@@ -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)
        {