Grazulex/
laravel-multipersona
g4t.io

Switch Between Personas in Laravel With the MultiPersona Package

16 views 98 stars by Paul Redmond

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.

README from Grazulex/laravel-multipersona Open on GitHub →

Laravel MultiPersona

[!TIP] What Laravel MultiPersona does for you — Let one user act as several personas — admin and client, company A and company B — and switch between them without extra logins or sessions. Backend-only, it works with your existing policies and middleware.

This package is free and maintained on my own time. If it saves you hours, a small contribution helps me keep it going: 💖 GitHub Sponsors · ☕ Buy Me a Coffee · PayPal

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

Latest Version Total Downloads License PHP Version Laravel Version Tests Code Style


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

Requirements

  • PHP 8.3 or higher
  • Laravel 12.x or 13.x
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.

💖 Support This Package

Laravel MultiPersona is free, open source and maintained on my own time. If it saves you hours, here is how you can give back:

📄 License

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


More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News