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

210 lines
7.6 KiB
JavaScript
Raw 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 tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const uuid_1 = require("oak-domain/lib/utils/uuid");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const triggers = [
{
name: "创建provider时填充数据",
action: "create",
when: "before",
entity: "oauthProvider",
fn: async ({ operation }, context) => {
(0, assert_1.default)(operation.data && !Array.isArray(operation.data), "oauthProvider create data 必须存在且为单条记录");
const { data } = operation;
(0, assert_1.default)(data.systemId, "oauthProvider 创建时必须指定 systemId");
return 0;
}
},
{
name: "创建provider时新增passport",
action: "create",
when: "after",
entity: "oauthProvider",
asRoot: true,
fn: async ({ operation }, context, option) => {
const { data } = operation;
let count = 0;
if (data?.systemId) {
const [passport] = await context.select('passport', {
data: {
id: 1,
systemId: 1,
type: 1,
},
filter: {
systemId: data?.systemId,
type: 'oauth',
},
indexFrom: 0,
count: 1,
}, { forUpdate: true });
if (!passport) {
await context.operate('passport', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
type: 'oauth',
enabled: false,
systemId: data?.systemId,
config: {
oauthIds: [data?.id],
}
}
}, option);
count++;
}
}
return count;
}
},
{
name: "删除provider时删除passport",
action: "remove",
when: "before",
entity: "oauthProvider",
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const [oauthProvider] = await context.select('oauthProvider', {
data: {
id: 1,
systemId: 1,
},
filter: filter,
}, { forUpdate: true });
let count = 0;
if (oauthProvider) {
const { id, systemId } = oauthProvider;
const [other] = await context.select('oauthProvider', {
data: {
id: 1,
systemId: 1,
},
filter: {
systemId,
id: {
$ne: id,
}
},
indexFrom: 0,
count: 1,
}, { forUpdate: true });
if (!other) {
await context.operate('passport', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'remove',
data: {},
filter: {
systemId,
type: 'oauth'
}
}, option);
count++;
}
else {
const [passport] = await context.select('passport', {
data: {
id: 1,
type: 1,
config: 1,
systemId: 1,
},
filter: {
type: 'oauth',
systemId,
config: {
oauthIds: {
$contains: [id],
}
}
}
}, { forUpdate: true });
if (passport) {
const { id: passportId, config } = passport;
let newConfig = (0, lodash_1.cloneDeep)(config);
(0, lodash_1.pull)(newConfig?.oauthIds, id);
await context.operate('passport', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
config: newConfig,
},
filter: {
id: passportId,
}
}, option);
}
}
}
return count;
}
},
{
name: '当provider禁用时更新passport',
entity: 'oauthProvider',
action: 'update',
when: 'after',
check: (operation) => {
const { data } = operation;
return data.hasOwnProperty('ableState') && data.ableState === 'disabled';
},
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const [oauthProvider] = await context.select('oauthProvider', {
data: {
id: 1,
systemId: 1,
ableState: 1,
},
filter: filter,
}, { forUpdate: true });
(0, assert_1.default)(oauthProvider, '禁用oauthProvider的filter请勿包含abledState');
let count = 0;
const { id, systemId } = oauthProvider;
const [passport] = await context.select('passport', {
data: {
id: 1,
type: 1,
config: 1,
systemId: 1,
enabled: 1,
},
filter: {
type: 'oauth',
systemId,
config: {
oauthIds: {
$contains: [id],
}
}
}
}, { forUpdate: true });
if (passport && passport.enabled) {
const { id: passportId, config } = passport;
let newConfig = (0, lodash_1.cloneDeep)(config);
(0, lodash_1.pull)(newConfig?.oauthIds, id);
if (newConfig?.oauthIds?.length <= 0) {
//无可支持的oauthProvider将启用了的passport关闭
await context.operate('passport', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
enabled: false,
config: newConfig,
},
filter: {
id: passport.id,
}
}, option);
count++;
}
}
return count;
}
}
];
exports.default = triggers;