Skip to content

Commit a280566

Browse files
committed
add crude caching of roles
1 parent 6c72f80 commit a280566

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

OpenFlow/src/DBHelper.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ export class DBHelper {
5151
if (ids.indexOf(role._id) == -1) {
5252
ids.push(role._id);
5353
result.push(role);
54-
console.log(role.name + " " + role._id);
54+
// console.log(role.name + " " + role._id);
5555
const _subroles: Role[] = await this.GetRoles(role._id, ident + 1);
5656
for (let y = 0; y < _subroles.length; y++) {
5757
const subrole = _subroles[y];
58-
console.log(role.name + " " + subrole.name + " " + subrole._id);
58+
// console.log(role.name + " " + subrole.name + " " + subrole._id);
5959
if (ids.indexOf(subrole._id) == -1) {
6060
ids.push(subrole._id);
6161
result.push(subrole);
@@ -65,21 +65,33 @@ export class DBHelper {
6565
}
6666
return result;
6767
}
68+
public static cached_roles: Role[] = [];
69+
public static cached_at: Date = new Date();
6870
public static async DecorateWithRoles(user: User): Promise<void> {
6971
if (!Config.decorate_roles_fetching_all_roles) {
72+
console.log("DecorateWithRoles::begin - multiple queries");
7073
const roles: Role[] = await this.GetRoles(user._id, 0);
7174
user.roles = [];
7275
roles.forEach(role => {
7376
user.roles.push(new Rolemember(role.name, role._id));
7477
});
7578
} else {
76-
let query: any = { _type: "role" };
77-
const _roles: Role[] = await Config.db.query<Role>(query, null, Config.expected_max_roles, 0, null, "users", Crypt.rootToken());
78-
if (_roles.length === 0 && user.username !== "root") {
79+
console.log("DecorateWithRoles::begin - load all roles at once");
80+
var end: number = new Date().getTime();
81+
var seconds = Math.round((end - this.cached_at.getTime()) / 1000);
82+
if (seconds > 60) {
83+
this.cached_roles = [];
84+
}
85+
if (this.cached_roles.length == 0) {
86+
let query: any = { _type: "role" };
87+
this.cached_roles = await Config.db.query<Role>(query, { "name": 1, "members": 1 }, Config.expected_max_roles, 0, null, "users", Crypt.rootToken());
88+
this.cached_at = new Date();
89+
}
90+
if (this.cached_roles.length === 0 && user.username !== "root") {
7991
throw new Error("System has no roles !!!!!!");
8092
}
8193
user.roles = [];
82-
_roles.forEach(role => {
94+
this.cached_roles.forEach(role => {
8395
let isMember: number = -1;
8496
if (role.members !== undefined) { isMember = role.members.map(function (e: Rolemember): string { return e._id; }).indexOf(user._id); }
8597
const beenAdded: number = user.roles.map(function (e: Rolemember): string { return e._id; }).indexOf(user._id);
@@ -91,7 +103,7 @@ export class DBHelper {
91103
while (foundone) {
92104
foundone = false;
93105
user.roles.forEach(userrole => {
94-
_roles.forEach(role => {
106+
this.cached_roles.forEach(role => {
95107
let isMember: number = -1;
96108
if (role.members !== undefined) { isMember = role.members.map(function (e: Rolemember): string { return e._id; }).indexOf(userrole._id); }
97109
const beenAdded: number = user.roles.map(function (e: Rolemember): string { return e._id; }).indexOf(role._id);
@@ -103,7 +115,7 @@ export class DBHelper {
103115
});
104116
}
105117
}
106-
118+
console.log("DecorateWithRoles::end");
107119
}
108120
public static async FindRoleByName(name: string): Promise<Role> {
109121
const items: Role[] = await Config.db.query<Role>({ name: name }, null, 1, 0, null, "users", Crypt.rootToken());

OpenFlow/src/DatabaseConnection.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ export class DatabaseConnection {
762762
Base.addRight(item, item._id, item.name, [Rights.read]);
763763
item = await this.CleanACL(item, user);
764764
await this.db.collection(collectionname).replaceOne({ _id: item._id }, item);
765+
DBHelper.cached_roles = [];
765766
}
766767
DatabaseConnection.traversejsondecode(item);
767768
if (Config.log_inserts) this._logger.debug("[" + user.username + "][" + collectionname + "] inserted " + item.name);
@@ -974,6 +975,7 @@ export class DatabaseConnection {
974975
}
975976
if (q.item._type === "role" && q.collectionname === "users") {
976977
q.item = await this.Cleanmembers(q.item as any, original);
978+
DBHelper.cached_roles = [];
977979
}
978980

979981
if (q.collectionname != "fs.files") {
@@ -1153,6 +1155,9 @@ export class DatabaseConnection {
11531155
if (Config.log_updates) this._logger.debug("[" + user.username + "][" + q.collectionname + "] InsertOrUpdateOne, Inserting as new in database");
11541156
q.result = await this.InsertOne(q.item, q.collectionname, q.w, q.j, q.jwt);
11551157
}
1158+
if (q.collectionname === "users" && q.item._type === "role") {
1159+
DBHelper.cached_roles = [];
1160+
}
11561161
return q;
11571162
}
11581163
private async _DeleteFile(id: string): Promise<void> {

0 commit comments

Comments
 (0)