mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: add PREM_VIEW_DISPLAYNAME (#410)
Co-authored-by: undefined <i@undefined.moe>
This commit is contained in:
1 parent
b9bc504917
commit
7ebbaacb2a
9 files changed
+40
-18
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "3.15.16",
|
"version": "3.15.17",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export const PERM = {
|
|||||||
// Domain Settings
|
// Domain Settings
|
||||||
PERM_VIEW: 1n << 0n,
|
PERM_VIEW: 1n << 0n,
|
||||||
PERM_EDIT_DOMAIN: 1n << 1n,
|
PERM_EDIT_DOMAIN: 1n << 1n,
|
||||||
|
PREM_VIEW_DISPLAYNAME: 1n << 67n,
|
||||||
PERM_MOD_BADGE: 1n << 2n,
|
PERM_MOD_BADGE: 1n << 2n,
|
||||||
|
|
||||||
// Problem
|
// Problem
|
||||||
@@ -101,6 +102,7 @@ export const Permission = (family: string, key: BigInt, desc: string) => ({ fami
|
|||||||
|
|
||||||
export const PERMS = [
|
export const PERMS = [
|
||||||
Permission('perm_general', PERM.PERM_VIEW, 'View this domain'),
|
Permission('perm_general', PERM.PERM_VIEW, 'View this domain'),
|
||||||
|
Permission('perm_general', PERM.PREM_VIEW_DISPLAYNAME, 'View domain user displayname'),
|
||||||
Permission('perm_general', PERM.PERM_EDIT_DOMAIN, 'Edit domain settings'),
|
Permission('perm_general', PERM.PERM_EDIT_DOMAIN, 'Edit domain settings'),
|
||||||
Permission('perm_general', PERM.PERM_MOD_BADGE, 'Show MOD badge'),
|
Permission('perm_general', PERM.PERM_MOD_BADGE, 'Show MOD badge'),
|
||||||
Permission('perm_problem', PERM.PERM_CREATE_PROBLEM, 'Create problems'),
|
Permission('perm_problem', PERM.PERM_CREATE_PROBLEM, 'Create problems'),
|
||||||
@@ -182,6 +184,7 @@ PERM.PERM_BASIC = PERM.PERM_VIEW
|
|||||||
| PERM.PERM_VIEW_RANKING;
|
| PERM.PERM_VIEW_RANKING;
|
||||||
|
|
||||||
PERM.PERM_DEFAULT = PERM.PERM_VIEW
|
PERM.PERM_DEFAULT = PERM.PERM_VIEW
|
||||||
|
| PERM.PREM_VIEW_DISPLAYNAME
|
||||||
| PERM.PERM_VIEW_PROBLEM
|
| PERM.PERM_VIEW_PROBLEM
|
||||||
| PERM.PERM_EDIT_PROBLEM_SELF
|
| PERM.PERM_EDIT_PROBLEM_SELF
|
||||||
| PERM.PERM_SUBMIT_PROBLEM
|
| PERM.PERM_SUBMIT_PROBLEM
|
||||||
|
|||||||
@@ -1,23 +1,27 @@
|
|||||||
import { ObjectID } from 'mongodb';
|
import { ObjectID } from 'mongodb';
|
||||||
import avatar from 'hydrooj/src/lib/avatar';
|
import avatar from 'hydrooj/src/lib/avatar';
|
||||||
|
import { PERM } from 'hydrooj/src/model/builtin';
|
||||||
import * as system from 'hydrooj/src/model/system';
|
import * as system from 'hydrooj/src/model/system';
|
||||||
|
import { User } from 'hydrooj/src/model/user';
|
||||||
import type { KoaContext } from '../server';
|
import type { KoaContext } from '../server';
|
||||||
|
|
||||||
function serializer(k: string, v: any) {
|
const serializer = (showDisplayName = false) => (k: string, v: any) => {
|
||||||
if (k.startsWith('_') && k !== '_id') return undefined;
|
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||||
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
||||||
|
if (v instanceof User && !showDisplayName) delete v.displayName;
|
||||||
return v;
|
return v;
|
||||||
}
|
};
|
||||||
|
|
||||||
export default (router, logger) => async (ctx: KoaContext, next) => {
|
export default (router, logger) => async (ctx: KoaContext, next) => {
|
||||||
const { request, response } = ctx.HydroContext;
|
const { request, response, user } = ctx.HydroContext;
|
||||||
ctx.renderHTML = (templateName, args) => {
|
ctx.renderHTML = (templateName, args) => {
|
||||||
const UserContext: any = {
|
const UserContext: any = {
|
||||||
...(ctx.HydroContext.user || {}),
|
...(user || {}),
|
||||||
avatar: avatar(ctx.HydroContext.user?.avatar || '', 128),
|
avatar: avatar(user?.avatar || '', 128),
|
||||||
viewLang: ctx.translate('__id'),
|
viewLang: ctx.translate('__id'),
|
||||||
};
|
};
|
||||||
const engine = global.Hydro.lib.template?.render || (() => JSON.stringify(args, serializer));
|
const engine = global.Hydro.lib.template?.render
|
||||||
|
|| (() => JSON.stringify(args, serializer(user?.hasPerm(PERM.PREM_VIEW_DISPLAYNAME))));
|
||||||
return engine(templateName, {
|
return engine(templateName, {
|
||||||
handler: ctx.handler,
|
handler: ctx.handler,
|
||||||
UserContext,
|
UserContext,
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { errorMessage } from '@hydrooj/utils/lib/utils';
|
import { errorMessage } from '@hydrooj/utils/lib/utils';
|
||||||
import { SystemError, UserFacingError } from 'hydrooj/src/error';
|
import { SystemError, UserFacingError } from 'hydrooj/src/error';
|
||||||
|
import { PERM } from 'hydrooj/src/model/builtin';
|
||||||
|
import { User } from 'hydrooj/src/model/user';
|
||||||
import type { KoaContext } from '../server';
|
import type { KoaContext } from '../server';
|
||||||
|
|
||||||
function serializer(k: string, v: any) {
|
const serializer = (showDisplayName = false) => (k: string, v: any) => {
|
||||||
if (k.startsWith('_') && k !== '_id') return undefined;
|
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||||
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
||||||
|
if (v instanceof User && !showDisplayName) delete v.displayName;
|
||||||
return v;
|
return v;
|
||||||
}
|
};
|
||||||
|
|
||||||
export default (logger) => async (ctx: KoaContext, next) => {
|
export default (logger) => async (ctx: KoaContext, next) => {
|
||||||
const {
|
const {
|
||||||
@@ -28,7 +31,7 @@ export default (logger) => async (ctx: KoaContext, next) => {
|
|||||||
response.body.UiContext = UiContext;
|
response.body.UiContext = UiContext;
|
||||||
response.body.UserContext = user;
|
response.body.UserContext = user;
|
||||||
}
|
}
|
||||||
response.body = JSON.stringify(response.body, serializer);
|
response.body = JSON.stringify(response.body, serializer(user?.hasPerm(PERM.PREM_VIEW_DISPLAYNAME)));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
response.body = new SystemError('Serialize failure', e.message);
|
response.body = new SystemError('Serialize failure', e.message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,12 @@ import {
|
|||||||
NotFoundError, PermissionError, PrivilegeError,
|
NotFoundError, PermissionError, PrivilegeError,
|
||||||
UserFacingError,
|
UserFacingError,
|
||||||
} from '../error';
|
} from '../error';
|
||||||
import { DomainDoc, User } from '../interface';
|
import { DomainDoc } from '../interface';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import { PERM, PRIV } from '../model/builtin';
|
import { PERM, PRIV } from '../model/builtin';
|
||||||
import * as opcount from '../model/opcount';
|
import * as opcount from '../model/opcount';
|
||||||
import * as system from '../model/system';
|
import * as system from '../model/system';
|
||||||
|
import { User } from '../model/user';
|
||||||
import { errorMessage } from '../utils';
|
import { errorMessage } from '../utils';
|
||||||
import * as bus from './bus';
|
import * as bus from './bus';
|
||||||
import * as decorators from './decorators';
|
import * as decorators from './decorators';
|
||||||
@@ -99,11 +100,12 @@ wsServer.on('error', (error) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const ignoredLimit = `,${argv.options.ignoredLimit},`;
|
const ignoredLimit = `,${argv.options.ignoredLimit},`;
|
||||||
function serializer(k: string, v: any) {
|
const serializer = (showDisplayName = false) => (k: string, v: any) => {
|
||||||
if (k.startsWith('_') && k !== '_id') return undefined;
|
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||||
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
||||||
|
if (v instanceof User && !showDisplayName) delete v.displayName;
|
||||||
return v;
|
return v;
|
||||||
}
|
};
|
||||||
|
|
||||||
export async function prepare() {
|
export async function prepare() {
|
||||||
app.keys = system.get('session.keys') as unknown as string[];
|
app.keys = system.get('session.keys') as unknown as string[];
|
||||||
@@ -366,7 +368,7 @@ export class ConnectionHandler extends HandlerCommon {
|
|||||||
conn: WebSocket;
|
conn: WebSocket;
|
||||||
|
|
||||||
send(data: any) {
|
send(data: any) {
|
||||||
this.conn.send(JSON.stringify(data, serializer));
|
this.conn.send(JSON.stringify(data, serializer(this.user?.hasPerm(PERM.PREM_VIEW_DISPLAYNAME))));
|
||||||
}
|
}
|
||||||
|
|
||||||
close(code: number, reason: string) {
|
close(code: number, reason: string) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { convertIniConfig } from '@hydrooj/utils/lib/cases';
|
|||||||
import { size } from '@hydrooj/utils/lib/utils';
|
import { size } from '@hydrooj/utils/lib/utils';
|
||||||
import { buildContent } from './lib/content';
|
import { buildContent } from './lib/content';
|
||||||
import { Logger } from './logger';
|
import { Logger } from './logger';
|
||||||
import { PRIV, STATUS } from './model/builtin';
|
import { PERM, PRIV, STATUS } from './model/builtin';
|
||||||
import * as contest from './model/contest';
|
import * as contest from './model/contest';
|
||||||
import * as discussion from './model/discussion';
|
import * as discussion from './model/discussion';
|
||||||
import * as document from './model/document';
|
import * as document from './model/document';
|
||||||
@@ -792,6 +792,16 @@ const scripts: UpgradeScript[] = [
|
|||||||
await system.set('server.center', 'https://hydro.ac/center');
|
await system.set('server.center', 'https://hydro.ac/center');
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
async function _65_66() {
|
||||||
|
await iterateAllDomain(async (ddoc) => {
|
||||||
|
Object.keys(ddoc.roles).forEach((role) => {
|
||||||
|
if (['guest', 'root'].includes(role)) return;
|
||||||
|
ddoc.roles[role] = (BigInt(ddoc.roles[role]) | PERM.PREM_VIEW_DISPLAYNAME).toString();
|
||||||
|
});
|
||||||
|
await domain.setRoles(ddoc._id, ddoc.roles);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export default scripts;
|
export default scripts;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/ui-default",
|
"name": "@hydrooj/ui-default",
|
||||||
"version": "4.39.6",
|
"version": "4.39.7",
|
||||||
"author": "undefined <i@undefined.moe>",
|
"author": "undefined <i@undefined.moe>",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"main": "hydro.js",
|
"main": "hydro.js",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<img class="small user-profile-avatar v-center" loading="lazy" src="{{ avatarUrl(udoc.avatar|default('')) }}" width="20" height="20">
|
<img class="small user-profile-avatar v-center" loading="lazy" src="{{ avatarUrl(udoc.avatar|default('')) }}" width="20" height="20">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<a class="user-profile-name" href="{{ url('user_detail', uid=udoc._id) }}">
|
<a class="user-profile-name" href="{{ url('user_detail', uid=udoc._id) }}">
|
||||||
{% if udoc.displayName and udoc.displayName != udoc.uname %}
|
{% if handler.user.hasPerm(perm.PREM_VIEW_DISPLAYNAME) and udoc.displayName and udoc.displayName != udoc.uname %}
|
||||||
{{ udoc.displayName }} ({{ udoc.uname }})
|
{{ udoc.displayName }} ({{ udoc.uname }})
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ udoc.uname }}
|
{{ udoc.uname }}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
</td>
|
</td>
|
||||||
<td class="col--submit-by">
|
<td class="col--submit-by">
|
||||||
<a href="{{ url('user_detail', uid=rdoc.uid) }}">
|
<a href="{{ url('user_detail', uid=rdoc.uid) }}">
|
||||||
{% if udoc.displayName %}
|
{% if handler.user.hasPerm(perm.PREM_VIEW_DISPLAYNAME) and udoc.displayName %}
|
||||||
{{ udoc.displayName }} ({{ udoc.uname }})
|
{{ udoc.displayName }} ({{ udoc.uname }})
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ udoc.uname }}
|
{{ udoc.uname }}
|
||||||
|
|||||||
Reference in new issue
Block a user