Skip to content

Latest commit

 

History

History
292 lines (217 loc) · 6.17 KB

File metadata and controls

292 lines (217 loc) · 6.17 KB

Artisan Commands Quick Reference

🔍 Viewing Relationships

Show User's Roles and Permissions

# By user ID
php artisan user:show 1

# By email
php artisan user:show john@example.com

# With specific guard
php artisan user:show 1 --guard=api

Shows:

  • User information (ID, name, email)
  • All roles assigned to the user
  • All permissions from those roles
  • Grouped by module for easy reading

Show Role's Permissions and Users

# By role name
php artisan role:show admin

# By role ID
php artisan role:show 1

# Include users with this role
php artisan role:show admin --with-users

# With specific guard
php artisan role:show admin --guard=web

Shows:

  • Role information (ID, name, description, guard)
  • All permissions assigned to the role
  • Optionally: All users with this role

Show Module's Permissions and Roles

# By module name
php artisan module:show users

# By module ID
php artisan module:show 1

# Include roles with access
php artisan module:show users --with-roles

# With specific guard
php artisan module:show users --guard=web

Shows:

  • Module information (ID, name, description, guard)
  • All available actions (permissions) for the module
  • Optionally: All roles that have access to this module

📋 Listing

List All Permissions

# List all permissions
php artisan permissions:list

# Filter by role
php artisan permissions:list --role=admin

# Filter by module
php artisan permissions:list --module=users

# Filter by guard
php artisan permissions:list --guard=api

🔧 Management

Sync Permissions from Controllers

# Preview what will be synced
php artisan permissions:sync --preview

# Sync from routes
php artisan permissions:sync --source=routes

# Sync from controllers directory
php artisan permissions:sync --source=controllers

# Sync with specific guard
php artisan permissions:sync --guard=api

# Sync from custom path
php artisan permissions:sync --source=controllers --path=/path/to/controllers

Create a Role

# Create role with description
php artisan role:create admin --description="Administrator role"

# Create role with specific guard
php artisan role:create editor --guard=api

Assign Permission to Role

# Assign single permission (auto-creates module and action if needed)
php artisan role:assign-permission admin users create

# Assign with specific guard
php artisan role:assign-permission editor posts update --guard=web

Assign All Permissions to Role

# Assign ALL permissions to a role
php artisan role:assign-all-permissions super-admin

# Assign all permissions for a specific module
php artisan role:assign-all-permissions manager --module=users

# Assign all permissions for a specific action across all modules
php artisan role:assign-all-permissions reader --action=read

# Combine filters: all 'create' permissions for 'users' module
php artisan role:assign-all-permissions editor --module=users --action=create

# Skip confirmation prompt (useful for scripts)
php artisan role:assign-all-permissions admin --force

# With specific guard
php artisan role:assign-all-permissions admin --guard=api

Shows:

  • Role information
  • List of all permissions that will be assigned (grouped by module)
  • Applied filters
  • Summary: newly assigned, already assigned, and total permissions

💡 Common Use Cases

Give a role full access to everything

php artisan role:assign-all-permissions super-admin --force

Give a role access to one module

php artisan role:assign-all-permissions manager --module=users

Give a role only read access everywhere

php artisan role:assign-all-permissions viewer --action=read

Find out which users have admin access

php artisan role:show admin --with-users

Check what permissions a user has

php artisan user:show john@example.com

See which roles can access the users module

php artisan module:show users --with-roles

List all permissions for a specific role

php artisan permissions:list --role=editor

Find all permissions for the users module

php artisan permissions:list --module=users

🎯 Quick Workflow

1. Initial Setup

# Sync permissions from your controllers
php artisan permissions:sync --preview
php artisan permissions:sync

# Create roles
php artisan role:create admin
php artisan role:create editor
php artisan role:create viewer

2. Assign Permissions

# Option A: Assign one by one
php artisan role:assign-permission admin users create
php artisan role:assign-permission admin users read
php artisan role:assign-permission admin users update
php artisan role:assign-permission admin users delete

# Option B: Assign all at once (faster!)
php artisan role:assign-all-permissions admin --module=users

# Option C: Give admin EVERYTHING
php artisan role:assign-all-permissions admin --force

3. Check Your Work

# Verify the role has the right permissions
php artisan role:show admin

# Verify a module has the right access
php artisan module:show users --with-roles

4. Assign to Users (in code)

$user = User::find(1);
$user->assignRole('admin');

5. Verify User Access

php artisan user:show 1

🔄 Maintenance

After adding new controllers

# Always sync after creating new controllers
php artisan permissions:sync

Check who has access to sensitive modules

php artisan module:show admin --with-roles
php artisan module:show users --with-roles

Audit user permissions

php artisan user:show admin@example.com

📊 Understanding the Relationships

User  →  has many  →  Roles
Role  →  has many  →  Permissions
Permission  =  Module + Action

Examples:
- User "John" → Role "admin" → Permission "users.create"
- Permission "users.create" = Module "users" + Action "create"

Use the commands to explore these relationships:

  • user:show - Start from a user, see their roles and permissions
  • role:show - Start from a role, see its permissions and users
  • module:show - Start from a module, see its actions and which roles have access