Skip to content

Commit e7a36a1

Browse files
authored
Assign policy for Multiple groups (#2086)
1 parent 41e1b4a commit e7a36a1

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

portal-ui/src/screens/Console/Groups/Groups.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const Groups = ({ classes, history }: IGroupsProps) => {
199199
{policyOpen && (
200200
<SetPolicy
201201
open={policyOpen}
202-
selectedGroup={checkedGroups[0]}
202+
selectedGroups={checkedGroups}
203203
selectedUser={null}
204204
closeModalAndRefresh={() => {
205205
setPolicyOpen(false);
@@ -241,7 +241,7 @@ const Groups = ({ classes, history }: IGroupsProps) => {
241241
text={"Assign Policy"}
242242
icon={<IAMPoliciesIcon />}
243243
color="primary"
244-
disabled={checkedGroups.length !== 1}
244+
disabled={checkedGroups.length < 1}
245245
variant={"outlined"}
246246
/>
247247
</SecureComponent>

portal-ui/src/screens/Console/Groups/GroupsDetails.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ const GroupsDetails = ({ classes, match }: IGroupDetailsProps) => {
347347
{policyOpen ? (
348348
<SetPolicy
349349
open={policyOpen}
350-
selectedGroup={groupName}
350+
selectedGroups={[groupName]}
351351
selectedUser={null}
352352
closeModalAndRefresh={() => {
353353
setPolicyOpen(false);

portal-ui/src/screens/Console/Policies/SetPolicy.tsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import React, { useEffect, useState } from "react";
17+
import React, { useEffect, useState, Fragment } from "react";
1818
import { useDispatch } from "react-redux";
1919
import get from "lodash/get";
2020
import { Theme } from "@mui/material/styles";
@@ -41,7 +41,7 @@ interface ISetPolicyProps {
4141
classes: any;
4242
closeModalAndRefresh: () => void;
4343
selectedUser: User | null;
44-
selectedGroup: string | null;
44+
selectedGroups: string[] | null;
4545
open: boolean;
4646
}
4747

@@ -63,7 +63,7 @@ const SetPolicy = ({
6363
classes,
6464
closeModalAndRefresh,
6565
selectedUser,
66-
selectedGroup,
66+
selectedGroups,
6767
open,
6868
}: ISetPolicyProps) => {
6969
const dispatch = useDispatch();
@@ -73,24 +73,23 @@ const SetPolicy = ({
7373
const [selectedPolicy, setSelectedPolicy] = useState<string[]>([]);
7474

7575
const setPolicyAction = () => {
76-
let entity = "user";
77-
let value = null;
78-
if (selectedGroup !== null) {
79-
entity = "group";
80-
value = selectedGroup;
76+
let users = null;
77+
let groups = null;
78+
if (selectedGroups !== null) {
79+
groups = selectedGroups;
8180
} else {
8281
if (selectedUser !== null) {
83-
value = selectedUser.accessKey;
82+
users = [selectedUser.accessKey] || [" "];
8483
}
8584
}
8685

8786
setLoading(true);
8887

8988
api
90-
.invoke("PUT", `/api/v1/set-policy`, {
89+
.invoke("PUT", `/api/v1/set-policy-multi`, {
9190
name: selectedPolicy,
92-
entityName: value,
93-
entityType: entity,
91+
groups: groups,
92+
users: users,
9493
})
9594
.then(() => {
9695
setLoading(false);
@@ -103,9 +102,9 @@ const SetPolicy = ({
103102
};
104103

105104
const fetchGroupInformation = () => {
106-
if (selectedGroup) {
105+
if (selectedGroups?.length === 1) {
107106
api
108-
.invoke("GET", `/api/v1/group/${encodeURLString(selectedGroup)}`)
107+
.invoke("GET", `/api/v1/group/${encodeURLString(selectedGroups[0])}`)
109108
.then((res: any) => {
110109
const groupPolicy: String = get(res, "policy", "");
111110
setActualPolicy(groupPolicy.split(","));
@@ -121,10 +120,10 @@ const SetPolicy = ({
121120
const resetSelection = () => {
122121
setSelectedPolicy(actualPolicy);
123122
};
124-
123+
125124
useEffect(() => {
126125
if (open) {
127-
if (selectedGroup !== null) {
126+
if (selectedGroups?.length === 1) {
128127
fetchGroupInformation();
129128
return;
130129
}
@@ -134,7 +133,7 @@ const SetPolicy = ({
134133
setSelectedPolicy(userPolicy);
135134
}
136135
// eslint-disable-next-line react-hooks/exhaustive-deps
137-
}, [open, selectedGroup, selectedUser]);
136+
}, [open, selectedGroups?.length, selectedUser]);
138137

139138
const userName = get(selectedUser, "accessKey", "");
140139

@@ -146,11 +145,14 @@ const SetPolicy = ({
146145
modalOpen={open}
147146
title="Set Policies"
148147
>
148+
149149
<Grid container>
150-
<Grid item xs={12}>
150+
{(selectedGroups?.length === 1 || selectedUser != null) &&
151+
<Fragment>
152+
<Grid item xs={12}>
151153
<PredefinedList
152-
label={`Selected ${selectedGroup !== null ? "Group" : "User"}`}
153-
content={selectedGroup !== null ? selectedGroup : userName}
154+
label={`Selected ${selectedGroups !== null ? "Group" : "User"}`}
155+
content={selectedGroups !== null ? selectedGroups[0] : userName}
154156
/>
155157
</Grid>
156158
<Grid item xs={12}>
@@ -159,6 +161,14 @@ const SetPolicy = ({
159161
content={actualPolicy.join(", ")}
160162
/>
161163
</Grid>
164+
</Fragment>
165+
}
166+
{selectedGroups && selectedGroups?.length > 1 &&
167+
<PredefinedList
168+
label={"Selected Groups"}
169+
content={selectedGroups.join(", ")}
170+
/>
171+
}
162172
<Grid item xs={12}>
163173
<div className={classes.tableBlock}>
164174
<PolicySelectors

restapi/admin_policies.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,17 @@ func setPolicyMultipleEntities(ctx context.Context, client MinioAdmin, policyNam
687687
}
688688
}
689689
for _, group := range groups {
690-
if err := client.setPolicy(ctx, policyName, string(group), true); err != nil {
690+
groupDesc, err := groupInfo(ctx, client, string(group))
691+
if err != nil {
692+
return err
693+
}
694+
allGroupPolicies := ""
695+
if len(groups) > 1 {
696+
allGroupPolicies = groupDesc.Policy + "," + policyName
697+
} else {
698+
allGroupPolicies = policyName
699+
}
700+
if err := client.setPolicy(ctx, allGroupPolicies, string(group), true); err != nil {
691701
return err
692702
}
693703
}

0 commit comments

Comments
 (0)