User authentication - adampatterson/Dingo-Framework GitHub Wiki

Overview

The user authentication library provides you with a fully featured user authentication. To load the user library manually you may do this:

load::library('user');

Setup

Dingo uses your database for storing user data, so the first thing you need to do is make a table in the database. This table can be named anything you want but must contain these 6 columns in this order:

  • id - Unique ID number of user. Contains a 11 character long auto incrementing integer.
  • email - E-mail address of user. Contains a string of varying length.
  • username - Unique username of user. Contains a string of varying length.
  • password - Hashed user password. Contains a long string.
  • type - User type. Contains a string of varying length.
  • data - Application specific data. Contains a JSON string of varying length. TEXT or MEDIUM_TEXT data types work best.

Users SQL

NOTE: Because the user library uses sessions, you must load the database and session libraries before the user library is loaded.

After you create your user table in your database the next step is to create a Dingo configuration file. Create and then open up application/config/development/user.php and add the following:

<?php if(!defined('DINGO')){die('External Access to File Denied');}

// User Database Connection
config::set('user_connection','default');

// User Database Table
config::set('user_table','users');

// User Types
config::set('user_types',array(
'banned'=>0,
'guest'=>1,
'user'=>2,
'mod'=>3,
'admin'=>4,
'owner'=>5
));

Change the user_table setting to the name of the database table you are using for storing user data. You may also change user_connection to switch which database connection is used.

Create

Creates a new user.

user::create(array(
'username'=>'ETbyrne',
'email'=>'[email protected]',
'password'=>'test',
'type'=>'admin'
));

Delete

Removes a user from the database. Accepts user ID, e-mail, or username.

user::delete(7);

Log In

Logs a user in. Accepts user ID, e-mail, or username as first argument. Second argument is the user's password. Returns TRUE or FALSE depending on if sucessful.

// Using e-mail
user::login('[email protected]','test');

// Using ID
user::login(1,'test');

// Using username
user::login('ETbyrne','test');

Log Out

Logs the current user out.

user::logout();

Check

Checks to see if a given ID/e-mail/username and password match a user. Accepts user ID, e-mail, or username as first argument. Second argument is the user's hashed password. Returns TRUE or FALSE depending on if a match is found.

user::check('[email protected]',user::hash('test'));

Valid

Returns TRUE or FALSE depending on if the current user is logged in and is a valid user.

if(user::valid())
{
echo 'Logged in!';
}
else
{
echo 'Not logged in!';
}

Is Type

Returns TRUE or FALSE depending on if the current user's type is greater than or equal to the given type.

if(user::is_type('admin'))
{
echo 'You are an admin!';
}
else
{
echo 'You are not an admin!';
}

ID

Returns current user's ID.

echo user::id();

E-mail

Returns current user's e-mail.

echo user::email();

Username

Returns current user's username.

echo user::username();

Type

Returns current user's type.

echo user::type();

Password

Returns current user's hashed password.

echo user::password();

Data

Returns a data key stored for the current user.

echo user::data('first_name');

Banned

Returns TRUE or FALSE depending on if the current user is banned.

if(user::banned())
{
echo 'You are banned!';
}
else
{
echo 'You are not banned!';
}

Get

Retrieves a specified user's information from the database. Accepts user ID, e-mail, or username. Returns FALSE if user could not be found, an array otherwise.

$user = user::get('ETbyrne');

Ban

Bans a user. Accepts user ID, e-mail, or username.

user::ban('ETbyrne');

Unique

Checks to see if a given ID, e-mail, or username previously exists. Accepts user ID, e-mail, or username. Returns TRUE if no matches are found, FALSE otherwise.

if(user::unique('ETbyrne'))
{
echo 'Username not taken!';
}
else
{
echo 'Username taken!';
}

Update

Updates a user's information. Accepts user ID, e-mail, or username. Returns an object with methods email, username, password, id, type, data, and save.

user::update('[email protected]')
       ->email('[email protected]')
       ->username('ETbyrne')
       ->type('user')
       ->password('123456')
       ->data('first_name','Evan')
       ->data('lasT_name','Byrne')
       ->save();