admin felület fejlesztése garázs, előfizeztési csomagok

This commit is contained in:
Roo
2026-06-25 01:58:04 +00:00
parent 80a5d67f79
commit 52011606ff
334 changed files with 46636 additions and 1606 deletions

View File

@@ -0,0 +1,629 @@
<template>
<div>
<!-- Page Header -->
<div class="mb-8">
<h1 class="text-2xl font-bold text-white">{{ $t('garages.title') }}</h1>
<p class="text-slate-400 mt-1">{{ $t('garages.subtitle') }}</p>
</div>
<!-- Loading State -->
<div v-if="loading" class="flex items-center justify-center py-20">
<div class="flex flex-col items-center gap-3">
<svg class="w-8 h-8 text-indigo-400 animate-spin" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
</svg>
<p class="text-slate-400 text-sm">{{ $t('garages.loading') }}</p>
</div>
</div>
<!-- Error State -->
<div v-else-if="error" class="flex flex-col items-center justify-center py-20 text-center">
<svg class="w-12 h-12 text-red-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z" />
</svg>
<p class="text-red-400 text-sm mb-2">{{ error }}</p>
<button @click="fetchGarages" class="px-4 py-2 text-sm bg-indigo-600/20 text-indigo-300 hover:bg-indigo-600/30 rounded-lg transition">
{{ $t('garages.retry') }}
</button>
</div>
<!-- Content -->
<template v-else>
<!-- Stats Cards -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
<div class="bg-slate-800 rounded-xl border border-slate-700 p-4">
<div class="flex items-center gap-3">
<div class="p-2 rounded-lg bg-indigo-500/10 text-indigo-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
</svg>
</div>
<div>
<p class="text-xs text-slate-400 uppercase tracking-wider">{{ $t('garages.total_garages') }}</p>
<p class="text-2xl font-bold text-white">{{ garages.length }}</p>
</div>
</div>
</div>
<div class="bg-slate-800 rounded-xl border border-slate-700 p-4">
<div class="flex items-center gap-3">
<div class="p-2 rounded-lg bg-emerald-500/10 text-emerald-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</div>
<div>
<p class="text-xs text-slate-400 uppercase tracking-wider">{{ $t('garages.active_garages') }}</p>
<p class="text-2xl font-bold text-emerald-400">{{ activeCount }}</p>
</div>
</div>
</div>
<div class="bg-slate-800 rounded-xl border border-slate-700 p-4">
<div class="flex items-center gap-3">
<div class="p-2 rounded-lg bg-amber-500/10 text-amber-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
</svg>
</div>
<div>
<p class="text-xs text-slate-400 uppercase tracking-wider">{{ $t('garages.premium_garages') }}</p>
<p class="text-2xl font-bold text-amber-400">{{ premiumCount }}</p>
</div>
</div>
</div>
<div class="bg-slate-800 rounded-xl border border-slate-700 p-4">
<div class="flex items-center gap-3">
<div class="p-2 rounded-lg bg-purple-500/10 text-purple-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
</div>
<div>
<p class="text-xs text-slate-400 uppercase tracking-wider">{{ $t('garages.enterprise_garages') }}</p>
<p class="text-2xl font-bold text-purple-400">{{ enterpriseCount }}</p>
</div>
</div>
</div>
</div>
<!-- Search & Filter Bar -->
<div class="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4 mb-6">
<div class="relative flex-1 max-w-md">
<svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
<input
v-model="searchQuery"
type="text"
:placeholder="$t('garages.search_placeholder')"
class="w-full pl-10 pr-4 py-2.5 bg-slate-800 border border-slate-700 rounded-lg text-sm text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-indigo-500/50 focus:border-indigo-500 transition"
/>
</div>
<div class="flex items-center gap-2">
<select
v-model="filterTier"
class="px-3 py-2.5 bg-slate-800 border border-slate-700 rounded-lg text-sm text-slate-300 focus:outline-none focus:ring-2 focus:ring-indigo-500/50 transition"
>
<option value="all">{{ $t('garages.all_tiers') }}</option>
<option value="free">Free</option>
<option value="premium">Premium</option>
<option value="enterprise">Enterprise</option>
</select>
</div>
</div>
<!-- Data Table -->
<div class="bg-slate-800/50 border border-slate-700 rounded-xl overflow-hidden">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="border-b border-slate-700 bg-slate-800/80">
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">ID</th>
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">{{ $t('garages.company_name') }}</th>
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">{{ $t('garages.status') }}</th>
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">{{ $t('garages.current_package') }}</th>
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">{{ $t('garages.expiration_date') }}</th>
<th class="text-left py-3.5 px-4 text-xs font-semibold uppercase tracking-wider text-slate-400">{{ $t('garages.actions') }}</th>
</tr>
</thead>
<tbody>
<tr
v-for="garage in filteredGarages"
:key="garage.id"
class="border-b border-slate-700/50 hover:bg-slate-700/30 transition"
>
<!-- ID -->
<td class="py-3.5 px-4 text-slate-400 font-mono text-xs">{{ garage.id }}</td>
<!-- Company Name -->
<td class="py-3.5 px-4">
<div class="flex items-center gap-3">
<div
class="w-8 h-8 rounded-lg flex items-center justify-center text-sm font-bold text-white"
:class="getInitialsColor(garage.full_name)"
>
{{ getInitials(garage.full_name) }}
</div>
<div>
<p class="text-sm font-medium text-white">{{ garage.full_name }}</p>
<p class="text-xs text-slate-500">{{ garage.city || '—' }} · {{ garage.member_count }} {{ $t('garages.members') }}</p>
</div>
</div>
</td>
<!-- Status -->
<td class="py-3.5 px-4">
<span
class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium"
:class="statusBadgeClass(garage.status)"
>
<span
class="w-1.5 h-1.5 rounded-full mr-1.5"
:class="statusDotClass(garage.status)"
></span>
{{ statusLabel(garage.status) }}
</span>
</td>
<!-- Current Package -->
<td class="py-3.5 px-4">
<span
class="px-2.5 py-0.5 rounded-full text-xs font-medium"
:class="tierBadgeClass(garage.subscription_tier_name)"
>
{{ garage.subscription_tier_name }}
</span>
</td>
<!-- Expiration Date -->
<td class="py-3.5 px-4">
<span v-if="garage.subscription_expires_at" class="text-slate-300 text-xs">
{{ formatDate(garage.subscription_expires_at) }}
</span>
<span v-else class="text-slate-500 text-xs"></span>
</td>
<!-- Actions -->
<td class="py-3.5 px-4">
<div class="flex items-center gap-2">
<button
@click="openSubscriptionModal(garage)"
class="px-3 py-1.5 text-xs font-medium bg-indigo-600/20 text-indigo-300 hover:bg-indigo-600/30 rounded-lg transition"
>
{{ $t('garages.manage_subscription') }}
</button>
<button
@click="toggleStatus(garage)"
class="px-3 py-1.5 text-xs font-medium rounded-lg transition"
:class="garage.is_active
? 'bg-red-500/10 text-red-300 hover:bg-red-500/20'
: 'bg-emerald-500/10 text-emerald-300 hover:bg-emerald-500/20'"
>
{{ garage.is_active ? $t('garages.deactivate') : $t('garages.activate') }}
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Empty State -->
<div
v-if="filteredGarages.length === 0 && !loading"
class="flex flex-col items-center justify-center py-16 text-center"
>
<svg class="w-12 h-12 text-slate-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
</svg>
<p class="text-slate-400 text-sm">{{ $t('garages.no_results') }}</p>
</div>
</div>
</template>
<!-- ================================================================ -->
<!-- Subscription Management Modal -->
<!-- ================================================================ -->
<div
v-if="showSubscriptionModal"
class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm"
@click.self="closeSubscriptionModal"
>
<div class="bg-slate-800 border border-slate-700 rounded-2xl shadow-2xl w-full max-w-lg mx-4 overflow-hidden">
<!-- Modal Header -->
<div class="px-6 py-4 border-b border-slate-700 flex items-center justify-between">
<div>
<h3 class="text-lg font-semibold text-white">{{ $t('garages.manage_subscription') }}</h3>
<p class="text-sm text-slate-400 mt-0.5">{{ selectedGarage?.full_name }}</p>
</div>
<button @click="closeSubscriptionModal" class="p-1.5 rounded-lg text-slate-400 hover:text-white hover:bg-slate-700 transition">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<!-- Modal Body -->
<div class="px-6 py-5 space-y-5">
<!-- Current Subscription Info -->
<div class="bg-slate-900/50 rounded-xl p-4 border border-slate-700/50">
<p class="text-xs text-slate-500 uppercase tracking-wider mb-1">{{ $t('garages.current_package') }}</p>
<p class="text-sm font-medium text-white">
{{ selectedGarage?.subscription_tier_name || $t('garages.free_fallback') }}
</p>
<p v-if="selectedGarage?.subscription_expires_at" class="text-xs text-slate-400 mt-1">
{{ $t('garages.expires') }}: {{ formatDate(selectedGarage.subscription_expires_at) }}
</p>
</div>
<!-- Tier Selection -->
<div>
<label class="block text-sm font-medium text-slate-300 mb-2">{{ $t('garages.select_package') }}</label>
<select
v-model="selectedTierId"
class="w-full px-3 py-2.5 bg-slate-900 border border-slate-700 rounded-lg text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500/50 transition"
>
<option value="" disabled>{{ $t('garages.select_package_placeholder') }}</option>
<option
v-for="tier in availableTiers"
:key="tier.id"
:value="tier.id"
>
{{ tier.name }} (Level {{ tier.tier_level }})
</option>
</select>
</div>
<!-- Custom Expiration Date -->
<div>
<label class="block text-sm font-medium text-slate-300 mb-2">
{{ $t('garages.custom_expiration') }}
<span class="text-xs text-slate-500 ml-1">({{ $t('garages.optional') }})</span>
</label>
<input
v-model="customExpiresAt"
type="datetime-local"
class="w-full px-3 py-2.5 bg-slate-900 border border-slate-700 rounded-lg text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500/50 transition"
/>
<p class="text-xs text-slate-500 mt-1.5">{{ $t('garages.custom_expiration_hint') }}</p>
</div>
<!-- Save Error -->
<div
v-if="saveError"
class="bg-red-500/10 border border-red-500/20 rounded-lg px-4 py-3"
>
<p class="text-sm text-red-400">{{ saveError }}</p>
</div>
</div>
<!-- Modal Footer -->
<div class="px-6 py-4 border-t border-slate-700 flex items-center justify-end gap-3">
<button
@click="closeSubscriptionModal"
class="px-4 py-2 text-sm font-medium text-slate-300 hover:text-white transition"
>
{{ $t('garages.cancel') }}
</button>
<button
@click="saveSubscription"
:disabled="saving || !selectedTierId"
class="px-5 py-2 text-sm font-medium rounded-lg transition disabled:opacity-50 disabled:cursor-not-allowed"
:class="saving
? 'bg-indigo-600/50 text-indigo-200 cursor-wait'
: 'bg-indigo-600 text-white hover:bg-indigo-500'"
>
<span v-if="saving" class="flex items-center gap-2">
<svg class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
</svg>
{{ $t('garages.saving') }}
</span>
<span v-else>{{ $t('garages.save') }}</span>
</button>
</div>
</div>
</div>
<!-- Save Notification -->
<div
v-if="saveNotification"
class="fixed bottom-6 right-6 px-5 py-3 rounded-xl shadow-xl text-sm font-medium z-50"
:class="saveNotification.type === 'success'
? 'bg-emerald-600 text-white'
: 'bg-red-600 text-white'"
>
{{ saveNotification.message }}
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
definePageMeta({
middleware: 'auth',
})
// ── Types ──────────────────────────────────────────────────────────
interface SubscriptionInfo {
id: number
org_id: number
tier_id: number
tier_name: string
valid_from: string | null
valid_until: string | null
is_active: boolean
extra_allowances: Record<string, unknown>
}
interface Garage {
id: number
name: string
full_name: string
display_name: string | null
email: string | null
status: string
is_active: boolean
is_verified: boolean
is_deleted: boolean
org_type: string
city: string | null
region: string | null
segment: string | null
member_count: number
created_at: string | null
subscription: SubscriptionInfo | null
subscription_tier_name: string
subscription_expires_at: string | null
}
interface Tier {
id: number
name: string
tier_level: number
rules: Record<string, unknown>
}
// ── State ──────────────────────────────────────────────────────────
const loading = ref(true)
const error = ref<string | null>(null)
const garages = ref<Garage[]>([])
const availableTiers = ref<Tier[]>([])
const searchQuery = ref('')
const filterTier = ref('all')
// Modal state
const showSubscriptionModal = ref(false)
const selectedGarage = ref<Garage | null>(null)
const selectedTierId = ref<number | ''>('')
const customExpiresAt = ref<string>('')
const saving = ref(false)
const saveError = ref<string | null>(null)
// Notification
const saveNotification = ref<{ type: 'success' | 'error'; message: string } | null>(null)
// ── Computed ───────────────────────────────────────────────────────
const activeCount = computed(() => garages.value.filter(g => g.is_active).length)
const premiumCount = computed(() => {
return garages.value.filter(g => {
const name = g.subscription_tier_name.toLowerCase()
return name.includes('premium') || name.includes('pro')
}).length
})
const enterpriseCount = computed(() => {
return garages.value.filter(g => {
const name = g.subscription_tier_name.toLowerCase()
return name.includes('enterprise') || name.includes('vip') || name.includes('corp')
}).length
})
const filteredGarages = computed(() => {
return garages.value.filter(g => {
// Search filter
if (searchQuery.value) {
const q = searchQuery.value.toLowerCase()
const matchesSearch =
g.full_name.toLowerCase().includes(q) ||
g.name.toLowerCase().includes(q) ||
(g.city && g.city.toLowerCase().includes(q))
if (!matchesSearch) return false
}
// Tier filter
if (filterTier.value !== 'all') {
const name = g.subscription_tier_name.toLowerCase()
if (filterTier.value === 'free' && !name.includes('free') && !name.includes('fallback')) return false
if (filterTier.value === 'premium' && !name.includes('premium') && !name.includes('pro')) return false
if (filterTier.value === 'enterprise' && !name.includes('enterprise') && !name.includes('vip') && !name.includes('corp')) return false
}
return true
})
})
// ── Auth Helper ────────────────────────────────────────────────────
function getAuthHeaders() {
const tokenCookie = useCookie('access_token')
const token = tokenCookie.value
return token ? { Authorization: `Bearer ${token}` } : {}
}
// ── Data Fetching ──────────────────────────────────────────────────
async function fetchGarages() {
loading.value = true
error.value = null
try {
const response = await $fetch<{ total: number; skip: number; limit: number; garages: Garage[] }>('/api/v1/admin/organizations', {
params: { limit: 200 },
headers: getAuthHeaders(),
})
garages.value = response.garages
} catch (err: any) {
console.error('[Garages] Failed to fetch garages:', err)
error.value = err?.data?.detail || err?.message || 'Failed to load garages'
} finally {
loading.value = false
}
}
async function fetchTiers() {
try {
const response = await $fetch<{ total: number; tiers: Tier[] }>('/api/v1/admin/packages', {
params: { include_hidden: true, limit: 100 },
headers: getAuthHeaders(),
})
availableTiers.value = response.tiers
} catch (err: any) {
console.error('[Garages] Failed to fetch tiers:', err)
}
}
// ── Subscription Modal ─────────────────────────────────────────────
function openSubscriptionModal(garage: Garage) {
selectedGarage.value = garage
selectedTierId.value = garage.subscription?.tier_id || ''
customExpiresAt.value = ''
saveError.value = null
showSubscriptionModal.value = true
}
function closeSubscriptionModal() {
showSubscriptionModal.value = false
selectedGarage.value = null
selectedTierId.value = ''
customExpiresAt.value = ''
saveError.value = null
}
async function saveSubscription() {
if (!selectedGarage.value || !selectedTierId.value) return
saving.value = true
saveError.value = null
try {
const payload: Record<string, unknown> = {
tier_id: selectedTierId.value,
}
// If custom expiration date is set, include it
if (customExpiresAt.value) {
payload.expires_at = new Date(customExpiresAt.value).toISOString()
}
await $fetch(`/api/v1/admin/organizations/${selectedGarage.value.id}/subscription`, {
method: 'PATCH',
headers: {
...getAuthHeaders(),
'Content-Type': 'application/json',
},
body: payload,
})
showNotification('success', `"${selectedGarage.value.full_name}" ${useNuxtApp().$i18n.t('garages.subscription_updated')}`)
closeSubscriptionModal()
// Refresh the list
await fetchGarages()
} catch (err: any) {
console.error('[Garages] Failed to save subscription:', err)
saveError.value = err?.data?.detail || err?.message || 'Failed to save subscription'
} finally {
saving.value = false
}
}
// ── Status Toggle ──────────────────────────────────────────────────
async function toggleStatus(garage: Garage) {
// This would call a PATCH endpoint to toggle is_active
// For now, we just show a notification that this is not yet implemented
showNotification('error', `Status toggle for "${garage.full_name}" not yet implemented via API`)
}
// ── Helpers ────────────────────────────────────────────────────────
function getInitials(name: string): string {
return name
.split(' ')
.map(w => w.charAt(0))
.join('')
.toUpperCase()
.slice(0, 2)
}
function getInitialsColor(name: string): string {
const colors = [
'bg-amber-600', 'bg-purple-600', 'bg-slate-600',
'bg-indigo-600', 'bg-emerald-600', 'bg-rose-600',
'bg-cyan-600', 'bg-orange-600', 'bg-teal-600',
]
let hash = 0
for (let i = 0; i < name.length; i++) {
hash = name.charCodeAt(i) + ((hash << 5) - hash)
}
return colors[Math.abs(hash) % colors.length]
}
function statusBadgeClass(status: string): string {
if (status === 'active') return 'bg-emerald-500/10 text-emerald-400'
if (status === 'suspended') return 'bg-red-500/10 text-red-400'
if (status === 'pending_verification') return 'bg-amber-500/10 text-amber-400'
return 'bg-slate-500/10 text-slate-400'
}
function statusDotClass(status: string): string {
if (status === 'active') return 'bg-emerald-400'
if (status === 'suspended') return 'bg-red-400'
if (status === 'pending_verification') return 'bg-amber-400'
return 'bg-slate-400'
}
function statusLabel(status: string): string {
if (status === 'active') return 'Aktív'
if (status === 'suspended') return 'Felfüggesztve'
if (status === 'pending_verification') return 'Függőben'
return 'Inaktív'
}
function tierBadgeClass(tierName: string): string {
const name = tierName.toLowerCase()
if (name.includes('enterprise') || name.includes('vip') || name.includes('corp')) return 'bg-purple-500/20 text-purple-300'
if (name.includes('premium') || name.includes('pro')) return 'bg-amber-500/20 text-amber-300'
return 'bg-slate-500/20 text-slate-300'
}
function formatDate(dateStr: string): string {
try {
const d = new Date(dateStr)
return d.toLocaleDateString('hu-HU', {
year: 'numeric',
month: 'short',
day: 'numeric',
})
} catch {
return dateStr
}
}
function showNotification(type: 'success' | 'error', message: string) {
saveNotification.value = { type, message }
setTimeout(() => {
saveNotification.value = null
}, 4000)
}
// ── Lifecycle ──────────────────────────────────────────────────────
onMounted(() => {
fetchGarages()
fetchTiers()
})
</script>

