2021-10-12 13:21:45 +02:00
|
|
|
/* eslint-disable react/jsx-no-target-blank */
|
2021-05-18 12:59:48 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-03-11 13:59:20 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
2021-04-07 09:04:48 +02:00
|
|
|
Avatar,
|
|
|
|
Button,
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
|
|
|
DialogTitle,
|
2021-05-18 12:59:48 +02:00
|
|
|
InputLabel,
|
2021-04-07 09:04:48 +02:00
|
|
|
IconButton,
|
2021-03-11 13:59:20 +01:00
|
|
|
List,
|
|
|
|
ListItem,
|
2021-04-07 09:04:48 +02:00
|
|
|
ListItemAvatar,
|
2021-03-11 13:59:20 +01:00
|
|
|
ListItemSecondaryAction,
|
|
|
|
ListItemText,
|
|
|
|
MenuItem,
|
2021-04-07 09:04:48 +02:00
|
|
|
Select,
|
2021-05-18 12:59:48 +02:00
|
|
|
FormControl,
|
2021-03-11 13:59:20 +01:00
|
|
|
} from '@material-ui/core';
|
2021-06-29 10:21:54 +02:00
|
|
|
import { Delete } from '@material-ui/icons';
|
2021-10-12 13:21:45 +02:00
|
|
|
import { Alert } from '@material-ui/lab';
|
2021-03-11 13:59:20 +01:00
|
|
|
|
|
|
|
import AddUserComponent from './access-add-user';
|
|
|
|
|
|
|
|
import projectApi from '../../store/project/api';
|
2021-05-18 12:59:48 +02:00
|
|
|
import PageContent from '../common/PageContent';
|
2021-10-12 13:21:45 +02:00
|
|
|
import useUiConfig from '../../hooks/api/getters/useUiConfig/useUiConfig';
|
|
|
|
|
2021-03-11 13:59:20 +01:00
|
|
|
|
|
|
|
function AccessComponent({ projectId, project }) {
|
|
|
|
const [roles, setRoles] = useState([]);
|
|
|
|
const [users, setUsers] = useState([]);
|
|
|
|
const [error, setError] = useState();
|
2021-10-12 13:21:45 +02:00
|
|
|
const { isOss } = useUiConfig();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchAccess();
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [projectId]);
|
|
|
|
|
|
|
|
|
2021-03-11 13:59:20 +01:00
|
|
|
|
|
|
|
const fetchAccess = async () => {
|
2021-07-07 11:04:36 +02:00
|
|
|
try {
|
|
|
|
const access = await projectApi.fetchAccess(projectId);
|
|
|
|
setRoles(access.roles);
|
|
|
|
setUsers(
|
|
|
|
access.users.map(u => ({ ...u, name: u.name || '(No name)' }))
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-03-11 13:59:20 +01:00
|
|
|
};
|
|
|
|
|
2021-10-12 13:21:45 +02:00
|
|
|
if (isOss()) {
|
|
|
|
return (
|
|
|
|
<PageContent>
|
|
|
|
<Alert severity="error">
|
|
|
|
Controlling access to projects requires a paid version of Unleash.
|
|
|
|
Check out <a href="https://www.getunleash.io" target="_blank">getunleash.io</a> to find out more.
|
|
|
|
</Alert>
|
|
|
|
</PageContent>);
|
2021-03-11 13:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleRoleChange = (userId, currRoleId) => async evt => {
|
|
|
|
const roleId = evt.target.value;
|
|
|
|
try {
|
|
|
|
await projectApi.removeUserFromRole(projectId, currRoleId, userId);
|
|
|
|
await projectApi.addUserToRole(projectId, roleId, userId);
|
|
|
|
const newUsers = users.map(u => {
|
|
|
|
if (u.id === userId) {
|
|
|
|
return { ...u, roleId };
|
|
|
|
} else return u;
|
|
|
|
});
|
|
|
|
setUsers(newUsers);
|
|
|
|
} catch (err) {
|
|
|
|
setError(err.message || 'Server problems when adding users.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const addUser = async (userId, roleId) => {
|
|
|
|
try {
|
|
|
|
await projectApi.addUserToRole(projectId, roleId, userId);
|
|
|
|
await fetchAccess();
|
|
|
|
} catch (err) {
|
|
|
|
setError(err.message || 'Server problems when adding users.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeAccess = (userId, roleId) => async () => {
|
|
|
|
try {
|
|
|
|
await projectApi.removeUserFromRole(projectId, roleId, userId);
|
|
|
|
const newUsers = users.filter(u => u.id !== userId);
|
|
|
|
setUsers(newUsers);
|
|
|
|
} catch (err) {
|
|
|
|
setError(err.message || 'Server problems when adding users.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCloseError = () => {
|
|
|
|
setError(undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-05-18 12:59:48 +02:00
|
|
|
<PageContent
|
|
|
|
style={{ minHeight: '400px' }}
|
|
|
|
>
|
2021-03-11 13:59:20 +01:00
|
|
|
<AddUserComponent roles={roles} addUserToRole={addUser} />
|
|
|
|
<Dialog
|
|
|
|
open={!!error}
|
|
|
|
onClose={handleCloseError}
|
|
|
|
aria-labelledby="alert-dialog-title"
|
|
|
|
aria-describedby="alert-dialog-description"
|
|
|
|
>
|
|
|
|
<DialogTitle id="alert-dialog-title">{'Error'}</DialogTitle>
|
|
|
|
<DialogContent>
|
2021-05-18 12:59:48 +02:00
|
|
|
<DialogContentText id="alert-dialog-description">
|
|
|
|
{error}
|
|
|
|
</DialogContentText>
|
2021-03-11 13:59:20 +01:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2021-05-18 12:59:48 +02:00
|
|
|
<Button
|
|
|
|
onClick={handleCloseError}
|
|
|
|
color="secondary"
|
|
|
|
autoFocus
|
|
|
|
>
|
2021-03-11 13:59:20 +01:00
|
|
|
Close
|
|
|
|
</Button>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
2021-05-18 12:59:48 +02:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
height: '1px',
|
|
|
|
width: '106.65%',
|
|
|
|
marginLeft: '-2rem',
|
|
|
|
backgroundColor: '#efefef',
|
|
|
|
marginTop: '2rem',
|
|
|
|
}}
|
|
|
|
></div>
|
2021-03-11 13:59:20 +01:00
|
|
|
<List>
|
|
|
|
{users.map(user => {
|
|
|
|
const labelId = `checkbox-list-secondary-label-${user.id}`;
|
|
|
|
return (
|
|
|
|
<ListItem key={user.id} button>
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar alt={user.name} src={user.imageUrl} />
|
|
|
|
</ListItemAvatar>
|
2021-05-18 12:59:48 +02:00
|
|
|
<ListItemText
|
|
|
|
id={labelId}
|
|
|
|
primary={user.name}
|
|
|
|
secondary={user.email || user.username}
|
|
|
|
/>
|
|
|
|
<ListItemSecondaryAction
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FormControl variant="outlined" size="small">
|
|
|
|
<InputLabel
|
|
|
|
style={{ backgroundColor: '#fff' }}
|
|
|
|
for="add-user-select-role-label"
|
|
|
|
>
|
|
|
|
Role
|
|
|
|
</InputLabel>
|
|
|
|
<Select
|
|
|
|
labelId={`role-${user.id}-select-label`}
|
|
|
|
id={`role-${user.id}-select`}
|
|
|
|
key={user.id}
|
|
|
|
placeholder="Choose role"
|
|
|
|
value={user.roleId || ''}
|
|
|
|
onChange={handleRoleChange(
|
|
|
|
user.id,
|
|
|
|
user.roleId
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<MenuItem value="" disabled>
|
|
|
|
Choose role
|
2021-03-11 13:59:20 +01:00
|
|
|
</MenuItem>
|
2021-05-18 12:59:48 +02:00
|
|
|
{roles.map(role => (
|
|
|
|
<MenuItem
|
|
|
|
key={`${user.id}:${role.id}`}
|
|
|
|
value={role.id}
|
|
|
|
>
|
|
|
|
{role.name}
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
2021-03-11 13:59:20 +01:00
|
|
|
<IconButton
|
2021-05-18 12:59:48 +02:00
|
|
|
style={{ marginLeft: '0.5rem' }}
|
2021-03-11 13:59:20 +01:00
|
|
|
edge="end"
|
|
|
|
aria-label="delete"
|
|
|
|
title="Remove access"
|
|
|
|
onClick={removeAccess(user.id, user.roleId)}
|
|
|
|
>
|
2021-06-29 10:21:54 +02:00
|
|
|
<Delete />
|
2021-03-11 13:59:20 +01:00
|
|
|
</IconButton>
|
|
|
|
</ListItemSecondaryAction>
|
|
|
|
</ListItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</List>
|
2021-05-18 12:59:48 +02:00
|
|
|
</PageContent>
|
2021-03-11 13:59:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessComponent.propTypes = {
|
|
|
|
projectId: PropTypes.string.isRequired,
|
|
|
|
project: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AccessComponent;
|