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

feat(mis): 白名单账户新增有效期字段,到期自动移除 #1243

Merged
merged 13 commits into from
May 16, 2024
Merged
6 changes: 6 additions & 0 deletions .changeset/chatty-sloths-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@scow/mis-web": patch
"@scow/mis-server": patch
usaveh marked this conversation as resolved.
Show resolved Hide resolved
---

getWhitelistedAccounts 新增返回字段 expirationDate,whitelistAccount 新增字段 expirationDate,在 getWhitelistedAccounts 新增每次查询会检测 中是否有账户过期,有的话会自动删除
7 changes: 7 additions & 0 deletions .changeset/flat-students-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@scow/demo-vagrant": patch
"@scow/mis-web": patch
"@scow/auth": patch
---

mis 系统下,管理员添加白名单新增白名单账户过期字段
5 changes: 5 additions & 0 deletions .changeset/plenty-avocados-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scow/grpc-api": minor
---

getWhitelistedAccounts 新增返回字段 expirationDate,whitelistAccount 新增字段 expirationDate,在 getWhitelistedAccounts 新增每次查询会检测 中是否有账户过期,有的话会自动删除
7 changes: 7 additions & 0 deletions apps/mis-server/src/entities/AccountWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ export class AccountWhitelist {
@Property()
operatorId: string;

// 当expirationTime为undefined时,即为永久有效
@Property({ nullable: true })
expirationTime?: Date;

constructor(init: {
account: EntityOrRef<Account>,
time?: Date,
comment: string
operatorId: string;
expirationTime?: Date
}) {
this.account = toRef(init.account);
this.time = init.time ?? new Date();
this.comment = init.comment;
this.operatorId = init.operatorId;
// undefined为永久有效
this.expirationTime = init.expirationTime ?? undefined;
}
}
10 changes: 10 additions & 0 deletions apps/mis-server/src/migrations/.snapshot-scow_server.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
"primary": false,
"nullable": false,
"mappedType": "string"
},
"expiration_time": {
"name": "expiration_time",
"type": "datetime",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 0,
"mappedType": "datetime"
}
},
"name": "account_whitelist",
Expand Down
13 changes: 13 additions & 0 deletions apps/mis-server/src/migrations/Migration20240515090037.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240515090037 extends Migration {

async up(): Promise<void> {
this.addSql('alter table `account_whitelist` add `expiration_time` datetime null;');
}

async down(): Promise<void> {
this.addSql('alter table `account_whitelist` drop column `expiration_time`;');
}

}
28 changes: 24 additions & 4 deletions apps/mis-server/src/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,36 @@ export const accountServiceServer = plugin((server) => {

const { tenantName } = request;

const results = await em.find(AccountWhitelist, { account: { tenant: { name: tenantName } } }, {
// 删除过期的白名单账户
const today = new Date();

// 查询所有相关信息,并删除过期的白名单账户
const results = await em.find(AccountWhitelist, {
$and: [
{ account: { tenant: { name: tenantName } } },
],
}, {
populate: ["account"],
});

// 删除过期的白名单账户
const expiredWhitelists = results.filter((x) => x.expirationTime && x.expirationTime < today);
if (expiredWhitelists.length > 0) {
await em.removeAndFlush(expiredWhitelists);
}

const owners = await em.find(UserAccount, {
account: { accountName: results.map((x) => x.account.$.accountName), tenant: { name: tenantName } },
role: EntityUserRole.OWNER,
}, { populate: ["user"]});

// 过滤结果,排除已删除的白名单账户
const validResults = results.filter((x) => !expiredWhitelists.includes(x));

return [{
accounts: results.map((x) => {
accounts: validResults.map((x) => {

const accountOwner = owners.find((o) => o.account.id === x.account.id)!.user.$;

return {
accountName: x.account.$.accountName,
comment: x.comment,
Expand All @@ -311,14 +327,16 @@ export const accountServiceServer = plugin((server) => {
ownerId: accountOwner.userId + "",
ownerName: accountOwner.name,
balance: decimalToMoney(x.account.$.balance),
expirationTime:x.expirationTime?.toISOString().includes("2099") ? undefined
: x.expirationTime?.toISOString(),
};

}),
}];
},

whitelistAccount: async ({ request, em, logger }) => {
const { accountName, comment, operatorId, tenantName } = request;
const { accountName, comment, operatorId, tenantName, expirationTime } = request;

const account = await em.findOne(Account, { accountName, tenant: { name: tenantName } },
{ populate: [ "tenant"]});
Expand All @@ -338,6 +356,8 @@ export const accountServiceServer = plugin((server) => {
time: new Date(),
comment,
operatorId,
// expirationTime为undefined时为永久有效
expirationTime:expirationTime ? new Date(expirationTime) : undefined,
});
account.whitelist = toRef(whitelist);

Expand Down
1 change: 1 addition & 0 deletions apps/mis-server/tests/admin/updateBlockStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ it("update block status with whitelist accounts", async () => {
accountName: data.blockedAccountB.accountName,
comment: "test",
operatorId: "123",
expirationTime:new Date("2025-01-01T00:00:00.000Z").toISOString(),
});

const blockedData = await updateBlockStatusInSlurm(
Expand Down
6 changes: 6 additions & 0 deletions apps/mis-server/tests/admin/whitelist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ it("unblocks account when added to whitelist", async () => {
accountName: a.accountName,
comment: "test",
operatorId: "123",
expirationTime:new Date("2025-01-01T00:00:00.000Z").toISOString(),
});

await reloadEntity(em, a);
Expand All @@ -72,6 +73,7 @@ it("blocks account when it is dewhitelisted and balance is < 0", async () => {
account: a,
comment: "",
operatorId: "123",
expirationTime:new Date("2025-01-01T00:00:00.000Z"),
});

await em.persistAndFlush(whitelist);
Expand Down Expand Up @@ -102,6 +104,7 @@ it("blocks account when it is dewhitelisted and balance is = 0", async () => {
account: a,
comment: "",
operatorId: "123",
expirationTime:new Date("2025-01-01T00:00:00.000Z"),
});

await em.persistAndFlush(whitelist);
Expand Down Expand Up @@ -134,6 +137,7 @@ it("charges user but don't block account if account is whitelist", async () => {
account : a,
comment: "123",
operatorId: "123",
expirationTime:new Date("2025-01-01T00:00:00.000Z"),
}));

await em.flush();
Expand Down Expand Up @@ -161,6 +165,7 @@ it("get whitelisted accounts", async () => {
comment: "",
operatorId: "123",
time: new Date("2023-01-01T00:00:00.000Z"),
expirationTime:new Date("2025-01-01T00:00:00.000Z"),
});

await em.persistAndFlush(whitelist);
Expand All @@ -183,6 +188,7 @@ it("get whitelisted accounts", async () => {
"comment": "",
"addTime": "2023-01-01T00:00:00.000Z",
balance: decimalToMoney(data.accountA.balance),
"expirationTime":"2025-01-01T00:00:00.000Z",
},
]);

Expand Down
11 changes: 11 additions & 0 deletions apps/mis-web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default {
amount: "Amount",
unit: "CNY",
comment: "Comment",
expirationTime:"expirationTime",

submit: "Submit",
time: "Time",
type: "Type",
Expand Down Expand Up @@ -232,6 +234,7 @@ export default {
blockThresholdAmountTooltip: "The account will be blocked "
+ "when the balance is less than the block threshold.",
comment: "Comment",
expirationTime:"expirationTime",
status: "Status",
statusTooltip: "Status:",
statusFrozenTooltip: "Frozen: The account has been frozen by the account administrator "
Expand Down Expand Up @@ -609,11 +612,19 @@ export default {
confirmRemoveWhiteText2: " from the whitelist?",
removeWhiteSuccess: "Successfully removed from the whitelist!",
removeWhite: "Remove from Whitelist",
expirationTime:"expirationTime",
},
addWhitelistedAccountButton: {
notExist: "Account does not exist!",
addSuccess: "Added successfully!",
addWhiteList: "Add Whitelisted Account",
expirationTime:"expirationTime",
custom:"Custom",
oneWeek:"One Week",
oneMonth:"One Month",
oneYear:"One Year",
permanent:"Permanent",

},
adminJobTable: {
batch: "Batch Search",
Expand Down
9 changes: 9 additions & 0 deletions apps/mis-web/src/i18n/zh_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default {
amount:"金额",
unit:"元",
comment:"备注",
expirationTime:"有效期",
submit:"提交",
time:"时间",
type:"类型",
Expand Down Expand Up @@ -232,6 +233,7 @@ export default {
blockThresholdAmountTooltip: "当账户余额低于此值时,账户将被封锁",

comment:"备注",
expirationTime:"有效期",
status:"状态",
statusTooltip: "账户状态说明",
statusFrozenTooltip: "冻结:账户已被账户管理员冻结,无法通过此账户提交作业",
Expand Down Expand Up @@ -609,11 +611,18 @@ export default {
confirmRemoveWhiteText2:"从白名单移除?",
removeWhiteSuccess:"移出白名单成功!",
removeWhite:"从白名单中去除",
expirationTime:"有效期",
},
addWhitelistedAccountButton:{
notExist:"账户不存在!",
addSuccess:"添加成功!",
addWhiteList:"添加白名单账户",
expirationTime:"有效期",
custom:"自定义",
oneWeek:"一周",
oneMonth:"一个月",
oneYear:"一年",
permanent:"永久有效",
},
adminJobTable:{
batch:"批量搜索",
Expand Down
1 change: 1 addition & 0 deletions apps/mis-web/src/models/UserSchemaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const WhitelistedAccount = Type.Object({
operatorId: Type.String(),
comment: Type.String(),
balance: Type.Optional(Money),
expirationTime:Type.Optional(Type.String({ format: "date-time" })),
});
export type WhitelistedAccount = Static<typeof WhitelistedAccount>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ export const AccountWhitelistTable: React.FC<Props> = ({
title={t(p("joinTime"))}
render={(time: string) => formatDateTime(time) }
/>
<Table.Column<WhitelistedAccount>
dataIndex="expirationTime"
title={t(p("expirationTime"))}
render={(time: string | undefined) => time ? formatDateTime(time) : "永久有效"}
/>
<Table.Column<WhitelistedAccount> dataIndex="comment" title={t(pCommon("comment"))} />
<Table.Column<WhitelistedAccount> dataIndex="operatorId" title={t(p("operatorId"))} />
<Table.Column<WhitelistedAccount>
Expand Down
Loading
Loading