Skip to content

Commit 929a573

Browse files
committed
Refactor tenant queue authorization
1 parent 0832e48 commit 929a573

1 file changed

Lines changed: 27 additions & 40 deletions

File tree

ProcessMaker/Http/Controllers/Admin/TenantQueueController.php

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,12 @@
1414

1515
class TenantQueueController extends Controller
1616
{
17-
/**
18-
* Constructor to check if tenant tracking is enabled.
19-
*/
20-
public function __construct()
21-
{
22-
// Check if tenant job tracking is enabled
23-
$enabled = TenantQueueServiceProvider::enabled();
24-
25-
if (!$enabled) {
26-
if (!app()->runningInConsole()) {
27-
abort(404, 'Tenant queue tracking is disabled');
28-
}
29-
}
30-
31-
// If the route binding has a tenant id, check if the user is allowed to access the tenant queue
32-
if ($id = (int) request()->route('tenantId')) {
33-
if (!TenantQueueServiceProvider::allowAllTenats() && $id !== app('currentTenant')?->id) {
34-
throw new AuthorizationException();
35-
}
36-
}
37-
}
38-
3917
/**
4018
* Show the tenant jobs dashboard.
4119
*/
4220
public function index()
4321
{
44-
if (!Auth::user()->is_administrator) {
45-
throw new AuthorizationException();
46-
}
22+
$this->checkPermissions();
4723

4824
return view('admin.tenant-queues.index');
4925
}
@@ -53,9 +29,7 @@ public function index()
5329
*/
5430
public function getTenants(): JsonResponse
5531
{
56-
if (!Auth::user()->is_administrator) {
57-
throw new AuthorizationException();
58-
}
32+
$this->checkPermissions();
5933

6034
$tenantsWithJobs = TenantQueueServiceProvider::getTenantsWithJobs();
6135

@@ -87,9 +61,7 @@ public function getTenants(): JsonResponse
8761
*/
8862
public function getTenantJobs(Request $request, string $tenantId): JsonResponse
8963
{
90-
if (!Auth::user()->is_administrator) {
91-
throw new AuthorizationException();
92-
}
64+
$this->checkPermissions();
9365

9466
$status = $request->get('status');
9567
$limit = min((int) $request->get('limit', 50), 100); // Max 100 jobs
@@ -125,9 +97,7 @@ public function getTenantStats(string $tenantId): JsonResponse
12597
*/
12698
public function getOverallStats(): JsonResponse
12799
{
128-
if (!Auth::user()->is_administrator) {
129-
throw new AuthorizationException();
130-
}
100+
$this->checkPermissions();
131101

132102
$tenantsWithJobs = TenantQueueServiceProvider::getTenantsWithJobs();
133103

@@ -163,9 +133,7 @@ public function getOverallStats(): JsonResponse
163133
*/
164134
public function getJobDetails(string $tenantId, string $jobId): JsonResponse
165135
{
166-
if (!Auth::user()->is_administrator) {
167-
throw new AuthorizationException();
168-
}
136+
$this->checkPermissions();
169137

170138
$tenantKey = "tenant_jobs:{$tenantId}:{$jobId}";
171139
$jobData = Redis::hgetall($tenantKey);
@@ -199,9 +167,7 @@ public function getJobDetails(string $tenantId, string $jobId): JsonResponse
199167
*/
200168
public function clearTenantJobs(string $tenantId): JsonResponse
201169
{
202-
if (!Auth::user()->is_administrator) {
203-
throw new AuthorizationException();
204-
}
170+
$this->checkPermissions();
205171

206172
try {
207173
$pattern = "tenant_jobs:{$tenantId}:*";
@@ -228,4 +194,25 @@ public function clearTenantJobs(string $tenantId): JsonResponse
228194
return response()->json(['error' => 'Failed to clear tenant job data'], 500);
229195
}
230196
}
197+
198+
private function checkPermissions(): void
199+
{
200+
// Check if tenant job tracking is enabled
201+
$enabled = TenantQueueServiceProvider::enabled();
202+
203+
if (!$enabled) {
204+
throw new AuthorizationException('Tenant queue tracking is disabled');
205+
}
206+
207+
if (!Auth::user()->is_administrator) {
208+
throw new AuthorizationException();
209+
}
210+
211+
// If the route binding has a tenant id, check if the user is allowed to access the tenant queue
212+
if ($id = (int) request()->route('tenantId')) {
213+
if (!TenantQueueServiceProvider::allowAllTenats() && $id !== app('currentTenant')?->id) {
214+
throw new AuthorizationException();
215+
}
216+
}
217+
}
231218
}

0 commit comments

Comments
 (0)