Skip to content

Commit

Permalink
fix(user): Add check user name and email on update profile, create an…
Browse files Browse the repository at this point in the history
…d update user
  • Loading branch information
EndyKaufman committed Dec 9, 2018
1 parent c9d6d3c commit ab770a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 50 deletions.
44 changes: 13 additions & 31 deletions src/libs/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AuthService {
if (this.coreConfig.port) {
this.localUri = `http://${this.coreConfig.domain}:${
this.coreConfig.port
}`;
}`;
} else {
this.localUri = `http://${this.coreConfig.domain}`;
}
Expand Down Expand Up @@ -69,31 +69,13 @@ export class AuthService {
} catch (error) {
throw new BadRequestException('Error in load groups');
}
if (options.email) {
let userOfEmail: { user };
try {
userOfEmail = await this.usersService.findByEmail(options);
} catch (error) {
userOfEmail = undefined;
}
if (userOfEmail) {
throw new ConflictException(
`User with email "${options.email}" is exists`
);
}
}
if (options.username) {
let userOfUsername: { user };
try {
userOfUsername = await this.usersService.findByUserName(options);
} catch (error) {
userOfUsername = undefined;
}
if (userOfUsername) {
throw new ConflictException(
`User with username "${options.username}" is exists`
);
}
try {
await this.usersService.assertUsernameAndEmail({
email: options.email,
username: options.username
});
} catch (error) {
throw error;
}
const group = this.groupsService.getGroupByName({ name: 'user' });
const newUser = await plainToClass(User, options).setPassword(
Expand All @@ -110,7 +92,7 @@ export class AuthService {
];
const redirect_uri: string = `${
this.fbConfig.login_dialog_uri
}?${queryParams.join('&')}`.replace('{host}', host);
}?${queryParams.join('&')}`.replace('{host}', host);
Logger.log(redirect_uri, AuthService.name + ':requestFacebookRedirectUri');
return {
redirect_uri
Expand Down Expand Up @@ -167,9 +149,9 @@ export class AuthService {
);
throw new BadRequestException(
error &&
error.response &&
error.response.data &&
error.response.data.error
error.response &&
error.response.data &&
error.response.data.error
? error.response.data.error.message
: error.message
);
Expand All @@ -184,7 +166,7 @@ export class AuthService {
];
const redirect_uri: string = `${
this.googlePlusConfig.login_dialog_uri
}?${queryParams.join('&')}`.replace('{host}', host);
}?${queryParams.join('&')}`.replace('{host}', host);
Logger.log(redirect_uri, AuthService.name + ':requestGoogleRedirectUri');
return {
redirect_uri
Expand Down
1 change: 0 additions & 1 deletion src/libs/core/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class User {

@Column({ length: 150, unique: true })
@MaxLength(150)
@IsOptional()
username: string = undefined;

@Column({ name: 'first_name', length: 30, nullable: true })
Expand Down
2 changes: 1 addition & 1 deletion src/libs/core/migrations/1524197725191-Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,5 @@ export class Init1524197725191 implements MigrationInterface {
);
}

public async down(queryRunner: QueryRunner): Promise<any> {}
public async down(queryRunner: QueryRunner): Promise<any> { }
}
17 changes: 9 additions & 8 deletions src/libs/core/services/account.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { Inject, Injectable, MethodNotAllowedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClassFromExist } from 'class-transformer';
import { Repository } from 'typeorm';
import { CORE_CONFIG_TOKEN } from '../configs/core.config';
import { User } from '../entities/user.entity';
import { ICoreConfig } from '../interfaces/core-config.interface';
import { UsersService } from './users.service';

@Injectable()
export class AccountService {
constructor(
@Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
@InjectRepository(User) private readonly usersRepository: Repository<User>
) {}
private readonly usersService: UsersService
) { }
async update(options: { id: number; user: User }) {
if (this.coreConfig.demo) {
throw new MethodNotAllowedException('Not allowed in DEMO mode');
}
try {
let user = await this.usersRepository.findOneOrFail(options.id, {
relations: ['groups', 'groups.permissions']
await this.usersService.assertUsernameAndEmail({
id: options.id,
email: options.user.email,
username: options.user.username
});
let { user } = await this.usersService.findById(options);
user = plainToClassFromExist(user, options.user);
await user.setPassword(options.user.password);
user = await this.usersRepository.save(user);
return user;
return await this.usersService.update({ id: options.id, item: user });
} catch (error) {
throw error;
}
Expand Down
51 changes: 42 additions & 9 deletions src/libs/core/services/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Inject,
Injectable,
MethodNotAllowedException,
NotFoundException
} from '@nestjs/common';
import { ConflictException, Inject, Injectable, MethodNotAllowedException, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CORE_CONFIG_TOKEN } from '../configs/core.config';
Expand All @@ -15,12 +10,45 @@ export class UsersService {
constructor(
@Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
@InjectRepository(User) private readonly repository: Repository<User>
) {}
) { }
async assertUsernameAndEmail(options: { id?: number, email: string, username: string }) {
if (options.email) {
let userOfEmail: { user };
try {
userOfEmail = await this.findByEmail(options);
} catch (error) {
userOfEmail = undefined;
}
if (userOfEmail && userOfEmail.user.id !== options.id) {
throw new ConflictException(
`User with email "${options.email}" is exists`
);
}
}
if (options.username) {
let userOfUsername: { user };
try {
userOfUsername = await this.findByUserName(options);
} catch (error) {
userOfUsername = undefined;
}
if (userOfUsername && userOfUsername.user.id !== options.id) {
throw new ConflictException(
`User with username "${options.username}" is exists`
);
}
}
}
async create(options: { item: User }) {
if (options.item.isSuperuser && this.coreConfig.demo) {
throw new MethodNotAllowedException('Not allowed in DEMO mode');
}
try {
await this.assertUsernameAndEmail({
id: options.item.id,
email: options.item.email,
username: options.item.username
});
options.item = await this.repository.save(options.item);
const { user } = await this.findById({ id: options.item.id });
return { user };
Expand All @@ -32,9 +60,14 @@ export class UsersService {
if (this.coreConfig.demo) {
throw new MethodNotAllowedException('Not allowed in DEMO mode');
}
options.item.lastLogin = new Date();
options.item.id = options.id;
try {
await this.assertUsernameAndEmail({
id: options.item.id,
email: options.item.email,
username: options.item.username
});
options.item.lastLogin = new Date();
options.item.id = options.id;
options.item = await this.repository.save(options.item);
const { user } = await this.findById({ id: options.item.id });
return { user };
Expand Down

0 comments on commit ab770a7

Please sign in to comment.