Projekt

Obecné

Profil

Stáhnout (3.29 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
import SupervisorAccountIcon from '@mui/icons-material/SupervisorAccount'
12

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

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

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

    
99
const getNavigationItems = (_userRoles: string[]): NavigationMenuItem[] => {
100
    const userRoles: string[] = [..._userRoles]
101
    // Add visitor role if not present and visitor role only if user has no roles
102
    if (!userRoles || userRoles.length === 0) {
103
        userRoles.push( visitorRole, visitorRoleOnly)
104
    } else {
105
        userRoles.push(visitorRole)
106
        userRoles.push(loggedInRole)
107
    }
108

    
109
    return items // else return everything the user has privileges to
110
        .filter((item) => {
111
            // If the user has any of the roles that are required to access this item
112
            // then return it
113
            return userRoles.some((role) => item.accessibleTo.has(role))
114
        })
115
        .sort((a, b) => a.position - b.position)
116
}
117

    
118
export default getNavigationItems
(3-3/3)