LaraCMS uses the Spatie Permission package for role-based access control (RBAC). The system provides flexible permission management with roles, permissions, and a special Super Admin role that bypasses all permission checks.
The User model integrates Spatie Permission at app/Models/User.php:10:
use Spatie\Permission\Traits\HasRoles;class User extends Authenticatable implements MustVerifyEmail, HasMedia{ use HasFactory, HasRoles, Notifiable, InteractsWithMedia;}
The HasRoles trait provides methods for assigning and checking roles and permissions.
Role::create(['name' => 'Super Admin']);// no perms – full access handled by Gate::before
Super Admin has unrestricted access to all features and bypasses all permission checks via a Gate policy. This role should only be assigned to trusted system administrators.
Permissions follow a consistent naming pattern: action.resource (e.g., create.posts, manage.users). This makes permissions easy to understand and maintain.
Super Admin role bypasses all permission checks via app/Providers/AppServiceProvider.php:28:
public function boot(): void{ // Implicitly grant "Super Admin" role all permissions // This works in the app by using gate-related functions like auth()->user->can() and @can() Gate::before(function ($user, $ability) { return $user->hasRole('Super Admin') ? true : null; });}
This Gate::before callback intercepts all permission checks and returns true for Super Admins before checking individual permissions.
Important: The Super Admin bypass only works with Laravel’s Gate methods like auth()->user()->can() and Blade’s @can() directive. Direct permission checks like hasPermissionTo() will not be bypassed.
use Illuminate\Support\Facades\Gate;if (Gate::allows('create.posts')) { // User can create posts}if (Gate::denies('delete.posts')) { // User cannot delete posts}
// Check permissionif ($user->can('edit.posts')) { // User has permission}// Check roleif ($user->hasRole('admin')) { // User is an admin}// Check any roleif ($user->hasAnyRole(['admin', 'editor'])) { // User has at least one of these roles}// Check all rolesif ($user->hasAllRoles(['admin', 'editor'])) { // User has all these roles}// Direct permission check (bypasses Super Admin gate)if ($user->hasPermissionTo('create.posts')) { // User has this specific permission}
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;class CreatePost extends Component{ use AuthorizesRequests; public function save() { $this->authorize('create.posts'); // Create post logic }}
// Assign single role$user->assignRole('editor');// Assign multiple roles$user->assignRole(['writer', 'editor']);// Sync roles (removes all existing roles and assigns new ones)$user->syncRoles(['editor']);// Remove role$user->removeRole('writer');
// Give permission to user$user->givePermissionTo('edit.posts');// Give multiple permissions$user->givePermissionTo(['edit.posts', 'delete.posts']);// Revoke permission$user->revokePermissionTo('delete.posts');
Permission names are not included in exception messages by default to prevent information leakage. Enable these options in development for better debugging.