mirror of
https://github.com/docmost/docmost.git
synced 2025-11-10 06:52:07 +10:00
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
|
import {
|
|
BadRequestException,
|
|
Injectable,
|
|
NotFoundException,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
|
import { comparePasswordHash } from 'src/common/helpers/utils';
|
|
import { Workspace } from '@docmost/db/types/entity.types';
|
|
import { validateSsoEnforcement } from '../auth/auth.util';
|
|
|
|
@Injectable()
|
|
export class UserService {
|
|
constructor(private userRepo: UserRepo) {}
|
|
|
|
async findById(userId: string, workspaceId: string) {
|
|
return this.userRepo.findById(userId, workspaceId);
|
|
}
|
|
|
|
async update(
|
|
updateUserDto: UpdateUserDto,
|
|
userId: string,
|
|
workspace: Workspace,
|
|
) {
|
|
const includePassword =
|
|
updateUserDto.email != null && updateUserDto.confirmPassword != null;
|
|
|
|
const user = await this.userRepo.findById(userId, workspace.id, {
|
|
includePassword,
|
|
});
|
|
|
|
if (!user) {
|
|
throw new NotFoundException('User not found');
|
|
}
|
|
|
|
// preference update
|
|
if (typeof updateUserDto.fullPageWidth !== 'undefined') {
|
|
return this.userRepo.updatePreference(
|
|
userId,
|
|
'fullPageWidth',
|
|
updateUserDto.fullPageWidth,
|
|
);
|
|
}
|
|
|
|
if (typeof updateUserDto.pageEditMode !== 'undefined') {
|
|
return this.userRepo.updatePreference(
|
|
userId,
|
|
'pageEditMode',
|
|
updateUserDto.pageEditMode.toLowerCase(),
|
|
);
|
|
}
|
|
|
|
if (updateUserDto.name) {
|
|
user.name = updateUserDto.name;
|
|
}
|
|
|
|
if (updateUserDto.email && user.email != updateUserDto.email) {
|
|
validateSsoEnforcement(workspace);
|
|
|
|
if (!updateUserDto.confirmPassword) {
|
|
throw new BadRequestException(
|
|
'You must provide a password to change your email',
|
|
);
|
|
}
|
|
|
|
const isPasswordMatch = await comparePasswordHash(
|
|
updateUserDto.confirmPassword,
|
|
user.password,
|
|
);
|
|
|
|
if (!isPasswordMatch) {
|
|
throw new BadRequestException('You must provide the correct password to change your email');
|
|
}
|
|
|
|
if (await this.userRepo.findByEmail(updateUserDto.email, workspace.id)) {
|
|
throw new BadRequestException('A user with this email already exists');
|
|
}
|
|
|
|
user.email = updateUserDto.email;
|
|
}
|
|
|
|
if (updateUserDto.avatarUrl) {
|
|
user.avatarUrl = updateUserDto.avatarUrl;
|
|
}
|
|
|
|
if (updateUserDto.locale) {
|
|
user.locale = updateUserDto.locale;
|
|
}
|
|
|
|
if (typeof updateUserDto.is2faEnabled !== 'undefined') {
|
|
user.is2faEnabled = updateUserDto.is2faEnabled;
|
|
}
|
|
if (typeof updateUserDto.twofaSecret !== 'undefined') {
|
|
user.twofaSecret = updateUserDto.twofaSecret;
|
|
}
|
|
if (typeof updateUserDto.twofaMethod !== 'undefined') {
|
|
user.twofaMethod = updateUserDto.twofaMethod;
|
|
}
|
|
if (typeof updateUserDto.twofaBackupCodes !== 'undefined') {
|
|
user.twofaBackupCodes = updateUserDto.twofaBackupCodes;
|
|
}
|
|
|
|
delete updateUserDto.confirmPassword;
|
|
|
|
await this.userRepo.updateUser(updateUserDto, userId, workspace.id);
|
|
return user;
|
|
}
|
|
}
|