Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Multitenancy\Broadcasting;

use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Contracts\Redis\Factory as Redis;

class TenantAwareBroadcastManager extends BroadcastManager
{
Expand All @@ -18,4 +19,13 @@ public function createPusherDriver($config)
{
return new TenantAwarePusherBroadcaster($this->pusher($config), $this->tenantId);
}

public function createRedisDriver($config)
{
return new TenantAwareRedisBroadcaster(
$this->app->make(Redis::class),
$config['connection'] ?? 'default',
$this->tenantId
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ProcessMaker\Multitenancy\Broadcasting;

use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
use Illuminate\Contracts\Redis\Factory as Redis;

class TenantAwareRedisBroadcaster extends RedisBroadcaster
{
private int $tenantId;

public function __construct(Redis $redis, string $connection, int $tenantId)
{
parent::__construct($redis, $connection);
$this->tenantId = $tenantId;
}

protected function formatChannels(array $channels)
{
$channels = array_map(function ($channel) {
$channel = (string) $channel;
if ($this->tenantId) {
// Check if channel starts with "private-"
if (str_starts_with($channel, 'private-')) {
return "private-tenant_{$this->tenantId}." . substr($channel, 8); // Remove "private-" prefix and add tenant before the rest
}

return "tenant_{$this->tenantId}.{$channel}";
}

return $channel;
}, $channels);

return $channels;
}
}
166 changes: 166 additions & 0 deletions routes/channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,169 @@
Broadcast::channel('ProcessMaker.Models.Process.{processId}.Language.{language}', function ($user, $processId, $language) {
return true;
});

// Tenant-aware channel authorizations
Broadcast::channel('tenant_{tenantId}.ProcessMaker.Models.User.{id}', function ($user, $tenantId, $id) {
// Handle anonymous users - they should not have access to user-specific channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

return (int) $user->id === (int) $id;
});

Broadcast::channel('tenant_{tenantId}.ProcessMaker.Models.ProcessRequest.{id}', function ($user, $tenantId, $id) {
if ($id === 'undefined' || $user === 'undefined') {
return false;
}

// Handle anonymous users - they should not have access to process request channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

if ($user->is_administrator) {
return true;
}

$request = ProcessRequest::find($id);

if (!$request) {
return false;
}

return $request->user_id === $user->id
|| !empty($request->participants()->where('users.id', $user->getKey())->first())
|| $request->process?->manager_id === $user->id;
});

Broadcast::channel('tenant_{tenantId}.ProcessMaker.Models.ProcessRequestToken.{id}', function ($user, $tenantId, $id) {
// Handle anonymous users - they should not have access to process request token channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

if ($user->is_administrator) {
return true;
}

$token = ProcessRequestToken::find($id);

if (!$token) {
return false;
}

return $user->getKey() === $token->user_id;
});

Broadcast::channel('tenant_{tenantId}.test.status', function ($user, $tenantId) {
return true;
});

Broadcast::channel('tenant_{tenantId}.ProcessMaker.Models.Process.{processId}.Language.{language}', function ($user, $tenantId, $processId, $language) {
return true;
});

// Private tenant-aware channel authorizations
Broadcast::channel('private-tenant_{tenantId}.ProcessMaker.Models.User.{id}', function ($user, $tenantId, $id) {
// Handle anonymous users - they should not have access to private channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

return (int) $user->id === (int) $id;
});

Broadcast::channel('private-tenant_{tenantId}.ProcessMaker.Models.ProcessRequest.{id}', function ($user, $tenantId, $id) {
if ($id === 'undefined' || $user === 'undefined') {
return false;
}

// Handle anonymous users - they should not have access to private channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

if ($user->is_administrator) {
return true;
}

$request = ProcessRequest::find($id);

if (!$request) {
return false;
}

return $request->user_id === $user->id
|| !empty($request->participants()->where('users.id', $user->getKey())->first())
|| $request->process?->manager_id === $user->id;
});

Broadcast::channel('private-tenant_{tenantId}.ProcessMaker.Models.ProcessRequestToken.{id}', function ($user, $tenantId, $id) {
// Handle anonymous users - they should not have access to private channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

if ($user->is_administrator) {
return true;
}

$token = ProcessRequestToken::find($id);

if (!$token) {
return false;
}

return $user->getKey() === $token->user_id;
});

Broadcast::channel('private-tenant_{tenantId}.test.status', function ($user, $tenantId) {
return true;
});

Broadcast::channel('private-tenant_{tenantId}.ProcessMaker.Models.Process.{processId}.Language.{language}', function ($user, $tenantId, $processId, $language) {
return true;
});

// Generic package channel authorizations - allow authenticated users to access package channels
Broadcast::channel('private-tenant_{tenantId}.ProcessMaker.Package.{packageName}.{model}.{id}', function ($user, $tenantId, $packageName, $model, $id) {
// Handle anonymous users - they should not have access to private package channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

// Allow authenticated users to access package channels
return true;
});

Broadcast::channel('tenant_{tenantId}.ProcessMaker.Package.{packageName}.{model}.{id}', function ($user, $tenantId, $packageName, $model, $id) {
// Handle anonymous users - they should not have access to package channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

// Allow authenticated users to access package channels
return true;
});

// Generic package channel authorizations without tenant prefix
Broadcast::channel('private-ProcessMaker.Package.{packageName}.{model}.{id}', function ($user, $packageName, $model, $id) {
// Handle anonymous users - they should not have access to private package channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

// Allow authenticated users to access package channels
return true;
});

Broadcast::channel('ProcessMaker.Package.{packageName}.{model}.{id}', function ($user, $packageName, $model, $id) {
// Handle anonymous users - they should not have access to package channels
if (isset($user->isAnonymous) && $user->isAnonymous) {
return false;
}

// Allow authenticated users to access package channels
return true;
});