misc improvements

This commit is contained in:
Erik 2022-11-24 00:06:53 +02:00
parent a7dac9f5f7
commit f4a04bc67b
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -3,6 +3,9 @@ const { ObjectId } = require('mongodb');
const { Util } = require('../../util'); const { Util } = require('../../util');
const UserApplicataion = require('./UserApplication'); const UserApplicataion = require('./UserApplication');
// Fields omitted in safeJson
const ProtectedFields = [ '_id', '_otpSecret', '_passwordHash' ];
class User { class User {
static defaultPermissions = { static defaultPermissions = {
@ -29,6 +32,7 @@ class User {
this._db = db; this._db = db;
this.temporary = data.temporary || false; this.temporary = data.temporary || false;
this.disabled = data.disabled || false;
this._id = data._id || null; this._id = data._id || null;
if (this.temporary) this._tempId = `temp-${Date.now()}`; if (this.temporary) this._tempId = `temp-${Date.now()}`;
@ -69,6 +73,7 @@ class User {
this._2fa = data.twoFactor || false; this._2fa = data.twoFactor || false;
this.cachedTimestamp = Date.now(); this.cachedTimestamp = Date.now();
this.createdTimestamp = data.createdTimestamp || Date.now();
} }
@ -171,21 +176,21 @@ class User {
otpSecret: this._otpSecret, otpSecret: this._otpSecret,
twoFactor: this._2fa, twoFactor: this._2fa,
applications: this._applications, applications: this._applications,
createdTimestamp: this.createdTimestamp,
disabled: this.disabled,
}; };
} }
get safeJson () { get safeJson () {
const { json } = this;
for (const key of ProtectedFields) delete json[key];
return { return {
...json,
id: this.id, id: this.id,
username: this.username,
displayName: this.displayName,
type: this.type,
permissions: this.permissions,
externalProfiles: Object.values(this.externalProfiles).map(prof => { externalProfiles: Object.values(this.externalProfiles).map(prof => {
return { id: prof.id, provider: prof.provider, username: prof.username }; return { id: prof.id, provider: prof.provider, username: prof.username };
}), }),
twoFactor: this._2fa,
applications: this._applications,
}; };
} }