libSmalldb  v0.3
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CookieAuth Class Reference

IAuth implementation using Smalldb state machine and shared session token in a cookie to identify SharedTokenMachine instance. More...

Inheritance diagram for CookieAuth:
Inheritance graph

Public Member Functions

 __construct ($config,\Smalldb::StateMachine::AbstractBackend $smalldb)
 Constructor.
 
 checkSession ()
 Check session - read & update cookies, setup session state machine and register callbacks. More...
 
 getSessionMachine ()
 Get session machine which manages all stuff around login and session.
 
 getUserId ()
 Get user's ID. More...
 
 hasUserRoles ($roles)
 Check whether user has given role(s). More...
 
 isAllMighty ()
 Is user all mighty? (Admin or something like that.) More...
 

Protected Member Functions

 getUserRoles ()
 Get list of user's roles, or null if not logged in. More...
 

Protected Attributes

 $smalldb
 Smalldb backend.
 
 $session_machine
 Reference to session state machine.
 
 $cookie_name = 'AuthToken'
 Cookie name.
 
 $cookie_ttl = 2592000
 Cookie duration [seconds] (default: 30 days)
 
 $session_ttl = 2592000
 Session duration [seconds] (default: 30 days)
 
 $user_id_property = 'user_id'
 Name of the session machine property with user's ID.
 
 $user_roles_property = 'user_roles'
 Name of the session machine property with user's role.
 
 $all_mighty_user_role = null
 Name of all mighty user role (admin)
 
 $all_mighty_cli = false
 Is command line all mighty?
 
 $session_machine_null_ref = 'session'
 Null reference to session machine (array; use session_machine_ref_prefix if not set)
 
 $session_machine_ref_prefix = 'session'
 Prefix of session machine reference (array; token ID will be appended)
 

Detailed Description

IAuth implementation using Smalldb state machine and shared session token in a cookie to identify SharedTokenMachine instance.

This class handles cookies only, it is up to session machine to maintain session.

To log in, use Auth::getSessionMachine() and invoke proper transition.

To log out, use only the defined logout transition, otherwise token will not get deleted.

Configuration options:

Member Function Documentation

checkSession ( )

Check session - read & update cookies, setup session state machine and register callbacks.

This must be called before using any state machines. No transitions are invoked at this point, only the session state machine reference is created.

Implements IAuth.

getUserId ( )

Get user's ID.

User's ID is limited to single scalar value (any integer or string).

Returns
User's ID, or NULL when nobody is logged in.

Implements IAuth.

hasUserRoles (   $roles)

Check whether user has given role(s).

Smalldb does not understand user roles, it can only check whether user's roles contain one of required values. User's roles are global and they are not related to any instance of anything.

User may have any number of roles.

Parameters
$rolesName of required role (string), or array of required roles (array of strings).
Returns
TRUE when user has at least one of requested roles, or FALSE otherwise.

Implements IAuth.

Here is the call graph for this function:

Here is the caller graph for this function:

isAllMighty ( )

Is user all mighty? (Admin or something like that.)

When this function returns true, access control will be disabled.

Implements IAuth.

Here is the call graph for this function:

getUserRoles ( )
protected

Get list of user's roles, or null if not logged in.

Note
User's roles are typically not property of the session machine, but they can be calculated property of the machine. Therefore, there is no need to complicate this API with users.

Here is the caller graph for this function: