mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: ignore _ prefixed keys when serializing
This commit is contained in:
1 parent
9a538130ee
commit
90cdc163ca
6 files changed
+53
-32
No files matched your search
@@ -248,7 +248,7 @@ class DomainJoinApplicationsHandler extends ManageHandler {
|
|||||||
else joinSettings.expire = moment().add(expire, 'hours').toDate();
|
else joinSettings.expire = moment().add(expire, 'hours').toDate();
|
||||||
if (method === domain.JOIN_METHOD_CODE) joinSettings.code = invitationCode;
|
if (method === domain.JOIN_METHOD_CODE) joinSettings.code = invitationCode;
|
||||||
}
|
}
|
||||||
await domain.edit(domainId, { join: joinSettings });
|
await domain.edit(domainId, { _join: joinSettings });
|
||||||
this.back();
|
this.back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,23 +70,24 @@ export interface Udoc extends Dictionary<any> {
|
|||||||
loginip: string,
|
loginip: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface User extends Dictionary<any> {
|
export interface User extends Record<string, any> {
|
||||||
udoc: () => Udoc,
|
|
||||||
dudoc: () => any,
|
|
||||||
_id: number,
|
_id: number,
|
||||||
|
_udoc: Udoc,
|
||||||
|
_dudoc: any,
|
||||||
|
_salt: string,
|
||||||
|
_hash: string,
|
||||||
|
_regip: string,
|
||||||
|
_loginip: string,
|
||||||
|
|
||||||
mail: string,
|
mail: string,
|
||||||
uname: string,
|
uname: string,
|
||||||
salt: () => string,
|
|
||||||
hash: () => string,
|
|
||||||
hashType: string,
|
hashType: string,
|
||||||
priv: number,
|
priv: number,
|
||||||
regat: Date,
|
regat: Date,
|
||||||
loginat: Date,
|
loginat: Date,
|
||||||
perm: () => bigint,
|
perm: bigint,
|
||||||
scope: () => bigint,
|
scope: bigint,
|
||||||
role: string,
|
role: string,
|
||||||
regip: () => string,
|
|
||||||
loginip: () => string,
|
|
||||||
hasPerm: (...perm: bigint[]) => boolean,
|
hasPerm: (...perm: bigint[]) => boolean,
|
||||||
hasPriv: (...priv: number[]) => boolean,
|
hasPriv: (...priv: number[]) => boolean,
|
||||||
checkPassword: (password: string) => void,
|
checkPassword: (password: string) => void,
|
||||||
@@ -296,14 +297,14 @@ export interface TrainingDoc extends Tdoc {
|
|||||||
dag: TrainingNode[],
|
dag: TrainingNode[],
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DomainDoc extends Dictionary<any> {
|
export interface DomainDoc extends Record<string, any> {
|
||||||
_id: string,
|
_id: string,
|
||||||
owner: number,
|
owner: number,
|
||||||
roles: Dictionary<string>,
|
roles: Dictionary<string>,
|
||||||
gravatar: string,
|
gravatar: string,
|
||||||
bulletin: string,
|
bulletin: string,
|
||||||
pidCounter: number,
|
pidCounter: number,
|
||||||
join?: any,
|
_join?: any,
|
||||||
host?: string,
|
host?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ class DomainModel {
|
|||||||
|
|
||||||
static getJoinSettings(ddoc: DomainDoc, roles: string[]) {
|
static getJoinSettings(ddoc: DomainDoc, roles: string[]) {
|
||||||
if (!ddoc.join) return null;
|
if (!ddoc.join) return null;
|
||||||
const joinSettings = ddoc.join;
|
const joinSettings = ddoc._join;
|
||||||
if (joinSettings.method === DomainModel.JOIN_METHOD_NONE) return null;
|
if (joinSettings.method === DomainModel.JOIN_METHOD_NONE) return null;
|
||||||
if (!roles.includes(joinSettings.role)) return null;
|
if (!roles.includes(joinSettings.role)) return null;
|
||||||
if (joinSettings.expire && joinSettings.expire < new Date()) return null;
|
if (joinSettings.expire && joinSettings.expire < new Date()) return null;
|
||||||
|
|||||||
@@ -18,40 +18,44 @@ const coll: Collection<Udoc> = db.collection('user');
|
|||||||
const logger = new Logger('model/user');
|
const logger = new Logger('model/user');
|
||||||
|
|
||||||
class User implements _User {
|
class User implements _User {
|
||||||
udoc: () => any;
|
|
||||||
dudoc: () => any;
|
|
||||||
_id: number;
|
_id: number;
|
||||||
|
|
||||||
|
_udoc: Udoc;
|
||||||
|
_dudoc: any;
|
||||||
|
_salt: string;
|
||||||
|
_hash: string;
|
||||||
|
_regip: string;
|
||||||
|
_loginip: string;
|
||||||
|
|
||||||
mail: string;
|
mail: string;
|
||||||
uname: string;
|
uname: string;
|
||||||
salt: () => string;
|
|
||||||
hash: () => string;
|
|
||||||
hashType: string;
|
hashType: string;
|
||||||
priv: number;
|
priv: number;
|
||||||
regat: Date;
|
regat: Date;
|
||||||
loginat: Date;
|
loginat: Date;
|
||||||
perm: () => bigint;
|
perm: bigint;
|
||||||
role: string;
|
role: string;
|
||||||
regip: () => string;
|
scope: bigint;
|
||||||
loginip: () => string;
|
|
||||||
scope: () => bigint;
|
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
|
||||||
constructor(udoc: Udoc, dudoc, scope = PERM.PERM_ALL) {
|
constructor(udoc: Udoc, dudoc, scope = PERM.PERM_ALL) {
|
||||||
this.udoc = () => udoc;
|
|
||||||
this.dudoc = () => dudoc;
|
|
||||||
this._id = udoc._id;
|
this._id = udoc._id;
|
||||||
|
|
||||||
|
this._udoc = udoc;
|
||||||
|
this._dudoc = dudoc;
|
||||||
|
this._salt = udoc.salt;
|
||||||
|
this._hash = udoc.hash;
|
||||||
|
this._regip = udoc.regip;
|
||||||
|
this._loginip = udoc.loginip;
|
||||||
|
|
||||||
this.mail = udoc.mail;
|
this.mail = udoc.mail;
|
||||||
this.uname = udoc.uname;
|
this.uname = udoc.uname;
|
||||||
this.salt = () => udoc.salt;
|
|
||||||
this.hash = () => udoc.hash;
|
|
||||||
this.hashType = udoc.hashType || 'hydro';
|
this.hashType = udoc.hashType || 'hydro';
|
||||||
this.priv = udoc.priv;
|
this.priv = udoc.priv;
|
||||||
this.regat = udoc.regat;
|
this.regat = udoc.regat;
|
||||||
this.regip = () => udoc.regip;
|
|
||||||
this.loginat = udoc.loginat;
|
this.loginat = udoc.loginat;
|
||||||
this.loginip = () => udoc.loginip;
|
this.perm = dudoc.perm;
|
||||||
this.perm = () => dudoc.perm;
|
this.scope = typeof scope === 'string' ? BigInt(scope) : scope;
|
||||||
this.scope = () => (typeof scope === 'string' ? BigInt(scope) : scope);
|
|
||||||
this.role = dudoc.role || 'default';
|
this.role = dudoc.role || 'default';
|
||||||
|
|
||||||
for (const key in setting.SETTINGS_BY_KEY) {
|
for (const key in setting.SETTINGS_BY_KEY) {
|
||||||
@@ -67,7 +71,7 @@ class User implements _User {
|
|||||||
|
|
||||||
hasPerm(...perm: bigint[]) {
|
hasPerm(...perm: bigint[]) {
|
||||||
for (const i in perm) {
|
for (const i in perm) {
|
||||||
if ((this.perm() & this.scope() & perm[i]) === perm[i]) return true;
|
if ((this.perm & this.scope & perm[i]) === perm[i]) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -82,7 +86,7 @@ class User implements _User {
|
|||||||
checkPassword(password: string) {
|
checkPassword(password: string) {
|
||||||
const h = global.Hydro.lib[`hash.${this.hashType}`];
|
const h = global.Hydro.lib[`hash.${this.hashType}`];
|
||||||
if (!h) throw new Error('Unknown hash method');
|
if (!h) throw new Error('Unknown hash method');
|
||||||
if (h(password, this.salt(), this) !== this.hash()) {
|
if (h(password, this._salt, this) !== this._hash) {
|
||||||
throw new LoginError(this.uname);
|
throw new LoginError(this.uname);
|
||||||
} else if (this.hashType !== 'hydro') {
|
} else if (this.hashType !== 'hydro') {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||||
|
|||||||
@@ -281,6 +281,12 @@ export const UiContextBase = {
|
|||||||
url_prefix: '/',
|
url_prefix: '/',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function serializer(k: string, v: any) {
|
||||||
|
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||||
|
if (typeof k === 'bigint') return `BigInt::${v.toString()}`;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
export class HandlerCommon {
|
export class HandlerCommon {
|
||||||
domainId: string;
|
domainId: string;
|
||||||
domain: DomainDoc;
|
domain: DomainDoc;
|
||||||
@@ -321,7 +327,7 @@ export class HandlerCommon {
|
|||||||
perm: this.user.perm.toString(),
|
perm: this.user.perm.toString(),
|
||||||
viewLang: this.translate('__id'),
|
viewLang: this.translate('__id'),
|
||||||
};
|
};
|
||||||
if (!global.Hydro.lib.template) return JSON.stringify(context);
|
if (!global.Hydro.lib.template) return JSON.stringify(context, serializer);
|
||||||
const res = await global.Hydro.lib.template.render(name, {
|
const res = await global.Hydro.lib.template.render(name, {
|
||||||
handler: this,
|
handler: this,
|
||||||
UserContext,
|
UserContext,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { FilterQuery, ObjectID } from 'mongodb';
|
|||||||
import AdmZip from 'adm-zip';
|
import AdmZip from 'adm-zip';
|
||||||
import Queue from 'p-queue';
|
import Queue from 'p-queue';
|
||||||
import yaml from 'js-yaml';
|
import yaml from 'js-yaml';
|
||||||
|
import { pick } from 'lodash';
|
||||||
import { convertIniConfig } from '@hydrooj/utils/lib/cases';
|
import { convertIniConfig } from '@hydrooj/utils/lib/cases';
|
||||||
import { Progress } from './ui';
|
import { Progress } from './ui';
|
||||||
import { Rdoc } from './interface';
|
import { Rdoc } from './interface';
|
||||||
@@ -289,6 +290,15 @@ const scripts: UpgradeScript[] = [
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
async function _21_22() {
|
||||||
|
const coll = db.collection('domain');
|
||||||
|
await iterateAllDomain(async (ddoc) => {
|
||||||
|
if (ddoc.join) {
|
||||||
|
await coll.updateOne(pick(ddoc, '_id'), { $set: { _join: ddoc.join }, $unset: { join: '' } });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export default scripts;
|
export default scripts;
|
||||||
Reference in new issue
Block a user