Skip to content

Commit 63462b4

Browse files
committed
Small optimizations
Signed-off-by: Mike Lischke <mike@lischke-online.de>
1 parent b02f2d0 commit 63462b4

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

src/server/Auth.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ interface IEntityGroupEntry {
5555
interface IResolvedPermission {
5656
/** The effective owner id, or null if no owner is set anywhere in the chain. */
5757
ownerId: number | null;
58-
/** All group assignments, collected from the entity and all ancestors. */
59-
groupEntries: IEntityGroupEntry[];
58+
/** All group assignments, collected from the entity and all ancestors (groupId → writable). */
59+
groupEntries: Map<number, boolean>;
6060
}
6161

6262
export interface ITokenPayload {
@@ -132,8 +132,8 @@ export class Auth {
132132

133133
private static readonly accessTokenExpiry = "15m";
134134

135-
private static scryptKeyLen = 64;
136-
private static scryptOptions = { N: 16384, r: 8, p: 1 };
135+
private static readonly scryptKeyLen = 64;
136+
private static readonly scryptOptions = { N: 16384, r: 8, p: 1 };
137137

138138
private static jwtSecret: string = (() => {
139139
// eslint-disable-next-line no-restricted-syntax
@@ -145,6 +145,8 @@ export class Auth {
145145
return secret;
146146
})();
147147

148+
private static readonly noUser = "anonymous";
149+
148150
private currentAdapter: IDatabaseAdapter;
149151

150152
public constructor(adapter: IDatabaseAdapter) {
@@ -261,7 +263,7 @@ export class Auth {
261263
*/
262264
public async hasUsers(): Promise<boolean> {
263265
const rows = await this.adapter.query<{ cnt: number; }>(
264-
"SELECT COUNT(*) AS cnt FROM users WHERE username != 'anonymous'",
266+
"SELECT COUNT(*) AS cnt FROM users WHERE username != ?", [Auth.noUser],
265267
);
266268

267269
return (rows[0]?.cnt ?? 0) > 0;
@@ -362,8 +364,8 @@ export class Auth {
362364
}
363365

364366
// Check group assignments.
365-
for (const entry of resolved.groupEntries) {
366-
if (!userGroupIds.has(entry.groupId)) {
367+
for (const [groupId, writable] of resolved.groupEntries) {
368+
if (!userGroupIds.has(groupId)) {
367369
continue;
368370
}
369371

@@ -373,7 +375,7 @@ export class Auth {
373375
}
374376

375377
// Write requires the writable flag.
376-
if (entry.writable) {
378+
if (writable) {
377379
return true;
378380
}
379381
}
@@ -424,26 +426,21 @@ export class Auth {
424426
let canRead = admin || isOwner;
425427
let canWrite = admin || isOwner;
426428

427-
const isWorld = worldId !== undefined
428-
&& resolved.groupEntries.some((e) => {
429-
return e.groupId === worldId;
430-
});
429+
const isWorld = worldId !== undefined && resolved.groupEntries.has(worldId);
431430

432-
for (const entry of resolved.groupEntries) {
433-
if (!userGroupIds.has(entry.groupId)) {
431+
for (const [groupId, writable] of resolved.groupEntries) {
432+
if (!userGroupIds.has(groupId)) {
434433
continue;
435434
}
436435

437436
canRead = true;
438437

439-
if (entry.writable) {
438+
if (writable) {
440439
canWrite = true;
441440
}
442441
}
443442

444-
const groupIds = resolved.groupEntries.map((e) => {
445-
return e.groupId;
446-
});
443+
const groupIds = Array.from(resolved.groupEntries.keys());
447444

448445
return {
449446
isOwner,
@@ -764,9 +761,7 @@ export class Auth {
764761

765762
return {
766763
ownerId,
767-
groupEntries: Array.from(groupEntries.entries()).map(([groupId, writable]) => {
768-
return { groupId, writable };
769-
}),
764+
groupEntries,
770765
};
771766
}
772767

0 commit comments

Comments
 (0)