Download the PHP package byjg/authuser without Composer
On this page you can find all versions of the php package byjg/authuser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byjg/authuser
More information about byjg/authuser
Files in byjg/authuser
Package authuser
Short Description A simple and customizable class for enable user authentication inside your application. It is available on XML files and Relational Databases.
License MIT
Informations about the package authuser
Auth User PHP
A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases.
The main purpose is just to handle all complexity of validate a user, add properties and create access token abstracting the database layer. This class can persist into session (or file, memcache, etc) the user data between requests.
Creating a Users handling class
Using the FileSystem (XML) as the user storage:
Using the Database as the user storage:
Note: See the Anydataset project to see the database available and the connection strings as well.
Check if user was previously authenticated
Saving extra info into the user session
You can save data in the session data exists only during the user is logged in. Once the user logged off the data stored with the user session will be released.
Store the data for the current user session:
Getting the data from the current user session:
Note: If the user is not logged an error will be throw
Adding a custom property to the users
Logout from a session
Important note about SessionContext
SessionContext
object will store the info about the current context.
As SessionContext uses CachePool interface defined in PSR-6 you can set any storage
to save your session context.
In our examples we are using a regular PHP Session for store the user context
(Factory::createSessionPool()
). But if you are using another store like MemCached
you have to define a UNIQUE prefix for that session. Note if TWO users have the same
prefix you probably have an unexpected result for the SessionContext.
Example for memcached:
If you do not know to create/manage that unique prefix prefer to use the regular Session object.
Architecture
- UserInterface contain the basic interface for the concrete implementation
- UsersDBDataset is a concrete implementation to retrieve/save user in a Database
- UserAnyDataset is a concrete implementation to retrieve/save user in a Xml file
- UserModel is the basic model get/set for the user
- UserPropertyModel is the basic model get/set for extra user property
- UserDefinition will map the model to the database
Database
The default structure adopted for store the user data in the database through the UsersDBDataset class is the follow:
Using the database structure above you can create the UsersDBDatase as follow:
Custom Database
If you have an existing database with different names but containing all fields above you can use the UserDefinition and UserPropertiesDefinition classes for customize this info.
Adding custom modifiers for read and update
Extending UserModel
It is possible extending the UserModel table, since you create a new class extending from UserModel to add the new fields.
For example, imagine your table has one field called "otherfield".
You'll have to extend like this:
After that you can use your new definition:
Install
Just type:
Running Tests
Because this project uses PHP Session you need to run the unit test the following manner:
Dependencies
All versions of authuser with dependencies
byjg/micro-orm Version ^5.0
byjg/cache-engine Version ^5.0
byjg/jwt-wrapper Version ^5.0