Package

Grazulex Laravel Multipersona

Laravel MultiPersona is a lightweight context-layer system for Laravel users. It allows a single user to switch between different roles, accounts, or tenants dynamically, without creating multiple logins or sessions.

Package

grazulex/laravel-multipersona

View on GitHub →

Laravel MultiPersona

Laravel MultiPersona

Laravel MultiPersona is a lightweight context-layer system for Laravel users.


Laravel MultiPersona is a lightweight context-layer system for Laravel users.
It allows a single user to switch between different roles, accounts, or tenants dynamically, without creating multiple logins or sessions.


πŸ” What It Solves

  • Switch between "personas" (admin ↔ client, company A ↔ company B)
  • Contextual permissions and role handling
  • No UI or API enforced – 100% backend, policy, and middleware-driven

πŸ“¦ Installation

composer require grazulex/laravel-multipersona

🧩 Core Concepts

  • Persona: A context attached to a user (e.g. company, role, project)
  • Active Persona: The currently selected context
  • Persona Manager: A service to query, switch, or manipulate personas
  • Middleware: Force or apply a persona context

βœ… Quick Example

// Get current active persona
$currentPersona = persona();

// List all user personas
$userPersonas = auth()->user()->personas;

// Create a new persona
$persona = auth()->user()->createPersona([
    'name' => 'Company Admin',
    'context' => [
        'role' => 'admin',
        'company_id' => 123,
        'permissions' => ['read', 'write', 'delete']
    ]
]);

// Switch to persona
auth()->user()->switchToPersona($persona);

🧱 What's Provided

  • Trait: HasPersonas for your User model
  • Middleware: EnsureActivePersona, SetPersonaFromRequest
  • Helpers: persona(), personas($user)
  • Events: Complete event system for persona lifecycle
  • Service: PersonaManager for programmatic access
  • Contract: PersonaInterface for custom implementations

❌ What's Not Included

  • No routes or controllers
  • No CLI or HTTP APIs
  • No UI layer – you choose how to expose it

πŸ“š Complete Documentation

Getting Started

Core Features

Advanced Topics

Use Cases

  • Multi-tenant SaaS: Users switch between different company contexts
  • Role-based Access: Same user, different permissions per context
  • Agency Management: Manage multiple client accounts
  • Marketplace Platforms: Buyer/seller context switching
  • Enterprise Systems: Department or project-based access

🎯 Real-World Examples

Multi-tenant Application

// User switches between companies
$companyA = $user->createPersona([
    'name' => 'Acme Corp Admin',
    'context' => [
        'company_id' => 1,
        'role' => 'admin',
        'permissions' => ['manage_users', 'view_reports']
    ]
]);

$companyB = $user->createPersona([
    'name' => 'TechStart User', 
    'context' => [
        'company_id' => 2,
        'role' => 'user',
        'permissions' => ['view_dashboard']
    ]
]);

Middleware Protection

// Protect routes requiring specific roles
Route::middleware(['auth', 'persona.required', 'role:admin'])->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'dashboard']);
});

Frontend Integration

<!-- Vue.js component -->
<PersonaSelector 
    :current-persona="currentPersona"
    :available-personas="availablePersonas"
    @persona-changed="handlePersonaChange"
/>

πŸ§ͺ Testing

The package includes comprehensive test coverage:

composer test

Current test metrics:

  • 58 tests across Unit, Feature, Integration, and Listeners
  • 87% code coverage
  • 149 assertions ensuring reliability

🀝 Contributing

We welcome contributions! See our Contributing Guide for details.

Development Setup

git clone https://github.com/grazulex/laravel-multipersona.git
cd laravel-multipersona
composer install
composer test

πŸ”’ Security

If you discover any security vulnerabilities, please review our Security Policy.

πŸ“„ License

Laravel MultiPersona is open-sourced software licensed under the MIT license.


Made with ❀️ for the Laravel community

πŸ“– Documentation β€’ πŸ› Report Issues β€’ πŸ’¬ Discussions

Grazulex Laravel Multipersona Related Articles

Switch Between Personas in Laravel With the MultiPersona Package image

Switch Between Personas in Laravel With the MultiPersona Package

Read article
Record and Replay Requests With Laravel ChronoTrace image

Record and Replay Requests With Laravel ChronoTrace

Read article
Laravel API Route image

Laravel API Route

Read article
Laravel Devtoolbox: Your Swiss Army Knife Artisan CLI image

Laravel Devtoolbox: Your Swiss Army Knife Artisan CLI

Read article
Generate Complete Application Modules with a Single Command using Laravel TurboMaker image

Generate Complete Application Modules with a Single Command using Laravel TurboMaker

Read article
Generate Secure Temporary Share Links for Files in Laravel image

Generate Secure Temporary Share Links for Files in Laravel

Read article

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios β€” send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing