forked from ddrilling/asb_cloud_front
Add component \ Private for routing by roles
This commit is contained in:
parent
71a21395fa
commit
0b377d11c6
@ -8,7 +8,7 @@ import locale from "antd/lib/locale/ru_RU"
|
|||||||
import Login from './pages/Login'
|
import Login from './pages/Login'
|
||||||
import Main from './pages/Main'
|
import Main from './pages/Main'
|
||||||
import { OpenAPI } from './services/api'
|
import { OpenAPI } from './services/api'
|
||||||
import { PrivateRoute } from './components/PrivateRoute'
|
import { PrivateRoute } from './components/Private'
|
||||||
|
|
||||||
//OpenAPI.BASE = 'http://localhost:3000'
|
//OpenAPI.BASE = 'http://localhost:3000'
|
||||||
OpenAPI.TOKEN = localStorage['token']
|
OpenAPI.TOKEN = localStorage['token']
|
||||||
|
24
src/components/Private/PrivateContent.jsx
Normal file
24
src/components/Private/PrivateContent.jsx
Normal file
@ -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)
|
||||||
|
}
|
11
src/components/Private/PrivateMenuItem.jsx
Normal file
11
src/components/Private/PrivateMenuItem.jsx
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Menu } from "antd";
|
||||||
|
import { isInRole } from './PrivateContent'
|
||||||
|
|
||||||
|
export const PrivateMenuItem = ({ children, roles, ...other }) => {
|
||||||
|
if(!isInRole(roles))
|
||||||
|
return null
|
||||||
|
|
||||||
|
return <Menu.Item {...other}>
|
||||||
|
{children}
|
||||||
|
</Menu.Item>;
|
||||||
|
}
|
3
src/components/Private/index.ts
Normal file
3
src/components/Private/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export {PrivateRoute} from './PrivateRoute'
|
||||||
|
export {privateContent, PrivateContent} from './PrivateContent'
|
||||||
|
export {PrivateMenuItem} from './PrivateMenuItem'
|
@ -1,10 +1,11 @@
|
|||||||
import { Menu } from "antd";
|
//import { Menu } from "antd";
|
||||||
import { FolderOutlined } from "@ant-design/icons";
|
import { FolderOutlined } from "@ant-design/icons";
|
||||||
import { Link, Route} from "react-router-dom";
|
import { Link, Route} from "react-router-dom";
|
||||||
import DocumentsTemplate from './DocumentsTemplate'
|
import DocumentsTemplate from './DocumentsTemplate'
|
||||||
|
import {PrivateMenuItem} from '../../components/Private'
|
||||||
|
|
||||||
export const documentCategories = [
|
export const documentCategories = [
|
||||||
{id:1, key:'fluidService', title:'Растворный сервис'},
|
{id:1, key:'fluidService', title:'Растворный сервис'/*, roles:['ff']*/},
|
||||||
{id:2, key:'cementing', title:'Цементирование'},
|
{id:2, key:'cementing', title:'Цементирование'},
|
||||||
{id:3, key:'nnb', title:'ННБ'},
|
{id:3, key:'nnb', title:'ННБ'},
|
||||||
{id:4, key:'gti', title:'ГТИ'},
|
{id:4, key:'gti', title:'ГТИ'},
|
||||||
@ -14,11 +15,11 @@ export const documentCategories = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const makeMenuItem = (keyValue, rootPath, title, other) => (
|
const makeMenuItem = (keyValue, rootPath, title, other) => (
|
||||||
<Menu.Item className="ant-menu-item"
|
<PrivateMenuItem className="ant-menu-item"
|
||||||
key={`${keyValue}`}
|
key={`${keyValue}`}
|
||||||
{...other}>
|
{...other}>
|
||||||
<Link to={{pathname: `${rootPath}/${keyValue}`}}>{title}</Link>
|
<Link to={{pathname: `${rootPath}/${keyValue}`}}>{title}</Link>
|
||||||
</Menu.Item>)
|
</PrivateMenuItem>)
|
||||||
|
|
||||||
const makeRouteItem = (keyValue, rootPath, other) => (
|
const makeRouteItem = (keyValue, rootPath, other) => (
|
||||||
<Route
|
<Route
|
||||||
|
@ -2,11 +2,11 @@ import { columnsMudDiagram} from './columnsMudDiagram'
|
|||||||
import { columnsDrillingFluid} from './columnsDrillingFluid'
|
import { columnsDrillingFluid} from './columnsDrillingFluid'
|
||||||
import { columnsNnb } from './columnsNnb'
|
import { columnsNnb } from './columnsNnb'
|
||||||
import { MeasureTable } from './MeasureTable'
|
import { MeasureTable } from './MeasureTable'
|
||||||
import { MeasureTable2 } from './MeasureTable2'
|
//import { MeasureTable2 } from './MeasureTable2'
|
||||||
|
|
||||||
export default function Measure({idWell}){
|
export default function Measure({idWell}){
|
||||||
return <>
|
return <>
|
||||||
<MeasureTable2
|
<MeasureTable
|
||||||
idWell={idWell}
|
idWell={idWell}
|
||||||
idCategory={1}
|
idCategory={1}
|
||||||
title='Замер бурового раствора'
|
title='Замер бурового раствора'
|
||||||
|
@ -17,6 +17,7 @@ import Measure from "./Measure";
|
|||||||
import { makeMenuItems } from "./Documents/menuItems";
|
import { makeMenuItems } from "./Documents/menuItems";
|
||||||
import WellOperations from "./WellOperations";
|
import WellOperations from "./WellOperations";
|
||||||
import DrillingProgram from "./Documents/DrillingProgram";
|
import DrillingProgram from "./Documents/DrillingProgram";
|
||||||
|
import {PrivateMenuItem} from '../components/Private'
|
||||||
|
|
||||||
const { Content } = Layout;
|
const { Content } = Layout;
|
||||||
|
|
||||||
@ -26,30 +27,28 @@ export default function Well() {
|
|||||||
|
|
||||||
const { SubMenu } = Menu;
|
const { SubMenu } = Menu;
|
||||||
|
|
||||||
return (
|
return <Layout>
|
||||||
<>
|
|
||||||
<Layout>
|
|
||||||
<Menu
|
<Menu
|
||||||
mode="horizontal"
|
mode="horizontal"
|
||||||
selectable={true}
|
selectable={true}
|
||||||
selectedKeys={[tab]}
|
selectedKeys={[tab]}
|
||||||
className="well_menu"
|
className="well_menu"
|
||||||
>
|
>
|
||||||
<Menu.Item key="telemetry" icon={<FundViewOutlined />}>
|
<PrivateMenuItem key="telemetry" icon={<FundViewOutlined />}>
|
||||||
<Link to={`${rootPath}/telemetry`}>Мониторинг</Link>
|
<Link to={`${rootPath}/telemetry`}>Мониторинг</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<Menu.Item key="message" icon={<AlertOutlined />}>
|
<PrivateMenuItem key="message" icon={<AlertOutlined/>} roles={[]}>
|
||||||
<Link to={`${rootPath}/message`}>Сообщения</Link>
|
<Link to={`${rootPath}/message`}>Сообщения</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<Menu.Item key="report" icon={<FilePdfOutlined />}>
|
<PrivateMenuItem key="report" icon={<FilePdfOutlined />}>
|
||||||
<Link to={`${rootPath}/report`}>Рапорт</Link>
|
<Link to={`${rootPath}/report`}>Рапорт</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<Menu.Item key="operations" icon={<FolderOutlined />}>
|
<PrivateMenuItem key="operations" icon={<FolderOutlined />}>
|
||||||
<Link to={`${rootPath}/operations/plan`}>Операции по скважине</Link>
|
<Link to={`${rootPath}/operations/plan`}>Операции по скважине</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<Menu.Item key="archive" icon={<DatabaseOutlined />}>
|
<PrivateMenuItem key="archive" icon={<DatabaseOutlined />}>
|
||||||
<Link to={`${rootPath}/archive`}>Архив</Link>
|
<Link to={`${rootPath}/archive`}>Архив</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<SubMenu
|
<SubMenu
|
||||||
key="document"
|
key="document"
|
||||||
title={
|
title={
|
||||||
@ -65,12 +64,12 @@ export default function Well() {
|
|||||||
>
|
>
|
||||||
{makeMenuItems(rootPath)}
|
{makeMenuItems(rootPath)}
|
||||||
</SubMenu>
|
</SubMenu>
|
||||||
<Menu.Item key="measure" icon={<ExperimentOutlined />}>
|
<PrivateMenuItem key="measure" icon={<ExperimentOutlined />}>
|
||||||
<Link to={`${rootPath}/measure`}>Измерения</Link>
|
<Link to={`${rootPath}/measure`}>Измерения</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
<Menu.Item key="drillingProgram" icon={<FolderOutlined />}>
|
<PrivateMenuItem key="drillingProgram" icon={<FolderOutlined />}>
|
||||||
<Link to={`${rootPath}/drillingProgram`}>Программа бурения</Link>
|
<Link to={`${rootPath}/drillingProgram`}>Программа бурения</Link>
|
||||||
</Menu.Item>
|
</PrivateMenuItem>
|
||||||
</Menu>
|
</Menu>
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
@ -107,6 +106,4 @@ export default function Well() {
|
|||||||
</Content>
|
</Content>
|
||||||
</Layout>
|
</Layout>
|
||||||
</Layout>
|
</Layout>
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user