Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 21 additions & 5 deletions src/buttons/RolesEditionButton/RolesEditionButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export const RolesEditionButton = ({
labels: tmpLabels,
isIconButton = false,
agents,
hasWriteSecurityPermission,
specificSharingRestriction,
canBeSharedWithAgent,
resourceRolesPermissionsMapping,
preventNoneRoleForAgents = false,
isReadOnly = false,
disabled = false,
onConfirmChanges,
specificAccessByAgent,
Expand Down Expand Up @@ -69,8 +71,10 @@ export const RolesEditionButton = ({
accessControlList={specificAccessByAgent}
defaultRole={defaultRole}
agents={agents}
canBeSharedWithAgent={canBeSharedWithAgent}
labels={labels.dialog}
isReadOnly={isReadOnly}
hasWriteSecurityPermission={hasWriteSecurityPermission}
specificSharingRestriction={specificSharingRestriction}
onConfirmChanges={onConfirmChanges}
closeDialog={closeDialog}
allRoles={allRoles}
Expand All @@ -88,9 +92,15 @@ RolesEditionButton.propTypes = {
*/
isIconButton: PropTypes.bool,
/**
* Defines whether user can edit or only see the resource's permissions; false by default
* Defines if current user has write security permission on the resource
* - true : selectors are enabled and share button is visible
* - false : selectors and share button are hidden
*/
isReadOnly: PropTypes.bool,
hasWriteSecurityPermission: PropTypes.bool,
/**
* * Defines restriction for sharing the resource
*/
specificSharingRestriction: PropTypes.string,
/**
* Defines the RolesEditionButton's state:
* - true : the button is disabled (the tooltip will guide users on how to enable the button)
Expand All @@ -100,8 +110,13 @@ RolesEditionButton.propTypes = {
/**
* List of all users or users groups in the workspace
*/

agents: PropTypes.array.isRequired,
/**
* Function that checks if the resource can be shared with the user
Comment thread
nborde-CSM marked this conversation as resolved.
* - returns null if the resource can be shared with the user
* - returns a string (reason) if the resource cannot be shared with the user
*/
canBeSharedWithAgent: PropTypes.func,
/**
* List of users or users groups having specific access to the resource
*/
Expand Down Expand Up @@ -161,6 +176,7 @@ RolesEditionButton.propTypes = {
noAdminError: 'string',
userSelected: 'string',
usersAccess: 'string',
disabledUserTooltip: 'function',
generalAccess: 'string',
removeAccess: 'string',
add: 'object',
Expand Down
5 changes: 3 additions & 2 deletions src/buttons/RolesEditionButton/RolesEditionButton.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const defaultProps = {
specificAccessByAgent: SAMPLE_AGENTS.specificAccessByAgent,
resourceRolesPermissionsMapping: RESOURCE_ROLES_PERMISSIONS_MAPPING,
preventNoneRoleForAgents: true,
isReadOnly: false,
defaultRole: 'viewer',
allRoles: ALL_ROLES,
allPermissions: ALL_PERMISSIONS,
Expand Down Expand Up @@ -73,7 +72,9 @@ describe('RoleEditionButton', () => {
defaultRole: defaultProps.defaultRole,
agents: defaultProps.agents,
labels: defaultProps.labels.dialog,
isReadOnly: defaultProps.isReadOnly,
hasWriteSecurityPermission: defaultProps.hasWriteSecurityPermission,
specificSharingRestriction: defaultProps.specificSharingRestriction,
canBeSharedWithAgent: defaultProps.canBeSharedWithAgent,
allRoles: defaultProps.allRoles,
allPermissions: defaultProps.allPermissions,
defaultAccessScope: defaultProps.defaultAccessScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
IconButton,
Typography,
Autocomplete,
Box,
} from '@mui/material';
import { styled } from '@mui/material/styles';
import { RoleEditor } from '../../../../inputs';
import { FadingTooltip } from '../../../../misc';
import { getIdentifierFromUserEmail } from '../../../../utils';
import { RolesAddingDialog } from './components';

Expand Down Expand Up @@ -54,7 +56,9 @@ const sortById = (agentA, agentB) => (agentA.id < agentB.id ? -1 : 1);

export const RolesEditionDialog = ({
labels: tmpLabels,
isReadOnly = false,
hasWriteSecurityPermission = true,
specificSharingRestriction,
canBeSharedWithAgent = () => null,
resourceRolesPermissionsMapping,
preventNoneRoleForAgents = false,
onConfirmChanges,
Expand All @@ -71,7 +75,6 @@ export const RolesEditionDialog = ({
const [selectedAgentForRoleAddition, setSelectedAgentForRoleAddition] = useState(null);
const [newAccessControlList, setNewAccessControlList] = useState([...accessControlList].sort(sortById));
const [newDefaultRole, setNewDefaultRole] = useState(defaultRole || '');

const labels = { ...DEFAULT_LABELS, ...tmpLabels };

useEffect(() => {
Expand Down Expand Up @@ -135,12 +138,14 @@ export const RolesEditionDialog = ({
<>
<DialogContent>
<Grid container spacing={2}>
{!isReadOnly && (
{hasWriteSecurityPermission && (
<Grid size={12}>
<Autocomplete
data-cy="share-scenario-dialog-agents-select"
ListboxProps={{ 'data-cy': 'share-scenario-dialog-agents-select-options' }}
getOptionDisabled={(option) => canBeSharedWithAgent(option) != null}
autoComplete
disabled={specificSharingRestriction != null}
disableClearable={true}
options={agentsWithoutSpecificAccess}
value={selectedAgentForRoleAddition?.id}
Expand All @@ -151,12 +156,24 @@ export const RolesEditionDialog = ({
<TextField {...params} placeholder={labels.addPeople} label={labels.addPeople} variant="filled" />
)}
renderOption={(props, option) => {
const tooltip = canBeSharedWithAgent(option);
return (
<li {...props}>
<span data-cy={`share-scenario-dialog-agents-select-${getIdentifierFromUserEmail(option.id)}`}>
{option.id}
</span>
</li>
<Box key={option.id} sx={{ display: 'flex', alignItems: 'center' }}>
<FadingTooltip title={tooltip} placement="right">
<span
style={{
display: 'inline-block',
width: 'fit-content',
cursor: canBeSharedWithAgent(option) == null ? 'not-allowed' : 'pointer',
}}
data-cy={`share-scenario-dialog-agents-select-${getIdentifierFromUserEmail(option.id)}`}
>
<Box component="li" {...props}>
{option.id}
</Box>
</span>
</FadingTooltip>
</Box>
);
}}
/>
Expand All @@ -173,7 +190,7 @@ export const RolesEditionDialog = ({
agentAccess={agent.role}
allRoles={preventNoneRoleForAgents ? allRolesWithoutNone : allRoles}
onOptionSelected={(event) => editSpecificAccess(event, agent)}
isReadOnly={isReadOnly}
isReadOnly={!hasWriteSecurityPermission || specificSharingRestriction != null}
actions={[
{
id: 'remove_specific_access',
Expand Down Expand Up @@ -201,26 +218,28 @@ export const RolesEditionDialog = ({
helperText={labels.editor.helperText}
allRoles={allRoles}
icon={workspaceIcon}
isReadOnly={isReadOnly}
isReadOnly={!hasWriteSecurityPermission || specificSharingRestriction != null}
onOptionSelected={(event) => setNewDefaultRole(event.target.value)}
/>
</Grid>
</Grid>
</DialogContent>
<DialogActions>
<Button data-cy="share-scenario-dialog-first-cancel-button" onClick={closeDialog} color="primary">
{isReadOnly ? labels.close : labels.cancel}
{hasWriteSecurityPermission ? labels.cancel : labels.close}
</Button>
{!isReadOnly && (
<Button
data-cy="share-scenario-dialog-submit-button"
onClick={confirmAndCloseDialog}
color="primary"
variant="contained"
disabled={hasNoAdmin}
>
{labels.share}
</Button>
{hasWriteSecurityPermission && (
<FadingTooltip title={specificSharingRestriction}>
<Button
data-cy="share-scenario-dialog-submit-button"
onClick={confirmAndCloseDialog}
color="primary"
variant="contained"
disabled={hasNoAdmin || specificSharingRestriction != null}
>
{labels.share}
</Button>
</FadingTooltip>
)}
</DialogActions>
</>
Expand Down Expand Up @@ -258,7 +277,7 @@ export const RolesEditionDialog = ({
<ArrowBackIcon />
</IconButton>
)}
{isReadOnly ? labels.readOnlyTitle : labels.title}
{hasWriteSecurityPermission ? labels.readOnlyTitle : labels.title}
</DialogTitle>
{dialogContent}
</StyledDialog>
Expand All @@ -283,12 +302,14 @@ RolesEditionDialog.propTypes = {
helperText: PropTypes.object,
}),
}),
isReadOnly: PropTypes.bool,
hasWriteSecurityPermission: PropTypes.bool,
specificSharingRestriction: PropTypes.string,
resourceRolesPermissionsMapping: PropTypes.object.isRequired,
preventNoneRoleForAgents: PropTypes.bool,
onConfirmChanges: PropTypes.func.isRequired,
accessControlList: PropTypes.array.isRequired,
agents: PropTypes.array.isRequired,
canBeSharedWithAgent: PropTypes.func,
defaultRole: PropTypes.string,
open: PropTypes.bool.isRequired,
closeDialog: PropTypes.func.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const selectRoleForAgent = (agentId, newRole) => {

const propsWithoutUsers = {
labels: LABELS.dialog,
isReadOnly: false,
resourceRolesPermissionsMapping: RESOURCE_ROLES_PERMISSIONS_MAPPING,
allRoles: ALL_ROLES,
allPermissions: ALL_PERMISSIONS,
Expand All @@ -78,9 +77,9 @@ const propsWithPreventNoneRole = {
preventNoneRoleForAgents: true,
};

const propsWithReadOnly = {
const propsWithWriteSecurityPermissionToFalse = {
...propsWithUsers,
isReadOnly: true,
hasWriteSecurityPermission: false,
};

const allRolesWithoutNone = ALL_ROLES.filter((role) => role.value.toLowerCase() !== 'none');
Expand Down Expand Up @@ -384,9 +383,9 @@ describe('RolesEditionDialog', () => {
});
});

describe('Read-only', () => {
describe('Write security to false', () => {
beforeEach(() => {
setUp(propsWithReadOnly);
setUp(propsWithWriteSecurityPermissionToFalse);
});

test('New user cannot be added', () => {
Expand Down