oak-general-business/lib/triggers/user.js

237 lines
8.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const uuid_1 = require("oak-domain/lib/utils/uuid");
const constants_1 = require("../constants");
const randomUser_1 = require("../utils/randomUser");
const assert_1 = require("oak-domain/lib/utils/assert");
const user_1 = require("../utils/user");
let NO_ANY_USER = true;
const triggers = [
{
name: '用户的初始状态默认为shadow',
entity: 'user',
action: 'create',
when: 'before',
fn: async ({ operation }, context) => {
const { data } = operation;
const systemId = context.getSystemId();
const setData = async (userData) => {
if (!userData.userState) {
userData.userState = 'shadow';
}
if (!userData.nickname) {
userData.nickname = (0, randomUser_1.randomName)('user_', 8);
}
if (userData.password) {
//存在明文密码时校验
await (0, user_1.checkPassword)(context, userData.password);
}
if (userData.password || userData.passwordSha1) {
userData.hasPassword = true;
userData.verifyPasswordAt = Date.now();
}
else {
userData.hasPassword = false;
}
userData.userSystem$user = [
{
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
systemId,
},
},
];
};
if (data instanceof Array) {
for (const item of data) {
await setData(item);
}
}
else {
await setData(data);
}
return 1;
},
},
{
name: '当用户更新信息带有密码时将hasPassword附上true',
entity: 'user',
action: 'update',
when: 'before',
fn: async ({ operation }, context) => {
const { data, filter } = operation;
const setData = async (userData) => {
if (userData.password) {
//存在明文密码时校验
await (0, user_1.checkPassword)(context, userData.password);
}
if (userData.password || userData.passwordSha1) {
userData.hasPassword = true;
userData.verifyPasswordAt = Date.now();
}
};
if (data instanceof Array) {
for (const item of data) {
await setData(item);
}
}
else {
await setData(data);
}
return 1;
},
},
{
name: '系统生成的第一个用户默认注册为root用户的初始状态默认为shadow',
entity: 'user',
action: 'create',
when: 'before',
fn: async ({ operation }, context) => {
const { data } = operation;
(0, assert_1.assert)(!(data instanceof Array));
if (NO_ANY_USER) {
const result = await context.select('user', {
data: {
id: 1,
},
filter: {
isRoot: true,
id: {
$ne: constants_1.ROOT_USER_ID,
},
},
indexFrom: 0,
count: 1,
}, {
dontCollect: true,
});
if (result.length === 0) {
Object.assign(data, {
isRoot: true,
});
}
else {
NO_ANY_USER = false;
}
}
return 0;
},
},
{
name: '当用户被激活时将所有的parasite作废',
entity: 'user',
action: 'activate',
when: 'after',
fn: async ({ operation }, context) => {
const { data, filter } = operation;
(0, assert_1.assert)(!(data instanceof Array));
const parasiteList = await context.select('parasite', {
data: {
id: 1,
},
filter: {
user: filter,
expired: false,
},
}, {});
const parasiteIds = parasiteList.map((ele) => ele.id);
if (parasiteIds.length > 0) {
await context.operate('parasite', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
expired: true,
},
filter: {
id: {
$in: parasiteIds,
},
},
}, { blockTrigger: true });
await context.operate('token', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'disable',
data: {},
filter: {
ableState: 'enabled',
entity: 'parasite',
entityId: {
$in: parasiteIds,
},
},
}, { blockTrigger: true });
}
// operation的级联写法目前不能正确解析上层对象对下层对象的filter关系
// data.parasite$user = {
// id: await generateNewIdAsync(),
// action: 'update',
// data: {
// expired: true,
// token$entity: {
// id: await generateNewIdAsync(),
// action: 'disable',
// data: {},
// filter: {
// ableState: 'enabled',
// parasite: {
// userId: {
// $in: {
// entity: 'user',
// data: {
// id: 1,
// },
// filter,
// }
// }
// }
// }
// },
// },
// filter: {
// userId: {
// $in: {
// entity: 'user',
// data: {
// id: 1,
// },
// filter,
// }
// }
// }
// };
return 1;
},
},
{
name: '当用户实名认证时,根据系统设置是否需要人工检查,未设置默认通过',
entity: 'user',
action: 'verify',
when: 'after',
fn: async ({ operation }, context) => {
const { data, filter } = operation;
const systemConfig = context.getApplication()?.system?.config;
const needManualVerification = systemConfig?.App?.needManualVerification;
if (!needManualVerification) {
const closeRootMode = context.openRootMode();
try {
await context.operate('user', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'accept',
data: {},
filter,
}, {});
closeRootMode();
}
catch (err) {
closeRootMode();
throw err;
}
}
return 1;
},
},
];
exports.default = triggers;