feat: role system
This commit is contained in:
@@ -23,12 +23,63 @@ interface NavigationItem {
|
||||
label: string;
|
||||
icon?: LucideIcon | React.ReactNode;
|
||||
path?: string;
|
||||
for_admin?: boolean;
|
||||
requiredRoles?: string[];
|
||||
onClick?: () => void;
|
||||
nestedItems?: NavigationItem[];
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export const ROUTE_REQUIRED_RESOURCES: Record<string, string[]> = {
|
||||
"/": [],
|
||||
|
||||
"/sight": ["sights"],
|
||||
"/sight/create": ["sights"],
|
||||
"/sight/:id/edit": ["sights"],
|
||||
|
||||
"/devices": ["devices", "vehicles", "routes", "carriers", "snapshot_rw"],
|
||||
|
||||
"/map": ["map"],
|
||||
|
||||
"/media": ["sights"],
|
||||
"/media/:id": ["sights"],
|
||||
"/media/:id/edit": ["sights"],
|
||||
|
||||
"/country": ["countries"],
|
||||
"/country/create": ["countries"],
|
||||
"/country/add": ["countries"],
|
||||
"/country/:id/edit": ["countries"],
|
||||
|
||||
"/city": ["cities", "countries"],
|
||||
"/city/create": ["cities", "countries"],
|
||||
"/city/:id/edit": ["cities", "countries"],
|
||||
|
||||
"/route": ["routes", "carriers"],
|
||||
"/route/create": ["routes", "carriers"],
|
||||
"/route/:id/edit": ["routes", "carriers"],
|
||||
|
||||
"/user": ["users"],
|
||||
"/user/create": ["users"],
|
||||
"/user/:id/edit": ["users"],
|
||||
|
||||
"/snapshot": ["snapshot_rw"],
|
||||
"/snapshot/create": ["snapshot_create", "devices_rw"],
|
||||
|
||||
"/carrier": ["carriers"],
|
||||
"/carrier/create": ["carriers"],
|
||||
"/carrier/:id/edit": ["carriers"],
|
||||
|
||||
"/station": ["stations"],
|
||||
"/station/create": ["stations"],
|
||||
"/station/:id": ["stations"],
|
||||
"/station/:id/edit": ["stations"],
|
||||
|
||||
"/vehicle/create": ["devices"],
|
||||
"/vehicle/:id/edit": ["devices"],
|
||||
|
||||
"/article": ["sights"],
|
||||
"/article/:id": ["sights"],
|
||||
};
|
||||
|
||||
export const NAVIGATION_ITEMS: {
|
||||
primary: NavigationItem[];
|
||||
secondary: NavigationItem[];
|
||||
@@ -39,7 +90,7 @@ export const NAVIGATION_ITEMS: {
|
||||
label: "Экспорт",
|
||||
icon: GitBranch,
|
||||
path: "/snapshot",
|
||||
for_admin: true,
|
||||
requiredRoles: ["snapshot_rw", "snapshot_create"],
|
||||
},
|
||||
{
|
||||
id: "map",
|
||||
@@ -52,14 +103,14 @@ export const NAVIGATION_ITEMS: {
|
||||
label: "Устройства",
|
||||
icon: Cpu,
|
||||
path: "/devices",
|
||||
for_admin: true,
|
||||
requiredRoles: ["devices_ro", "devices_rw"],
|
||||
},
|
||||
{
|
||||
id: "users",
|
||||
label: "Пользователи",
|
||||
icon: Users,
|
||||
path: "/user",
|
||||
for_admin: true,
|
||||
requiredRoles: ["users_ro", "users_rw"],
|
||||
},
|
||||
{
|
||||
id: "all",
|
||||
@@ -71,18 +122,21 @@ export const NAVIGATION_ITEMS: {
|
||||
label: "Достопримечательности",
|
||||
icon: Landmark,
|
||||
path: "/sight",
|
||||
requiredRoles: ["sights_ro", "sights_rw"],
|
||||
},
|
||||
{
|
||||
id: "stations",
|
||||
label: "Остановки",
|
||||
icon: PersonStanding,
|
||||
path: "/station",
|
||||
requiredRoles: ["stations_ro", "stations_rw"],
|
||||
},
|
||||
{
|
||||
id: "routes",
|
||||
label: "Маршруты",
|
||||
icon: Split,
|
||||
path: "/route",
|
||||
requiredRoles: ["routes_ro", "routes_rw"],
|
||||
},
|
||||
|
||||
{
|
||||
@@ -90,14 +144,14 @@ export const NAVIGATION_ITEMS: {
|
||||
label: "Страны",
|
||||
icon: Earth,
|
||||
path: "/country",
|
||||
for_admin: true,
|
||||
requiredRoles: ["countries_ro", "countries_rw"],
|
||||
},
|
||||
{
|
||||
id: "cities",
|
||||
label: "Города",
|
||||
icon: Building2,
|
||||
path: "/city",
|
||||
for_admin: true,
|
||||
requiredRoles: ["cities_ro", "cities_rw"],
|
||||
},
|
||||
{
|
||||
id: "carriers",
|
||||
@@ -105,7 +159,7 @@ export const NAVIGATION_ITEMS: {
|
||||
// @ts-ignore
|
||||
icon: () => <img src={carrierIcon} alt="Перевозчики" />,
|
||||
path: "/carrier",
|
||||
for_admin: true,
|
||||
requiredRoles: ["carriers_ro", "carriers_rw"],
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -123,6 +177,20 @@ export const NAVIGATION_ITEMS: {
|
||||
],
|
||||
};
|
||||
|
||||
function collectRoles(list: NavigationItem[]): string[] {
|
||||
const roles = new Set<string>(["admin"]);
|
||||
const walk = (items: NavigationItem[]) => {
|
||||
for (const item of items) {
|
||||
item.requiredRoles?.forEach((r) => roles.add(r));
|
||||
item.nestedItems && walk(item.nestedItems);
|
||||
}
|
||||
};
|
||||
walk(list);
|
||||
return Array.from(roles);
|
||||
}
|
||||
|
||||
export const ALL_ROLES = collectRoles(NAVIGATION_ITEMS.primary);
|
||||
|
||||
export const VEHICLE_TYPES = [
|
||||
{ label: "Автобус", value: 3 },
|
||||
{ label: "Троллейбус", value: 2 },
|
||||
|
||||
Reference in New Issue
Block a user