1
1
# Auth User PHP
2
2
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 )
5
4
[ ![ 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/ )
10
8
11
9
A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
12
10
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.
15
13
16
14
## Creating a Users handling class
17
15
18
-
19
- ** Using the FileSystem (XML) as the user storage**
16
+ Using the FileSystem (XML) as the user storage:
20
17
21
18
``` php
22
19
<?php
23
20
$users = new UsersAnyDataset('/tmp/pass.anydata.xml');
24
21
```
25
22
26
- ** Using the Database as the user storage**
23
+ Using the Database as the user storage:
27
24
28
25
``` php
29
26
<?php
@@ -37,14 +34,13 @@ $users = new ByJG\Authenticate\UsersDBDataset(
37
34
* Note* : See the [ Anydataset project] ( https://github.com/byjg/anydataset#connection-based-on-uri ) to see the
38
35
database available and the connection strings as well.
39
36
40
- ** Using the Moodle as the user storage**
37
+ Using the Moodle as the user storage:
41
38
42
39
``` php
43
40
<?php
44
41
$users = new UsersMoodleDataset('connection');
45
42
```
46
43
47
-
48
44
## Authenticate a user with your username and password and persist into the session
49
45
50
46
``` php
@@ -53,7 +49,7 @@ $user = $users->isValidUser('someuser', '12345');
53
49
if (!is_null($user))
54
50
{
55
51
$userId = $user->getUserid();
56
-
52
+
57
53
$sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
58
54
$sessionContext->registerLogin($userId);
59
55
}
@@ -67,7 +63,7 @@ $sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::cre
67
63
68
64
// Check if the user is authenticated
69
65
if ($sessionContext->isAuthenticated()) {
70
-
66
+
71
67
// Get the userId of the authenticated users
72
68
$userId = $sessionContext->userInfo();
73
69
@@ -77,20 +73,20 @@ if ($sessionContext->isAuthenticated()) {
77
73
}
78
74
```
79
75
80
- ## Saving extra info into the user session
76
+ ## Saving extra info into the user session
81
77
82
78
You can save data in the session data exists only during the user is logged in. Once the user logged off the
83
79
data stored with the user session will be released.
84
80
85
- ** Store the data for the current user session**
81
+ Store the data for the current user session:
86
82
87
83
``` php
88
84
<?php
89
85
$sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
90
86
$sessionContext->setSessionData('key', 'value');
91
87
```
92
88
93
- ** Getting the data from the current user session**
89
+ Getting the data from the current user session:
94
90
95
91
``` php
96
92
<?php
@@ -100,7 +96,7 @@ $value = $sessionContext->getSessionData('key');
100
96
101
97
Note: If the user is not logged an error will be throw
102
98
103
- ## Adding a custom property to the users;
99
+ ## Adding a custom property to the users
104
100
105
101
``` php
106
102
<?php
@@ -115,17 +111,18 @@ $users->save();
115
111
<?php
116
112
$sessionContext->registerLogout();
117
113
```
118
- # Important note about SessionContext
119
114
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.
121
118
As 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.
123
120
124
121
In our examples we are using a regular PHP Session for store the user context
125
122
(` Factory::createSessionPool() ` ). But if you are using another store like MemCached
126
123
you have to define a UNIQUE prefix for that session. Note if TWO users have the same
127
124
prefix you probably have an unexpected result for the SessionContext.
128
-
125
+
129
126
Example for memcached:
130
127
131
128
``` php
@@ -135,39 +132,37 @@ $sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::cre
135
132
136
133
If you do not know to create/manage that unique prefix ** prefer to use the regular Session object.**
137
134
138
-
139
135
## Architecture
140
136
141
137
``` text
142
- ┌───────────────────┐
143
- │ SessionContext │
144
- └───────────────────┘
145
- │
138
+ ┌───────────────────┐
139
+ │ SessionContext │
140
+ └───────────────────┘
141
+ │
146
142
┌────────────────────────┐ ┌────────────────────────┐
147
143
│ UserDefinition │─ ─ ┐ │ ─ ─ ┤ UserModel │
148
144
└────────────────────────┘ ┌───────────────────┐ │ └────────────────────────┘
149
145
┌────────────────────────┐ └────│ UsersInterface │────┐ ┌────────────────────────┐
150
146
│ UserPropertyDefinition │─ ─ ┘ └───────────────────┘ ─ ─ ┤ UserPropertyModel │
151
147
└────────────────────────┘ ▲ └────────────────────────┘
152
- │
153
- ┌────────────────────────┼─────────────────────────┐
154
- │ │ │
155
- │ │ │
156
- │ │ │
157
- ┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
158
- │ UsersAnyDataset │ │ UsersDBDataset │ │ UsersMoodleDataset │
159
- └───────────────────┘ └───────────────────┘ └────────────────────┘
148
+ │
149
+ ┌────────────────────────┼─────────────────────────┐
150
+ │ │ │
151
+ │ │ │
152
+ │ │ │
153
+ ┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
154
+ │ UsersAnyDataset │ │ UsersDBDataset │ │ UsersMoodleDataset │
155
+ └───────────────────┘ └───────────────────┘ └────────────────────┘
160
156
```
161
157
162
158
- UserInterface contain the basic interface for the concrete implementation
163
159
- UsersDBDataset is a concrete implementation to retrieve/save user in a Database
164
160
- 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.
166
162
- UserModel is the basic model get/set for the user
167
163
- UserPropertyModel is the basic model get/set for extra user property
168
164
- UserDefinition will map the model to the database
169
165
170
-
171
166
### Database
172
167
173
168
The default structure adopted for store the user data in the database through the
@@ -184,7 +179,7 @@ create table users
184
179
created datetime,
185
180
admin enum(' Y' ,' N' ),
186
181
187
- constraint pk_users primary key (userid)
182
+ constraint pk_users primary key (userid)
188
183
)
189
184
ENGINE= InnoDB;
190
185
@@ -264,12 +259,11 @@ $userDefinition->defineClosureForSelect(UserDefinition::FIELD_CREATED, function
264
259
$userDefinition->markPropertyAsReadOnly(UserDefinition::FIELD_CREATED);
265
260
```
266
261
267
-
268
262
## Extending UserModel
269
263
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.
271
265
272
- For example, imagine your table has one field called "otherfield".
266
+ For example, imagine your table has one field called "otherfield".
273
267
274
268
You'll have to extend like this:
275
269
@@ -278,7 +272,7 @@ You'll have to extend like this:
278
272
/**
279
273
* This class is your model
280
274
* 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
282
276
*/
283
277
class MyUserModel extends UserModel
284
278
{
@@ -317,22 +311,29 @@ $users = new ByJG\Authenticate\UsersDBDataset(
317
311
);
318
312
```
319
313
320
-
321
-
322
314
## Install
323
315
324
- Just type:
316
+ Just type:
325
317
326
- ```
327
- composer require "byjg/authuser=4.3.* "
318
+ ``` bash
319
+ composer require " byjg/authuser"
328
320
```
329
321
330
322
## Running Tests
331
323
332
324
Because this project uses PHP Session you need to run the unit test the following manner:
333
-
325
+
326
+ ``` bash
327
+ ./vendor/bin/phpunit --stderr
334
328
```
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
336
337
```
337
338
338
339
0 commit comments