Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 1x 66x 1x 66x 1x 4x 20x 2x 1x 1x 11x 11x 1x 22x 17x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 6x 5x 34x 34x 34x 48x 32x 34x 34x 34x 34x 34x 34x 34x 34x 34x 27x 27x 1x 34x 2x 2x 2x 3x 3x 3x 1x 1x 1x 7x 6x 6x 6x 6x 6x 2x 4x 2x 2x 4x 4x 4x 6x 4x 4x 4x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x | <template>
<PageLayout
:title="t('navigation.roles')"
:description="t('role.rolesDescription')"
class="roles-page"
>
<template #action>
<button
class="btn btn-outline-secondary me-2 refresh-roles-btn"
:disabled="loading"
@click="refreshRoles"
>
<font-awesome-icon icon="fa-solid fa-rotate-right" class="me-md-2" />
<span class="d-none d-md-inline">{{ t('common.refresh') }}</span>
</button>
<button
v-if="authStore.currentFlow && hasPermission('roles:create')"
class="btn btn-success"
data-tour="roles-create"
@click="createNewRole"
>
<font-awesome-icon icon="fa-solid fa-plus" class="me-md-2" />
<span class="d-none d-md-inline">{{ t('role.createNew') }}</span>
</button>
</template>
<RoleFormSidebar
:is-open="showRoleDialog"
:mode="editingRole ? 'edit' : 'create'"
:form-data="roleForm"
:default-language="defaultLanguage"
:enabled-languages="enabledLanguages"
:current-language="currentFormLanguage"
:loading="formLoading"
:error="formError || ''"
@update:form-data="roleForm = $event"
@update:current-language="currentFormLanguage = $event"
@close="cancelRoleForm"
@submit="saveRole"
/>
<div class="container-fluid">
<NoActiveFlowNotice v-if="!authStore.currentFlow" />
<template v-else>
<EmptyState v-if="!canListRoles" variant="info">
{{ t('role.noListPermission') }}
</EmptyState>
<ErrorAlert v-else-if="error" :title="t('common.accessDenied')" :message="error" />
<LoadingSpinner v-else-if="loading" />
<EmptyState v-else-if="rolesStore.roles.length === 0" variant="info">
{{ t('role.noRoles') }}
</EmptyState>
</template>
</div>
<!-- Roles List -->
<template
v-if="authStore.currentFlow && canListRoles && !loading && rolesStore.roles.length > 0"
>
<SearchFilterHeader
v-model="roleSearchQuery"
mode="client"
:items="roleViewModels"
:search-fields="roleSearchFields"
:sort-value="roleSortField"
:sort-direction="roleSortDirection"
:get-sort-value="getRoleSortValue"
:placeholder="t('role.searchPlaceholder')"
>
<template #extra>
<SortControl
:options="roleSortOptions"
:field-value="roleSortField"
:direction="roleSortDirection"
:label="t('common.sortBy')"
@update:field-value="roleSortField = $event"
@toggle-direction="roleSortDirection = roleSortDirection === 'asc' ? 'desc' : 'asc'"
/>
</template>
<template #default="{ filteredItems }">
<SystemItemHeader
:fields="ROLE_SCHEMA"
:get-translation="getTranslation"
:scrollbar-width="scrollbarWidth"
/>
<EmptyState v-if="filteredItems.length === 0" variant="info">
{{ t('common.noResults') }}
</EmptyState>
<VirtualCardGrid
v-else
:items="filteredItems"
:has-more="rolesStore.hasMore"
:is-loading="loading"
single-column
@load-more="loadMoreRolesSafe"
@scrollbar-width-change="scrollbarWidth = $event"
>
<template #card="{ item }">
<SystemItemCard :fields="ROLE_SCHEMA" :item="item" :get-translation="getTranslation">
<template #actions="{ item: role }">
<button
v-if="!role.isSystemRole && hasPermission('roles:update')"
class="btn btn-sm btn-secondary action-icon"
title="Edit"
@click.stop="editRole(role)"
>
<font-awesome-icon icon="fa-solid fa-pen" />
</button>
<button
v-if="!role.isSystemRole && hasPermission('roles:delete')"
class="btn btn-sm btn-danger action-icon"
title="Delete"
@click.stop="deleteRoleConfirm(role)"
>
<font-awesome-icon icon="fa-solid fa-trash" />
</button>
</template>
</SystemItemCard>
</template>
</VirtualCardGrid>
</template>
</SearchFilterHeader>
</template>
<ConfirmModal
:is-open="confirmDeleteRole.open"
:title="t('role.deleteRole')"
:message="confirmDeleteRole.message"
:confirm-label="t('common.delete')"
variant="danger"
@confirm="doDeleteRole"
@cancel="confirmDeleteRole.open = false"
/>
</PageLayout>
<FloatingPageActions :scroll-top-title="t('common.backToTop')" />
</template>
<script setup lang="ts">
// @implements UC-RBAC-001.1, UC-RBAC-003.1, UC-RBAC-004.1, UC-RBAC-005.2, UC-RBAC-006.1
import PageLayout from '@/components/PageLayout.vue'
import RoleFormSidebar from '@/components/RoleFormSidebar.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.vue'
import SystemItemHeader from '@/components/SystemItemHeader.vue'
import SystemItemCard from '@/components/SystemItemCard.vue'
import ConfirmModal from '@/components/ConfirmModal.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import NoActiveFlowNotice from '@/components/NoActiveFlowNotice.vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import FloatingPageActions from '@/components/FloatingPageActions.vue'
import SearchFilterHeader from '@/components/SearchFilterHeader.vue'
import SortControl, { type SortOption } from '@/components/SortControl.vue'
import { ROLE_SCHEMA } from '@/schemas/role-schema'
import { toRoleViewModel, type RoleViewModel } from '@/utils/systemListViewModels'
import { ref, onMounted, computed, reactive } from 'vue'
import { useRolesStore } from '@/stores/roles'
import { useAuthStore } from '@/stores/auth'
import { useLanguage } from '@/composables/useLanguage'
import { useUILanguage } from '@/composables/useUILanguage'
import { usePermissions } from '@/composables/usePermissions'
import { useToastStore } from '@/stores/toast'
import { storeToRefs } from 'pinia'
import { getErrorMessage } from '@/utils/errors'
const rolesStore = useRolesStore()
const authStore = useAuthStore()
const toastStore = useToastStore()
const { getTranslation, defaultLanguage, enabledLanguages } = useLanguage()
const { t } = useUILanguage()
const { hasPermission } = usePermissions()
const { error, loading } = storeToRefs(rolesStore)
const scrollbarWidth = ref(0)
// Listing may be denied while creating is still allowed — skip the fetch to
// avoid a guaranteed 403 and show a neutral message instead of an error.
const canListRoles = computed(() => hasPermission('roles:list'))
const roleViewModels = computed(() => rolesStore.roles.map((role) => toRoleViewModel(role)))
// Client-side search/sort, applied only to the roles already loaded — roles
// are paginated (see loadMoreRolesSafe), so a role further down the list that
// hasn't been fetched yet won't be matched until it's loaded.
const roleSearchQuery = ref('')
const roleSearchFields = [
(role: RoleViewModel) => getTranslation(role.name),
(role: RoleViewModel) => getTranslation(role.description)
]
const roleSortField = ref('name')
const roleSortDirection = ref<'asc' | 'desc'>('asc')
const roleSortOptions = computed<SortOption[]>(() => [
{ value: 'name', label: t('common.name') },
{ value: 'createdAt', label: t('common.createdAt') }
])
function getRoleSortValue(role: RoleViewModel, field: string): string | number {
if (field === 'createdAt') return new Date(role.createdAt).getTime()
return getTranslation(role.name).toLowerCase()
}
const showRoleDialog = ref(false)
const editingRole = ref<any>(null)
const roleForm = ref<{
name: string | Record<string, string>
description: string | Record<string, string>
permissions: string[]
}>({
name: { en: '', fr: '' },
description: { en: '', fr: '' },
permissions: [] as string[]
})
const formLoading = ref(false)
const formError = ref('')
const confirmDeleteRole = reactive({ open: false, roleId: '', message: '' })
// Global language selector for multilingual fields
const currentFormLanguage = ref(defaultLanguage.value)
const currentFlowId = computed(() => authStore.currentFlow || '')
async function refreshRoles() {
if (!currentFlowId.value || !canListRoles.value) return
try {
await rolesStore.fetchRoles(currentFlowId.value)
} catch (err) {
// Error is already stored in the store
console.error('Failed to fetch roles:', err)
}
}
onMounted(refreshRoles)
function createNewRole() {
editingRole.value = null
roleForm.value = {
name: { en: '', fr: '' },
description: { en: '', fr: '' },
permissions: []
}
showRoleDialog.value = true
}
function editRole(role: any) {
editingRole.value = role
roleForm.value = {
name:
typeof role.name === 'string' ? { en: role.name, fr: '' } : role.name || { en: '', fr: '' },
description:
typeof role.description === 'string'
? { en: role.description, fr: '' }
: role.description || { en: '', fr: '' },
permissions: [...role.permissions]
}
showRoleDialog.value = true
}
function cancelRoleForm() {
showRoleDialog.value = false
editingRole.value = null
roleForm.value = {
name: { en: '', fr: '' },
description: { en: '', fr: '' },
permissions: []
}
}
async function saveRole() {
if (!currentFlowId.value) return
formLoading.value = true
formError.value = ''
const isEditing = !!editingRole.value
try {
if (editingRole.value) {
await rolesStore.updateRole(currentFlowId.value, editingRole.value.roleId, roleForm.value)
} else {
await rolesStore.createRole(currentFlowId.value, roleForm.value)
}
showRoleDialog.value = false
toastStore.success(t(isEditing ? 'role.updateSuccess' : 'role.createSuccess'))
} catch (error: any) {
console.error('Error saving role:', error)
formError.value = getErrorMessage(error, 'Failed to save role')
toastStore.error(t(isEditing ? 'role.updateError' : 'role.createError'))
} finally {
formLoading.value = false
}
}
function deleteRoleConfirm(role: any) {
confirmDeleteRole.roleId = role.roleId
confirmDeleteRole.message = t('common.confirmDeleteRole').replace(
'{name}',
getTranslation(role.name)
)
confirmDeleteRole.open = true
}
async function doDeleteRole() {
confirmDeleteRole.open = false
formLoading.value = true
formError.value = ''
try {
await rolesStore.deleteRole(currentFlowId.value, confirmDeleteRole.roleId)
toastStore.success(t('role.deleteSuccess'))
} catch (error: any) {
formError.value = getErrorMessage(error, 'Failed to delete role')
toastStore.error(t('role.deleteError'))
} finally {
formLoading.value = false
}
}
function loadMoreRolesSafe() {
Eif (currentFlowId.value) {
rolesStore.loadMoreRoles(currentFlowId.value)
}
}
</script>
|