As said in the introduction of the Auth package, an authentication system comes with
three different drivers, each dealing with a part of the system:
The Simpleauth login driver provides the logic for creating, updating, deleting and validating user accounts
stored in a local database table, for getting information about those accounts, for generating or resetting
passwords, and for login and logout operations (creating a user session).
The Simpleauth group driver stores its group definitions in the simpleauth configuration file. It provides the logic
for checking if a user is a member of a group, and for retrieving group information, or the roles defined to to a group.
The Simpleauth acl driver is role driven, and stores its acl definitions in the simpleauth configuration file.
It provides the logic for checking if a user has access to a named ACL.
As explained in the introduction, the Auth package provides a static interface to most of the method available, providing you
only a single driver set. These methods will be defined below as "Static: Yes". All methods are also accessable via chaining on the
Auth instance object, including the ones marked as static.
The validate_user method validates user credentials. This method supports both email address and username as valid credential.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$username_or_email |
''
|
Either the users username or email address |
$password |
''
|
The users password |
|
Returns |
mixed. false if the user didn't pass validation, or an array with the users information if the credentials were valid. |
Example |
if ($user = Auth::validate_user($name, $pass))
{
echo $user['username'];
}
|
This method only validates if a given combination of username and password is correct. It does NOT log the user in!
The login method performs a login request. If a parameter is not given, it will attempt to
retrieve it from posted data, used the form fieldnames configured in the simpleauth.php configuration file.
Internally it calls the validate_user() method to validate the request before attempting a login.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$username_or_email |
''
|
Either the users username or email address |
$password |
''
|
The users password |
|
Returns |
boolean. returns true if the login was succesful, false otherwise. |
Example |
if (Auth::login($name, $pass))
{
}
|
After a succesful login, the 'username' and the current 'login_hash' will be available as a session variable.
The check method checks if a logged-in user is present.
Static |
Yes |
Parameters |
None
|
Returns |
boolean. returns true if a logged-in user is present, false otherwise. |
Example |
if ( ! Auth::check())
{
Response::redirect('/login');
}
|
The force_login method performs a forced login request. You can use this for an automated login, when you know
the users id, but you don't know the password. This can be used for example to implement a "remember me" feature.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$user_id |
required |
The id of the user you want to login |
|
Returns |
boolean. returns true if the login was succesful, false otherwise. |
Example |
if (Auth::force_login(12121))
{
}
|
The logout method logs out the current logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
boolean. returns true if the logout was succesful, false otherwise. |
Example |
Auth::logout();
|
If you have enabled guest user support, the guest user will be setup after a succesful logout.
The create_user allows method you to create a new user record in the users table.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$username |
required |
The name of the user you want to create |
$password |
required |
The password you want to assign to the user |
$email |
required |
The email address of the user you want to create |
$group |
1 |
The group you want to assign this user to. By default, the user is assigned to group 1. |
$profile_fields |
array()
|
Any additional profile fields or user metadata you want to assign to this user |
|
Throws |
SimpleUserUpdateException |
Returns |
mixed. returns the id of the user record created, or false if the creation failed. |
Example |
Auth::create_user(
'anewuser',
'thisismysecretpassword',
'anewuser@example.org',
1,
array(
'fullname' => 'A. New User',
)
);
|
The update_user method allows you to update values in the user record,
either for a named user, or for the current logged-in user.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$values |
array()
|
Associative array with column name / value pairs for the columns or profile fields you want to update. |
$username |
null
|
The name of the user you want to update |
|
Throws |
SimpleUserUpdateException |
Returns |
boolean. returns true if columns were updated, or false if no columns were affected. |
Example |
Auth::update_user(
array(
'email' => 'anewemail@example.org',
'password' => 'thisismynewpassword',
'old_password' => 'thisismysecretpassword',
'phone' => '+1 (555) 123-1212',
)
);
|
This method is primary provided to allow the user to do profile updates. And as a security measure, it will not allow you
to change the username, and if you want to change the password, you have to pass the current password in $values as "old_password".
The change_password method allows you to change a users password.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$old_password |
required |
The users current password |
$new_password |
required |
The users new password |
$username |
null
|
The name of the user of whom you want to change the password |
|
Throws |
SimpleUserUpdateException |
Returns |
boolean. returns true if the password was changed, or false if the old password was incorrect. |
Example |
Auth::change_password('mycurrentpassword','mynewpassword');
|
This method is provided to allow the user to change his password. And as a security measure, you have to
pass the current password in "old_password".
The reset_password method allows you to assign a new random password to a user.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$username |
required |
The name of the user for whom you want to reset the password |
|
Throws |
SimpleUserUpdateException |
Returns |
string, the generated random password. |
Example |
$new_password = Auth::reset_password('thisusername');
|
The delete_user method allows you to delete a user account.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$username |
required |
The name of the user account you want to delete |
|
Throws |
SimpleUserUpdateException |
Returns |
boolean. returns true if the user account was deleted, or false if it failed (because the username did not exist). |
Example |
Auth::delete_user('thisusername');
|
The create_login_hash method generates a new login hash for the currently logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
string, the generated login hash. |
Example |
Auth::create_login_hash();
|
This method is normally used when a user logs in, but you can use this as an extra security measure by rotating
the hash regularly for logged-in users.
Note that login hash protection is not used when multiple logins is enabled!
The get method is a generic getter for user properties, either from the user record, or from the metadata.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$field |
required |
The name of the property you want the value of |
$default |
null
|
The value to return is the property is not set |
|
Returns |
mixed. |
Example |
$created_at = Auth::get('created_at');
|
The get_user_id method returns an array structure containing the drivers id value, and the id of
the currently logged-in user. It uses this structure because you can have multiple login drivers active, each can have
a different id for the currently logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
mixed. returns a numeric array in the form array([0]=>driver_id, [1]=>user_id) if a user is logged in, or false otherwise. |
Example |
$id_info = Auth::get_user_id();
if ($id_info)
{
foreach ($id_info as $info)
{
echo 'Driver: ',$info[0],' with ID ',$info[1],'<br />';
}
}
else
{
echo 'Nobody is logged in!';
}
list($driver, $userid) = Auth::get_user_id();
list(, $userid) = Auth::get_user_id();
|
If guest user support is enabled, you will never get false returned, because the method will return the user id of the guest user.
The get_groups method returns the user groups assigned to the currently logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
mixed. returns an array in the form array(array(driver, group_id)) if a user is logged in, or false otherwise. |
Example |
$id_info = Auth::get_groups();
if ($id_info)
{
foreach ($id_info as $info)
{
echo 'Driver: ',$info[0],' with group ID ',$info[1],'<br />';
}
}
else
{
echo 'Nobody is logged in!';
}
list($driver, $groupid) = Auth::get_groups();
|
If guest user support is enabled, you will never get false returned, because the method will return the group information of the guest user.
The get_email method returns the email address assigned to the currently logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
mixed. returns an email address if a user is logged in, or false if there is no user logged-in. |
Example |
$email = Auth::get_email();
if ($email === false)
{
}
if (empty($email))
{
}
|
If guest user support is enabled, you will never get false returned, because the method will return the email address of the guest user.
The get_screen_name method returns the screen- or display name of the currently logged-in user.
Static |
Yes |
Parameters |
None
|
Returns |
mixed. returns a string containing the name, or false if there is no user logged-in. |
Example |
$name = Auth::get_screen_name();
if (empty($name))
{
}
|
If guest user support is enabled, you will never get false returned, because the method will return the user name of the guest user.
The get_profile_fields method returns one or all stored profile fields for the logged-in user.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$field |
null
|
The name of the profile field you want to get. |
$default |
null
|
The value you want returned if the requested profile field does not exist. |
|
Returns |
mixed |
Example |
$profile = Auth::get_profile_fields();
|
The member method allows you to check if a user is member of a given group.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$group |
required |
The id of the group you want to check for membership |
$user |
null
|
A specific user, identified by the result of get_user_id(), or null for the currently logged-in user. |
|
Returns |
boolean. true if the user is member of the group, or false if not. |
Example |
if (Auth::member(100))
{
}
|
The groups method allows you to fetch all defined groups, or all defined groups in a specific driver.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$driver |
null
|
The name of the group driver you want to get groups of |
|
Returns |
array. If the group driver does not exist or no groups are defined, an empty array is returned. |
Example |
$groups = Auth::groups();
$groups = Auth::group()->groups();
$groups = Auth::group('Simplegroup')->groups();
|
You need to call this method on the Auth group drivers instance if you want groups defined by a specific driver.
If you use multiple drivers, this method only works reliably if the group id's are unique over all drivers!
The roles method allows you to fetch all defined roles, or all defined roles in a specific driver.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$driver |
null
|
The name of the group driver you want to get roles of |
|
Returns |
array. If the acl driver does not exist or no roles are defined, an empty array is returned. |
Example |
$roles = Auth::roles();
$roles = Auth::acl()->roles();
$roles = Auth::acl('Simpleacl')->roles();
|
You need to call this method on the Auth acl drivers instance if you want groups defined by a specific driver.
If you use multiple drivers, this method only works reliably if the role id's are unique over all drivers!
The get_name method allows you to fetch the name of a specific group. If no group is given, it will return the name
of the group of the current logged-in user.
Static |
No |
Parameters |
Param |
Default |
Description |
$group |
null
|
The id of the group you want to get the name of |
|
Returns |
mixed. The name of the group, or null if a group with the given id does not exist. |
Example |
$name = Auth::group()->get_name();
$name = Auth::group('Simplegroup')->get_name(100);
|
If you use multiple drivers, this method only works reliably if the group id's are unique over all drivers!
The has_access method allows you to check if the current logged-in user has access to a specific location with specific rights.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$condition |
required |
The access condition you want to check |
|
Returns |
boolean. true if the user has access, or false if not. |
Example |
if (Auth::has_access('blog.read'))
{
}
if (Auth::instance('simpleauth')->has_access('blog.[read,write,delete]'))
{
}
if (Auth::has_access(array('blog' => array('read'), 'comments' => array('read')))
{
}
if (Auth::acl('simpleacl')->has_access('blog.[read,write,delete]'))
{
}
|
The guest_login allows you to check if guest access is enabled.
Static |
Yes |
Parameters |
None
|
Returns |
boolean. returns true if the guest support is enabled, or false if not. |
Example |
if ( ! Auth::check() and ! Auth::guest_login())
{
Response::redirect('/login');
}
|
The remember_me method allows you to set a remember_me cookie. If $user_id is not given, the current logged-in user is used.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$user_id |
null
|
The id of the user to be remembered |
|
Returns |
boolean. returns true if the remember me cookie was correctly set, or false
if there was no user_id present, or the remember_me functionality was disabled in the configuration.
|
Example |
if (\Input::method() == 'POST')
{
if (\Auth::instance()->login(\Input::param('username'), \Input::param('password')))
{
if (\Input::param('rememberme', false))
{
\Auth::remember_me();
}
else
{
\Auth::dont_remember_me();
}
\Response::redirect('/');
}
else
{
$data['username'] = \Input::param('username');
$data['login_error'] = \Lang::get('login.failure');
}
}
|
The dont_remember_me method destroys the remember_me cookie, if present.
Static |
Yes |
Parameters |
None
|
Example |
See remember_me() for an example
|