mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
优化domain.user存储结构
This commit is contained in:
1 parent
3eb593d134
commit
a93da2badb
17 files changed
+220
-239
No files matched your search
+14
-43
@@ -1,20 +1,18 @@
|
||||
import { RoleAlreadyExistError, ValidationError, PermissionError } from '../error';
|
||||
import { RoleAlreadyExistError, ValidationError } from '../error';
|
||||
import * as user from '../model/user';
|
||||
import * as domain from '../model/domain';
|
||||
import * as system from '../model/system';
|
||||
import { DOMAIN_SETTINGS, DOMAIN_SETTINGS_BY_KEY } from '../model/setting';
|
||||
import { PERM, PERMS_BY_FAMILY } from '../model/builtin';
|
||||
import paginate from '../lib/paginate';
|
||||
import {
|
||||
Route, Handler, Types, param,
|
||||
} from '../service/server';
|
||||
import { isTitle, isUname } from '../lib/validator';
|
||||
|
||||
class DomainRankHandler extends Handler {
|
||||
@param('page', Types.UnsignedInt, true)
|
||||
async get(domainId: string, page = 1) {
|
||||
const [dudocs, upcount, ucount] = await paginate(
|
||||
user.getMultiInDomain(domainId).sort({ rating: -1 }),
|
||||
domain.getMultiInDomain(domainId).sort({ rating: -1 }),
|
||||
page,
|
||||
100,
|
||||
);
|
||||
@@ -34,31 +32,6 @@ class DomainRankHandler extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
class DomainCreateHandler extends Handler {
|
||||
async prepare() {
|
||||
if (this.user.priv !== 1) {
|
||||
if (!await system.get('user.create_domain')) throw new PermissionError('domain_create');
|
||||
}
|
||||
}
|
||||
|
||||
async get() {
|
||||
const path = [
|
||||
['Hydro', 'homepage'],
|
||||
['domain_create', null],
|
||||
];
|
||||
this.response.body = { path };
|
||||
this.response.template = 'domain_create.html';
|
||||
}
|
||||
|
||||
@param('id', Types.String, isUname)
|
||||
@param('name', Types.String, isTitle)
|
||||
async post(domainId: string, id: string, name: string) {
|
||||
await domain.add(id, this.user._id, name);
|
||||
this.response.body = { domainId: id };
|
||||
this.response.redirect = this.url('homepage', { domainId: id });
|
||||
}
|
||||
}
|
||||
|
||||
class ManageHandler extends Handler {
|
||||
domain: any;
|
||||
|
||||
@@ -105,13 +78,13 @@ class DomainUserHandler extends ManageHandler {
|
||||
async get({ domainId }) {
|
||||
const rudocs = {};
|
||||
const [dudocs, roles] = await Promise.all([
|
||||
user.getMultiInDomain(domainId, {
|
||||
domain.getMultiInDomain(domainId, {
|
||||
$and: [
|
||||
{ role: { $ne: 'default' } },
|
||||
{ role: { $ne: null } },
|
||||
],
|
||||
}).toArray(),
|
||||
user.getRoles(domainId),
|
||||
domain.getRoles(domainId),
|
||||
]);
|
||||
const uids = dudocs.map((dudoc) => dudoc.uid);
|
||||
const udict = await user.getList(domainId, uids);
|
||||
@@ -133,14 +106,14 @@ class DomainUserHandler extends ManageHandler {
|
||||
}
|
||||
|
||||
async postSetUser({ domainId, uid, role }) {
|
||||
await user.setRole(domainId, uid, role);
|
||||
await domain.setUserRole(domainId, uid, role);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
class DomainPermissionHandler extends ManageHandler {
|
||||
async get({ domainId }) {
|
||||
const roles = await user.getRoles(domainId);
|
||||
const roles = await domain.getRoles(domainId);
|
||||
const path = [
|
||||
['Hydro', 'homepage'],
|
||||
['domain', null],
|
||||
@@ -159,14 +132,14 @@ class DomainPermissionHandler extends ManageHandler {
|
||||
roles[role] = this.request.body[role].join('');
|
||||
}
|
||||
}
|
||||
await user.setRoles(domainId, roles);
|
||||
await domain.setRoles(domainId, roles);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
class DomainRoleHandler extends ManageHandler {
|
||||
async get({ domainId }) {
|
||||
const roles = await user.getRoles(domainId);
|
||||
const roles = await domain.getRoles(domainId);
|
||||
const path = [
|
||||
['Hydro', 'homepage'],
|
||||
['domain', null],
|
||||
@@ -177,12 +150,11 @@ class DomainRoleHandler extends ManageHandler {
|
||||
}
|
||||
|
||||
async postAdd({ domainId, role }) {
|
||||
const [r, u] = await Promise.all([
|
||||
user.getRole(domainId, role),
|
||||
user.getRole(domainId, 'default'),
|
||||
]);
|
||||
if (r) throw new RoleAlreadyExistError(role);
|
||||
await user.addRole(domainId, role, u.perm);
|
||||
const roles = await domain.getRoles(this.domain);
|
||||
const rdict: any = {};
|
||||
for (const r of roles) rdict[r._id] = r.perm;
|
||||
if (rdict[role]) throw new RoleAlreadyExistError(role);
|
||||
await domain.addRole(domainId, role, rdict.default.perm);
|
||||
this.back();
|
||||
}
|
||||
|
||||
@@ -192,14 +164,13 @@ class DomainRoleHandler extends ManageHandler {
|
||||
throw new ValidationError('role');
|
||||
}
|
||||
}
|
||||
await user.deleteRoles(domainId, roles);
|
||||
await domain.deleteRoles(domainId, roles);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
export async function apply() {
|
||||
Route('ranking', '/ranking', DomainRankHandler);
|
||||
Route('domain_create', '/domain/create', DomainCreateHandler);
|
||||
Route('domain_dashboard', '/domain/dashboard', DomainDashboardHandler);
|
||||
Route('domain_edit', '/domain/edit', DomainEditHandler);
|
||||
Route('domain_user', '/domain/user', DomainUserHandler);
|
||||
|
||||
+43
-3
@@ -24,7 +24,9 @@ import * as discussion from '../model/discussion';
|
||||
import * as token from '../model/token';
|
||||
import * as training from '../model/training';
|
||||
import { PERM, PRIV } from '../model/builtin';
|
||||
import { isContent, isPassword, isEmail } from '../lib/validator';
|
||||
import {
|
||||
isContent, isPassword, isEmail, isTitle,
|
||||
} from '../lib/validator';
|
||||
|
||||
const { geoip, useragent } = global.Hydro.lib;
|
||||
|
||||
@@ -133,7 +135,7 @@ class HomeSecurityHandler extends Handler {
|
||||
['home_security', null],
|
||||
];
|
||||
this.response.template = 'home_security.html';
|
||||
this.response.body = { sessions, geoipProvider: (geoip || {}).provider, path };
|
||||
this.response.body = { sessions, geoipProvider: geoip?.provider, path };
|
||||
if (useragent) this.response.body.icon = useragent.icon;
|
||||
}
|
||||
|
||||
@@ -210,7 +212,8 @@ class HomeSettingsHandler extends Handler {
|
||||
async post(args: any) {
|
||||
const $set = {};
|
||||
for (const key in args) {
|
||||
if (setting.SETTINGS_BY_KEY[key] && !setting.SETTINGS_BY_KEY[key].disabled) {
|
||||
if (setting.SETTINGS_BY_KEY[key]
|
||||
&& !(setting.SETTINGS_BY_KEY[key].flag & setting.FLAG_DISABLED)) {
|
||||
$set[key] = args[key];
|
||||
}
|
||||
}
|
||||
@@ -236,6 +239,41 @@ class UserChangemailWithCodeHandler extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
class HomeDomainHandler extends Handler {
|
||||
async get() {
|
||||
const dudict = await domain.getDictUserByDomainId(this.user._id);
|
||||
const dids = Object.keys(dudict);
|
||||
const ddocs = await domain.getMulti({ _id: { $in: dids } }).toArray();
|
||||
const canManage = {};
|
||||
for (const ddoc of ddocs) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const udoc = await user.getById(ddoc._id, this.user._id);
|
||||
canManage[ddoc._id] = udoc.hasPerm(PERM.PERM_EDIT_DOMAIN)
|
||||
|| udoc.hasPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
|
||||
}
|
||||
this.response.template = 'home_domain.html';
|
||||
this.response.body = { ddocs, dudict, canManage };
|
||||
}
|
||||
}
|
||||
|
||||
class HomeDomainCreateHandler extends Handler {
|
||||
async get() {
|
||||
this.response.body = 'home_domain_create.html';
|
||||
}
|
||||
|
||||
@param('id', Types.String)
|
||||
@param('name', Types.String, isTitle)
|
||||
@param('bulletin', Types.String, isContent)
|
||||
@param('gravatar', Types.String, isEmail, true)
|
||||
async post(_: string, id: string, name: string, bulletin: string, gravatar: string) {
|
||||
gravatar = gravatar || this.user.gravatar || this.user.mail || 'guest@hydro.local';
|
||||
const domainId = await domain.add(id, this.user._id, name, bulletin);
|
||||
await domain.edit(domainId, { gravatar });
|
||||
this.response.redirect = this.url('domain_manage', { domainId });
|
||||
this.response.body = { domainId };
|
||||
}
|
||||
}
|
||||
|
||||
class HomeMessagesHandler extends Handler {
|
||||
async get() {
|
||||
// TODO(iceboy): projection, pagination.
|
||||
@@ -324,6 +362,8 @@ async function apply() {
|
||||
Route('home_security', '/home/security', HomeSecurityHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('user_changemail_with_code', '/home/changeMail/:code', UserChangemailWithCodeHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('home_settings', '/home/settings/:category', HomeSettingsHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('home_domain', '/home/domain', HomeDomainHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('home_domain_create', '/home/domain/create', HomeDomainCreateHandler, PRIV.PRIV_CREATE_DOMAIN);
|
||||
Route('home_messages', '/home/messages', HomeMessagesHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('home_file', '/home/file', HomeFileHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Connection('home_messages_conn', '/home/messages-conn', HomeMessagesConnectionHandler, PRIV.PRIV_USER_PROFILE);
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as record from '../model/record';
|
||||
import * as problem from '../model/problem';
|
||||
import * as builtin from '../model/builtin';
|
||||
import * as contest from '../model/contest';
|
||||
import * as user from '../model/user';
|
||||
import * as domain from '../model/domain';
|
||||
import * as task from '../model/task';
|
||||
import * as bus from '../service/bus';
|
||||
import {
|
||||
@@ -29,7 +29,7 @@ async function _postJudge(rdoc) {
|
||||
if (accept && !rdoc.rejudged) {
|
||||
tasks.push(
|
||||
problem.inc(rdoc.domainId, rdoc.pid, 'nAccept', 1),
|
||||
user.incDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1),
|
||||
domain.incUserInDomain(rdoc.domainId, rdoc.uid, 'nAccept', 1),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import * as file from '../model/file';
|
||||
import * as problem from '../model/problem';
|
||||
import * as record from '../model/record';
|
||||
import * as domain from '../model/domain';
|
||||
import * as user from '../model/user';
|
||||
import * as solution from '../model/solution';
|
||||
import * as system from '../model/system';
|
||||
@@ -219,7 +220,7 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
const rid = await record.add(domainId, {
|
||||
uid: this.user._id, lang, code, pid: this.pdoc.docId,
|
||||
}, true);
|
||||
await user.incDomain(domainId, this.user._id, 'nSubmit');
|
||||
await domain.incUserInDomain(domainId, this.user._id, 'nSubmit');
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = this.url('record_detail', { rid });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user