In this example we will cover how to make a provider that extends the system by attempting to log a user in against another user database (a website CMS or forum for example). If this fails, then we will try to authenticate the user against the activeCollab user database.
We will start by creating a CustomAuthenticationProvider.class.php within the /activecollab/angie/classes/auth/providers folder. We'll use this code for the foundation:
require_once 'BasicAuthenticationProvider.class.php';
/**
* Custom authentication provider implementation
*/
class CustomAuthenticationProvider extends BasicAuthenticationProvider {
}
Note that we are using BasicAuthenticationProvider as a foundation for our provider. This way we get a cookie and session handling automatically, without the need to implement them once again.
Let's implement the behavior now. In this example, we want to check if a user exists and that their password is valid in another database after they click hit the Submit button on an activeCollab form. For that, we will need to override the authentication method, like this:
function authenticate($credentials) {
// Inherit activeCollab authentication
$active_collab_auth = parent::authenticate($credentials);
// It failed, go for other source
if(is_error($active_collab_auth)) {
// Connect to database
if(!$link = mysql_connect('localhost', 'root', '')) {
return new Error('Failed to connect');
} // if
if(!mysql_select_db('other_application')) {
return new Error('Failed to select other_application database');
} // if
// Collect credentials
$email = trim(array_var($credentials, 'email'));
$password = array_var($credentials, 'password');
$remember = (boolean) array_var($credentials, 'remember', false);
// Find user in other database
$result = mysql_query("SELECT password FROM users WHERE email = '" . mysql_real_escape_string($email) . "'");
if($result) {
if($row = mysql_fetch_assoc($result)) {
// Password is OK
if($row['password'] == $password) {
// Let's see if we already have this user
$user = Users::findByEmail($email);
// It not lets create a new account
if(!instance_of($user, 'User')) {
$user = new User();
$user->setAttributes(array(
'email' => $email,
'password' => $row['password'],
));
$user->setCompanyId(11);
$user->resetToken();
$save = $user->save();
if(is_error($save)) {
return new Error('Failed to create an account. Reason: ' . $save->getMessage());
} // if
} // if
// And done, log user in
return $this->logUserIn($user, array(
'remember' => $remember,
'new_visit' => true,
));
} // if
} // if
} // if
return new Error('User is not registered');
} else {
return $active_collab_auth;
} // if
} // authenticate
To enable this newly created provider, open the config/config.php file and add the following line to the block with constant definitions:
define('AUTH_PROVIDER', 'CustomAuthenticationProvider');