Projekt

Obecné

Profil

Stáhnout (2.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { OverridableComponent } from '@mui/material/OverridableComponent'
2
import HomeIcon from '@mui/icons-material/Home'
3
import { MenuBook } from '@mui/icons-material'
4
import LibraryBooksIcon from '@mui/icons-material/LibraryBooks'
5
import MapIcon from '@mui/icons-material/Map'
6
import PersonIcon from '@mui/icons-material/Person'
7
import LoginIcon from '@mui/icons-material/Login'
8
import { SvgIconTypeMap } from '@mui/material'
9

    
10
export interface NavigationMenuItem {
11
    name: string
12
    path: string
13
    // All privileges that can access this menu item
14
    accessibleTo: Set<string>
15
    icon: OverridableComponent<SvgIconTypeMap<{}, 'svg'>>
16
    position: number
17
}
18

    
19
const visitorRole = 'VISITOR'
20
const visitorRoleOnly = 'VISITOR_ONLY'
21
const visitorAccess = new Set([visitorRole])
22

    
23
const items: NavigationMenuItem[] = [
24
    {
25
        name: 'Home',
26
        path: '/',
27
        accessibleTo: visitorAccess,
28
        icon: HomeIcon,
29
        position: 0,
30
    },
31
    {
32
        name: 'Catalog',
33
        path: '/catalog',
34
        accessibleTo: visitorAccess,
35
        icon: MenuBook,
36
        position: 1,
37
    },
38
    {
39
        name: 'Map',
40
        path: '/map',
41
        accessibleTo: visitorAccess,
42
        icon: MapIcon,
43
        position: 2,
44
    },
45
    {
46
        name: 'Sources',
47
        path: '/sources',
48
        accessibleTo: visitorAccess,
49
        icon: LibraryBooksIcon,
50
        position: 3,
51
    },
52
    // TODO add statistics
53
    {
54
        name: 'Admin',
55
        path: '/admin',
56
        accessibleTo: new Set(['ADMIN']),
57
        icon: PersonIcon,
58
        position: 4,
59
    },
60
    // TODO move this to the top
61
    {
62
        name: 'Login',
63
        path: '/login',
64
        accessibleTo: new Set([visitorRoleOnly]),
65
        icon: LoginIcon,
66
        position: 5,
67
    },
68
]
69

    
70
const getNavigationItems = (_userRoles: string[]): NavigationMenuItem[] => {
71
    const userRoles: string[] = [..._userRoles]
72
    // Add visitor role if not present and visitor role only if user has no roles
73
    if (!userRoles || userRoles.length === 0) {
74
        userRoles.push( visitorRole, visitorRoleOnly)
75
    } else {
76
        userRoles.push(visitorRole)
77
    }
78

    
79
    return items // else return everything the user has privileges to
80
        .filter((item) => {
81
            // If the user has any of the roles that are required to access this item
82
            // then return it
83
            return userRoles.some((role) => item.accessibleTo.has(role))
84
        })
85
        .sort((a, b) => a.position - b.position)
86
}
87

    
88
export default getNavigationItems
(3-3/3)