From 2b199cd15c19b8d57c18e13b2880e72ef250e508 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 17 Oct 2023 19:52:36 +0200 Subject: [PATCH] permission check --- src/Components/AppRoutes/index.js | 2 +- src/Components/SideMenu/index.js | 2 +- src/Pages/Robotics/Robots/index.js | 206 ++++++++++++++++++----------- src/utils.js | 10 +- 4 files changed, 138 insertions(+), 82 deletions(-) diff --git a/src/Components/AppRoutes/index.js b/src/Components/AppRoutes/index.js index dfdc743..3dfc351 100644 --- a/src/Components/AppRoutes/index.js +++ b/src/Components/AppRoutes/index.js @@ -254,7 +254,7 @@ export default function AppRoutes() { {hasPermission( appContext.userPermissions, - Constants.PERMISSIONS.ROBOTICS.ROBOTS + Constants.PERMISSIONS.ROBOTICS.ROBOTS.VIEW ) && ( ), }, - { + ]; + + if ( + hasPermission( + appContext.userPermissions, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.VIEW_ROBOTS_ADDRESSES + ) + ) { + items.push({ title: t("robotics.robots.column.address"), dataIndex: "address", key: "address", @@ -178,7 +190,10 @@ export default function Robots() { ), - }, + }); + } + + items.push( { title: t("robotics.robots.column.connectedAt"), dataIndex: "connectedAt", @@ -193,88 +208,107 @@ export default function Robots() { title: t("robotics.robots.column.createdAt"), dataIndex: "createdAt", key: "createdAt", - }, - { + } + ); + + if ( + hasOnePermission( + appContext.userPermissions, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.EDIT_ROBOT_NAME, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.DISCONNECT_ROBOT + ) + ) { + items.push({ title: t("robotics.robots.column.actions"), dataIndex: "actions", key: "actions", render: (_, record) => ( - setSelectedRobotName(e.target.value)} - maxLength={Constants.GLOBALS.MAX_ROBOTICS_ROBOT_NAME_LENGTH} - /> - } - okButtonProps={{ - disabled: - selectedRobotName.length < - Constants.GLOBALS.MIN_ROBOTICS_ROBOT_NAME_LENGTH || - selectedRobotName.length > - Constants.GLOBALS.MAX_ROBOTICS_ROBOT_NAME_LENGTH, - }} - okText={t("common.button.confirm")} - cancelText={t("common.button.cancel")} - onConfirm={() => - myFetch( - `/robot`, - "PATCH", - { - robotId: record.id, - robotName: selectedRobotName, - }, - {}, - myFetchContentType.JSON, - Constants.ROBOTICS_API_ADDRESS - ).catch((err) => { - if (err === 422) { - notificationApi["error"]({ - message: t( - "robotics.robots.popconfirmEdit.errorNotification.message" - ), - description: t( - "robotics.robots.popconfirmEdit.errorNotification.description" - ), - }); - } - }) - } - > - setSelectedRobotName(record.name)}> - {t("common.text.edit")} - - + {hasPermission( + appContext.userPermissions, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.EDIT_ROBOT_NAME + ) && ( + setSelectedRobotName(e.target.value)} + maxLength={Constants.GLOBALS.MAX_ROBOTICS_ROBOT_NAME_LENGTH} + /> + } + okButtonProps={{ + disabled: + selectedRobotName.length < + Constants.GLOBALS.MIN_ROBOTICS_ROBOT_NAME_LENGTH || + selectedRobotName.length > + Constants.GLOBALS.MAX_ROBOTICS_ROBOT_NAME_LENGTH, + }} + okText={t("common.button.confirm")} + cancelText={t("common.button.cancel")} + onConfirm={() => + myFetch( + `/robot`, + "PATCH", + { + robotId: record.id, + robotName: selectedRobotName, + }, + {}, + myFetchContentType.JSON, + Constants.ROBOTICS_API_ADDRESS + ).catch((err) => { + if (err === 422) { + notificationApi["error"]({ + message: t( + "robotics.robots.popconfirmEdit.errorNotification.message" + ), + description: t( + "robotics.robots.popconfirmEdit.errorNotification.description" + ), + }); + } + }) + } + > + setSelectedRobotName(record.name)}> + {t("common.text.edit")} + + + )} - - myFetch( - `/robot/${record.id}`, - "DELETE", - null, - {}, - myFetchContentType.JSON, - Constants.ROBOTICS_API_ADDRESS - ) - } - > - {t("common.text.disconnect")} - + {hasPermission( + appContext.userPermissions, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.DISCONNECT_ROBOT + ) && ( + + myFetch( + `/robot/${record.id}`, + "DELETE", + null, + {}, + myFetchContentType.JSON, + Constants.ROBOTICS_API_ADDRESS + ) + } + > + {t("common.text.disconnect")} + + )} ), - }, - ]; + }); + } return items; }; @@ -335,7 +369,15 @@ export default function Robots() { dataIndex: "firmwareVersion", key: "firmwareVersion", }, - { + ]; + + if ( + hasPermission( + appContext.userPermissions, + Constants.PERMISSIONS.ROBOTICS.ROBOTS.AUTHORIZE_DENY_UNAUTHORIZED_ROBOTS + ) + ) { + items.push({ title: t("robotics.robots.column.actions"), dataIndex: "actions", key: "actions", @@ -386,8 +428,8 @@ export default function Robots() { ), - }, - ]; + }); + } return items; }; @@ -689,6 +731,12 @@ export default function Robots() {