Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/auth/social.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { HttpStatus, Injectable } from '@nestjs/common';
import { AppException } from 'omniboxd/common/exceptions/app.exception';
import { I18nService } from 'nestjs-i18n';
import { CacheService } from 'omniboxd/common/cache.service';
import { NamespacesService } from 'omniboxd/namespaces/namespaces.service';
import { isNameBlocked } from 'omniboxd/utils/blocked-names';

export interface UserSocialState {
type: string;
Expand All @@ -24,6 +26,7 @@ export class SocialService {
private readonly userService: UserService,
private readonly i18n: I18nService,
private readonly cacheService: CacheService,
private readonly namespacesService: NamespacesService,
) {}

/**
Expand Down Expand Up @@ -71,6 +74,33 @@ export class SocialService {
);
}

private async isUsernameValid(
username: string,
manager: EntityManager,
): Promise<boolean> {
if (isNameBlocked(username)) {
return false;
}

const user = await this.userService.findByUsername(username, manager);
if (user) {
return false;
}

const namespaceName = this.i18n.t('namespace.userNamespaceName', {
args: { userName: username },
});
const namespace = await this.namespacesService.getNamespaceByName(
namespaceName,
manager,
);
if (namespace) {
return false;
}

return true;
}

async getValidUsername(
nickname: string,
manager: EntityManager,
Expand All @@ -81,21 +111,18 @@ export class SocialService {
username = nickname.slice(0, this.maxUsernameLength);
}
if (username.length >= this.minUsernameLength) {
const user = await this.userService.findByUsername(username, manager);
if (!user) {
const ok = await this.isUsernameValid(username, manager);
if (ok) {
return username;
}
}

username = nickname.slice(0, this.maxUsernameLength - 5);
for (let i = 0; i < 5; i++) {
const suffix = this.generateSuffix();
const user = await this.userService.findByUsername(
username + suffix,
manager,
);
if (!user) {
return username + suffix;
const curUsername = username + this.generateSuffix();
const ok = await this.isUsernameValid(curUsername, manager);
if (ok) {
return curUsername;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/namespaces/dto/create-namespace.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { IsString, IsNotEmpty } from 'class-validator';
import { IsString, IsNotEmpty, MinLength, MaxLength } from 'class-validator';

export class CreateNamespaceDto {
@IsString()
@IsNotEmpty()
@MinLength(2)
@MaxLength(64)
name: string;
}
10 changes: 9 additions & 1 deletion src/namespaces/dto/update-namespace.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { IsString, IsOptional, IsNotEmpty } from 'class-validator';
import {
IsString,
IsOptional,
IsNotEmpty,
MinLength,
MaxLength,
} from 'class-validator';

export class UpdateNamespaceDto {
@IsString()
@IsOptional()
@IsNotEmpty()
@MinLength(2)
@MaxLength(64)
name?: string;
}
2 changes: 1 addition & 1 deletion src/namespaces/namespaces.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('NamespacesController (e2e)', () => {
});

it('should handle very long namespace names', async () => {
const longName = 'A'.repeat(255); // Test boundary conditions
const longName = 'A'.repeat(64); // Test boundary conditions
const longNameWorkspace = {
name: longName,
};
Expand Down
21 changes: 19 additions & 2 deletions src/namespaces/namespaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ResourcesService } from 'omniboxd/resources/resources.service';
import { ResourceMetaDto } from 'omniboxd/resources/dto/resource-meta.dto';
import { AppException } from 'omniboxd/common/exceptions/app.exception';
import { I18nService } from 'nestjs-i18n';
import { isNameBlocked } from 'omniboxd/utils/blocked-names';

@Injectable()
export class NamespacesService {
Expand Down Expand Up @@ -173,7 +174,10 @@ export class NamespacesService {
name: string,
manager: EntityManager,
): Promise<Namespace> {
if ((await manager.countBy(Namespace, { name })) > 0) {
if (
isNameBlocked(name) ||
(await manager.countBy(Namespace, { name })) > 0
) {
const message = this.i18n.t('namespace.errors.namespaceConflict');
throw new AppException(
message,
Expand All @@ -197,6 +201,16 @@ export class NamespacesService {
return namespace;
}

async getNamespaceByName(
name: string,
entityManager?: EntityManager,
): Promise<Namespace | null> {
const repo = entityManager
? entityManager.getRepository(Namespace)
: this.namespaceRepository;
return repo.findOne({ where: { name } });
}

async update(
id: string,
updateDto: UpdateNamespaceDto,
Expand All @@ -207,7 +221,10 @@ export class NamespacesService {
: this.namespaceRepository;
const namespace = await this.getNamespace(id, manager);
if (updateDto.name && updateDto.name !== namespace.name) {
if ((await repo.countBy({ name: updateDto.name })) > 0) {
if (
isNameBlocked(updateDto.name) ||
(await repo.countBy({ name: updateDto.name })) > 0
) {
const message = this.i18n.t('namespace.errors.namespaceConflict');
throw new AppException(
message,
Expand Down
6 changes: 3 additions & 3 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CreateUserOptionDto } from 'omniboxd/user/dto/create-user-option.dto';
import { UpdateUserBindingDto } from 'omniboxd/user/dto/update-user-binding.dto';
import { CreateUserBindingDto } from 'omniboxd/user/dto/create-user-binding.dto';
import { Injectable, HttpStatus } from '@nestjs/common';
import { isUsernameBlocked } from 'omniboxd/utils/blocked-usernames';
import { isNameBlocked } from 'omniboxd/utils/blocked-names';
import { AppException } from 'omniboxd/common/exceptions/app.exception';
import { I18nService } from 'nestjs-i18n';
import { CacheService } from 'omniboxd/common/cache.service';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class UserService {
}

async create(account: CreateUserDto, manager?: EntityManager) {
if (account.username && isUsernameBlocked(account.username)) {
if (account.username && isNameBlocked(account.username)) {
const message = this.i18n.t('user.errors.accountAlreadyExists');
throw new AppException(
message,
Expand Down Expand Up @@ -341,7 +341,7 @@ export class UserService {
HttpStatus.BAD_REQUEST,
);
}
if (account.username && isUsernameBlocked(account.username)) {
if (account.username && isNameBlocked(account.username)) {
const message = this.i18n.t('user.errors.accountAlreadyExists');
throw new AppException(
message,
Expand Down
Loading