Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Already added roles not visible in GW environment issue #860

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ function AddEditGWEnvironment(props) {
const [invalidRoles, setInvalidRoles] = useState([]);
const [roleValidity, setRoleValidity] = useState(true);
const { gatewayTypes } = settings;
const [initialState, setInitialState] = useState({
displayName: '',
description: '',
gatewayType: gatewayTypes && gatewayTypes.length > 1 ? 'Regular' : gatewayTypes[0],
type: 'hybrid',
vhosts: [defaultVhost],
permissions: {
roles: [],
permissionType: 'PUBLIC',
},
const initialPermissions = dataRow && dataRow.permissions
? dataRow.permissions
: { roles: [], permissionType: 'PUBLIC' };
const [initialState, setInitialState] = useState(() => {
return {
displayName: '',
description: '',
gatewayType: gatewayTypes && gatewayTypes.length > 1 ? 'Regular' : gatewayTypes[0],
type: 'hybrid',
vhosts: [defaultVhost],
permissions: initialPermissions,
};
});
const [editMode, setIsEditMode] = useState(false);

Expand All @@ -159,6 +161,14 @@ function AddEditGWEnvironment(props) {
name, displayName, description, vhosts, type, gatewayType, permissions,
} = state;

const [roles, setRoles] = useState([]);

useEffect(() => {
if (permissions && permissions.roles) {
setRoles(permissions.roles);
}
}, [permissions]);

let permissionType = '';
if (permissions) {
permissionType = state.permissions.permissionType;
Expand All @@ -170,6 +180,8 @@ function AddEditGWEnvironment(props) {
if (invalidRolesArray.length === 0) {
setRoleValidity(true);
}
} else if (roles.includes(role)) {
setRoles(roles.filter((existingRole) => existingRole !== role));
} else {
setValidRoles(validRoles.filter((existingRole) => existingRole !== role));
}
Expand All @@ -180,6 +192,12 @@ function AddEditGWEnvironment(props) {
const promise = restApi.validateSystemRole(base64url.encode(role));
promise
.then(() => {
// Check if the role is already added
if (roles.includes(role) || validRoles.includes(role) || invalidRoles.includes(role)) {
Alert.error('Role already added: ' + role);
return;
}

setValidRoles(validRoles.concat(role));
if (invalidRoles.length === 0) {
setRoleValidity(true);
Expand All @@ -197,6 +215,7 @@ function AddEditGWEnvironment(props) {
}
});
};

const onChange = (e) => {
if (e.target.name === 'GatewayPermissionRestrict') {
permissionType = e.target.value;
Expand Down Expand Up @@ -398,7 +417,7 @@ function AddEditGWEnvironment(props) {
});
}
permissions.permissionType = state.permissions.permissionType;
permissions.roles = validRoles;
permissions.roles = roles.concat(validRoles);
let promiseAPICall;
if (dataRow) {
// assign the update promise to the promiseAPICall
Expand Down Expand Up @@ -761,7 +780,7 @@ function AddEditGWEnvironment(props) {
}}
name='GatewayEnvironmentPermissions'
variant='outlined'
value={validRoles.concat(invalidRoles)}
value={roles.concat(validRoles, invalidRoles)}
alwaysShowPlaceholder={false}
placeholder='Enter roles and press Enter'
blurBehavior='clear'
Expand Down
Loading