]>
src.bluestatic.org Git - bugdar.git/blob - includes/auth/auth.php
2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-2007 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
22 require_once('./includes/api_user.php');
25 * Abstract Authentication
27 * This is an abstract class that is used to provide authentication for
31 * @copyright Copyright (c)2002 - 2007, Blue Static
39 * The database connection to AUTHENTICATE against; can be to a separate database
45 * The database connection to the BUGDAR database
57 * Array of user data from the AUTHENTICATION database
63 * Array of user data from the BUGDAR database
69 * Mapping of Bugdar fields to authentication database fields; these will be synced between databases upon login.
70 * AT THE VERY MINIMUM, YOU MUST MAP THESE FIELDS:
73 var $fieldMap = array(
75 'displayname' => null,
79 // ###################################################################
83 function __construct()
87 $this->registry
=& $bugsys;
88 $this->db
=& $bugsys->db
;
90 $this->registry
->debug('authentication system: ' . get_class($this));
92 $this->_setupDatabase();
95 // ###################################################################
99 function Authentication()
101 $this->__construct();
104 // ###################################################################
106 * Returns the information array for the Bugdar user. This must be
107 * called after an authentication method.
109 function fetchBugdarUser()
111 return $this->bugdarUser
;
114 // ###################################################################
116 * Sets up the database to authenticate against. You can create a new
117 * database object here. Whatever you choose, you need to reference
118 * Authentication->authDb to the object
120 function _setupDatabase()
123 $this->authDb
= new DB_MySQL($this->registry
);
125 include 'includes/auth/config.php';
126 $this->authDb
->connect(
127 $config['auth']['dbServer'],
128 $config['auth']['dbUser'],
129 $config['auth']['dbPassword'],
130 $config['auth']['dbName'],
135 // ###################################################################
137 * Returns the sanitized value of the user ID or unique identifier
138 * found in the cookie of an already-authenticated user.
140 function _fetchCookieUniqueId() {}
142 // ###################################################################
144 * Returns the sanitized value of the authentication key or cookie-safe
145 * password found in the cookies of an already-authenticated user.
147 function _fetchCookiePassword() {}
149 // ###################################################################
151 * Returns an array of user data fetched using the user information
152 * values found in cookies. It should NOT be responsible for verifying
153 * the authentication information, but only fetching it.
155 function _fetchUserUsingCookies() {}
157 // ###################################################################
159 * Returns TRUE if the cookie data values are valid in the data array
160 * returned from _fetchUserUsingCookies(), and FALSE if they are not.
162 function _verifyCookieData() {}
164 // ###################################################################
166 * Authenticates the user using cookie data. You shouldn't need to
167 * customize this method if you implement all the helpers correctly.
168 * Returns TRUE if the cookies are valid and the user is logged in.
170 function authenticateCookies()
172 if (!$this->_fetchCookieUniqueId() OR !$this->_fetchCookiePassword())
177 $this->authUser
= $this->_fetchUserUsingCookies();
178 if (!$this->authUser
)
180 $this->authUser
= null;
184 if ($this->_verifyCookieData())
186 $this->_setCookies(true);
187 $this->bugdarUser
= $this->_fetchBugdarUserFromAuthUser();
188 if ($this->_syncBugdarUser())
190 $this->bugdarUser
= $this->_fetchBugdarUserFromAuthUser();
196 $this->authUser
= null;
197 $this->clearCookies();
202 // ###################################################################
204 * Returns an array with the authentication user information, found
205 * by the unique login identifier passed to the function.
207 function _fetchUserWithIdentifier($string) {}
209 // ###################################################################
211 * Verifies that the authUser's password matches the plain-text password
212 * passed to this function. This is basically the transformation of
213 * the plaintext to the hashed password and the result of the comparison.
215 function _verifyLoginUser($password) {}
217 // ###################################################################
219 * Authenticates a user at login from two keys: an identifier and
220 * a password. In Bugdar, the identifier is an email, but it can be
221 * any unique string found in the authentication database. Returns
222 * TRUE if the authentication is successful, and FALSE if not. Also
223 * determines if the cookies are sticky ("rememember me" login)
225 function authenticateLogin($string, $password, $sticky = false)
227 $this->authUser
= $this->_fetchUserWithIdentifier($string);
229 if (!$this->authUser
)
231 $this->authUser
= null;
235 if ($this->_verifyLoginUser($password))
237 $this->_setCookies($sticky);
238 $this->bugdarUser
= $this->_fetchBugdarUserFromAuthUser();
239 $this->_syncBugdarUser();
244 $this->authUser
= null;
249 // ###################################################################
251 * Returns the BUGDAR user array from the data in the AUTHENTICATION user
252 * array. If the Bugdar user does not exist, call _createBugdarUser()
253 * to add the user into the Bugdar database. This is necessary so Bugdar options
254 * can be saved in the Bugdar database (and not in the auth one), however
255 * authentication details will NOT be stored in the Bugdar database.
257 function _fetchBugdarUserFromAuthUser()
259 $user = $this->db
->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE authid = '" . $this->authUser
[ $this->fieldMap
['authid'] ] . "'");
262 $this->_createBugdarUser();
263 return $this->_fetchBugdarUserFromAuthUser();
268 // ###################################################################
270 * Creates a Bugdar user with the authentication details specified in
271 * the auth array and returns it. You need to call this in
272 * _fetchBugdarUserFromAuthUser() and use the UserAPI to create the user.
273 * This will create a new user in Bugdar with the data from the authentication DB
274 * with the fields specified in fieldMap.
276 function _createBugdarUser()
278 $user = new UserAPI($this->registry
);
280 // if the email already exists in the DB, it must be the same person so just hook up the authid
281 if ($check = $this->db
->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email = '" . $this->db
->escape_string($this->authUser
[ $this->fieldMap
['email'] ]) . "'"))
283 $user->set('userid', $check['userid']);
284 $user->set_condition();
285 $user->set('authid', $this->authUser
[ $this->fieldMap
['authid'] ]);
289 return $user->objdata
;
293 $user = new UserAPI($this->registry
);
294 foreach ($this->fieldMap
AS $bugdar => $authdb)
296 $user->set($bugdar, $this->authUser
["$authdb"]);
298 $user->set('usergroupid', 2);
299 $user->set('password', $this->registry->funct->rand());
302 return $user->values;
306 // ###################################################################
308 * Syncs a Bugdar user's fieldMap'ed values to the authentication DB's
309 * values. This allows the users to stay mostly-in-sync for the most
310 * basic of information (like email, timezone, etc.). Passwords are
311 * NOT synced. Returns TRUE if the user data was changed.
313 function _syncBugdarUser()
315 $fields = $this->fieldMap;
316 unset($fields['authid']);
317 unset($fields['password']);
321 $user = new UserAPI($this->registry);
322 $user->set('userid', $this->bugdarUser['userid']);
323 $user->set_condition();
324 foreach ($fields AS $bugdar => $auth)
326 if ($this->bugdarUser["$bugdar"] != $this->authUser
["$auth"])
328 $user->set($bugdar, $this->authUser["$auth"]);
340 // ###################################################################
342 * Responsible for unsetting all authentication cookies because they
345 function clearCookies() {}
347 // ###################################################################
349 * Sets the authentication cookies; this is done both at login and
350 * for renewing the cookies upon successful cookie validation. The
351 * option it takes determines whether the cookies are sticky or not.
353 function _setCookies($permanent = false) {}
356 /*=====================================================================*\
357 || ###################################################################
360 || ###################################################################
361 \*=====================================================================*/