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.

Auth setup

Configuration starts with telling the Auth package that you are going to use the SimpleAuth driver. This is done through the auth.php 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 default file is already configured for the SimpleAuth package. You will find an explaination of this config file here.

After you have done this, you can choose to autoload the package through the always_load section of the app/config/config.php.

設定

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:

パラメータ 規定値 説明
db_connection string
null
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
'users'
Name of the users table to use.
table_columns array
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
true
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
array()
Defined groups, to be used by the SimpleAuth groups driver. See here for an description of the groups array structure.
roles array
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
'put_some_salt_in_here'
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
'username'
Name of the input field on the login form that contains the username.
password_post_key string
'password'
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 DEFAULT 0,
	`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`
	)
)

This is a sample login action:

public function action_login()
{
	$data = array();

	// If so, you pressed the submit button. let's go over the steps
	if (Input::post())
	{
		// first of all, let's get a auth object
		$auth = Auth::instance();

		// check the credentials. This assumes that you have the previous table created and
		// you have used the table definition and configuration as mentioned above.
		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);
}