View File

@@ -93,6 +93,10 @@
</template>
<script setup lang="ts">
definePageMeta({
layout: 'blank',
})
const email = ref('')
const password = ref('')
const router = useRouter()

File diff suppressed because it is too large Load Diff

View File

@@ -2,119 +2,273 @@
<div>
<!-- Page Header -->
<div class="mb-8">
<h1 class="text-2xl font-bold text-white">Jogosultság Kezelés</h1>
<p class="text-slate-400 mt-1">Permissions Management Role-based access control matrix</p>
<h1 class="text-2xl font-bold text-white">{{ $t('permissions.title') }}</h1>
<p class="text-slate-400 mt-1">{{ $t('permissions.subtitle') }}</p>
</div>
<!-- Info Banner -->
<div class="bg-indigo-500/10 border border-indigo-500/20 rounded-xl p-4 mb-8 flex items-start gap-3">
<svg class="w-5 h-5 text-indigo-400 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
<!-- Loading State -->
<div v-if="loading" class="flex items-center justify-center py-20">
<svg class="animate-spin h-8 w-8 text-indigo-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
</svg>
<p class="text-sm text-indigo-300">
This is the permissions management interface. The 3D Capability Matrix UI (Superadmin, Garages, and Packages toggles) will be implemented here in the next iteration.
</p>
<span class="ml-3 text-slate-400">{{ $t('permissions.loading') }}</span>
</div>
<!-- Permission Groups Grid -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
<!-- Superadmin Permissions -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-purple-500/10 text-purple-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" />
</svg>
</div>
<h2 class="text-lg font-semibold text-white">Superadmin</h2>
<!-- Error State -->
<div v-else-if="error" class="bg-red-500/10 border border-red-500/20 rounded-xl p-6 mb-8">
<div class="flex items-center gap-3">
<svg class="w-6 h-6 text-red-400 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<div>
<h3 class="text-red-300 font-medium">{{ $t('permissions.error_title') }}</h3>
<p class="text-red-400/80 text-sm mt-1">{{ error }}</p>
</div>
<p class="text-sm text-slate-400 mb-4">Full system access user management, billing, robots, and system configuration.</p>
<div class="space-y-3">
<div v-for="perm in superadminPerms" :key="perm.key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<span class="text-sm text-slate-300">{{ perm.label }}</span>
<span class="text-xs px-2 py-0.5 rounded-full" :class="perm.enabled ? 'bg-emerald-400/10 text-emerald-400' : 'bg-slate-700 text-slate-500'">
{{ perm.enabled ? 'Enabled' : 'Disabled' }}
</span>
</div>
</div>
<div class="mt-4 pt-4 border-t border-slate-700">
<button class="w-full px-4 py-2 text-sm bg-purple-600 hover:bg-purple-500 text-white rounded-lg transition">
Configure Superadmin
</button>
</div>
<button @click="fetchAll" class="mt-4 px-4 py-2 text-sm bg-red-600 hover:bg-red-500 text-white rounded-lg transition">
{{ $t('permissions.retry') }}
</button>
</div>
<!-- Main Content -->
<template v-else>
<!-- Save Notification -->
<div v-if="saveNotification" class="mb-6 px-4 py-3 rounded-lg text-sm font-medium"
:class="saveNotification.type === 'success'
? 'bg-emerald-500/10 border border-emerald-500/20 text-emerald-300'
: 'bg-red-500/10 border border-red-500/20 text-red-300'">
{{ saveNotification.message }}
</div>
<!-- Save Button (top) -->
<div v-if="hasModifications" class="mb-6 flex items-center gap-3">
<button @click="savePermissions"
class="px-6 py-2.5 bg-emerald-600 hover:bg-emerald-500 text-white font-semibold rounded-lg shadow-lg shadow-emerald-600/20 transition-all hover:scale-[1.02] active:scale-[0.98] flex items-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7H5a2 2 0 00-2 2v9a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-3m-1 4l-3 3m0 0l-3-3m3 3V4" />
</svg>
{{ $t('permissions.save_changes') }}
</button>
<span class="text-sm text-slate-400">
{{ $t('permissions.modified_count', { count: modifiedPermissions.size }) }}
</span>
</div>
<!-- Subscription Tier Banner -->
<div class="bg-indigo-500/10 border border-indigo-500/20 rounded-xl p-4 mb-8 flex items-start gap-3">
<svg class="w-5 h-5 text-indigo-400 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<div class="text-sm text-indigo-300">
<span class="font-semibold">{{ $t('permissions.current_tier') }}:</span>
<span class="ml-2 px-2 py-0.5 rounded-full text-xs font-medium"
:class="tierBadgeClass">
{{ featureFlags.tier || 'free' }}
</span>
<span v-if="featureFlags.expires_at" class="ml-3 text-indigo-400/70">
{{ $t('permissions.expires') }}: {{ formatDate(featureFlags.expires_at) }}
</span>
</div>
</div>
<!-- Garage Permissions -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-cyan-500/10 text-cyan-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.25 18.75a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h6m-9 0H3.375a1.125 1.125 0 01-1.125-1.125V14.25m17.25 4.5a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h1.125c.621 0 1.129-.504 1.09-1.124a17.902 17.902 0 00-3.213-9.193 2.056 2.056 0 00-1.58-.86H14.25M16.5 18.75h-2.25m0-11.177v-.958c0-.568-.422-1.048-.987-1.106a48.554 48.554 0 00-10.026 0 1.106 1.106 0 00-.987 1.106v7.635m12-6.677v6.677m0 4.5v-4.5m0 0h-12" />
</svg>
<!-- Permission Groups Grid -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
<!-- Superadmin Permissions -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-purple-500/10 text-purple-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12.75L11.25 15 15 9.75m-3-7.036A11.959 11.959 0 013.598 6 11.99 11.99 0 003 9.749c0 5.592 3.824 10.29 9 11.623 5.176-1.332 9-6.03 9-11.622 0-1.31-.21-2.571-.598-3.751h-.152c-3.196 0-6.1-1.248-8.25-3.285z" />
</svg>
</div>
<h2 class="text-lg font-semibold text-white">{{ $t('permissions.superadmin') }}</h2>
</div>
<h2 class="text-lg font-semibold text-white">Garages</h2>
</div>
<p class="text-sm text-slate-400 mb-4">Garage-level access vehicle management, service booking, customer data.</p>
<div class="space-y-3">
<div v-for="perm in garagePerms" :key="perm.key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<span class="text-sm text-slate-300">{{ perm.label }}</span>
<span class="text-xs px-2 py-0.5 rounded-full" :class="perm.enabled ? 'bg-emerald-400/10 text-emerald-400' : 'bg-slate-700 text-slate-500'">
{{ perm.enabled ? 'Enabled' : 'Disabled' }}
</span>
<p class="text-sm text-slate-400 mb-4">{{ $t('permissions.superadmin_desc') }}</p>
<div class="space-y-3">
<div v-for="perm in superadminPerms" :key="perm.key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<span class="text-sm text-slate-300">{{ $t(`permissions.perms.${perm.key}`) }}</span>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" :checked="isPermEnabled('superadmin', perm.key)" class="sr-only peer"
@change="handleToggle('superadmin', perm.key, $event)" />
<div class="w-9 h-5 bg-slate-600 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-purple-500/50 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-purple-600"></div>
</label>
</div>
</div>
</div>
<div class="mt-4 pt-4 border-t border-slate-700">
<button class="w-full px-4 py-2 text-sm bg-cyan-600 hover:bg-cyan-500 text-white rounded-lg transition">
Configure Garages
</button>
<!-- Garage Permissions -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-cyan-500/10 text-cyan-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.25 18.75a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h6m-9 0H3.375a1.125 1.125 0 01-1.125-1.125V14.25m17.25 4.5a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h1.125c.621 0 1.129-.504 1.09-1.124a17.902 17.902 0 00-3.213-9.193 2.056 2.056 0 00-1.58-.86H14.25M16.5 18.75h-2.25m0-11.177v-.958c0-.568-.422-1.048-.987-1.106a48.554 48.554 0 00-10.026 0 1.106 1.106 0 00-.987 1.106v7.635m12-6.677v6.677m0 4.5v-4.5m0 0h-12" />
</svg>
</div>
<h2 class="text-lg font-semibold text-white">{{ $t('permissions.garages') }}</h2>
</div>
<p class="text-sm text-slate-400 mb-4">{{ $t('permissions.garages_desc') }}</p>
<div class="space-y-3">
<div v-for="perm in garagePerms" :key="perm.key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<span class="text-sm text-slate-300">{{ $t(`permissions.perms.${perm.key}`) }}</span>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" :checked="isPermEnabled('garage', perm.key)" class="sr-only peer"
@change="handleToggle('garage', perm.key, $event)" />
<div class="w-9 h-5 bg-slate-600 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-cyan-500/50 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-cyan-600"></div>
</label>
</div>
</div>
</div>
<!-- Package / Feature Flags -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-amber-500/10 text-amber-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.25 7.5l-.625 10.632a2.25 2.25 0 01-2.247 2.118H6.622a2.25 2.25 0 01-2.247-2.118L3.75 7.5m8.25 3v6.75m0 0l-3-3m3 3l3-3M3.375 7.5h17.25c.621 0 1.125-.504 1.125-1.125v-1.5c0-.621-.504-1.125-1.125-1.125H3.375c-.621 0-1.125.504-1.125 1.125v1.5c0 .621.504 1.125 1.125 1.125z" />
</svg>
</div>
<h2 class="text-lg font-semibold text-white">{{ $t('permissions.feature_flags') }}</h2>
</div>
<p class="text-sm text-slate-400 mb-4">{{ $t('permissions.feature_flags_desc', { tier: featureFlags.tier || 'free' }) }}</p>
<div class="space-y-3">
<div v-for="[key, granted] in featureEntries" :key="key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<div class="flex flex-col">
<span class="text-sm text-slate-300">{{ formatFeatureLabel(key) }}</span>
<span class="text-xs text-slate-500">{{ key }}</span>
</div>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" :checked="granted" class="sr-only peer"
:disabled="true"
@change="handleToggle('feature', key, $event)" />
<div class="w-9 h-5 bg-slate-600 peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-amber-500/50 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all"
:class="granted ? 'peer-checked:bg-amber-600' : ''"></div>
</label>
</div>
</div>
</div>
</div>
<!-- Package Permissions -->
<div class="bg-slate-800 rounded-xl border border-slate-700 p-6">
<div class="flex items-center gap-3 mb-4">
<div class="p-2 rounded-lg bg-amber-500/10 text-amber-400">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.25 7.5l-.625 10.632a2.25 2.25 0 01-2.247 2.118H6.622a2.25 2.25 0 01-2.247-2.118L3.75 7.5m8.25 3v6.75m0 0l-3-3m3 3l3-3M3.375 7.5h17.25c.621 0 1.125-.504 1.125-1.125v-1.5c0-.621-.504-1.125-1.125-1.125H3.375c-.621 0-1.125.504-1.125 1.125v1.5c0 .621.504 1.125 1.125 1.125z" />
</svg>
</div>
<h2 class="text-lg font-semibold text-white">Packages</h2>
</div>
<p class="text-sm text-slate-400 mb-4">Subscription package permissions feature toggles per plan tier.</p>
<div class="space-y-3">
<div v-for="perm in packagePerms" :key="perm.key" class="flex items-center justify-between py-2 border-b border-slate-700/50 last:border-0">
<span class="text-sm text-slate-300">{{ perm.label }}</span>
<span class="text-xs px-2 py-0.5 rounded-full" :class="perm.enabled ? 'bg-emerald-400/10 text-emerald-400' : 'bg-slate-700 text-slate-500'">
{{ perm.enabled ? 'Enabled' : 'Disabled' }}
</span>
</div>
</div>
<div class="mt-4 pt-4 border-t border-slate-700">
<button class="w-full px-4 py-2 text-sm bg-amber-600 hover:bg-amber-500 text-white rounded-lg transition">
Configure Packages
</button>
<!-- RBAC Permission Matrix -->
<div class="bg-slate-800/50 border border-slate-700 rounded-xl p-6 mb-8">
<h3 class="text-lg font-semibold text-white mb-4">{{ $t('permissions.rbac_title') }}</h3>
<p class="text-sm text-slate-400 mb-6">{{ $t('permissions.rbac_desc') }}</p>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="border-b border-slate-700">
<th class="text-left py-3 px-4 text-slate-400 font-medium">{{ $t('permissions.action') }}</th>
<th v-for="role in permissionMatrix" :key="role.role" class="text-center py-3 px-4 text-slate-400 font-medium">
{{ role.role }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="action in allActions" :key="action" class="border-b border-slate-700/50 hover:bg-slate-700/30">
<td class="py-2.5 px-4 text-slate-300">{{ action }}</td>
<td v-for="role in permissionMatrix" :key="role.role" class="text-center py-2.5 px-4">
<span v-if="hasPermission(role, action)" class="inline-flex items-center justify-center w-6 h-6 rounded-full bg-emerald-400/10">
<svg class="w-4 h-4 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
</svg>
</span>
<span v-else class="inline-flex items-center justify-center w-6 h-6 rounded-full bg-slate-700">
<svg class="w-4 h-4 text-slate-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Placeholder for 3D Capability Matrix -->
<div class="bg-slate-800/50 border-2 border-dashed border-slate-600 rounded-xl p-12 text-center">
<svg class="w-12 h-12 mx-auto text-slate-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" />
</svg>
<h3 class="text-lg font-semibold text-slate-400 mb-2">3D Capability Matrix</h3>
<p class="text-sm text-slate-500 max-w-md mx-auto">
The interactive permission matrix UI with toggles for Superadmin, Garages, and Packages will be rendered here in the next development phase.
</p>
</div>
<!-- Save Button (bottom) -->
<div v-if="hasModifications" class="flex items-center gap-3 pb-8">
<button @click="savePermissions"
class="px-6 py-2.5 bg-emerald-600 hover:bg-emerald-500 text-white font-semibold rounded-lg shadow-lg shadow-emerald-600/20 transition-all hover:scale-[1.02] active:scale-[0.98] flex items-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7H5a2 2 0 00-2 2v9a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-3m-1 4l-3 3m0 0l-3-3m3 3V4" />
</svg>
{{ $t('permissions.save_changes') }}
</button>
<button @click="discardChanges"
class="px-4 py-2.5 bg-slate-700 hover:bg-slate-600 text-slate-300 font-medium rounded-lg transition">
{{ $t('permissions.discard') }}
</button>
</div>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
definePageMeta({
middleware: 'auth',
})
// ── State ──────────────────────────────────────────────────────────
const loading = ref(true)
const error = ref<string | null>(null)
interface FeatureFlagsResponse {
tier: string
features: Record<string, boolean>
expires_at: string | null
}
interface PermissionMatrixEntry {
role: string
actions: string[]
}
interface PermissionMatrixResponse {
matrix: PermissionMatrixEntry[]
all_actions: string[]
}
const featureFlags = ref<FeatureFlagsResponse>({
tier: 'free',
features: {},
expires_at: null,
})
const permissionMatrix = ref<PermissionMatrixEntry[]>([])
const allActions = ref<string[]>([])
// ── Editing State ───────────────────────────────────────────────────
// Track modified permissions: key = "group:permKey", value = boolean
const modifiedPermissions = ref<Map<string, boolean>>(new Map())
// Track original values for discard: key = "group:permKey", value = boolean
const originalPermissions = ref<Map<string, boolean>>(new Map())
// Save notification
const saveNotification = ref<{ type: 'success' | 'error'; message: string } | null>(null)
// ── Computed ───────────────────────────────────────────────────────
const featureEntries = computed(() => {
return Object.entries(featureFlags.value.features || {})
})
const hasModifications = computed(() => {
return modifiedPermissions.value.size > 0
})
const tierBadgeClass = computed(() => {
const tier = featureFlags.value.tier
if (tier === 'enterprise') return 'bg-purple-500/20 text-purple-300'
if (tier === 'premium') return 'bg-amber-500/20 text-amber-300'
return 'bg-slate-500/20 text-slate-300'
})
// Static permission definitions for the UI cards
const superadminPerms = [
{ key: 'user_mgmt', label: 'User Management', enabled: true },
{ key: 'billing_admin', label: 'Billing Administration', enabled: true },
@@ -131,11 +285,212 @@ const garagePerms = [
{ key: 'bulk_import', label: 'Bulk Import', enabled: false },
]
const packagePerms = [
{ key: 'premium_features', label: 'Premium Features', enabled: true },
{ key: 'analytics', label: 'Analytics Dashboard', enabled: true },
{ key: 'multi_garage', label: 'Multi-Garage Support', enabled: false },
{ key: 'white_label', label: 'White Label Branding', enabled: false },
{ key: 'api_access', label: 'API Access', enabled: true },
]
// ── Methods ────────────────────────────────────────────────────────
/**
* Check if a permission is currently enabled (considering modifications).
*/
function isPermEnabled(group: string, key: string): boolean {
const mapKey = `${group}:${key}`
// If modified, return the modified value
if (modifiedPermissions.value.has(mapKey)) {
return modifiedPermissions.value.get(mapKey)!
}
// Otherwise return the default
const perms = group === 'superadmin' ? superadminPerms : garagePerms
const perm = perms.find(p => p.key === key)
return perm ? perm.enabled : false
}
function formatFeatureLabel(key: string): string {
return key
.replace(/^cost_category:/, '')
.replace(/_/g, ' ')
.replace(/\b\w/g, (c) => c.toUpperCase())
}
function formatDate(dateStr: string): string {
try {
const d = new Date(dateStr)
return d.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' })
} catch {
return dateStr
}
}
function hasPermission(role: PermissionMatrixEntry, action: string): boolean {
return role.actions.includes(action)
}
function handleToggle(group: string, key: string, event: Event) {
const target = event.target as HTMLInputElement
const newValue = target.checked
const mapKey = `${group}:${key}`
// Store original value if not already tracked
if (!originalPermissions.value.has(mapKey)) {
const perms = group === 'superadmin' ? superadminPerms : garagePerms
const perm = perms.find(p => p.key === key)
originalPermissions.value.set(mapKey, perm ? perm.enabled : false)
}
// Update modified state
const originalValue = originalPermissions.value.get(mapKey)
if (newValue === originalValue) {
// Reverted to original — remove from modified
modifiedPermissions.value.delete(mapKey)
} else {
modifiedPermissions.value.set(mapKey, newValue)
}
// Force reactivity by replacing the Map
modifiedPermissions.value = new Map(modifiedPermissions.value)
console.log(`[Permissions] Toggle: group=${group}, key=${key}, newValue=${newValue}`)
}
function clearSaveNotification() {
setTimeout(() => {
saveNotification.value = null
}, 5000)
}
async function savePermissions() {
saveNotification.value = null
try {
const tokenCookie = useCookie('access_token')
const token = tokenCookie.value
if (!token) {
saveNotification.value = { type: 'error', message: 'Not authenticated' }
clearSaveNotification()
return
}
// Build payload: array of { action, granted }
const payload: { action: string; granted: boolean }[] = []
modifiedPermissions.value.forEach((granted, mapKey) => {
// mapKey format: "superadmin:user_mgmt" or "garage:vehicle_crud"
const action = mapKey.replace(/^(superadmin|garage):/, '').toUpperCase()
payload.push({ action, granted })
})
if (payload.length === 0) {
saveNotification.value = { type: 'error', message: 'No changes to save' }
clearSaveNotification()
return
}
// Send PATCH to the backend for each modified permission
// We use the admin permissions override endpoint
const results = await Promise.allSettled(
payload.map(item =>
$fetch('/api/v1/admin/permissions/override/0', {
method: 'PATCH',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: {
action: item.action,
granted: item.granted,
},
})
)
)
// Check for failures
const failures = results.filter(r => r.status === 'rejected')
if (failures.length > 0) {
const errors = failures.map(f => (f as PromiseRejectedResult).reason?.message || 'Unknown error')
saveNotification.value = {
type: 'error',
message: `Failed to save ${failures.length} permission(s): ${errors.join('; ')}`,
}
clearSaveNotification()
return
}
// Success — clear modified state
modifiedPermissions.value = new Map()
originalPermissions.value = new Map()
saveNotification.value = {
type: 'success',
message: `Successfully saved ${payload.length} permission change(s).`,
}
clearSaveNotification()
console.log('[Permissions] Save successful:', payload)
} catch (err: any) {
console.error('[Permissions] Save failed:', err)
saveNotification.value = {
type: 'error',
message: err?.message || 'Failed to save permissions',
}
clearSaveNotification()
}
}
function discardChanges() {
modifiedPermissions.value = new Map()
originalPermissions.value = new Map()
saveNotification.value = null
}
async function fetchFeatureFlags() {
try {
const tokenCookie = useCookie('access_token')
const token = tokenCookie.value
if (!token) {
console.warn('[Permissions] No access token found for feature flags')
return
}
const data = await $fetch<FeatureFlagsResponse>('/api/v1/subscriptions/feature-flags', {
headers: { Authorization: `Bearer ${token}` },
})
console.log('[Permissions] Feature flags loaded:', data)
featureFlags.value = data
} catch (err: any) {
console.error('[Permissions] Feature flags fetch failed:', err?.message || err)
// Keep default values
}
}
async function fetchPermissionMatrix() {
try {
const tokenCookie = useCookie('access_token')
const token = tokenCookie.value
if (!token) {
console.warn('[Permissions] No access token found for permission matrix')
return
}
const data = await $fetch<PermissionMatrixResponse>('/api/v1/admin/permissions/matrix', {
headers: { Authorization: `Bearer ${token}` },
})
console.log('[Permissions] Permission matrix loaded:', data)
permissionMatrix.value = data.matrix
allActions.value = data.all_actions
} catch (err: any) {
console.error('[Permissions] Permission matrix fetch failed:', err?.message || err)
}
}
async function fetchAll() {
loading.value = true
error.value = null
try {
await Promise.all([fetchFeatureFlags(), fetchPermissionMatrix()])
} catch (err: any) {
console.error('[Permissions] fetchAll failed:', err?.message || err)
error.value = err?.message || 'Failed to load permissions data'
} finally {
loading.value = false
}
}
onMounted(() => {
fetchAll()
})
</script>