Projekt

Obecné

Profil

Stáhnout (3 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
import DataSaverOffIcon from '@mui/icons-material/DataSaverOff'
10
import LogoutIcon from '@mui/icons-material/Logout'
11

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

    
21
const visitorRole = 'VISITOR'
22
const visitorRoleOnly = 'VISITOR_ONLY'
23
const loggedInRole = 'LOGGED_IN'
24
const visitorAccess = new Set([visitorRole])
25
const adminAccess = new Set(['ADMIN'])
26
const loggedInAccess = new Set([loggedInRole])
27

    
28
const items: NavigationMenuItem[] = [
29
    {
30
        name: 'Home',
31
        path: '/',
32
        accessibleTo: visitorAccess,
33
        icon: HomeIcon,
34
        position: 0,
35
    },
36
    {
37
        name: 'Catalog',
38
        path: '/catalog',
39
        accessibleTo: visitorAccess,
40
        icon: MenuBook,
41
        position: 1,
42
    },
43
    {
44
        name: 'Map',
45
        path: '/map',
46
        accessibleTo: visitorAccess,
47
        icon: MapIcon,
48
        position: 2,
49
    },
50
    {
51
        name: 'Sources',
52
        path: '/sources',
53
        accessibleTo: visitorAccess,
54
        icon: LibraryBooksIcon,
55
        position: 3,
56
    },
57
    // TODO add statistics
58
    {
59
        name: 'Admin',
60
        path: '/admin',
61
        accessibleTo: adminAccess,
62
        icon: PersonIcon,
63
        position: 5,
64
    },
65
    // TODO move this to the top
66
    {
67
        name: 'Login',
68
        path: '/login',
69
        accessibleTo: new Set([visitorRoleOnly]),
70
        icon: LoginIcon,
71
        position: 5,
72
    },
73
    {
74
        name: 'Statistics',
75
        path: '/stats',
76
        accessibleTo: adminAccess,
77
        icon: DataSaverOffIcon,
78
        position: 4,
79
    },
80
    {
81
        name: 'Logout',
82
        path: '/logout',
83
        accessibleTo: loggedInAccess,
84
        icon: LogoutIcon,
85
        position: 1337,
86
    }
87
]
88

    
89
const getNavigationItems = (_userRoles: string[]): NavigationMenuItem[] => {
90
    const userRoles: string[] = [..._userRoles]
91
    // Add visitor role if not present and visitor role only if user has no roles
92
    if (!userRoles || userRoles.length === 0) {
93
        userRoles.push( visitorRole, visitorRoleOnly)
94
    } else {
95
        userRoles.push(visitorRole)
96
        userRoles.push(loggedInRole)
97
    }
98

    
99
    return items // else return everything the user has privileges to
100
        .filter((item) => {
101
            // If the user has any of the roles that are required to access this item
102
            // then return it
103
            return userRoles.some((role) => item.accessibleTo.has(role))
104
        })
105
        .sort((a, b) => a.position - b.position)
106
}
107

    
108
export default getNavigationItems
(3-3/3)