From 0b377d11c641fc845546be8b1f14cb2e67772254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Fri, 3 Sep 2021 14:27:00 +0500 Subject: [PATCH] Add component \ Private for routing by roles --- src/App.js | 2 +- src/components/Private/PrivateContent.jsx | 24 ++++++++++++ src/components/Private/PrivateMenuItem.jsx | 11 ++++++ src/components/{ => Private}/PrivateRoute.jsx | 0 src/components/Private/index.ts | 3 ++ src/pages/Documents/menuItems.jsx | 9 +++-- src/pages/Measure/index.jsx | 4 +- src/pages/Well.jsx | 37 +++++++++---------- 8 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 src/components/Private/PrivateContent.jsx create mode 100644 src/components/Private/PrivateMenuItem.jsx rename src/components/{ => Private}/PrivateRoute.jsx (100%) create mode 100644 src/components/Private/index.ts diff --git a/src/App.js b/src/App.js index 92c83ef..49ee5eb 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,7 @@ import locale from "antd/lib/locale/ru_RU" import Login from './pages/Login' import Main from './pages/Main' import { OpenAPI } from './services/api' -import { PrivateRoute } from './components/PrivateRoute' +import { PrivateRoute } from './components/Private' //OpenAPI.BASE = 'http://localhost:3000' OpenAPI.TOKEN = localStorage['token'] diff --git a/src/components/Private/PrivateContent.jsx b/src/components/Private/PrivateContent.jsx new file mode 100644 index 0000000..878644f --- /dev/null +++ b/src/components/Private/PrivateContent.jsx @@ -0,0 +1,24 @@ +const admins = ['администратор', 'админ', 'admin'] + +export const isInRole = (roles) => { + if(!roles?.length) + return true + const role = localStorage['roleName']?.toLowerCase() + if(admins.indexOf(role) > -1) + return true + for(const r of roles) + if(r.toLowerCase() === role) + return true + return false +} + +export const privateContent = (roles, children) => { + if(isInRole(roles)) + return children + else + return null +} + +export const PrivateContent = ({roles, children}) => { + return privateContent(roles, children) +} \ No newline at end of file diff --git a/src/components/Private/PrivateMenuItem.jsx b/src/components/Private/PrivateMenuItem.jsx new file mode 100644 index 0000000..070866f --- /dev/null +++ b/src/components/Private/PrivateMenuItem.jsx @@ -0,0 +1,11 @@ +import { Menu } from "antd"; +import { isInRole } from './PrivateContent' + +export const PrivateMenuItem = ({ children, roles, ...other }) => { + if(!isInRole(roles)) + return null + + return + {children} + ; +} diff --git a/src/components/PrivateRoute.jsx b/src/components/Private/PrivateRoute.jsx similarity index 100% rename from src/components/PrivateRoute.jsx rename to src/components/Private/PrivateRoute.jsx diff --git a/src/components/Private/index.ts b/src/components/Private/index.ts new file mode 100644 index 0000000..c813da3 --- /dev/null +++ b/src/components/Private/index.ts @@ -0,0 +1,3 @@ +export {PrivateRoute} from './PrivateRoute' +export {privateContent, PrivateContent} from './PrivateContent' +export {PrivateMenuItem} from './PrivateMenuItem' \ No newline at end of file diff --git a/src/pages/Documents/menuItems.jsx b/src/pages/Documents/menuItems.jsx index 59068a9..7abf3c7 100644 --- a/src/pages/Documents/menuItems.jsx +++ b/src/pages/Documents/menuItems.jsx @@ -1,10 +1,11 @@ -import { Menu } from "antd"; +//import { Menu } from "antd"; import { FolderOutlined } from "@ant-design/icons"; import { Link, Route} from "react-router-dom"; import DocumentsTemplate from './DocumentsTemplate' +import {PrivateMenuItem} from '../../components/Private' export const documentCategories = [ - {id:1, key:'fluidService', title:'Растворный сервис'}, + {id:1, key:'fluidService', title:'Растворный сервис'/*, roles:['ff']*/}, {id:2, key:'cementing', title:'Цементирование'}, {id:3, key:'nnb', title:'ННБ'}, {id:4, key:'gti', title:'ГТИ'}, @@ -14,11 +15,11 @@ export const documentCategories = [ ] const makeMenuItem = (keyValue, rootPath, title, other) => ( - {title} - ) + ) const makeRouteItem = (keyValue, rootPath, other) => ( - - + return - }> + }> Мониторинг - - }> - Сообщения - - }> + + } roles={[]}> + Сообщения + + }> Рапорт - - }> + + }> Операции по скважине - - }> + + }> Архив - + {makeMenuItems(rootPath)} - }> + }> Измерения - - }> + + }> Программа бурения - + @@ -107,6 +106,4 @@ export default function Well() { - - ); }