Skip to content

Commit

Permalink
Merge pull request #10 from byjg/5.0
Browse files Browse the repository at this point in the history
PHP 8.1.
  • Loading branch information
byjg authored Oct 29, 2024
2 parents b7f762f + 39d0d41 commit 72f7773
Show file tree
Hide file tree
Showing 30 changed files with 623 additions and 808 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: byjg
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ jobs:
strategy:
matrix:
php-version:
- "8.3"
- "8.2"
- "8.1"
- "8.0"
- "7.4"

steps:
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/psalm
- run: ./vendor/bin/phpunit --stderr

Documentation:
Expand Down
5 changes: 5 additions & 0 deletions .idea/runConfigurations/Psalm.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![GitHub license](https://img.shields.io/github/license/byjg/php-authuser.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-authuser.svg)](https://github.com/byjg/php-authuser/releases/)

A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
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.
Expand Down Expand Up @@ -34,27 +34,6 @@ $users = new ByJG\Authenticate\UsersDBDataset(
*Note*: See the [Anydataset project](https://github.com/byjg/anydataset#connection-based-on-uri) to see the
database available and the connection strings as well.

Using the Moodle as the user storage:

```php
<?php
$users = new UsersMoodleDataset('connection');
```

## Authenticate a user with your username and password and persist into the session

```php
<?php
$user = $users->isValidUser('someuser', '12345');
if (!is_null($user))
{
$userId = $user->getUserid();

$sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
$sessionContext->registerLogin($userId);
}
```

## Check if user was previously authenticated

```php
Expand Down Expand Up @@ -151,14 +130,13 @@ If you do not know to create/manage that unique prefix **prefer to use the regul
│ │ │
│ │ │
┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
│ UsersAnyDataset │ │ UsersDBDataset │ │ UsersMoodleDataset
│ UsersAnyDataset │ │ UsersDBDataset │ │ xxxxxxxxxxxxxxxxxx
└───────────────────┘ └───────────────────┘ └────────────────────┘
```

- 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
- UsersMoodleDatabase is a concrete implementation to retrieve users in a Moodle database structure.
- 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
Expand Down
18 changes: 12 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
{
"name": "byjg/authuser",
"description": "A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.",
"description": "A simple and customizable class for enable user authentication inside your application. It is available on XML files and Relational Databases.",
"autoload": {
"psr-4": {
"ByJG\\Authenticate\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.4",
"byjg/micro-orm": "4.9.*",
"byjg/cache-engine": "4.9.*",
"byjg/jwt-wrapper": "4.9.*"
"php": ">=8.1 <8.4",
"byjg/micro-orm": "^5.0",
"byjg/cache-engine": "^5.0",
"byjg/jwt-wrapper": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^5.9"
},
"license": "MIT"
}
18 changes: 18 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
16 changes: 9 additions & 7 deletions src/Definition/PasswordDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ByJG\Authenticate\Definition;

use InvalidArgumentException;

class PasswordDefinition
{
const MINIMUM_CHARS = "minimum_chars";
Expand All @@ -13,7 +15,7 @@ class PasswordDefinition
const ALLOW_SEQUENTIAL = "allow_sequential";
const ALLOW_REPEATED = "allow_repeated";

protected $rules = [];
protected array $rules = [];

public function __construct($rules = null)
{
Expand All @@ -32,28 +34,28 @@ public function __construct($rules = null)
}
}

public function setRule($rule, $value)
public function setRule(string $rule, string|bool|int $value): void
{
if (!array_key_exists($rule, $this->rules)) {
throw new \InvalidArgumentException("Invalid rule");
throw new InvalidArgumentException("Invalid rule");
}
$this->rules[$rule] = $value;
}

public function getRules()
public function getRules(): array
{
return $this->rules;
}

public function getRule($rule)
public function getRule($rule): string|bool|int
{
if (!array_key_exists($rule, $this->rules)) {
throw new \InvalidArgumentException("Invalid rule");
throw new InvalidArgumentException("Invalid rule");
}
return $this->rules[$rule];
}

public function matchPassword($password)
public function matchPassword(string $password): bool
{
// match password against the rules
if (strlen($password) < $this->rules[self::MINIMUM_CHARS]) {
Expand Down
Loading

0 comments on commit 72f7773

Please sign in to comment.