feat(backend): handle username always in lowercase

This should make all usernames of new users into lowercase. Usernames are also searched in the DB as lowercase.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Philip Molares 2023-05-13 14:56:42 +02:00 committed by Tilman Vatteroth
parent 9625900d1c
commit 0a8945d934
23 changed files with 99 additions and 58 deletions

View file

@ -1,12 +1,13 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { IsLowercase, IsString } from 'class-validator';
import { BaseDto } from '../utils/base.dto.';
import { Username } from '../utils/username';
export class UserInfoDto extends BaseDto {
/**
@ -14,8 +15,9 @@ export class UserInfoDto extends BaseDto {
* @example "john.smith"
*/
@IsString()
@IsLowercase()
@ApiProperty()
username: string;
username: Username;
/**
* The display name

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -20,6 +20,7 @@ import { HistoryEntry } from '../history/history-entry.entity';
import { Identity } from '../identity/identity.entity';
import { MediaUpload } from '../media/media-upload.entity';
import { Note } from '../notes/note.entity';
import { Username } from '../utils/username';
@Entity()
export class User {
@ -29,7 +30,7 @@ export class User {
@Column({
unique: true,
})
username: string;
username: Username;
@Column()
displayName: string;
@ -77,7 +78,7 @@ export class User {
private constructor() {}
public static create(
username: string,
username: Username,
displayName: string,
): Omit<User, 'id' | 'createdAt' | 'updatedAt'> {
const newUser = new User();

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -9,6 +9,7 @@ import { Repository } from 'typeorm';
import { AlreadyInDBError, NotInDBError } from '../errors/errors';
import { ConsoleLoggerService } from '../logger/console-logger.service';
import { Username } from '../utils/username';
import {
FullUserInfoDto,
UserInfoDto,
@ -29,12 +30,12 @@ export class UsersService {
/**
* @async
* Create a new user with a given username and displayName
* @param username - the username the new user shall have
* @param displayName - the display name the new user shall have
* @param {Username} username - the username the new user shall have
* @param {string} displayName - the display name the new user shall have
* @return {User} the user
* @throws {AlreadyInDBError} the username is already taken.
*/
async createUser(username: string, displayName: string): Promise<User> {
async createUser(username: Username, displayName: string): Promise<User> {
const user = User.create(username, displayName);
try {
return await this.userRepository.save(user);
@ -77,12 +78,12 @@ export class UsersService {
/**
* @async
* Get the user specified by the username
* @param {string} username the username by which the user is specified
* @param {Username} username the username by which the user is specified
* @param {UserRelationEnum[]} [withRelations=[]] if the returned user object should contain certain relations
* @return {User} the specified user
*/
async getUserByUsername(
username: string,
username: Username,
withRelations: UserRelationEnum[] = [],
): Promise<User> {
const user = await this.userRepository.findOne({