mirror of
https://github.com/docmost/docmost.git
synced 2026-05-19 16:04:17 +08:00
@@ -1,18 +1,27 @@
|
||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||
import {
|
||||
BadRequestException,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { comparePasswordHash } from 'src/common/helpers/utils';
|
||||
import { comparePasswordHash, diffAuditTrackedFields } from 'src/common/helpers/utils';
|
||||
import { Workspace } from '@docmost/db/types/entity.types';
|
||||
import { validateSsoEnforcement } from '../auth/auth.util';
|
||||
import { AuditEvent, AuditResource } from '../../common/events/audit-events';
|
||||
import {
|
||||
AUDIT_SERVICE,
|
||||
IAuditService,
|
||||
} from '../../integrations/audit/audit.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private userRepo: UserRepo) {}
|
||||
constructor(
|
||||
private userRepo: UserRepo,
|
||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||
) {}
|
||||
|
||||
async findById(userId: string, workspaceId: string) {
|
||||
return this.userRepo.findById(userId, workspaceId);
|
||||
@@ -51,6 +60,8 @@ export class UserService {
|
||||
);
|
||||
}
|
||||
|
||||
const userBefore = { name: user.name, email: user.email, locale: user.locale };
|
||||
|
||||
if (updateUserDto.name) {
|
||||
user.name = updateUserDto.name;
|
||||
}
|
||||
@@ -91,6 +102,23 @@ export class UserService {
|
||||
delete updateUserDto.confirmPassword;
|
||||
|
||||
await this.userRepo.updateUser(updateUserDto, userId, workspace.id);
|
||||
|
||||
const changes = diffAuditTrackedFields(
|
||||
['name', 'email'],
|
||||
updateUserDto,
|
||||
userBefore,
|
||||
user,
|
||||
);
|
||||
|
||||
if (changes) {
|
||||
this.auditService.log({
|
||||
event: AuditEvent.USER_UPDATED,
|
||||
resourceType: AuditResource.USER,
|
||||
resourceId: userId,
|
||||
changes,
|
||||
});
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user