|
|
@@ -3,25 +3,94 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use App\Models\UserTypePermission;
|
|
|
-use Cache;
|
|
|
|
|
|
class UserTypePermissionService
|
|
|
{
|
|
|
+ private const CACHE_TTL = 60 * 60 * 24;
|
|
|
+
|
|
|
public function allGuestPermissions(): ?Collection
|
|
|
{
|
|
|
- return Cache::remember("permissions_guest", 60 * 60 * 24, function () {
|
|
|
- return UserTypePermission::with('permissions')->where("user_type", "guest")->get();
|
|
|
+ return Cache::remember("permissions_guest", self::CACHE_TTL, function () {
|
|
|
+ return UserTypePermission::with('permission')->where("user_type", "guest")->get();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public function allPermissionsByUserType(
|
|
|
string $userType,
|
|
|
): ?Collection {
|
|
|
- return Cache::remember("permissions_role_{$userType}", 60 * 60 * 24, function () use ($userType) {
|
|
|
+ return Cache::remember("permissions_role_{$userType}", self::CACHE_TTL, function () use ($userType) {
|
|
|
return UserTypePermission::with('permission')
|
|
|
->where('user_type', $userType)
|
|
|
->get();
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Bits atribuídos ao perfil, indexados por permission_id (para montar a matriz).
|
|
|
+ *
|
|
|
+ * @return array<int, int>
|
|
|
+ */
|
|
|
+ public function bitsByPermissionId(string $userType): array
|
|
|
+ {
|
|
|
+ return UserTypePermission::where('user_type', $userType)
|
|
|
+ ->pluck('bits', 'permission_id')
|
|
|
+ ->map(fn ($bits) => (int) $bits)
|
|
|
+ ->all();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sincroniza a matriz de permissões de um perfil.
|
|
|
+ *
|
|
|
+ * @param array<int, array{permission_id: int, bits: int}> $items
|
|
|
+ */
|
|
|
+ public function syncPermissions(string $userType, array $items): void
|
|
|
+ {
|
|
|
+ DB::transaction(function () use ($userType, $items) {
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $permissionId = (int) $item['permission_id'];
|
|
|
+ $bits = (int) $item['bits'];
|
|
|
+
|
|
|
+ // withTrashed para reaproveitar linha antes soft-deleted (evita duplicatas).
|
|
|
+ $existing = UserTypePermission::withTrashed()
|
|
|
+ ->where('user_type', $userType)
|
|
|
+ ->where('permission_id', $permissionId)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($bits <= 0) {
|
|
|
+ // Sem nenhum bit = sem acesso: remove a linha para manter a tabela enxuta.
|
|
|
+ $existing?->delete();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($existing) {
|
|
|
+ if ($existing->trashed()) {
|
|
|
+ $existing->restore();
|
|
|
+ }
|
|
|
+ $existing->bits = $bits;
|
|
|
+ $existing->save();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ UserTypePermission::create([
|
|
|
+ 'user_type' => $userType,
|
|
|
+ 'permission_id' => $permissionId,
|
|
|
+ 'bits' => $bits,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->forgetCache($userType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function forgetCache(string $userType): void
|
|
|
+ {
|
|
|
+ Cache::forget("permissions_role_{$userType}");
|
|
|
+
|
|
|
+ if (strtolower($userType) === 'guest') {
|
|
|
+ Cache::forget("permissions_guest");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|