@backstage-community/plugin-rbac-backend
Version:
75 lines (71 loc) • 2.44 kB
JavaScript
;
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: ["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: ["metadata.name", "metadata.namespace", "spec.parent"]
},
{ token }
);
return items;
}
traverse(group, allGroups, current_depth) {
const groupName = `group:${group.metadata.namespace?.toLocaleLowerCase(
"en-US"
)}/${group.metadata.name.toLocaleLowerCase("en-US")}`;
if (!super.hasEntityRef(groupName)) {
super.setNode(groupName);
}
if (this.maxDepth !== undefined && current_depth >= this.maxDepth) {
return;
}
const depth = current_depth + 1;
const parent = group.spec?.parent;
const parentGroup = allGroups.find((g) => g.metadata.name === parent);
if (parentGroup) {
const parentName = `group:${group.metadata.namespace?.toLocaleLowerCase(
"en-US"
)}/${parentGroup.metadata.name.toLocaleLowerCase("en-US")}`;
super.setEdge(parentName, groupName);
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