core: write builtin users to database

This commit is contained in:
undefined committed 2021-06-05 15:12:01 +08:00
1 parent 7b75a961bc
commit d0d970efbf
3 files changed
+12 -51

No files matched your search

-34
View File
@@ -11,7 +11,6 @@ import {
USER_GENDER_MALE, USER_GENDER_FEMALE, USER_GENDER_OTHER,
USER_GENDER_ICONS, USER_GENDERS, USER_GENDER_RANGE,
} from '@hydrooj/utils/lib/status';
import type { Udoc } from '../interface';
export * from '@hydrooj/utils/lib/status';
@@ -268,39 +267,6 @@ PRIV.PRIV_DEFAULT = PRIV.PRIV_USER_PROFILE
// people whose rank is less than 1% will get Level 10
export const LEVELS = [100, 90, 70, 55, 40, 30, 20, 10, 5, 2, 1];
export const BUILTIN_USERS: Udoc[] = [
{
_id: 0,
mail: 'Guest@hydro.local',
mailLower: 'guest@hydro.local',
uname: 'Guest',
unameLower: 'guest',
salt: '',
hash: '',
hashType: 'hydro',
regat: new Date(0),
regip: '127.0.0.1',
loginat: new Date(0),
loginip: '127.0.0.1',
priv: PRIV.PRIV_REGISTER_USER,
},
{
_id: 1,
mail: 'Hydro@hydro.local',
mailLower: 'hydro@hydro.local',
uname: 'Hydro',
unameLower: 'hydro',
salt: '',
hash: '',
hashType: 'hydro',
regat: new Date(0),
regip: '127.0.0.1',
loginat: new Date(0),
loginip: '127.0.0.1',
priv: PRIV.PRIV_USER_PROFILE,
},
];
export const BUILTIN_ROLES = {
guest: PERM.PERM_BASIC,
default: PERM.PERM_DEFAULT,
+4 -14
View File
@@ -5,7 +5,7 @@ import * as system from './system';
import token from './token';
import * as setting from './setting';
import domain from './domain';
import { BUILTIN_USERS, PERM, PRIV } from './builtin';
import { PERM, PRIV } from './builtin';
import { ArgMethod } from '../utils';
import { UserNotFoundError, UserAlreadyExistError, LoginError } from '../error';
import { User as _User, Udoc, Udict } from '../interface';
@@ -142,9 +142,7 @@ class UserModel {
@ArgMethod
static async getById(domainId: string, _id: number, scope: bigint | string = PERM.PERM_ALL): Promise<User | null> {
if (cache.has(`${_id}/${domainId}`)) return cache.get(`${_id}/${domainId}`);
const udoc = _id === 0 || _id === 1
? BUILTIN_USERS[_id]
: await coll.findOne({ _id });
const udoc = await coll.findOne({ _id });
if (!udoc) return null;
const dudoc = await domain.getDomainUser(domainId, udoc);
if (typeof scope === 'string') scope = BigInt(scope);
@@ -167,11 +165,7 @@ class UserModel {
static async getByUname(domainId: string, uname: string): Promise<User | null> {
const unameLower = uname.trim().toLowerCase();
if (cache.has(`${unameLower}/${domainId}`)) return cache.get(`${unameLower}/${domainId}`);
const udoc = (unameLower === 'guest')
? BUILTIN_USERS[0]
: unameLower === 'hydro'
? BUILTIN_USERS[1]
: await coll.findOne({ unameLower });
const udoc = await coll.findOne({ unameLower });
if (!udoc) return null;
const dudoc = await domain.getDomainUser(domainId, udoc);
const res = await new UserModel.User(udoc, dudoc).init();
@@ -185,11 +179,7 @@ class UserModel {
static async getByEmail(domainId: string, mail: string): Promise<User | null> {
const mailLower = mail.trim().toLowerCase();
if (cache.has(`${mailLower}/${domainId}`)) return cache.get(`${mailLower}/${domainId}`);
const udoc = (mailLower === 'guest@hydro.local')
? BUILTIN_USERS[0]
: mailLower === 'hydro@hydro.local'
? BUILTIN_USERS[1]
: await coll.findOne({ mailLower });
const udoc = await coll.findOne({ mailLower });
if (!udoc) return null;
const dudoc = await domain.getDomainUser(domainId, udoc);
const res = await new UserModel.User(udoc, dudoc).init();
+8 -3
View File
@@ -398,11 +398,16 @@ const scripts: UpgradeScript[] = [
await iterateAllDomain((ddoc) => RecordModel.coll.updateMany({ domainId: ddoc._id }, { $set: { pdomain: ddoc._id } }));
return true;
},
// Add send_message priv to user
async function _30_31() {
const _FRESH_INSTALL_IGNORE = 1;
await iterateAllUser(async (udoc) => {
await user.setPriv(udoc._id, udoc.priv | PRIV.PRIV_SEND_MESSAGE);
});
await iterateAllUser((udoc) => user.setPriv(udoc._id, udoc.priv | PRIV.PRIV_SEND_MESSAGE));
return true;
},
// Write builtin users to database
async function _31_32() {
await user.create('Guest@hydro.local', 'Guest', String.random(32), 0, '127.0.0.1', PRIV.PRIV_REGISTER_USER);
await user.create('Hydro@hydro.local', 'Hydro', String.random(32), 1, '127.0.0.1', PRIV.PRIV_USER_PROFILE);
return true;
},
];