Repository navigation
Expand file tree
/
Copy pathPermissionServiceManager.php
More file actions
153 lines (126 loc) · 4.29 KB
/
Copy pathPermissionServiceManager.php
File metadata and controls
153 lines (126 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace ProcessMaker\Services;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Contracts\PermissionCacheInterface;
use ProcessMaker\Contracts\PermissionRepositoryInterface;
use ProcessMaker\Contracts\PermissionStrategyInterface;
use ProcessMaker\Services\PermissionStrategies\CachedPermissionStrategy;
class PermissionServiceManager
{
private PermissionRepositoryInterface $repository;
private PermissionCacheInterface $cacheService;
private ?PermissionStrategyInterface $defaultStrategy = null;
public function __construct(
PermissionRepositoryInterface $repository,
PermissionCacheInterface $cacheService
) {
$this->repository = $repository;
$this->cacheService = $cacheService;
// Register default strategy
$this->defaultStrategy = new CachedPermissionStrategy($cacheService, $repository);
}
/**
* Get all permissions for a user
*/
public function getUserPermissions(int $userId): array
{
// Try cache first
$cachedPermissions = $this->cacheService->getUserPermissions($userId);
if ($cachedPermissions !== null) {
return $cachedPermissions;
}
// Get from repository and cache
$permissions = $this->repository->getUserPermissions($userId);
$this->cacheService->cacheUserPermissions($userId, $permissions);
return $permissions;
}
/**
* Check if user has permission
*/
public function userHasPermission(int $userId, string $permission): bool
{
return $this->defaultStrategy->hasPermission($userId, $permission);
}
/**
* Check if user has any of the given permissions
*/
public function userHasAnyPermission(int $userId, array $permissions): bool
{
foreach ($permissions as $permission) {
if ($this->userHasPermission($userId, $permission)) {
return true;
}
}
return false;
}
/**
* Check if user has all of the given permissions
*/
public function userHasAllPermissions(int $userId, array $permissions): bool
{
foreach ($permissions as $permission) {
if (!$this->userHasPermission($userId, $permission)) {
return false;
}
}
return true;
}
/**
* Warm up cache for a user
*/
public function warmUpUserCache(int $userId): void
{
try {
$permissions = $this->repository->getUserPermissions($userId);
$this->cacheService->cacheUserPermissions($userId, $permissions);
Log::info("Warmed up permission cache for user {$userId}");
} catch (\Exception $e) {
Log::error("Failed to warm up permission cache for user {$userId}: " . $e->getMessage());
}
}
/**
* Invalidate cache for a user
*/
public function invalidateUserCache(int $userId): void
{
$this->cacheService->invalidateUserPermissions($userId);
Log::info("Invalidated permission cache for user {$userId}");
}
/**
* Invalidate caches for multiple users.
*/
public function invalidateUserCaches(array $userIds): void
{
$userIds = array_values(array_unique(array_map('intval', $userIds)));
foreach ($userIds as $userId) {
if ($userId <= 0) {
continue;
}
$this->cacheService->invalidateUserPermissions($userId);
}
Log::info('Invalidated permission cache for affected users', [
'user_count' => count($userIds),
'user_ids' => $userIds,
]);
}
/**
* Invalidate the caches affected by a group permission change.
*/
public function invalidateAffectedCachesForGroup(int $groupId): void
{
$this->cacheService->invalidateGroupPermissions($groupId);
$userIds = $this->repository->getAffectedUserIdsForGroup($groupId);
$this->invalidateUserCaches($userIds);
Log::info("Invalidated permission cache for group {$groupId}", [
'affected_user_count' => count($userIds),
'affected_user_ids' => $userIds,
]);
}
/**
* Invalidate all permission caches
*/
public function invalidateAll(): void
{
$this->cacheService->clearAll();
}
}