Download the PHP package squareetlabs/laravel-teams-permissions without Composer
On this page you can find all versions of the php package squareetlabs/laravel-teams-permissions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download squareetlabs/laravel-teams-permissions
More information about squareetlabs/laravel-teams-permissions
Files in squareetlabs/laravel-teams-permissions
Package laravel-teams-permissions
Short Description A comprehensive Laravel package for advanced team-based permission management in multi-tenant applications. Features RBAC, granular permissions, entity-specific abilities, global groups, audit logging, REST API, and seamless Laravel integration.
License MIT
Informations about the package laravel-teams-permissions
Squareetlabs/LaravelTeamsPermissions
A comprehensive Laravel package designed for advanced team-based permission management in multi-tenant applications based on [Jurager/Teams]. This package provides a flexible and powerful system for organizing users into teams, assigning granular permissions through roles and groups, and managing entity-specific access controls.
Core Functionality:
-
Team Management: Create and manage teams with owners and members. Each team operates as an independent workspace with its own set of roles, permissions, and members.
-
Role-Based Access Control (RBAC): Define custom roles for each team with specific permission sets. Roles can be assigned to users within a team, providing a flexible way to manage access levels. Team owners automatically have full access to all permissions.
-
Permission System: Implement fine-grained permissions using a code-based system (e.g.,
posts.create,users.edit). Permissions are global entities that can be assigned to roles and groups across multiple teams. Supports wildcard permissions for flexible access patterns. -
Group Management: Organize users into groups within teams or globally. Groups can have their own permission sets, and permissions assigned to a group take precedence over individual user permissions within a team. This allows for efficient permission management when multiple users need the same access level.
-
Global Groups: Create groups without team association to grant users access across all teams with the group's permissions. Perfect for scenarios like support teams, administrators, or auditors who need consistent access across multiple teams without being individually added to each one.
-
Entity-Specific Abilities: Grant or deny permissions for specific model instances (e.g., allowing a user to edit a particular post but not others). This provides the most granular level of access control, enabling fine-tuned permissions for individual resources.
-
Multi-Tenant Support: Built from the ground up for multi-tenant applications where each team represents a tenant. Teams are completely isolated, ensuring data security and access control between different tenants.
-
Caching & Performance: Optional intelligent caching system to optimize permission checks, reducing database queries and improving application performance.
-
Audit Logging: Optional comprehensive audit trail that logs all team-related actions including role assignments, permission changes, and member additions/removals.
-
REST API: Optional complete REST API for team management, enabling frontend applications and third-party integrations to manage teams programmatically.
- Laravel Integration: Seamlessly integrates with Laravel's built-in authorization system, including Policies, Blade directives, and middleware for route protection.
Key Features
- ✅ Team Management: Create and manage teams with owners and members
- ✅ Roles & Permissions: Flexible role system with granular permissions
- ✅ Groups: Organize users into groups with shared permissions
- ✅ Abilities: Entity-specific permissions for individual models
- ✅ Smart Caching: Caching system to optimize permission checks
- ✅ Audit Logging: Complete action logging for teams (optional)
- ✅ REST API: Complete API for team management (optional)
- ✅ Blade Directives: Blade directives for permission checks in views
- ✅ Policies: Integration with Laravel's Policy system
- ✅ Rate Limiting: Protection against invitation spam
- ✅ Middleware: Middleware for route protection
- ✅ Artisan Commands: CLI tools for management
Requirements
- PHP >= 8.1
- Laravel 8.x, 9.x, 10.x, 11.x or 12.x
Installation
1. Install the Package
2. Publish Configuration and Migrations
This will publish:
config/teams.php- Configuration file- Database migrations
3. Configure the User Model
Add the HasTeams trait to your User model:
4. Run Migrations
⚠️ IMPORTANT: Always do backups before running migrations.
[!NOTE] If you wish to use custom foreign keys and table names, modify
config/teams.phpbefore running migrations.
5. Optional Configuration
Enable Caching
To improve performance, enable caching in .env:
Enable Audit Logging
To log all team actions:
[!NOTE] If you enable audit logging after running migrations, you'll need to publish and run the audit migration:
Enable REST API
To expose a REST API for team management:
Configuration
The configuration file config/teams.php contains all options:
Custom Models
Cache
Audit
See config/teams.php for all available options.
Basic Usage
Creating a Team
Adding Roles and Permissions
Adding Team Members
Checking Permissions
Teams
Available Methods
Users
The HasTeams trait provides the following methods:
Roles & Permissions
Creating Roles with Permissions
Wildcard Permissions
You can use wildcards for permissions:
posts.*- All permissions starting withposts.*- All permissions (if enabled in config)
Checking Permissions
Wildcard Permissions
You can enable wildcard permissions in configuration:
Users with these permissions will have full access to the team.
Abilities
Abilities allow specific permissions for individual entities.
Adding an Ability
Checking an Ability
Access Levels
Abilities use an access level system:
| Level | Value | Description |
|---|---|---|
DEFAULT |
0 | No explicit permissions |
FORBIDDEN |
1 | Access denied |
ROLE_ALLOWED |
2 | Allowed by role |
ROLE_FORBIDDEN |
3 | Forbidden by role |
GROUP_ALLOWED |
4 | Allowed by group |
GROUP_FORBIDDEN |
5 | Forbidden by group |
USER_ALLOWED |
5 | Specifically allowed to user |
USER_FORBIDDEN |
6 | Specifically forbidden to user |
GLOBAL_ALLOWED |
6 | Global permissions |
Access is granted if the allowed level >= forbidden level.
Groups
Groups allow organizing users with shared permissions.
Creating and Managing Groups
Global Groups
Groups without team_id are global and apply to all teams:
Middleware
The package provides middleware for route protection.
Configuration
Middleware is automatically registered as role, permission, and ability.
Usage in Routes
OR Operations
AND Operations
Blade Directives
The package includes Blade directives for permission checks in views:
Policies
The package integrates with Laravel's Policy system.
Generate a Policy
This generates a policy extending TeamPolicy:
Using the Policy
REST API
If you enable the REST API, you'll have access to complete endpoints for team management.
Enable API
Available Endpoints
Authentication
The API requires Sanctum authentication:
Artisan Commands
The package includes several useful commands:
Team Management
Permission Management
Utilities
Caching
The caching system significantly improves permission check performance.
Configuration
Clear Cache
Or programmatically:
Audit Logging
The audit system logs all important team actions.
Enable Audit
Audited Events
role_assigned- Role assignmentpermission_granted- Permission grantedpermission_revoked- Permission revokedteam_member_added- Member addedteam_member_removed- Member removed
Query Logs
[!NOTE] If you enable audit logging after running migrations, you'll need to run:
Events
The package fires events for important actions:
Available Events
TeamCreating/TeamCreatedTeamUpdating/TeamUpdatedTeamDeletedTeamMemberAdding/TeamMemberAddedTeamMemberRemoving/TeamMemberRemovedTeamMemberUpdatedTeamMemberInviting/TeamMemberInvited
Validation
The package includes validation rules:
Usage Examples
Blog System with Teams
Multi-tenant SaaS Application
Testing
The package includes factories and seeders for testing:
Troubleshooting
Error: "Model class for key user not found"
Make sure your User model is configured in config/teams.php:
Error: "AuditTableMissingException"
If you enable audit logging after running migrations:
Or disable audit logging in config/teams.php:
Cache Not Updating
Clear cache manually:
API Documentation
Classes and Methods
HasTeams Trait
Methods available on User model:
ownsTeam(Team $team): bool- Check if user owns the teamallTeams(): Collection- Get all teams (owned and belongs to)ownedTeams(): HasMany- Get teams user ownsteams(): BelongsToMany- Get teams user belongs tobelongsToTeam(Team $team): bool- Check if user belongs to teamteamRole(Team $team): ?Role- Get user's role in teamhasTeamRole(Team $team, string|array $roles, bool $require = false): bool- Check if user has role(s)teamPermissions(Team $team, ?string $scope = null): array- Get user's permissions for teamhasTeamPermission(Team $team, string|array $permissions, bool $require = false, ?string $scope = null): bool- Check if user has permission(s)teamAbilities(Team $team, Model $entity, bool $forbidden = false): Collection- Get user's abilities for entityhasTeamAbility(Team $team, string $permission, Model $action_entity): bool- Check if user has abilityallowTeamAbility(Team $team, string $permission, Model $action_entity, ?Model $target_entity = null): void- Allow abilityforbidTeamAbility(Team $team, string $permission, Model $action_entity, ?Model $target_entity = null): void- Forbid abilitydeleteTeamAbility(Team $team, string $permission, Model $action_entity, ?Model $target_entity = null): void- Delete abilityscopeWithTeamPermissions($query)- Eager load team permissions
HasMembers Trait
Methods available on Team model:
owner(): BelongsTo- Get team ownerusers(): BelongsToMany- Get team membersabilities(): HasMany- Get team abilitiesroles(): HasMany- Get team rolesgroups(): HasMany- Get team groupsinvitations(): HasMany- Get pending invitationsallUsers(): Collection- Get all users including ownerhasUser(User $user): bool- Check if user is memberaddUser(User $user, string $role_keyword): void- Add user to teamupdateUser(User $user, string $role_keyword): void- Update user's roledeleteUser(User $user): void- Remove user from teaminviteUser(string $email, int|string $keyword): void- Invite user by emailinviteAccept(int $invitation_id): void- Accept invitationhasUserWithEmail(string $email): bool- Check if team has user with emailuserRole(User $user): ?Role- Get user's role in teamuserHasPermission(User $user, string|array $permissions, bool $require = false): bool- Check if user has permissionhasRole(int|string|null $keyword = null): bool- Check if team has rolegetRole(int|string $keyword): ?Role- Get role by ID or codeaddRole(string $code, array $permissions, ?string $name = null, ?string $description = null): Role- Add roleupdateRole(int|string $keyword, array $permissions, ?string $name = null, ?string $description = null): Role- Update roledeleteRole(int|string $keyword): bool- Delete rolehasGroup(int|string|null $keyword = null): bool- Check if team has groupgetGroup(int|string $keyword): ?Group- Get group by ID or codeaddGroup(string $code, array $permissions = [], ?string $name = null): Group- Add groupupdateGroup(int|string $keyword, array $permissions = [], ?string $name = null): Group- Update groupdeleteGroup(int|string $keyword): bool- Delete grouppurge(): void- Delete team and all relationsgetPermissionIds(array $codes): array- Get permission IDs for codes
PermissionCache Service
remember(string $key, callable $callback, ?int $ttl = null): mixed- Cache a valueflush(): void- Flush all cacheforget(string $key): void- Forget specific keyget(string $key, mixed $default = null): mixed- Get cached value
AuditService Service
log(string $action, mixed $team, mixed $user, mixed $subject = null, ?array $oldValues = null, ?array $newValues = null): void- Log audit eventlogRoleAssigned(mixed $team, mixed $user, mixed $role): void- Log role assignmentlogPermissionGranted(mixed $team, mixed $user, string $permission): void- Log permission grantedlogPermissionRevoked(mixed $team, mixed $user, string $permission): void- Log permission revokedlogTeamMemberAdded(mixed $team, mixed $user, mixed $member, mixed $role): void- Log member addedlogTeamMemberRemoved(mixed $team, mixed $user, mixed $member): void- Log member removed
TeamPolicy Base Class
checkTeamPermission(Model $user, Model $team, string $permission): bool- Check team permissioncheckTeamAbility(Model $user, Model $team, string $ability, Model $model): bool- Check team abilitycheckTeamRole(Model $user, Model $team, string|array $roles): bool- Check team rolegetTeamFromModel(Model $model): ?Model- Get team from model
Contributing
Contributions are welcome. Please:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Changelog
See CHANGELOG.md for the full list of changes.
License
This package is open-sourced software licensed under the MIT license.
Support
For support, please open an issue on GitHub.
Authors
- Squareetlabs - [email protected]
All versions of laravel-teams-permissions with dependencies
ext-json Version *
illuminate/support Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/http Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/bus Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/mail Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/queue Version ^8.0|^9.0|^10.0|^11.0|^12.0