201 lines
9.2 KiB
JavaScript
201 lines
9.2 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.getUserIdsByActionAuth = exports.mergeUser = void 0;
|
||
var tslib_1 = require("tslib");
|
||
var types_1 = require("oak-domain/lib/types");
|
||
var uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
var lodash_1 = require("oak-domain/lib/utils/lodash");
|
||
var assert_1 = tslib_1.__importDefault(require("assert"));
|
||
function mergeUser(params, context, innerLogic) {
|
||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||
var from, to, schema, _a, _b, _c, _d, _e, _f;
|
||
var _g, _h;
|
||
return tslib_1.__generator(this, function (_j) {
|
||
switch (_j.label) {
|
||
case 0:
|
||
if (!innerLogic && !context.isRoot()) {
|
||
throw new types_1.OakUserUnpermittedException('不允许执行mergeUser操作');
|
||
}
|
||
from = params.from, to = params.to;
|
||
(0, assert_1.default)(from);
|
||
(0, assert_1.default)(to);
|
||
(0, assert_1.default)(from !== to, '不能merge到相同user');
|
||
schema = context.getSchema();
|
||
_b = (_a = context).operate;
|
||
_c = ['token'];
|
||
_g = {};
|
||
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
|
||
case 1:
|
||
/* for (const entity in schema) {
|
||
if (['oper', 'modi', 'operEntity', 'modiEntity', 'userEntityGrant', 'wechatQrCode'].includes(entity)) {
|
||
continue;
|
||
}
|
||
|
||
const entityDesc = schema[entity];
|
||
if (entityDesc.view) {
|
||
continue;
|
||
}
|
||
const { attributes } = entityDesc;
|
||
for (const attr in attributes) {
|
||
const attrDef = attributes[attr as keyof typeof attributes];
|
||
if (attrDef.type === 'ref' && attrDef.ref === 'user') {
|
||
await context.operate(entity, {
|
||
action: 'update',
|
||
data: {
|
||
[attr]: to,
|
||
},
|
||
filter: {
|
||
[attr]: from,
|
||
}
|
||
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
|
||
}
|
||
if (attr === 'entity' && attributes.hasOwnProperty('entityId')) {
|
||
await context.operate(entity, {
|
||
action: 'update',
|
||
data: {
|
||
entityId: to,
|
||
},
|
||
filter: {
|
||
entity: 'user',
|
||
entityId: from,
|
||
}
|
||
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
|
||
}
|
||
}
|
||
} */
|
||
return [4 /*yield*/, _b.apply(_a, _c.concat([(_g.id = _j.sent(),
|
||
_g.action = 'disable',
|
||
_g.data = {},
|
||
_g.filter = {
|
||
ableState: 'enabled',
|
||
playerId: from, // todo 这里是playerId, root如果正在扮演该用户待处理
|
||
},
|
||
_g), { dontCollect: true }]))];
|
||
case 2:
|
||
/* for (const entity in schema) {
|
||
if (['oper', 'modi', 'operEntity', 'modiEntity', 'userEntityGrant', 'wechatQrCode'].includes(entity)) {
|
||
continue;
|
||
}
|
||
|
||
const entityDesc = schema[entity];
|
||
if (entityDesc.view) {
|
||
continue;
|
||
}
|
||
const { attributes } = entityDesc;
|
||
for (const attr in attributes) {
|
||
const attrDef = attributes[attr as keyof typeof attributes];
|
||
if (attrDef.type === 'ref' && attrDef.ref === 'user') {
|
||
await context.operate(entity, {
|
||
action: 'update',
|
||
data: {
|
||
[attr]: to,
|
||
},
|
||
filter: {
|
||
[attr]: from,
|
||
}
|
||
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
|
||
}
|
||
if (attr === 'entity' && attributes.hasOwnProperty('entityId')) {
|
||
await context.operate(entity, {
|
||
action: 'update',
|
||
data: {
|
||
entityId: to,
|
||
},
|
||
filter: {
|
||
entity: 'user',
|
||
entityId: from,
|
||
}
|
||
} as any, { dontCollect: true, dontCreateOper: true, dontCreateModi: true });
|
||
}
|
||
}
|
||
} */
|
||
_j.sent();
|
||
_e = (_d = context).operate;
|
||
_f = ['user'];
|
||
_h = {};
|
||
return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
|
||
case 3: return [4 /*yield*/, _e.apply(_d, _f.concat([(_h.id = _j.sent(),
|
||
_h.action = 'merge',
|
||
_h.data = {
|
||
refId: to,
|
||
userState: 'merged',
|
||
},
|
||
_h.filter = {
|
||
$or: [
|
||
{
|
||
id: from,
|
||
},
|
||
{
|
||
userState: 'merged',
|
||
refId: from,
|
||
}
|
||
],
|
||
},
|
||
_h), {}]))];
|
||
case 4:
|
||
_j.sent();
|
||
return [2 /*return*/];
|
||
}
|
||
});
|
||
});
|
||
}
|
||
exports.mergeUser = mergeUser;
|
||
/**
|
||
* 获取有对entity进行actions操作权限的用户Id(不包含root)
|
||
* @param params
|
||
* @param context
|
||
*/
|
||
function getUserIdsByActionAuth(params, context) {
|
||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||
var entity, entityId, actions, overlap, filter, actionAuths, userRelations;
|
||
return tslib_1.__generator(this, function (_a) {
|
||
switch (_a.label) {
|
||
case 0:
|
||
entity = params.entity, entityId = params.entityId, actions = params.actions, overlap = params.overlap;
|
||
filter = {
|
||
destEntity: entity,
|
||
relation: {
|
||
entity: entity,
|
||
entityId: entityId,
|
||
},
|
||
};
|
||
if (overlap) {
|
||
Object.assign(filter, {
|
||
deActions: {
|
||
$overlaps: actions,
|
||
},
|
||
});
|
||
}
|
||
else {
|
||
Object.assign(filter, {
|
||
deActions: {
|
||
$contains: actions,
|
||
},
|
||
});
|
||
}
|
||
return [4 /*yield*/, context.select('actionAuth', {
|
||
data: {
|
||
id: 1,
|
||
relation: {
|
||
id: 1,
|
||
userRelation$relation: {
|
||
$entity: 'userRelation',
|
||
data: {
|
||
id: 1,
|
||
userId: 1,
|
||
},
|
||
},
|
||
},
|
||
},
|
||
filter: filter,
|
||
}, { dontCollect: true })];
|
||
case 1:
|
||
actionAuths = _a.sent();
|
||
userRelations = actionAuths.map(function (ele) { return ele.relation.userRelation$relation; });
|
||
return [2 /*return*/, (0, lodash_1.uniq)(Array.prototype.concat.apply([], userRelations).map(function (ele) { return ele.userId; }))];
|
||
}
|
||
});
|
||
});
|
||
}
|
||
exports.getUserIdsByActionAuth = getUserIdsByActionAuth;
|