@backstage-community/plugin-rbac-backend
Version:
82 lines (78 loc) • 2.52 kB
JavaScript
;
var catalogModel = require('@backstage/catalog-model');
var ancestorSearchMemo = require('./ancestor-search-memo.cjs.js');
class AncestorSearchMemoSQLite extends ancestorSearchMemo.AncestorSearchMemo {
constructor(userEntityRef, catalogApi, auth, maxDepth) {
super();
this.userEntityRef = userEntityRef;
this.catalogApi = catalogApi;
this.auth = auth;
this.maxDepth = maxDepth;
}
async getAllASMGroups() {
const { token } = await this.auth.getPluginRequestToken({
onBehalfOf: await this.auth.getOwnServiceCredentials(),
targetPluginId: "catalog"
});
const { items } = await this.catalogApi.getEntities(
{
filter: { kind: "Group" },
fields: ["kind", "metadata.name", "metadata.namespace", "spec.parent"]
},
{ token }
);
return items;
}
async getUserASMGroups() {
const { token } = await this.auth.getPluginRequestToken({
onBehalfOf: await this.auth.getOwnServiceCredentials(),
targetPluginId: "catalog"
});
const { items } = await this.catalogApi.getEntities(
{
filter: { kind: "Group", "relations.hasMember": this.userEntityRef },
fields: ["kind", "metadata.name", "metadata.namespace", "spec.parent"]
},
{ token }
);
return items;
}
traverse(group, allGroups, current_depth) {
const groupRef = catalogModel.stringifyEntityRef(group);
if (!super.hasEntityRef(groupRef)) {
super.setNode(groupRef);
}
if (this.maxDepth !== void 0 && current_depth >= this.maxDepth) {
return;
}
const depth = current_depth + 1;
const parent = group.spec?.parent;
if (!parent) {
return;
}
const parentRef = catalogModel.stringifyEntityRef(
catalogModel.parseEntityRef(parent, {
defaultKind: "group",
defaultNamespace: group.metadata.namespace
})
);
const parentGroup = allGroups.find(
(g) => catalogModel.stringifyEntityRef(g) === parentRef
);
if (parentGroup) {
super.setEdge(parentRef, groupRef);
if (super.isAcyclic()) {
this.traverse(parentGroup, allGroups, depth);
}
}
}
async buildUserGraph() {
const userGroups = await this.getUserASMGroups();
const allGroups = await this.getAllASMGroups();
userGroups.forEach(
(group) => this.traverse(group, allGroups, 0)
);
}
}
exports.AncestorSearchMemoSQLite = AncestorSearchMemoSQLite;
//# sourceMappingURL=ancestor-search-memo-sqlite.cjs.js.map