Skip to main content

Overview

The WirechatUser contract defines the authorization interface that your User model must implement to control access to WireChat features. This contract ensures users have proper permissions for creating chats, groups, and accessing the WireChat panel.

Contract Location

Wirechat\Wirechat\Contracts\WirechatUser

Implementation

Your User model must implement this contract:
use Wirechat\Wirechat\Contracts\WirechatUser;
use Wirechat\Wirechat\Traits\InteractsWithWirechat;

class User extends Authenticatable implements WirechatUser
{
    use InteractsWithWirechat;
    
    // Implement required methods...
}

Required Methods

canCreateGroups()

Determine if the user can create new groups.
return
bool
required
Returns true if the user is allowed to create groups, false otherwise
public function canCreateGroups(): bool
{
    // Example: Only verified users can create groups
    return $this->hasVerifiedEmail();
}
This method is called before allowing users to access group creation forms or process group creation requests.

canCreateChats()

Determine if the user can create new chats with other users.
return
bool
required
Returns true if the user is allowed to create private chats, false otherwise
public function canCreateChats(): bool
{
    // Example: All authenticated users can create chats
    return true;
}
This permission is checked when users attempt to start private conversations with other users.

canAccessWirechatPanel()

Determine if the user can access the WireChat panel.
panel
Panel
required
The panel instance being accessed
return
bool
required
Returns true if the user can access the specified panel, false otherwise
use Wirechat\Wirechat\Panel;

public function canAccessWirechatPanel(Panel $panel): bool
{
    // Example: Check user role or permissions
    return $this->hasRole('user');
}
Multiple panels can be registered in WireChat. This method receives the specific panel being accessed, allowing for granular access control.

Complete Example

Here’s a complete implementation example:
namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Wirechat\Wirechat\Contracts\WirechatUser;
use Wirechat\Wirechat\Traits\InteractsWithWirechat;
use Wirechat\Wirechat\Panel;

class User extends Authenticatable implements WirechatUser
{
    use InteractsWithWirechat;
    
    /**
     * Determine if the user can create new groups.
     */
    public function canCreateGroups(): bool
    {
        // Only verified users with 'create-groups' permission
        return $this->hasVerifiedEmail() && 
               $this->can('create-groups');
    }
    
    /**
     * Determine if the user can create new chats with other users.
     */
    public function canCreateChats(): bool
    {
        // All verified users can create chats
        return $this->hasVerifiedEmail();
    }
    
    /**
     * Determine if the user can access wirechat panel.
     */
    public function canAccessWirechatPanel(Panel $panel): bool
    {
        // Check if user has appropriate role
        if ($panel->getId() === 'admin') {
            return $this->hasRole('admin');
        }
        
        return true; // Default panel accessible to all
    }
}

Authorization Flow

1

User Authentication

User must be authenticated to access WireChat features
2

Panel Access Check

canAccessWirechatPanel() is called to verify panel access
3

Feature-Specific Checks

When creating groups: canCreateGroups() is calledWhen creating chats: canCreateChats() is called
4

Authorization Failure

If any check returns false, a 403 Forbidden response is returned