PHPackages                             richard-roman/laravel-dynamic-access - 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. richard-roman/laravel-dynamic-access

ActiveLibrary

richard-roman/laravel-dynamic-access
====================================

Dynamic multi-tenant access management on top of Spatie Laravel Permission

v1.0.0(1mo ago)02MITPHPPHP ^8.3

Since Jul 12Pushed 1mo agoCompare

[ Source](https://github.com/Richard-Roman/laravel-dynamic-access)[ Packagist](https://packagist.org/packages/richard-roman/laravel-dynamic-access)[ RSS](/packages/richard-roman-laravel-dynamic-access/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

Laravel Dynamic Access Manager
==============================

[](#laravel-dynamic-access-manager)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a177f005e61e0e9a63f161362df0762752aaf490c59f3f6654bcde4076a16d0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726963686172642d726f6d616e2f6c61726176656c2d64796e616d69632d6163636573732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/richard-roman/laravel-dynamic-access)[![Total Downloads](https://camo.githubusercontent.com/4c32b6e236878df27df3a370cc0ef08b4de41e34f1dda6a78f419b78f2ee8acd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726963686172642d726f6d616e2f6c61726176656c2d64796e616d69632d6163636573732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/richard-roman/laravel-dynamic-access)[![Build Status](https://camo.githubusercontent.com/6cd0688bab9920746854ad34086aba00105bc074efb271888ce951786166a42f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726963686172642d726f6d616e2f6c61726176656c2d64796e616d69632d6163636573732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/richard-roman/laravel-dynamic-access/actions)

A robust, portable, and secure Laravel package to manage dynamic access permissions on top of [Spatie Laravel Permission](https://github.com/spatie/laravel-permission). Tailored for multi-tenant applications, it allows granular access control mapped dynamically through roles, modules, and companies.

---

Key Features
------------

[](#key-features)

- **Dynamic Access Mapping**: Manage roles, modules (`core.modulos`), actions (`iam.acciones`), and access matrices (`iam.accesos`) dynamically.
- **Spatie Permission Integration**: Syncs access matrices to Spatie permissions automatically via a dynamic, database-driven builder.
- **Multi-Tenant Design**: Uses a customizable `TenantResolver` to isolate permissions per company/team in real-time.
- **Artisan Reconciliation**: Quick sync and clean-up command (`php artisan access:reconcile`) to keep the DB and Spatie cache aligned.
- **Packagist-Ready**: Fully decoupled from the host application, with dynamic resolution of models and highly configurable routing.

---

Prerequisites
-------------

[](#prerequisites)

This package assumes your host application already has the following tables:

- `core.empresas` — companies/tenants table with an `id_empresa` primary key
- `core.modulos` — modules table (the package migration **alters** this table to add `id_empresa`, `id_modulo_padre`, and `url` columns)

> **Important**: If `core.modulos` does not yet have `id_empresa`, publish and run the package migrations **before** using any module endpoints.

---

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

[](#installation)

Install the package via Composer:

```
composer require richard-roman/laravel-dynamic-access
```

Publish the configuration file:

```
php artisan vendor:publish --provider="DynamicAccess\DynamicAccessServiceProvider" --tag="dynamic-access-config"
```

Publish and run the migrations:

```
php artisan vendor:publish --provider="DynamicAccess\DynamicAccessServiceProvider" --tag="dynamic-access-migrations"
php artisan migrate
```

> The migrations will:
>
> 1. **Alter** `core.modulos` — adding `id_empresa` (tenant FK), `id_modulo_padre` (self-reference hierarchy), and `url`.
> 2. **Create** `iam.acciones` — action definitions per module.
> 3. **Create** `iam.accesos` — the access matrix (source of truth).

---

Configuration
-------------

[](#configuration)

The configuration file is published at `config/dynamic-access.php`:

```
return [
    // Automatically register package API routes
    'register_routes' => true,

    // Prefix for all package routes (registered under /api/{route_prefix})
    'route_prefix' => 'access-manager',

    // Middleware group applied to all package routes
    // The package always appends 'dynamic.team' to this group automatically
    'middleware_group' => ['auth'],

    // Spatie Permission name required for write endpoints (POST/PUT/DELETE)
    // Set to null to disable this check.
    'manage_permission' => 'manage-access',

    // Authentication guard used when creating roles and permissions with Spatie
    // Change if your host uses a guard other than 'web' (e.g. 'api', 'sanctum')
    'guard_name' => 'web',

    // Customize database table names for host integration
    'tables' => [
        'modulos'  => 'core.modulos',
        'acciones' => 'iam.acciones',
        'accesos'  => 'iam.accesos',
        'empresas' => 'core.empresas',
        'roles'    => 'iam.roles',
    ],

    // Eloquent model class for roles — resolved dynamically at runtime
    'models' => [
        'role' => \App\Models\IAM\Rol::class,
    ],
];
```

---

Multi-Tenant Integration
------------------------

[](#multi-tenant-integration)

The host application must implement and bind the `DynamicAccess\Contracts\TenantResolver` contract to resolve the active tenant ID at runtime.

### 1. Implement the Contract

[](#1-implement-the-contract)

```
namespace App\Services;

use DynamicAccess\Contracts\TenantResolver;

class AppTenantResolver implements TenantResolver
{
    public function getCurrentTenantId(): ?int
    {
        // Return your active tenant/company ID (e.g. from session, subdomain, header, etc.)
        return tenant('id') ?? auth()->user()?->id_empresa;
    }
}
```

### 2. Bind it in `AppServiceProvider`

[](#2-bind-it-in-appserviceprovider)

```
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use DynamicAccess\Contracts\TenantResolver;
use App\Services\AppTenantResolver;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(TenantResolver::class, AppTenantResolver::class);
    }
}
```

> If no binding is registered, the package falls back to a null resolver (no tenant scope). This is safe for CLI commands but will return empty results on API endpoints.

---

Running Tests
-------------

[](#running-tests)

```
./vendor/bin/phpunit
```

Tests run against SQLite in-memory with schema emulation — no external database required.

---

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for recent changes.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance90

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

49d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/76d7af680d58b999ca253ebe6ab7cb88c5e3d0c75bed17e68cbdf99d3f9de15b?d=identicon)[Richard-Roman](/maintainers/Richard-Roman)

---

Top Contributors

[![Richard-Roman](https://avatars.githubusercontent.com/u/116965598?v=4)](https://github.com/Richard-Roman "Richard-Roman (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/richard-roman-laravel-dynamic-access/health.svg)

```
[![Health](https://phpackages.com/badges/richard-roman-laravel-dynamic-access/health.svg)](https://phpackages.com/packages/richard-roman-laravel-dynamic-access)
```

###  Alternatives

[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1346.4k29](/packages/fleetbase-core-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
