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

Support create/edit user role on user page #236

Merged
merged 9 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions client/src/http/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
UpdateUserParams,
CreateRoleParams,
DeleteRoleParams,
AssignRoleParams,
UnassignRoleParams,
} from '../pages/user/Types';
import BaseModel from './BaseModel';

Expand Down Expand Up @@ -45,6 +47,20 @@ export class UserHttp extends BaseModel {
return super.delete({ path: `${this.USER_URL}/roles/${data.roleName}` });
}

static updateUserRole(data: AssignRoleParams) {
return super.update({
path: `${this.USER_URL}/${data.username}/role/update`,
data,
});
}

static unassignUserRole(data: UnassignRoleParams) {
return super.update({
path: `${this.USER_URL}/${data.username}/role/unassign`,
data,
});
}

get _names() {
return this.names;
}
Expand Down
7 changes: 5 additions & 2 deletions client/src/i18n/en/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const userTrans = {
createTitle: 'Create User',
updateTitle: 'Update Milvus User',
updateRoleTitle: 'Update User Roles',
user: 'User',
users: 'Users',
deleteWarning: 'You are trying to drop user. This action cannot be undone.',
Expand All @@ -10,11 +11,13 @@ const userTrans = {
update: 'Update password',
isNotSame: 'Not same as new password',
deleteTip:
'Please select at least one item to drop and root can not be dropped.',

'Please select at least one item to drop and the root user can not be dropped.',
deleteEditRoleTip: 'root role is not editable.',
role: 'Role',
editRole: 'Edit Role',
roles: 'Roles',
createRoleTitle: 'Create Role',
updateRoleSuccess: 'User Role',
};

export default userTrans;
59 changes: 56 additions & 3 deletions client/src/pages/user/CreateUser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { makeStyles, Theme } from '@material-ui/core';
import {
makeStyles,
Theme,
Checkbox,
FormGroup,
FormControlLabel,
Typography,
} from '@material-ui/core';
import { FC, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import DialogTemplate from '@/components/customDialog/DialogTemplate';
Expand All @@ -10,11 +17,21 @@ import { CreateUserProps, CreateUserParams } from './Types';

const useStyles = makeStyles((theme: Theme) => ({
input: {
margin: theme.spacing(3, 0, 0.5),
margin: theme.spacing(2, 0, 0.5),
},
dialogWrapper: {
maxWidth: theme.spacing(70),
'& .MuiFormControlLabel-root': {
width: theme.spacing(20),
},
},
}));

const CreateUser: FC<CreateUserProps> = ({ handleCreate, handleClose }) => {
const CreateUser: FC<CreateUserProps> = ({
handleCreate,
handleClose,
roles,
}) => {
const { t: commonTrans } = useTranslation();
const { t: userTrans } = useTranslation('user');
const { t: btnTrans } = useTranslation('btn');
Expand All @@ -24,10 +41,14 @@ const CreateUser: FC<CreateUserProps> = ({ handleCreate, handleClose }) => {
const [form, setForm] = useState<CreateUserParams>({
username: '',
password: '',
roles: [],
});

// selected Role
const checkedForm = useMemo(() => {
return formatForm(form);
}, [form]);

const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);

const classes = useStyles();
Expand Down Expand Up @@ -87,6 +108,7 @@ const CreateUser: FC<CreateUserProps> = ({ handleCreate, handleClose }) => {
confirmLabel={btnTrans('create')}
handleConfirm={handleCreateUser}
confirmDisabled={disabled}
dialogClass={classes.dialogWrapper}
>
<>
{createConfigs.map(v => (
Expand All @@ -98,6 +120,37 @@ const CreateUser: FC<CreateUserProps> = ({ handleCreate, handleClose }) => {
key={v.label}
/>
))}

<Typography variant="h5" component="span">
{userTrans('roles')}
</Typography>

<FormGroup row>
{roles.map((r: any, index: number) => (
shanghaikid marked this conversation as resolved.
Show resolved Hide resolved
<FormControlLabel
control={
<Checkbox
onChange={(e: React.ChangeEvent<HTMLInputElement>, checked: boolean) => {
let newRoles = [...form.roles];

if (!checked) {
newRoles = newRoles.filter(
(n: string | number) => n === r.vlaue
);
} else {
newRoles.push(r.value);
}

setForm(v => ({ ...v, roles: [...newRoles] }));
}}
/>
}
key={index}
label={r.label}
value={r.value}
/>
))}
</FormGroup>
</>
</DialogTemplate>
);
Expand Down
24 changes: 24 additions & 0 deletions client/src/pages/user/Types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { Option } from '@/components/customSelector/Types';

export interface UserData {
name: string;
roleName?: string;
roles: string[];
}

export interface CreateUserParams {
username: string;
password: string;
roles: string[];
}

export interface UpdateUserRoleParams {
roles: string[];
}

export interface UpdateUserRoleProps {
onUpdate: (data: UpdateUserRoleParams) => void;
handleClose: () => void;
username: string;
roles: string[];
}

export interface CreateUserProps {
handleCreate: (data: CreateUserParams) => void;
handleClose: () => void;
roles: Option[];
shanghaikid marked this conversation as resolved.
Show resolved Hide resolved
}

export interface UpdateUserProps {
Expand Down Expand Up @@ -41,6 +58,13 @@ export interface DeleteRoleParams {
roleName: string;
}

export interface AssignRoleParams {
username: string;
roles: string[];
}

export interface UnassignRoleParams extends AssignRoleParams {}

export interface RoleData {
name: string;
}
Expand Down
105 changes: 105 additions & 0 deletions client/src/pages/user/UpdateUserRole.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
makeStyles,
Theme,
Checkbox,
FormGroup,
FormControlLabel,
} from '@material-ui/core';
import { FC, useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import DialogTemplate from '@/components/customDialog/DialogTemplate';
import { UpdateUserRoleProps, UpdateUserRoleParams } from './Types';
import { UserHttp } from '@/http/User';

const useStyles = makeStyles((theme: Theme) => ({
input: {
margin: theme.spacing(2, 0, 0.5),
},
dialogWrapper: {
maxWidth: theme.spacing(70),
'& .MuiFormControlLabel-root': {
width: theme.spacing(20),
},
},
}));

const UpdateUserRole: FC<UpdateUserRoleProps> = ({
onUpdate,
handleClose,
roles,
username,
}) => {
const { t: userTrans } = useTranslation('user');
const { t: btnTrans } = useTranslation('btn');
const [allRoles, setAllRoles] = useState([]);

const [form, setForm] = useState<UpdateUserRoleParams>({
roles: roles,
});

const classes = useStyles();

const handleUpdate = async () => {
await UserHttp.updateUserRole({
username: username,
roles: form.roles,
});
onUpdate(form);
};

const fetchAllRoles = async () => {
const roles = await UserHttp.getRoles();

setAllRoles(roles.results.map((r: any) => r.role.name));
};

useEffect(() => {
fetchAllRoles();
}, []);

return (
<DialogTemplate
title={userTrans('updateRoleTitle')}
handleClose={handleClose}
confirmLabel={btnTrans('update')}
handleConfirm={handleUpdate}
confirmDisabled={false}
dialogClass={classes.dialogWrapper}
>
<>
<FormGroup row>
{allRoles.map((r: any, index: number) => (
<FormControlLabel
control={
<Checkbox
onChange={(
e: React.ChangeEvent<HTMLInputElement>,
checked: boolean
) => {
let newRoles = [...form.roles];

if (!checked) {
newRoles = newRoles.filter(
(n: string | number) => n !== r
);
} else {
newRoles.push(r);
}

setForm(v => ({ ...v, roles: [...newRoles] }));
}}
/>
}
key={index}
label={r}
value={r}
checked={form.roles.indexOf(r) !== -1}
/>
))}
</FormGroup>
</>
</DialogTemplate>
);
};

export default UpdateUserRole;
Loading