Skip to content

Commit

Permalink
Some bugfix for access control module (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
yang1666204 authored Aug 27, 2024
1 parent 0a2e38c commit 5954227
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 38 deletions.
7 changes: 5 additions & 2 deletions ui/src/components/customModal/HandleAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export default function HandleAccountModal({
form.submit();
} catch (err) {}
};
const { data: allRolesRes } = useRequest(access.listAllRoles);
const { data: allRolesRes } = useRequest(access.listAllRoles, {
refreshDeps: [visible],
});
const rolesOption = useMemo(() => {
return (
allRolesRes?.data.map((role) => ({
Expand Down Expand Up @@ -73,7 +75,8 @@ export default function HandleAccountModal({
roles: editValue?.roles.map((item) => item.name),
});
}
}, [type, editValue]);
}, [type, editValue, visible]);

return (
<CustomModal
title={intl.formatMessage(
Expand Down
13 changes: 12 additions & 1 deletion ui/src/components/customModal/HandleRoleModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface HandleRoleModalProps {
setVisible: (visible: boolean) => void;
successCallback?: () => void;
editValue?: AcRole;
existingRole?: string[];
type: Type;
}

Expand Down Expand Up @@ -146,6 +147,7 @@ export default function HandleRoleModal({
setVisible,
successCallback,
editValue,
existingRole,
type,
}: HandleRoleModalProps) {
const [form] = Form.useForm();
Expand All @@ -168,6 +170,15 @@ export default function HandleRoleModal({
}
});
const onFinish = async (formData: AcCreateRoleParam) => {
if (type === Type.CREATE && existingRole?.includes(formData.name)) {
message.warning(
intl.formatMessage({
id: 'src.components.customModal.08DC5F92',
defaultMessage: '角色已存在',
}),
);
return;
}
const res =
type === Type.CREATE
? await access.createRole(formData)
Expand Down Expand Up @@ -201,7 +212,7 @@ export default function HandleRoleModal({
title={intl.formatMessage(
{
id: 'src.components.customModal.1F81961E',
defaultMessage: "{ConditionalExpression0}角色",
defaultMessage: '{ConditionalExpression0}角色',
},
{
ConditionalExpression0:
Expand Down
65 changes: 40 additions & 25 deletions ui/src/components/customModal/ResetPwdModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import CustomModal from '.';
interface ResetPwdModalProps {
visible: boolean;
setVisible: (visible: boolean) => void;
showOriginPwd?: boolean;
targetUser?: string;
successCallback?: () => void;
}

export default function ResetPwdModal({
visible,
setVisible,
showOriginPwd = true,
targetUser,
successCallback,
}: ResetPwdModalProps) {
const [form] = Form.useForm();
Expand All @@ -30,7 +34,15 @@ export default function ResetPwdModal({
) => {
values.oldPassword = encryptText(values.oldPassword!, publicKey) as string;
values.password = encryptText(values.password!, publicKey) as string;
const res = await access.resetPassword(omit(values, ['confirmPassword']));
let res;
if (showOriginPwd) {
res = await access.resetPassword(omit(values, ['confirmPassword']));
} else {
// Administrator changes password
res = await access.patchAccount(targetUser!, {
password: values.password,
});
}
if (res.successful) {
message.success(
intl.formatMessage({
Expand All @@ -57,31 +69,34 @@ export default function ResetPwdModal({
}}
>
<Form form={form} onFinish={onFinish}>
<Form.Item
labelCol={{ span: 4 }}
rules={[
{
required: true,
message: intl.formatMessage({
id: 'src.components.customModal.D33CD1F7',
defaultMessage: '请输入',
}),
},
]}
label={intl.formatMessage({
id: 'src.components.customModal.ADFC0106',
defaultMessage: '原密码',
})}
name={'oldPassword'}
>
<Input
type="password"
placeholder={intl.formatMessage({
id: 'src.components.customModal.E97DEF21',
defaultMessage: '请输入',
{showOriginPwd && (
<Form.Item
labelCol={{ span: 4 }}
rules={[
{
required: true,
message: intl.formatMessage({
id: 'src.components.customModal.D33CD1F7',
defaultMessage: '请输入',
}),
},
]}
label={intl.formatMessage({
id: 'src.components.customModal.ADFC0106',
defaultMessage: '原密码',
})}
/>
</Form.Item>
name={'oldPassword'}
>
<Input
type="password"
placeholder={intl.formatMessage({
id: 'src.components.customModal.E97DEF21',
defaultMessage: '请输入',
})}
/>
</Form.Item>
)}

<Form.Item
validateFirst
labelCol={{ span: 4 }}
Expand Down
3 changes: 2 additions & 1 deletion ui/src/i18n/strings/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1140,5 +1140,6 @@
"src.pages.Alert.Subscriptions.B4AB7DEF": "Create",
"src.pages.Access.B52ED09C": "Last login time",
"src.constants.22EDA0DC": "Requires kernel version> = 4.2.1.4 (except 4.2.2.x)",
"src.components.customModal.F7DD1D45": "Scaling Zone"
"src.components.customModal.F7DD1D45": "Scaling Zone",
"src.components.customModal.08DC5F92": "Role already exists"
}
3 changes: 2 additions & 1 deletion ui/src/i18n/strings/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1139,5 +1139,6 @@
"src.api.1717A275": "无权限访问",
"src.pages.Access.B52ED09C": "最后一次登录时间",
"src.constants.22EDA0DC": "要求内核版本 >= 4.2.1.4 (4.2.2.x 除外)",
"src.components.customModal.F7DD1D45": "扩缩容Zone"
"src.components.customModal.F7DD1D45": "扩缩容Zone",
"src.components.customModal.08DC5F92": "角色已存在"
}
18 changes: 17 additions & 1 deletion ui/src/pages/Access/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default function Accounts({
const [editData, setEditData] = useState<AcAccount>();
const [modalVisible, setModalVisible] = useState<boolean>(false);
const [resetModalVisible, setResetModalVisible] = useState<boolean>(false);
const [showOriginPwd, setShowOriginPwd] = useState(true);
const [targetUser, setTargetUser] = useState('');

const { run: deleteAccount } = useRequest(accessReq.deleteAccount, {
manual: true,
onSuccess: ({ successful }) => {
Expand Down Expand Up @@ -122,7 +125,18 @@ export default function Accounts({
return (
<Space>
<Button
onClick={() => setResetModalVisible(true)}
onClick={() => {
setResetModalVisible(true);
if (
!isMyself &&
myself?.roles.some((role) => role.name === 'admin')
) {
setShowOriginPwd(false);
setTargetUser(record.username);
} else {
setShowOriginPwd(true);
}
}}
disabled={otherAdmin || (!access.acwrite && !isMyself)}
type="link"
>
Expand Down Expand Up @@ -183,6 +197,8 @@ export default function Accounts({

<ResetPwdModal
visible={resetModalVisible}
showOriginPwd={showOriginPwd}
targetUser={targetUser}
setVisible={setResetModalVisible}
/>
</div>
Expand Down
15 changes: 12 additions & 3 deletions ui/src/pages/Access/Roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ import { Type } from './type';
interface RolesProps {
allRoles: AcRole[] | undefined;
refreshRoles: () => void;
existingRoles: string[];
}

export default function Roles({ allRoles, refreshRoles }: RolesProps) {
export default function Roles({
allRoles,
refreshRoles,
existingRoles,
}: RolesProps) {
const [modalVisible, setModalVisible] = useState<boolean>(false);
const access = useAccess();
const [editData, setEditData] = useState<AcRole>();
Expand Down Expand Up @@ -91,9 +96,13 @@ export default function Roles({ allRoles, refreshRoles }: RolesProps) {
})}
</Button>
<Button
disabled={disabled}
disabled={disabled || existingRoles.includes(record.name)}
type="link"
style={disabled ? {} : { color: '#ff4b4b' }}
style={
disabled || existingRoles.includes(record.name)
? {}
: { color: '#ff4b4b' }
}
onClick={() =>
showDeleteConfirm({
title: intl.formatMessage({
Expand Down
22 changes: 20 additions & 2 deletions ui/src/pages/Access/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { useAccess } from '@umijs/max';
import { useRequest } from 'ahooks';
import type { TabsProps } from 'antd';
import { Button, Tabs } from 'antd';
import { useState } from 'react';
import { uniq } from 'lodash';

import { useMemo, useState } from 'react';
import Accounts from './Accounts';
import Roles from './Roles';
import { ActiveKey, Type } from './type';
Expand All @@ -28,6 +30,15 @@ export default function Access() {
const onChange = (key: ActiveKey) => {
setActiveKey(key);
};
const existingRoles = useMemo(() => {
return (
uniq(
allAccounts
?.map((account) => account.roles.map((role) => role.name))
.flat(),
) || []
);
}, [allAccounts]);

const items: TabsProps['items'] = [
{
Expand All @@ -46,7 +57,13 @@ export default function Access() {
id: 'src.pages.Access.FB4D558D',
defaultMessage: '角色',
}),
children: <Roles allRoles={allRoles} refreshRoles={refreshRoles} />,
children: (
<Roles
allRoles={allRoles}
refreshRoles={refreshRoles}
existingRoles={existingRoles}
/>
),
},
];

Expand Down Expand Up @@ -96,6 +113,7 @@ export default function Access() {
<HandleRoleModal
visible={modalVisible}
setVisible={setModalVisible}
existingRole={existingRoles}
type={Type.CREATE}
successCallback={refreshRoles}
/>
Expand Down
4 changes: 2 additions & 2 deletions ui/src/pages/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const OverviewPage: React.FC = () => {
}}
>
<Row justify="start" gutter={[16, 16]}>
{access && (
{access.systemread || access.systemwrite ? (
<>
<OverviewStatus />
<Col span={24}>
<EventsTable />
</Col>
<NodesTable />
</>
)}
) : null}
</Row>
</PageContainer>
);
Expand Down

0 comments on commit 5954227

Please sign in to comment.