chore: add user relation enum

this enum is used to specify which relation of the user object should be populated.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-08-31 13:39:36 +02:00 committed by Yannick Bungers
parent e37caf1e6a
commit 5985c4e67d
3 changed files with 18 additions and 7 deletions

View file

@ -20,6 +20,7 @@ import { User } from '../users/user.entity';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { import {
bufferToBase64Url, bufferToBase64Url,
checkPassword,
hashPassword, hashPassword,
} from '../utils/password'; } from '../utils/password';
import { TimestampMillis } from '../utils/timestamp'; import { TimestampMillis } from '../utils/timestamp';

View file

@ -0,0 +1,10 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
export enum UserRelationEnum {
AUTHTOKENS = 'authTokens',
IDENTITIES = 'identities',
}

View file

@ -10,6 +10,7 @@ import { Repository } from 'typeorm';
import { AlreadyInDBError, NotInDBError } from '../errors/errors'; import { AlreadyInDBError, NotInDBError } from '../errors/errors';
import { ConsoleLoggerService } from '../logger/console-logger.service'; import { ConsoleLoggerService } from '../logger/console-logger.service';
import { UserInfoDto } from './user-info.dto'; import { UserInfoDto } from './user-info.dto';
import { UserRelationEnum } from './user-relation.enum';
import { User } from './user.entity'; import { User } from './user.entity';
@Injectable() @Injectable()
@ -73,17 +74,16 @@ export class UsersService {
* @async * @async
* Get the user specified by the username * Get the user specified by the username
* @param {string} userName the username by which the user is specified * @param {string} userName the username by which the user is specified
* @param {boolean} [withTokens=false] if the returned user object should contain authTokens * @param {UserRelationEnum[]} [withRelations=[]] if the returned user object should contain certain relations
* @return {User} the specified user * @return {User} the specified user
*/ */
async getUserByUsername(userName: string, withTokens = false): Promise<User> { async getUserByUsername(
const relations: string[] = []; userName: string,
if (withTokens) { withRelations: UserRelationEnum[] = [],
relations.push('authTokens'); ): Promise<User> {
}
const user = await this.userRepository.findOne({ const user = await this.userRepository.findOne({
where: { userName: userName }, where: { userName: userName },
relations: relations, relations: withRelations,
}); });
if (user === undefined) { if (user === undefined) {
throw new NotInDBError(`User with username '${userName}' not found`); throw new NotInDBError(`User with username '${userName}' not found`);