PHP Class A1_Core

bcrypt is highly recommended by many to safely store passwords. For more information, see http://codahale.com/how-to-safely-store-a-password/ Based on Kohana's AUTH, Fred Wu's AUTHLITE and Woody Gilk's Bonafide
Mostra file Open project: wouterrr/a1

Protected Properties

Property Type Description
$_config
$_name
$_sess
$_user

Public Methods

Method Description
check ( $password, $hash ) : boolean Checks if password matches hash
check_password ( $user, $password ) : boolean Validates a password against a user. This can be used to confirm user in actions where you ask for password while user is logged in to be extra safe (eg when deleting account)
complete_login ( $user, $remember = FALSE ) : TRUE Updates session, set remember cookie (if required)
failed_login ( $user ) : FALSE Registers a failed login attempt
get_user ( ) : object Returns the user - if any
hash ( $input, $salt = NULL, $cost = NULL ) : string Generates bcrypt hash for input
instance ( $_name = 'a1' ) : object Return a static instance of A1.
logged_in ( ) : boolean Returns TRUE is a user is currently logged in
login ( $username, $password, $remember = FALSE ) : mixed Attempt to log in a user.
logout ( $destroy = FALSE ) : boolean Log out a user by removing the related session variables.
session ( $id = NULL ) : Session (Initializes &) Returns the session we're working with
set_user ( $user ) : object Sets the user that is logged in
store_user_in_session ( $user ) : void Stores user model in session

Protected Methods

Method Description
__construct ( $_name = 'a1', $_config ) : void Loads Session and configuration options.
_get_failed_attempts ( $user ) : void Returns the number of failed login attempts
_increment_failed_attempts ( $user ) : void Increment the number of failed login attempts since last successfull login
_increment_logins ( $user ) : void Increment the number of logins of the user by 1
_load_user ( $username ) : object Loads the user object from database using username
_reset_failed_attempts ( $user ) : void Reset the number of failed login attempts
_save_user ( $user ) : void Saves the user object
_set_last_attempt ( $user ) : void Sets the last attempt field of the user object to current time
_set_last_login ( $user ) : void Sets the last login field of the user object to current time
find_user ( ) : object Finds the user in the session (if any)

Method Details

__construct() protected method

Loads Session and configuration options.
protected __construct ( $_name = 'a1', $_config ) : void
return void

_get_failed_attempts() protected method

Returns the number of failed login attempts
protected _get_failed_attempts ( $user ) : void
return void

_increment_failed_attempts() protected method

Increment the number of failed login attempts since last successfull login
protected _increment_failed_attempts ( $user ) : void
return void

_increment_logins() protected method

Increment the number of logins of the user by 1
protected _increment_logins ( $user ) : void
return void

_load_user() abstract protected method

Loads the user object from database using username
abstract protected _load_user ( $username ) : object
return object User Object

_reset_failed_attempts() protected method

Reset the number of failed login attempts
protected _reset_failed_attempts ( $user ) : void
return void

_save_user() protected method

Saves the user object
protected _save_user ( $user ) : void
return void

_set_last_attempt() protected method

Sets the last attempt field of the user object to current time
protected _set_last_attempt ( $user ) : void
return void

_set_last_login() protected method

Sets the last login field of the user object to current time
protected _set_last_login ( $user ) : void
return void

check() public method

Checks if password matches hash
public check ( $password, $hash ) : boolean
return boolean password matches hashed password

check_password() public method

if ( $a1->check_password($user, $this->request->post('password'))) { delete account or some other special action }
public check_password ( $user, $password ) : boolean
return boolean Success

complete_login() public method

Updates session, set remember cookie (if required)
public complete_login ( $user, $remember = FALSE ) : TRUE
return TRUE

failed_login() public method

Registers a failed login attempt
public failed_login ( $user ) : FALSE
return FALSE

find_user() protected method

Finds the user in the session (if any)
protected find_user ( ) : object
return object / FALSE

get_user() public method

Returns the user - if any
public get_user ( ) : object
return object / FALSE

hash() public method

Generates bcrypt hash for input
public hash ( $input, $salt = NULL, $cost = NULL ) : string
return string hashed input value

instance() public static method

Return a static instance of A1.
public static instance ( $_name = 'a1' ) : object
return object

logged_in() public method

Returns TRUE is a user is currently logged in
public logged_in ( ) : boolean
return boolean

login() public method

Attempt to log in a user.
public login ( $username, $password, $remember = FALSE ) : mixed
return mixed user if succesfull, FALSE otherwise

logout() public method

Log out a user by removing the related session variables.
public logout ( $destroy = FALSE ) : boolean
return boolean

session() public method

(Initializes &) Returns the session we're working with
public session ( $id = NULL ) : Session
return Session

set_user() public method

Sets the user that is logged in
public set_user ( $user ) : object
return object / FALSE

store_user_in_session() public method

Stores user model in session
public store_user_in_session ( $user ) : void
return void

Property Details

$_config protected_oe property

protected $_config

$_name protected_oe property

protected $_name

$_sess protected_oe property

protected $_sess

$_user protected_oe property

protected $_user