https://github.com/samsonasik/mezzio-authentication-with-authorization
mezzio authentication with ACL authorization demo
https://github.com/samsonasik/mezzio-authentication-with-authorization
acl authentication authorization login mezzio php remember-me session
Last synced: about 1 year ago
JSON representation
mezzio authentication with ACL authorization demo
- Host: GitHub
- URL: https://github.com/samsonasik/mezzio-authentication-with-authorization
- Owner: samsonasik
- License: bsd-3-clause
- Created: 2020-01-21T12:32:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-11-19T21:43:57.000Z (over 4 years ago)
- Last Synced: 2025-03-28T12:38:41.173Z (about 1 year ago)
- Topics: acl, authentication, authorization, login, mezzio, php, remember-me, session
- Language: PHP
- Homepage:
- Size: 808 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Mezzio 3 with authentication with authorization


[](https://codecov.io/gh/samsonasik/mezzio-authentication-with-authorization)
[](https://packagist.org/packages/samsonasik/mezzio-authentication-with-authorization)
Introduction
------------
A Mezzio 3 Skeleton Application with Authentication and Authorization Example.
Features
--------
- Authentication secured with csrf
- Authentication using prg for usability
- Authentication with remember me functionality
- Authentication notification with Session Flash
- Authorization with ACL
- isGranted check in the Layout
- getRole check in the Layout
Install
-------
```bash
$ composer create-project samsonasik/mezzio-authentication-with-authorization -sdev
$ cd mezzio-authentication-with-authorization
$ cp config/autoload/local.php.dist config/autoload/local.php
```
Configuration
-------------
Configure your `config/autoload/local.php` with your local DB config with username and password field. There are examples of `dsn` for both `PostgreSQL` and `MySQL` that you can modify.
For PostgreSQL
--------------
The following commands are example if you are using PostgreSQL (assumption using user "postgres" and create db named "mezzio"), you can create users table with insert username and bcrypt hashed password with pgcrypto extension into users table:
```sql
$ createdb -Upostgres mezzio
Password:
$ psql -Upostgres mezzio
Password for user postgres:
psql (12.1)
Type "help" for help.
mezzio=# CREATE TABLE users(username character varying(255) PRIMARY KEY NOT NULL, password text NOT NULL, role character varying(255) NOT NULL DEFAULT 'user');
CREATE TABLE
mezzio=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION
mezzio=# INSERT INTO users(username, password, role) VALUES('samsonasik', crypt('123456', gen_salt('bf')), 'user');
INSERT 0 1
mezzio=# INSERT INTO users(username, password, role) VALUES('admin', crypt('123456', gen_salt('bf')), 'admin');
INSERT 0 1
```
and you will get the following data:

For MySQL
--------------
The following commands are example if you are using MySQL (assumption using user "root" and create db named "mezzio"), you can create users table with insert username and bcrypt hashed password:
```sql
$ mysql -u root -p -e 'create database mezzio'
Enter password:
$ mysql -u root
Enter password:
mysql> use mezzio
Database changed
mysql> CREATE TABLE users(username varchar(255) PRIMARY KEY NOT NULL, password text NOT NULL, role varchar(255) NOT NULL DEFAULT 'user');
Query OK, 0 rows affected (0.01 sec)
mezzio=# INSERT INTO users(username, password, role) VALUES('samsonasik','$2a$06$Nt2zePoCfApfBGrfZbHZIudIwZpCNqorTjbKNZtPoLCVic8goZDsi', 'user');
Query OK, 1 row affected (0.01 sec)
mezzio=# INSERT INTO users(username, password, role) VALUES('admin', '$2a$06$Y2TtankzyiK/OF1yZA4GsOJBhuoP7o99XbfufEeJ0OOJwjUcPB9LO', 'admin');
Query OK, 1 row affected (0.01 sec)
```
and you will get the following data:

The Authorization Config
------------------------
The authorization configuration saved at `config/autoload/global.php` as ACL:
```php
[
'roles' => [
'guest' => [],
'user' => ['guest'],
'admin' => ['user'],
],
'resources' => [
'api.ping.view',
'home.view',
'admin.view',
'login.form',
'logout.access',
],
'allow' => [
'guest' => [
'login.form',
'api.ping.view',
],
'user' => [
'logout.access',
'home.view',
],
'admin' => [
'admin.view',
],
],
],
// ...
];
```
Running
-------
1. Clear browser cache
2. Run the php -S command:
```php
$ php -S localhost:8080 -t public
```
3. Open browser: http://localhost:8080
4. Login with username : samsonasik, password: 123456 OR username : admin, password : 123456. If you're a logged in user with "user" role, and open `/admin` page, it will show like the following (403 Forbidden), eg: see in [Firefox developer tools](https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor) under "Network" monitor:

Test
----
Tests are located under `test` directory, you can run test with composer command:
```bash
$ composer test
```