PHPackages                             rotexsoft/versatile-acl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. rotexsoft/versatile-acl

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

rotexsoft/versatile-acl
=======================

A simple light-weight and highly customizable package that can be used for implementing access control in any php application.

6.0.0(3mo ago)431BSD-3-ClausePHPPHP &gt;=8.2.0CI passing

Since Dec 4Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/rotexsoft/versatile-acl)[ Packagist](https://packagist.org/packages/rotexsoft/versatile-acl)[ Docs](https://github.com/rotexsoft/versatile-acl)[ RSS](/packages/rotexsoft-versatile-acl/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (7)Dependencies (4)Versions (11)Used By (0)

Versatile Acl
=============

[](#versatile-acl)

[![PHP Tests and Code Quality Tools](https://github.com/rotexsoft/versatile-acl/workflows/Run%20PHP%20Tests%20and%20Code%20Quality%20Tools/badge.svg)](https://github.com/rotexsoft/versatile-acl/actions?query=workflow%3A%22Run+PHP+Tests+and+Code+Quality+Tools%22) [![GitHub release (latest SemVer)](https://camo.githubusercontent.com/699c31b6195498c029918fb24ba36357245939292000da3aa76ac151592f0dc7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f726f746578736f66742f766572736174696c652d61636c3f6c6162656c3d6c617465737425323072656c65617365)](https://camo.githubusercontent.com/699c31b6195498c029918fb24ba36357245939292000da3aa76ac151592f0dc7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f726f746578736f66742f766572736174696c652d61636c3f6c6162656c3d6c617465737425323072656c65617365) [![GitHub](https://camo.githubusercontent.com/4cd6633f94e05e149d2bf90c531616aaafbb31e99b3612847c089eed9aa6f193/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/4cd6633f94e05e149d2bf90c531616aaafbb31e99b3612847c089eed9aa6f193/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f726f746578736f66742f766572736174696c652d61636c) [![Coveralls branch](https://camo.githubusercontent.com/dba861f19ea997398cb120110fd4ec0e381516c61506a71c76f6d40509921507/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f726f746578736f66742f766572736174696c652d61636c2f6d6173746572)](https://coveralls.io/github/rotexsoft/versatile-acl?branch=master) [![GitHub code size in bytes](https://camo.githubusercontent.com/01d17f555d5cfde7af3d559dede410195ff582be14eb5364df634a920d61c660/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/01d17f555d5cfde7af3d559dede410195ff582be14eb5364df634a920d61c660/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f726f746578736f66742f766572736174696c652d61636c) [![Packagist Downloads](https://camo.githubusercontent.com/a9a0634bbcbbba33d1e50d7e168fa1536e96a428ce8f45b908c4ff8f7ac720d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/a9a0634bbcbbba33d1e50d7e168fa1536e96a428ce8f45b908c4ff8f7ac720d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f746578736f66742f766572736174696c652d61636c) [![GitHub top language](https://camo.githubusercontent.com/5ece2e6cf5094b42fe5300131b05e7e251c221dc5d588db2d4ee2d00ab9f92aa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/5ece2e6cf5094b42fe5300131b05e7e251c221dc5d588db2d4ee2d00ab9f92aa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f726f746578736f66742f766572736174696c652d61636c) [![Packagist PHP Version Support](https://camo.githubusercontent.com/bfdf33fcd8f521d8acf1aabad0b0a81e80ab687c0ff39b09992519bb9d646ea1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/bfdf33fcd8f521d8acf1aabad0b0a81e80ab687c0ff39b09992519bb9d646ea1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f726f746578736f66742f766572736174696c652d61636c) [![GitHub commits since latest release (by date) for a branch](https://camo.githubusercontent.com/fcf6472165e3108d07d3823f6cca43b451340b377d5d8af85988c2ab758b07fc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f726f746578736f66742f766572736174696c652d61636c2f6c61746573742f6d6173746572)](https://camo.githubusercontent.com/fcf6472165e3108d07d3823f6cca43b451340b377d5d8af85988c2ab758b07fc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f726f746578736f66742f766572736174696c652d61636c2f6c61746573742f6d6173746572) [![GitHub last commit (branch)](https://camo.githubusercontent.com/31cb65a982195b9487c87ffdbaacf60867a586e6d001ad739708fd9b7f89c27c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f726f746578736f66742f766572736174696c652d61636c2f6d6173746572)](https://camo.githubusercontent.com/31cb65a982195b9487c87ffdbaacf60867a586e6d001ad739708fd9b7f89c27c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f726f746578736f66742f766572736174696c652d61636c2f6d6173746572) [![GitHub Release Date](https://camo.githubusercontent.com/aa7d0e16ecd4ff19365ab5a4d7e0ee29bfa20f0d581c9c3ea3d0dbc291e730bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f726f746578736f66742f766572736174696c652d61636c)](https://camo.githubusercontent.com/aa7d0e16ecd4ff19365ab5a4d7e0ee29bfa20f0d581c9c3ea3d0dbc291e730bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f726f746578736f66742f766572736174696c652d61636c) [![Libraries.io dependency status for GitHub repo](https://camo.githubusercontent.com/191d25fef674fad85afefe5875617e7fe17b64ed4b6221c9393537cddffbf37e/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f6769746875622f726f746578736f66742f766572736174696c652d61636c)](https://libraries.io/packagist/rotexsoft%2Fversatile-acl)

A simple, highly flexible and customizable access control package for PHP applications.

Installation
------------

[](#installation)

**Via composer:** (Requires PHP 8.2+).

```
    composer require rotexsoft/versatile-acl
```

Branching
---------

[](#branching)

These are the branches in this repository:

- **master:** contains code for the latest major version of this package.
- **5.x:** contains code for the **5.x** version of this package. No new features, only bug fixes.
- **4.x:** contains code for the **4.x** version of this package. No new features, only bug fixes.
- **1.X:** contains code for the **1.X** version of this package. Abandoned.

Introduction
------------

[](#introduction)

A PHP application can use this package to define **Permissionable Entities** (e.g. application users or groups that users can belong to).

- Each entity is an instance of **[\\VersatileAcl\\Interfaces\\PermissionableEntityInterface](src/interfaces/PermissionableEntityInterface.php)**which is implemented by **[\\VersatileAcl\\GenericPermissionableEntity](src/GenericPermissionableEntity.php)** in this package.
- Each entity can be associated to another entity as a parent Entity.
- Each entity can have one or more permissions defined. These are **direct permissions**
    - A **permission** in this package is an object that represents whether or not an **action** (represented by a case-insensitive string) can be performed by an entity on a **resource** (represented by a case-insensitive string).
    - A permission is an instance of **[\\VersatileAcl\\Interfaces\\PermissionInterface](src/interfaces/PermissionInterface.php)** which is implemented by **[\\VersatileAcl\\GenericPermission](src/GenericPermission.php)** in this package.
- Each entity also inherits permissions from its parent entities.
    - The package allows you to give direct permissions a higher priority than inherited permissions (the default behavior) and also allows you to do the reverse, if you so desire.

Below is an overview of the classes in this package:

[![example blog database schema](class-diagram-lite.svg)](class-diagram-lite.svg)

Click [here](class-diagram.png) to see the full Class Diagram for this package.

In your applications, you will be mostly be working with instances of **[VersatileAcl\\VersatileAcl](src/VersatileAcl.php)**; this class exposes most of the functionality of the underlying classes in this package listed below:

- **[\\VersatileAcl\\GenericPermissionableEntity](src/GenericPermissionableEntity.php) :** Represents an entity in your application
- **[\\VersatileAcl\\GenericPermissionableEntitiesCollection](src/GenericPermissionableEntitiesCollection.php) :** A collection class for storing one or more entities in your application
- **[\\VersatileAcl\\GenericPermission](src/GenericPermission.php) :** Represents a permission to be assigned to an entity in your application
- **[\\VersatileAcl\\GenericPermissionsCollection](src/GenericPermissionsCollection.php) :** A collection class for storing one or more permissions belonging to a particular entity in your application. It is possible to assign the same instance of this class to more than one entity, but it is recommended that you maintain separate instances of this class for each entity in your application.

Example Real-world Usage
------------------------

[](#example-real-world-usage)

We will be using a blog application that has a users table containing information about registered blog users (the users in this table are also authors of blog posts and commentators on blog posts in the application), a posts table and a comments table. Below is the schema for the sample application:

[![example blog database schema](docs/blog.png)](docs/blog.png)

Below are the relationship rules for the blog application

- A user can author many posts
- A user can make one or more comments on each post
- A post can have one or more comments associated with it

Below are some access control group definitions that are relevant to this sample blog application:

Group NameResourceActionAllowedadminallallyescomments-moderatorscommentapproveyescomments-moderatorscommentdeleteyescomments-ownerscommentallyesposts-moderatorspostapproveyesposts-moderatorspostdeleteyesposts-ownerspostallyes> **NOTE:** the permissions associated with the **comments-owners** and **posts-owners** will require an assertion callback that further checks that members of the group can only perform actions on the comments or posts they own (not comments and posts owned by other users).

Let's model these groups and permissions using [VersatileAcl\\VersatileAcl](src/VersatileAcl.php).

```
