2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 - [#]year[#], 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 // ###################################################################
97 * Returns the information array for the Bugdar user. This must be
98 * called after an authentication method.
100 function fetchBugdarUser()
102 return $this->bugdarUser
;
105 // ###################################################################
107 * Sets up the database to authenticate against. You can create a new
108 * database object here. Whatever you choose, you need to reference
109 * Authentication->authDb to the object
111 function _setupDatabase() {}
113 // ###################################################################
115 * Returns the sanitized value of the user ID or unique identifier
116 * found in the cookie of an already-authenticated user.
118 function _fetchCookieUniqueId() {}
120 // ###################################################################
122 * Returns the sanitized value of the authentication key or cookie-safe
123 * password found in the cookies of an already-authenticated user.
125 function _fetchCookiePassword() {}
127 // ###################################################################
129 * Returns an array of user data fetched using the user information
130 * values found in cookies. It should NOT be responsible for verifying
131 * the authentication information, but only fetching it.
133 function _fetchUserUsingCookies() {}
135 // ###################################################################
137 * Returns TRUE if the cookie data values are valid in the data array
138 * returned from _fetchUserUsingCookies(), and FALSE if they are not.
140 function _verifyCookieData() {}
142 // ###################################################################
144 * Authenticates the user using cookie data. You shouldn't need to
145 * customize this method if you implement all the helpers correctly.
146 * Returns TRUE if the cookies are valid and the user is logged in.
148 function authenticateCookies()
150 if (!$this->_fetchCookieUniqueId() OR !$this->_fetchCookiePassword())
152 $this->clearCookies();
156 $this->authUser
= $this->_fetchUserUsingCookies();
158 if (!$this->authUser
)
160 $this->authUser
= null
;
161 $this->clearCookies();
165 if ($this->_verifyCookieData())
167 $this->_setCookies(true
);
168 $this->_syncBugdarUser();
169 $this->bugdarUser
= $this->_fetchBugdarUserFromAuthUser();
174 $this->authUser
= null
;
175 $this->clearCookies();
180 // ###################################################################
182 * Returns an array with the authentication user information, found
183 * by the unique login identifier passed to the function.
185 function _fetchUserWithIdentifier($string) {}
187 // ###################################################################
189 * Verifies that the authUser's password matches the plain-text password
190 * passed to this function. This is basically the transformation of
191 * the plaintext to the hashed password and the result of the comparison.
193 function _verifyLoginUser($password) {}
195 // ###################################################################
197 * Authenticates a user at login from two keys: an identifier and
198 * a password. In Bugdar, the identifier is an email, but it can be
199 * any unique string found in the authentication database. Returns
200 * TRUE if the authentication is successful, and FALSE if not. Also
201 * determines if the cookies are sticky ("rememember me" login)
203 function authenticateLogin($string, $password, $sticky = false
)
205 $this->authUser
= $this->_fetchUserWithIdentifier($string);
207 if (!$this->authUser
)
209 $this->authUser
= null
;
213 if ($this->_verifyLoginUser($password))
215 $this->_setCookies($sticky);
216 $this->_syncBugdarUser();
217 $this->bugdarUser
= $this->_fetchBugdarUserFromAuthUser();
222 $this->authUser
= null
;
227 // ###################################################################
229 * Returns the BUGDAR user array from the data in the AUTHENTICATION user
230 * array. If the Bugdar user does not exist, call _createBugdarUser()
231 * to add the user into the Bugdar database. This is necessary so Bugdar options
232 * can be saved in the Bugdar database (and not in the auth one), however
233 * authentication details will NOT be stored in the Bugdar database.
235 function _fetchBugdarUserFromAuthUser() {}
237 // ###################################################################
239 * Creates a Bugdar user with the authentication details specified in
240 * the auth array and returns it. You need to call this in
241 * _fetchBugdarUserFromAuthUser() and use the UserAPI to create the user.
242 * This will create a new user in Bugdar with the data from the authentication DB
243 * with the fields specified in fieldMap.
245 function _createBugdarUser()
247 $user = new UserAPI($this->registry
);
249 // if the email already exists in the DB, it must be the same person so just hook up the authid
250 if ($check = $this->db
->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email = '" . $this->db
->escape_string($this->authUser
[ $this->fieldMap
['email'] ]) . "'"))
252 $user->set('userid', $check['userid']);
253 $user->set_condition();
254 $user->set('authid', $this->authUser
[ $this->fieldMap
['authid'] ]);
258 return $user->objdata
;
262 $user = new UserAPI($this->registry
);
263 foreach ($this->fieldMap
AS $bugdar => $authdb)
265 $user->set($bugdar, $this->authUser
["$authDb"]);
267 $user->set('usergroupid', 2);
270 return $user->values
;
274 // ###################################################################
276 * Syncs a Bugdar user's fieldMap'ed values to the authentication DB's
277 * values. This allows the users to stay mostly-in-sync for the most
278 * basic of information (like email, timezone, etc.). Passwords are
281 function _syncBugdarUser()
283 $fields = $this->fieldMap
;
284 unset($fields['authid']);
285 unset($fields['password']);
289 $user = new UserAPI($this->registry
);
290 $user->set('userid', $this->authUser
[ $this->fieldMap
['authid'] ]);
291 $user->set_condition();
292 foreach ($fields AS $bugdar => $authdb)
294 if ($this->bugdarUser
["$bugdar"] != $this->authUser
["$authDb"])
296 $user->set($bugdar, $this->authUser
["$authdb"]);
306 // ###################################################################
308 * Responsible for unsetting all authentication cookies because they
311 function clearCookies() {}
313 // ###################################################################
315 * Sets the authentication cookies; this is done both at login and
316 * for renewing the cookies upon successful cookie validation. The
317 * option it takes determines whether the cookies are sticky or not.
319 function _setCookies($permanent = false
) {}
322 /*=====================================================================*\
323 || ###################################################################
326 || ###################################################################
327 \*=====================================================================*/