From e7a36a1ff1bea595d1e8b0ea6fb7bc30739cf16d Mon Sep 17 00:00:00 2001 From: jinapurapu <65002498+jinapurapu@users.noreply.github.com> Date: Fri, 3 Jun 2022 21:54:06 -0700 Subject: [PATCH] Assign policy for Multiple groups (#2086) --- .../src/screens/Console/Groups/Groups.tsx | 4 +- .../screens/Console/Groups/GroupsDetails.tsx | 2 +- .../screens/Console/Policies/SetPolicy.tsx | 50 +++++++++++-------- restapi/admin_policies.go | 12 ++++- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/portal-ui/src/screens/Console/Groups/Groups.tsx b/portal-ui/src/screens/Console/Groups/Groups.tsx index fb3899bcf..a3655d573 100644 --- a/portal-ui/src/screens/Console/Groups/Groups.tsx +++ b/portal-ui/src/screens/Console/Groups/Groups.tsx @@ -199,7 +199,7 @@ const Groups = ({ classes, history }: IGroupsProps) => { {policyOpen && ( { setPolicyOpen(false); @@ -241,7 +241,7 @@ const Groups = ({ classes, history }: IGroupsProps) => { text={"Assign Policy"} icon={} color="primary" - disabled={checkedGroups.length !== 1} + disabled={checkedGroups.length < 1} variant={"outlined"} /> diff --git a/portal-ui/src/screens/Console/Groups/GroupsDetails.tsx b/portal-ui/src/screens/Console/Groups/GroupsDetails.tsx index 4c48ff718..254947c39 100644 --- a/portal-ui/src/screens/Console/Groups/GroupsDetails.tsx +++ b/portal-ui/src/screens/Console/Groups/GroupsDetails.tsx @@ -347,7 +347,7 @@ const GroupsDetails = ({ classes, match }: IGroupDetailsProps) => { {policyOpen ? ( { setPolicyOpen(false); diff --git a/portal-ui/src/screens/Console/Policies/SetPolicy.tsx b/portal-ui/src/screens/Console/Policies/SetPolicy.tsx index 5fd8a449d..79e0b7bc5 100644 --- a/portal-ui/src/screens/Console/Policies/SetPolicy.tsx +++ b/portal-ui/src/screens/Console/Policies/SetPolicy.tsx @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import React, { useEffect, useState } from "react"; +import React, { useEffect, useState, Fragment } from "react"; import { useDispatch } from "react-redux"; import get from "lodash/get"; import { Theme } from "@mui/material/styles"; @@ -41,7 +41,7 @@ interface ISetPolicyProps { classes: any; closeModalAndRefresh: () => void; selectedUser: User | null; - selectedGroup: string | null; + selectedGroups: string[] | null; open: boolean; } @@ -63,7 +63,7 @@ const SetPolicy = ({ classes, closeModalAndRefresh, selectedUser, - selectedGroup, + selectedGroups, open, }: ISetPolicyProps) => { const dispatch = useDispatch(); @@ -73,24 +73,23 @@ const SetPolicy = ({ const [selectedPolicy, setSelectedPolicy] = useState([]); const setPolicyAction = () => { - let entity = "user"; - let value = null; - if (selectedGroup !== null) { - entity = "group"; - value = selectedGroup; + let users = null; + let groups = null; + if (selectedGroups !== null) { + groups = selectedGroups; } else { if (selectedUser !== null) { - value = selectedUser.accessKey; + users = [selectedUser.accessKey] || [" "]; } } setLoading(true); api - .invoke("PUT", `/api/v1/set-policy`, { + .invoke("PUT", `/api/v1/set-policy-multi`, { name: selectedPolicy, - entityName: value, - entityType: entity, + groups: groups, + users: users, }) .then(() => { setLoading(false); @@ -103,9 +102,9 @@ const SetPolicy = ({ }; const fetchGroupInformation = () => { - if (selectedGroup) { + if (selectedGroups?.length === 1) { api - .invoke("GET", `/api/v1/group/${encodeURLString(selectedGroup)}`) + .invoke("GET", `/api/v1/group/${encodeURLString(selectedGroups[0])}`) .then((res: any) => { const groupPolicy: String = get(res, "policy", ""); setActualPolicy(groupPolicy.split(",")); @@ -121,10 +120,10 @@ const SetPolicy = ({ const resetSelection = () => { setSelectedPolicy(actualPolicy); }; - + useEffect(() => { if (open) { - if (selectedGroup !== null) { + if (selectedGroups?.length === 1) { fetchGroupInformation(); return; } @@ -134,7 +133,7 @@ const SetPolicy = ({ setSelectedPolicy(userPolicy); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [open, selectedGroup, selectedUser]); + }, [open, selectedGroups?.length, selectedUser]); const userName = get(selectedUser, "accessKey", ""); @@ -146,11 +145,14 @@ const SetPolicy = ({ modalOpen={open} title="Set Policies" > + - + {(selectedGroups?.length === 1 || selectedUser != null) && + + @@ -159,6 +161,14 @@ const SetPolicy = ({ content={actualPolicy.join(", ")} /> + + } + {selectedGroups && selectedGroups?.length > 1 && + + }
1 { + allGroupPolicies = groupDesc.Policy + "," + policyName + } else { + allGroupPolicies = policyName + } + if err := client.setPolicy(ctx, allGroupPolicies, string(group), true); err != nil { return err } }