Skip to content

Commit

Permalink
fix: 使用外部认证系统时,外部系统未实现的功能在用户使用时提示用户功能未实现 (#1135)
Browse files Browse the repository at this point in the history
## 使用外部认证系统时,外部系统未实现的功能在用户使用时提示用户功能未实现
### 主要是增加了修改密码、修改邮箱、新增用户的提示
  • Loading branch information
OYX-1 authored Feb 23, 2024
1 parent a535126 commit 08359cb
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .changeset/heavy-flies-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@scow/mis-server": patch
"@scow/portal-web": patch
"@scow/mis-web": patch
"@scow/auth": patch
---

使用外部认证系统时,外部系统未实现的功能在用户使用时提示用户功能未实现
5 changes: 1 addition & 4 deletions apps/auth/src/routes/changeEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ export const changeEmailRoute = fp(async (f) => {
},
async (req, rep) => {


if (!f.auth.changeEmail) {
return await rep.code(501).send(null);
return await rep.code(501).send({ code: "NOT_SUPPORTED" });
}



const { identityId, newEmail } = req.body;

const result = await f.auth.changeEmail(identityId, newEmail, req);
Expand Down
2 changes: 1 addition & 1 deletion apps/auth/src/routes/changePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const changePasswordRoute = fp(async (f) => {
async (req, rep) => {

if (!f.auth.changePassword) {
return await rep.code(501).send(null);
return await rep.code(501).send({ code: "NOT_SUPPORTED" });
}

const { identityId, newPassword } = req.body;
Expand Down
2 changes: 1 addition & 1 deletion apps/auth/src/routes/checkPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const checkPasswordRoute = fp(async (f) => {
},
async (req, rep) => {
if (!f.auth.checkPassword) {
return await rep.code(501).send(null);
return await rep.code(501).send({ code: "NOT_SUPPORTED" });
}

const { identityId, password } = req.query;
Expand Down
2 changes: 1 addition & 1 deletion apps/auth/src/routes/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const createUserRoute = fp(async (f) => {
},
async (req, rep) => {
if (!f.auth.createUser) {
return await rep.code(501).send(null);
return await rep.code(501).send({ code: "NOT_SUPPORTED" });
}

const { ...rest } = req.body;
Expand Down
2 changes: 1 addition & 1 deletion apps/auth/src/routes/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const getUserRoute = fp(async (f) => {
},
async (req, rep) => {
if (!f.auth.getUser) {
return await rep.code(501).send(null);
return await rep.code(501).send({ code: "NOT_SUPPORTED" });
}

const { identityId } = req.query;
Expand Down
22 changes: 18 additions & 4 deletions apps/mis-server/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,24 @@ export const userServiceServer = plugin((server) => {
identityId: userId,
newEmail,
}, logger)
.catch(async () => {
throw <ServiceError> {
code: Status.UNKNOWN, message: "LDAP failed to change email",
};
.catch(async (e) => {
switch (e.status) {

case "NOT_FOUND":
throw <ServiceError>{
code: Status.NOT_FOUND, message: `User ${userId} is not found.`,
};

case "NOT_SUPPORTED":
throw <ServiceError>{
code: Status.UNIMPLEMENTED, message: "Changing email is not supported ",
};

default:
throw <ServiceError> {
code: Status.UNKNOWN, message: "LDAP failed to change email",
};
}
});
}

Expand Down
3 changes: 3 additions & 0 deletions apps/mis-web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ export default {
oldPassword: "Old Password",
newPassword: "New Password",
confirmPassword: "Confirm Password",
userNotExist:"User Not Exist",
unavailable:"This feature is not available in the current configuration",
},
tenant: {
accountWhitelistTable: {
Expand Down Expand Up @@ -904,6 +906,7 @@ export default {
addCompleted: "Added Successfully!",
createTenantFailMessage: "Failed to create tenant",
createTenant: "Create Tenant",
unavailable:"This feature is not available in the current configuration",
},
},
systemDebug: {
Expand Down
3 changes: 3 additions & 0 deletions apps/mis-web/src/i18n/zh_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ export default {
oldPassword:"原密码",
newPassword:"新密码",
confirmPassword:"确认密码",
userNotExist:"用户不存在",
unavailable:"本功能在当前配置下不可用",
},
tenant:{
accountWhitelistTable:{
Expand Down Expand Up @@ -904,6 +906,7 @@ export default {
addCompleted: "添加完成!",
createTenantFailMessage: "创建租户失败",
createTenant: "创建租户",
unavailable:"本功能在当前配置下不可用",
},
},
systemDebug: {
Expand Down
2 changes: 2 additions & 0 deletions apps/mis-web/src/pageComponents/profile/ChangeEmailModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export const ChangeEmailModal: React.FC<Props> = ({
setLoading(true);

await api.changeEmail({ body: { userId:userStore.user?.identityId as string, newEmail } })
.httpError(404, () => { message.error(t(p("userNotExist"))); })
.httpError(500, () => { message.error(t(p("changeEmailFail"))); })
.httpError(501, () => { message.error(t(p("unavailable"))); })
.then(() => {
form.resetFields();
form.setFieldValue("oldEmail", newEmail);
Expand Down
13 changes: 12 additions & 1 deletion apps/mis-web/src/pageComponents/profile/ChangePasswordModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ export const ChangePasswordModal: React.FC<Props> = ({
const { oldPassword, newPassword } = await form.validateFields();
setLoading(true);
api.checkPassword({ query: { password: oldPassword } })
.httpError(404, () => {
message.error(t(p("userNotExist")));
})
.httpError(501, () => {
message.error(t(p("unavailable")));
})
.then((result) => {
if (result.success) {
return api.changePassword({ body: { newPassword } })
.httpError(404, () => {
message.error(t(p("userNotExist")));
})
.httpError(501, () => {
message.error(t(p("unavailable")));
})
.httpError(400, (e) => {
if (e.code === "PASSWORD_NOT_VALID") {
message.error(getRuntimeI18nConfigText(languageId, "passwordPatternMessage"));
};
throw e;
})
.then(() => {
form.resetFields();
Expand Down
33 changes: 18 additions & 15 deletions apps/mis-web/src/pages/admin/tenants/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,25 @@ const CreateTenantPageForm: React.FC = () => {
userEmail,
userPassword,
},
}).httpError(409, (e) => {
modal.error({
title: t("common.addFail"),
content: t(p("existInSCOWDatabase"),
[e.code === "TENANT_ALREADY_EXISTS" ? t("common.tenant") : t("common.user")]),
okText: t("common.ok"),
});
}).httpError(400, (e) => {
if (e.code === "USERID_NOT_VALID") {
message.error(userIdRule?.message);
};
if (e.code === "PASSWORD_NOT_VALID") {
message.error(getRuntimeI18nConfigText(languageId, "passwordPatternMessage"));
};
throw e;
})
.httpError(409, (e) => {
modal.error({
title: t("common.addFail"),
content: t(p("existInSCOWDatabase"),
[e.code === "TENANT_ALREADY_EXISTS" ? t("common.tenant") : t("common.user")]),
okText: t("common.ok"),
});
})
.httpError(400, (e) => {
if (e.code === "USERID_NOT_VALID") {
message.error(userIdRule?.message);
};
if (e.code === "PASSWORD_NOT_VALID") {
message.error(getRuntimeI18nConfigText(languageId, "passwordPatternMessage"));
};
throw e;
})
.httpError(501, () => { message.error(t(p("unavailable"))); })
.then((createdInAuth) => {
!createdInAuth.createdInAuth ?
modal.info({
Expand Down
7 changes: 6 additions & 1 deletion apps/mis-web/src/pages/api/admin/changePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { changePassword as libChangePassword } from "@scow/lib-auth";
import { changePassword as libChangePassword, getCapabilities } from "@scow/lib-auth";
import { Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
import { OperationResult, OperationType } from "src/models/operationLog";
Expand Down Expand Up @@ -56,6 +56,11 @@ export default /* #__PURE__*/typeboxRoute(
return { 501: null };
}

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.changePassword) {
return { 501: null };
}

const auth = authenticate((info) => info.platformRoles.includes(PlatformRole.PLATFORM_ADMIN));

const info = await auth(req, res);
Expand Down
11 changes: 11 additions & 0 deletions apps/mis-web/src/pages/api/profile/changeEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { asyncClientCall } from "@ddadaal/tsgrpc-client";
import { Status } from "@grpc/grpc-js/build/src/constants";
import { getCapabilities } from "@scow/lib-auth";
import { UserServiceClient } from "@scow/protos/build/server/user";
import { Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
import { getClient } from "src/utils/client";
import { runtimeConfig } from "src/utils/config";
import { handlegRPCError } from "src/utils/server";


Expand All @@ -40,6 +42,9 @@ export const ChangeEmailSchema = typeboxRouteSchema({

/** 修改失败 */
500: Type.Null(),

/** 本功能在当前配置下不可用。 */
501: Type.Null(),
},
});

Expand All @@ -50,6 +55,11 @@ export default /* #__PURE__*/typeboxRoute(ChangeEmailSchema, async (req, res) =>

if (!info) { return; }

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.changeEmail) {
return { 501: null };
}

const { userId, newEmail } = req.body;

const client = getClient(UserServiceClient);
Expand All @@ -62,5 +72,6 @@ export default /* #__PURE__*/typeboxRoute(ChangeEmailSchema, async (req, res) =>
.catch(handlegRPCError({
[Status.NOT_FOUND]: () => ({ 404: null }),
[Status.UNKNOWN]: () => ({ 500: null }),
[Status.UNIMPLEMENTED]: () => ({ 501: null }),
}));
});
7 changes: 6 additions & 1 deletion apps/mis-web/src/pages/api/profile/changePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { changePassword as libChangePassword } from "@scow/lib-auth";
import { changePassword as libChangePassword, getCapabilities } from "@scow/lib-auth";
import { Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
import { publicConfig, runtimeConfig } from "src/utils/config";
Expand Down Expand Up @@ -49,6 +49,11 @@ export default /* #__PURE__*/typeboxRoute(ChangePasswordSchema, async (req, res)
return { 501: null };
}

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.changePassword) {
return { 501: null };
}

const auth = authenticate(() => true);

const info = await auth(req, res);
Expand Down
7 changes: 6 additions & 1 deletion apps/mis-web/src/pages/api/profile/checkPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { checkPassword as libCheckPassword } from "@scow/lib-auth";
import { checkPassword as libCheckPassword, getCapabilities } from "@scow/lib-auth";
import { Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
import { runtimeConfig } from "src/utils/config";
Expand Down Expand Up @@ -40,6 +40,11 @@ export default typeboxRoute(CheckPasswordSchema, async (req, res) => {

if (!info) { return; }

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.checkPassword) {
return { 501: null };
}

const { password } = req.query;

return await libCheckPassword(runtimeConfig.AUTH_INTERNAL_URL, {
Expand Down
7 changes: 6 additions & 1 deletion apps/mis-web/src/pages/api/tenant/changePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { asyncClientCall } from "@ddadaal/tsgrpc-client";
import { changePassword as libChangePassword } from "@scow/lib-auth";
import { changePassword as libChangePassword, getCapabilities } from "@scow/lib-auth";
import { GetUserInfoResponse, UserServiceClient } from "@scow/protos/build/server/user";
import { Type } from "@sinclair/typebox";
import { authenticate } from "src/auth/server";
Expand Down Expand Up @@ -59,6 +59,11 @@ export default /* #__PURE__*/typeboxRoute(
return { 501: null };
}

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.changePassword) {
return { 501: null };
}

const { identityId, newPassword } = req.body;

const client = getClient(UserServiceClient);
Expand Down
11 changes: 9 additions & 2 deletions apps/mis-web/src/pages/api/tenant/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { typeboxRoute, typeboxRouteSchema } from "@ddadaal/next-typed-api-routes-runtime";
import { asyncClientCall } from "@ddadaal/tsgrpc-client";
import { status } from "@grpc/grpc-js";
import { getCapabilities } from "@scow/lib-auth";
import { getCurrentLanguageId } from "@scow/lib-web/build/utils/systemLanguage";
import { TenantServiceClient } from "@scow/protos/build/server/tenant";
import { Type } from "@sinclair/typebox";
Expand All @@ -21,7 +22,7 @@ import { OperationResult, OperationType } from "src/models/operationLog";
import { PlatformRole } from "src/models/User";
import { callLog } from "src/server/operationLog";
import { getClient } from "src/utils/client";
import { publicConfig } from "src/utils/config";
import { publicConfig, runtimeConfig } from "src/utils/config";
import { getUserIdRule } from "src/utils/createUser";
import { handlegRPCError, parseIp } from "src/utils/server";

Expand Down Expand Up @@ -57,14 +58,20 @@ export const CreateTenantSchema = typeboxRouteSchema({
message: Type.String(),
}),

500: Type.Null(),
/** 本功能在当前配置下不可用。 */
501: Type.Null(),
},
});

const passwordPattern = publicConfig.PASSWORD_PATTERN && new RegExp(publicConfig.PASSWORD_PATTERN);

export default /* #__PURE__*/typeboxRoute(CreateTenantSchema, async (req, res) => {

const ldapCapabilities = await getCapabilities(runtimeConfig.AUTH_INTERNAL_URL);
if (!ldapCapabilities.createUser) {
return { 501: null };
}

const { tenantName, userId, userName, userEmail, userPassword } = req.body;

const languageId = getCurrentLanguageId(req, publicConfig.SYSTEM_LANGUAGE_CONFIG);
Expand Down
2 changes: 2 additions & 0 deletions apps/portal-web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export default {
oldPassword: "Old Password",
newPassword: "New Password",
confirm: "Confirm Password",
userNotExist:"User Not Exist",
unavailable:"This feature is not available in the current configuration",
},
},
// job
Expand Down
2 changes: 2 additions & 0 deletions apps/portal-web/src/i18n/zh_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export default {
oldPassword:"原密码",
newPassword: "新密码",
confirm: "确认密码",
userNotExist:"用户不存在",
unavailable:"本功能在当前配置下不可用",
},
},
// job
Expand Down
Loading

0 comments on commit 08359cb

Please sign in to comment.