SimpleAuth - Introduction
SimpleAuth is, as it's name implies, a simple authentication system which is included in the Auth package. Besides being a working Auth implementation, it is also an example for other Auth drivers. You can use this as a reference when building your own driver.
Configuration
The SimpleAuth authentication system is configured through a configuration file, not suprisingly through the 'simpleauth' configuration file. A default file is provided in the Auth package. You should copy this file to your app/config folder before making any changes.
The following configuration values can be defined:
Param | Type | Default | Description |
---|---|---|---|
db_connection | string |
|
Name of the database connection to use. This should match the definition in your applications db.php configuration file. Set it to null to use the default DB instance. |
table_name | string |
|
Name of the users table to use. |
table_columns | array |
|
List of columns to select from the users table, or '*' to select all columns. You have to at least include 'username', 'password', 'email', 'last_login', 'login_hash', 'group' and 'profile_fields'. |
guest_login | boolean |
|
If true a dummy 'guest' user will be created if no one is logged in. This allows you to use the group and acl drivers even when no one is logged in. |
groups | array |
|
Defined groups, to be used by the SimpleAuth groups driver. See here for an description of the groups array structure. |
roles | array |
|
Defined roles, to be used by the SimpleAuth acl driver. See here for an description of the roles array structure. |
login_hash_salt | string |
|
To make the passwords used by the SimpleAuth drivers extra secure, a salt value is used when hashing the passwords to store them into the database. Make sure you change this default to a very random string! To hash passwords, SimpleAuth uses PBKDF2, a very secure hashing mechanism. |
username_post_key | string |
|
Name of the input field on the login form that contains the username. |
password_post_key | string |
|
Name of the input field on the login form that contains the password. |
Database table
SimpleAuth relies on a single table. Here is the SQL to create it:
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`password` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`group` INT NOT NULL DEFAULT 1 ,
`email` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`last_login` VARCHAR( 25 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`login_hash` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`profile_fields` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`created_at` INT( 11 ) UNSIGNED NOT NULL ,
UNIQUE (
`username` ,
`email`
)
)
Example
This is a sample login action:
public function action_login()
{
$data = array();
if (Input::post())
{
// Ok, you pressed the submit button. let's go over
// first of all, let's get a auth object
$auth = Auth::instance();
// check the credentials. This assumes that you have the previous table created
if ($auth->login())
{
// credentials ok, go right in
Response::redirect('success_page');
}
else
{
// Oops, no soup for you. try to login again.
// Set some values to repopulate the username field and give some error text back to the view
$data['username'] = Input::post('username');
$data['login_error'] = 'Wrong username/password combo. Try again';
}
}
// Show the login form
echo View::forge('auth/login',$data);
}