207 lines
7.3 KiB
JavaScript
207 lines
7.3 KiB
JavaScript
import assert from 'assert';
|
||
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
|
||
import { cloneDeep, pull } from 'oak-domain/lib/utils/lodash';
|
||
const triggers = [
|
||
{
|
||
name: "创建provider时,填充数据",
|
||
action: "create",
|
||
when: "before",
|
||
entity: "oauthProvider",
|
||
fn: async ({ operation }, context) => {
|
||
assert(operation.data && !Array.isArray(operation.data), "oauthProvider create data 必须存在且为单条记录");
|
||
const { data } = operation;
|
||
assert(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 generateNewIdAsync(),
|
||
action: 'create',
|
||
data: {
|
||
id: await 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 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 = cloneDeep(config);
|
||
pull(newConfig?.oauthIds, id);
|
||
await context.operate('passport', {
|
||
id: await 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 });
|
||
assert(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 = cloneDeep(config);
|
||
pull(newConfig?.oauthIds, id);
|
||
if (newConfig?.oauthIds?.length <= 0) {
|
||
//无可支持的oauthProvider,将启用了的passport关闭
|
||
await context.operate('passport', {
|
||
id: await generateNewIdAsync(),
|
||
action: 'update',
|
||
data: {
|
||
enabled: false,
|
||
config: newConfig,
|
||
},
|
||
filter: {
|
||
id: passport.id,
|
||
}
|
||
}, option);
|
||
count++;
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
}
|
||
];
|
||
export default triggers;
|