feat: 增加用户封禁标识与断开连接功能

pull/70/head
moonrailgun 2 years ago
parent 7fb9ddc4b5
commit e8a705dad7

@ -1,4 +1,5 @@
{
"k127fc33c": "User banned",
"k158d2868": "No delete permission",
"k16605863": "Token content is incorrect",
"k17f8532": "No message found",

@ -1,4 +1,5 @@
{
"k127fc33c": "用户被封禁",
"k158d2868": "没有删除权限",
"k16605863": "Token 内容不正确",
"k17f8532": "没有找到消息",

@ -397,6 +397,27 @@ export const TcSocketIOService = (
},
},
/**
*
*/
tickUser: {
visibility: 'public',
params: {
userId: 'string',
},
async handler(this: TcService, ctx: TcContext<{ userId: string }>) {
const userId = ctx.params.userId;
const io: SocketServer = this.io;
const remoteSockets = await io
.in(buildUserRoomId(userId))
.fetchSockets();
remoteSockets.forEach((remoteSocket) => {
remoteSocket.disconnect(true);
});
},
},
/**
*
*/

@ -106,6 +106,14 @@ export class User extends TimeStamps implements Base {
})
emailVerified: boolean;
/**
*
*/
@prop({
default: false,
})
banned: boolean;
/**
*
*/

@ -2,6 +2,7 @@ import { TcCacheCleaner } from '../../../mixins/cache.cleaner.mixin';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import type {
User,
UserDocument,
UserLoginRes,
UserModel,
@ -19,6 +20,7 @@ import {
EntityError,
db,
call,
NoPermissionError,
} from 'tailchat-server-sdk';
import {
generateRandomNumStr,
@ -282,10 +284,15 @@ class UserService extends TcService {
}
const res = await this.comparePassword(password, user.password);
if (!res)
if (!res) {
throw new EntityError(t('密码错误'), 422, '', [
{ field: 'password', message: t('密码错误') },
]);
}
if (user.banned === true) {
throw new NoPermissionError(t('用户被封禁'), 403);
}
// Transform user entity (remove password and all protected fields)
const doc = await this.transformDocuments(ctx, {}, user);
@ -630,7 +637,12 @@ class UserService extends TcService {
throw new EntityError(t('Token 内容不正确'));
}
const doc = await this.getById(decoded._id);
const user = await this.transformDocuments(ctx, {}, doc);
const user: User = await this.transformDocuments(ctx, {}, doc);
if (user.banned === true) {
throw new NoPermissionError(t('用户被封禁'));
}
const json = await this.transformEntity(user, true, ctx.meta.token);
return json;
}

Loading…
Cancel
Save