11# Auth User PHP
22
3-
4- [ ![ Build Status] ( https://github.com/byjg/authuser/actions/workflows/phpunit.yml/badge.svg?branch=master )] ( https://github.com/byjg/authuser/actions/workflows/phpunit.yml )
3+ [ ![ Build Status] ( https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml/badge.svg?branch=master )] ( https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml )
54[ ![ Opensource ByJG] ( https://img.shields.io/badge/opensource-byjg-success.svg )] ( http://opensource.byjg.com )
6- [ ![ GitHub source] ( https://img.shields.io/badge/Github-source-informational?logo=github )] ( https://github.com/byjg/authuser/ )
7- [ ![ GitHub license] ( https://img.shields.io/github/license/byjg/authuser.svg )] ( https://opensource.byjg.com/opensource/licensing.html )
8- [ ![ GitHub release] ( https://img.shields.io/github/release/byjg/authuser.svg )] ( https://github.com/byjg/authuser/releases/ )
9-
5+ [ ![ GitHub source] ( https://img.shields.io/badge/Github-source-informational?logo=github )] ( https://github.com/byjg/php-authuser/ )
6+ [ ![ GitHub license] ( https://img.shields.io/github/license/byjg/php-authuser.svg )] ( https://opensource.byjg.com/opensource/licensing.html )
7+ [ ![ GitHub release] ( https://img.shields.io/github/release/byjg/php-authuser.svg )] ( https://github.com/byjg/php-authuser/releases/ )
108
119A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
1210
13- The main purpose is just to handle all complexity of validate a user, add properties and create access token abstracting the database layer.
14- This class can persist into session (or file, memcache, etc) the user data between requests.
11+ The main purpose is just to handle all complexity of validate a user, add properties and create access token abstracting the database layer.
12+ This class can persist into session (or file, memcache, etc) the user data between requests.
1513
1614## Creating a Users handling class
1715
18-
19- ** Using the FileSystem (XML) as the user storage**
16+ Using the FileSystem (XML) as the user storage:
2017
2118``` php
2219<?php
2320$users = new UsersAnyDataset('/tmp/pass.anydata.xml');
2421```
2522
26- ** Using the Database as the user storage**
23+ Using the Database as the user storage:
2724
2825``` php
2926<?php
@@ -37,14 +34,13 @@ $users = new ByJG\Authenticate\UsersDBDataset(
3734* Note* : See the [ Anydataset project] ( https://github.com/byjg/anydataset#connection-based-on-uri ) to see the
3835database available and the connection strings as well.
3936
40- ** Using the Moodle as the user storage**
37+ Using the Moodle as the user storage:
4138
4239``` php
4340<?php
4441$users = new UsersMoodleDataset('connection');
4542```
4643
47-
4844## Authenticate a user with your username and password and persist into the session
4945
5046``` php
@@ -53,7 +49,7 @@ $user = $users->isValidUser('someuser', '12345');
5349if (!is_null($user))
5450{
5551 $userId = $user->getUserid();
56-
52+
5753 $sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
5854 $sessionContext->registerLogin($userId);
5955}
@@ -67,7 +63,7 @@ $sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::cre
6763
6864// Check if the user is authenticated
6965if ($sessionContext->isAuthenticated()) {
70-
66+
7167 // Get the userId of the authenticated users
7268 $userId = $sessionContext->userInfo();
7369
@@ -77,20 +73,20 @@ if ($sessionContext->isAuthenticated()) {
7773}
7874```
7975
80- ## Saving extra info into the user session
76+ ## Saving extra info into the user session
8177
8278You can save data in the session data exists only during the user is logged in. Once the user logged off the
8379data stored with the user session will be released.
8480
85- ** Store the data for the current user session**
81+ Store the data for the current user session:
8682
8783``` php
8884<?php
8985$sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
9086$sessionContext->setSessionData('key', 'value');
9187```
9288
93- ** Getting the data from the current user session**
89+ Getting the data from the current user session:
9490
9591``` php
9692<?php
@@ -100,7 +96,7 @@ $value = $sessionContext->getSessionData('key');
10096
10197Note: If the user is not logged an error will be throw
10298
103- ## Adding a custom property to the users;
99+ ## Adding a custom property to the users
104100
105101``` php
106102<?php
@@ -115,17 +111,18 @@ $users->save();
115111<?php
116112$sessionContext->registerLogout();
117113```
118- # Important note about SessionContext
119114
120- ` SessionContext ` object will store the info about the current context.
115+ ## Important note about SessionContext
116+
117+ ` SessionContext ` object will store the info about the current context.
121118As SessionContext uses CachePool interface defined in PSR-6 you can set any storage
122- to save your session context.
119+ to save your session context.
123120
124121In our examples we are using a regular PHP Session for store the user context
125122(` Factory::createSessionPool() ` ). But if you are using another store like MemCached
126123you have to define a UNIQUE prefix for that session. Note if TWO users have the same
127124prefix you probably have an unexpected result for the SessionContext.
128-
125+
129126Example for memcached:
130127
131128``` php
@@ -135,39 +132,37 @@ $sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::cre
135132
136133If you do not know to create/manage that unique prefix ** prefer to use the regular Session object.**
137134
138-
139135## Architecture
140136
141137``` text
142- ┌───────────────────┐
143- │ SessionContext │
144- └───────────────────┘
145- │
138+ ┌───────────────────┐
139+ │ SessionContext │
140+ └───────────────────┘
141+ │
146142┌────────────────────────┐ ┌────────────────────────┐
147143│ UserDefinition │─ ─ ┐ │ ─ ─ ┤ UserModel │
148144└────────────────────────┘ ┌───────────────────┐ │ └────────────────────────┘
149145┌────────────────────────┐ └────│ UsersInterface │────┐ ┌────────────────────────┐
150146│ UserPropertyDefinition │─ ─ ┘ └───────────────────┘ ─ ─ ┤ UserPropertyModel │
151147└────────────────────────┘ ▲ └────────────────────────┘
152- │
153- ┌────────────────────────┼─────────────────────────┐
154- │ │ │
155- │ │ │
156- │ │ │
157- ┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
158- │ UsersAnyDataset │ │ UsersDBDataset │ │ UsersMoodleDataset │
159- └───────────────────┘ └───────────────────┘ └────────────────────┘
148+ │
149+ ┌────────────────────────┼─────────────────────────┐
150+ │ │ │
151+ │ │ │
152+ │ │ │
153+ ┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
154+ │ UsersAnyDataset │ │ UsersDBDataset │ │ UsersMoodleDataset │
155+ └───────────────────┘ └───────────────────┘ └────────────────────┘
160156```
161157
162158- UserInterface contain the basic interface for the concrete implementation
163159- UsersDBDataset is a concrete implementation to retrieve/save user in a Database
164160- UserAnyDataset is a concrete implementation to retrieve/save user in a Xml file
165- - UsersMoodleDatabase is a concrete implementation to retrieve users in a Moodle database structure.
161+ - UsersMoodleDatabase is a concrete implementation to retrieve users in a Moodle database structure.
166162- UserModel is the basic model get/set for the user
167163- UserPropertyModel is the basic model get/set for extra user property
168164- UserDefinition will map the model to the database
169165
170-
171166### Database
172167
173168The default structure adopted for store the user data in the database through the
@@ -184,7 +179,7 @@ create table users
184179 created datetime,
185180 admin enum(' Y' ,' N' ),
186181
187- constraint pk_users primary key (userid)
182+ constraint pk_users primary key (userid)
188183)
189184ENGINE= InnoDB;
190185
@@ -264,12 +259,11 @@ $userDefinition->defineClosureForSelect(UserDefinition::FIELD_CREATED, function
264259$userDefinition->markPropertyAsReadOnly(UserDefinition::FIELD_CREATED);
265260```
266261
267-
268262## Extending UserModel
269263
270- It is possible extending the UserModel table, since you create a new class extending from UserModel to add the new fields.
264+ It is possible extending the UserModel table, since you create a new class extending from UserModel to add the new fields.
271265
272- For example, imagine your table has one field called "otherfield".
266+ For example, imagine your table has one field called "otherfield".
273267
274268You'll have to extend like this:
275269
@@ -278,7 +272,7 @@ You'll have to extend like this:
278272/**
279273 * This class is your model
280274 * This need to support the basic field plus your new fields
281- * already set in your definition class
275+ * already set in your definition class
282276 */
283277class MyUserModel extends UserModel
284278{
@@ -317,22 +311,29 @@ $users = new ByJG\Authenticate\UsersDBDataset(
317311);
318312```
319313
320-
321-
322314## Install
323315
324- Just type:
316+ Just type:
325317
326- ```
327- composer require "byjg/authuser=4.3.* "
318+ ``` bash
319+ composer require " byjg/authuser"
328320```
329321
330322## Running Tests
331323
332324Because this project uses PHP Session you need to run the unit test the following manner:
333-
325+
326+ ``` bash
327+ ./vendor/bin/phpunit --stderr
334328```
335- phpunit --stderr
329+
330+ ## Dependencies
331+
332+ ``` mermaid
333+ flowchart TD
334+ byjg/authuser --> byjg/micro-orm
335+ byjg/authuser --> byjg/cache-engine
336+ byjg/authuser --> byjg/jwt-wrapper
336337```
337338
338339
0 commit comments