Skip to content

Commit

Permalink
add account check and lowercase username
Browse files Browse the repository at this point in the history
  • Loading branch information
alweber-cap committed Sep 22, 2023
1 parent 251981f commit ee02ac1
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions apps/server/src/modules/user-import/uc/user-import.uc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Configuration } from '@hpi-schul-cloud/commons';
import { BadRequestException, ForbiddenException, Injectable, InternalServerErrorException } from '@nestjs/common';
import { UserAlreadyAssignedToImportUserError } from '@shared/common';
import {
BadRequestException,
ForbiddenException,
Injectable,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { EntityNotFoundError, UserAlreadyAssignedToImportUserError } from '@shared/common';
import {
Account,
Counted,
Expand All @@ -23,6 +29,7 @@ import { AccountService } from '@src/modules/account/services/account.service';
import { AccountDto } from '@src/modules/account/services/dto/account.dto';
import { AuthorizationService } from '@src/modules/authorization';
import { LegacySchoolService } from '@src/modules/legacy-school';
import { AccountSaveDto } from '../../account/services/dto';
import {
MigrationMayBeCompleted,
MigrationMayNotBeCompleted,
Expand Down Expand Up @@ -281,17 +288,35 @@ export class UserImportUc {
user.ldapDn = importUser.ldapDn;
user.externalId = importUser.externalId;

const account: AccountDto = await this.accountService.findByUserIdOrFail(user.id);
const account: AccountDto = await this.getAccount(user);

account.systemId = importUser.system.id;
account.password = undefined;
account.username = `${school.externalId}/${importUser.loginName}`;
account.username = `${school.externalId}/${importUser.loginName}`.toLowerCase();

await this.userRepo.save(user);
await this.accountService.save(account);
await this.importUserRepo.delete(importUser);
}

private async getAccount(user: User): Promise<AccountDto> {
let account: AccountDto | null = await this.accountService.findByUserId(user.id);

if (!account) {
const newAccount: AccountSaveDto = new AccountSaveDto({
userId: user.id,
username: user.email,
});

await this.accountService.saveWithValidation(newAccount);
account = await this.accountService.findByUserId(user.id);
}
if (account) {
return account;
}
throw new NotFoundException(account);
}

private async getMigrationSystem(): Promise<SystemEntity> {
const systemId = Configuration.get('FEATURE_USER_MIGRATION_SYSTEM_ID') as string;
const system = await this.systemRepo.findById(systemId);
Expand Down

0 comments on commit ee02ac1

Please sign in to comment.