grazulex/laravel-multipersona
Composer 安装命令:
composer require 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.
README 文档
README
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:
HasPersonasfor yourUsermodel - Middleware:
EnsureActivePersona,SetPersonaFromRequest - Helpers:
persona(),personas($user) - Events: Complete event system for persona lifecycle
- Service:
PersonaManagerfor programmatic access - Contract:
PersonaInterfacefor 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
- 📖 Installation Guide - Complete setup instructions
- � Usage Guide - Basic and advanced usage examples
- ⚡ Quick Start Example - Working code examples
Core Features
- 🎭 Events Guide - Event system and listeners
- 🛡️ Middleware Guide - Route protection and context
- 🏗️ Architecture - System design and components
Advanced Topics
- 🎯 Advanced Patterns - Multi-tenant, role hierarchy, delegation
- 🌐 Frontend Integration - Vue.js, React, Alpine.js examples
- 📋 API Reference - Complete method documentation
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
grazulex/laravel-multipersona 适用场景与选型建议
grazulex/laravel-multipersona 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 325 次下载、GitHub Stars 达 97, 最近一次更新时间为 2025 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Context」 「middleware」 「laravel」 「Persona」 「roles」 「tenants」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 grazulex/laravel-multipersona 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grazulex/laravel-multipersona 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 grazulex/laravel-multipersona 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat Context for testing Symfony Api
Shoot aims to make providing data to your templates more manageable
Context support (request, session and application scope) for n2n framework.
Slim Framework 3 CSRF protection middleware utilities
Standalone CLI that generates codebase context (JSON + Markdown) for AI agents.
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 325
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 98
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-05
