All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.foodtechlab.lib.auth.service.domain.credential.usecases.InitCredentialsUseCase Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package ru.foodtechlab.lib.auth.service.domain.credential.usecases;

import com.rcore.commons.utils.PasswordCryptographer;
import com.rcore.commons.utils.PhoneNumberFormatter;
import com.rcore.commons.utils.PhoneNumberValidator;
import com.rcore.commons.utils.StringUtils;
import com.rcore.domain.commons.usecase.AbstractCreateUseCase;
import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.commons.usecase.model.SingletonEntityOutputValues;
import com.rcore.domain.commons.validators.ValidationDomain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.foodtechlab.lib.auth.service.domain.Domain;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.Email;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.PhoneNumber;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.CredentialWithPhoneAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.CredentialWithUsernameAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.IncorrectConfirmationCodeException;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.InitCredentialAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.credential.port.CredentialIdGenerator;
import ru.foodtechlab.lib.auth.service.domain.credential.port.CredentialRepository;
import ru.foodtechlab.lib.auth.service.domain.role.entity.RoleEntity;
import ru.foodtechlab.lib.auth.service.domain.role.usecases.CreateRoleUseCase;
import ru.foodtechlab.lib.auth.service.domain.role.usecases.FindRoleByCodeUseCase;
import ru.foodtechlab.lib.auth.service.domain.preference.service.PersonalCodeResolver;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;

public class InitCredentialsUseCase extends AbstractCreateUseCase, CredentialRepository, InitCredentialsUseCase.InputValues> {

    private final FindRoleByCodeUseCase findRoleByCode;
    private final CreateRoleUseCase createRole;
    private final FindCredentialByPhoneNumberUseCase findCredentialByPhone;
    private final FindCredentialByUsernameUseCase findCredentialByUsername;
    private final PasswordCryptographer passwordCryptographer;
    private final PersonalCodeResolver personalCodeResolver;
    private final CheckAvailableInitCredentialsUseCase checkAvailable;
    private final String secretWord;

    public InitCredentialsUseCase(CredentialRepository repository,
                                  CredentialIdGenerator idGenerator,
                                  FindRoleByCodeUseCase findRoleByCode,
                                  CreateRoleUseCase createRole,
                                  FindCredentialByPhoneNumberUseCase findCredentialByPhone,
                                  FindCredentialByUsernameUseCase findCredentialByUsername,
                                  PasswordCryptographer passwordCryptographer,
                                  PersonalCodeResolver personalCodeResolver,
                                  CheckAvailableInitCredentialsUseCase checkAvailable,
                                  String secretWord) {
        super(repository, idGenerator);
        this.findRoleByCode = findRoleByCode;
        this.createRole = createRole;
        this.findCredentialByPhone = findCredentialByPhone;
        this.findCredentialByUsername = findCredentialByUsername;
        this.passwordCryptographer = passwordCryptographer;
        this.personalCodeResolver = personalCodeResolver;
        this.checkAvailable = checkAvailable;
        this.secretWord = secretWord;
    }

    @Override
    public SingletonEntityOutputValues execute(InitCredentialsUseCase.InputValues inputValues) {

        validate(inputValues);

        List roles = new ArrayList<>();

        //Проверка ролей
        findRoleByCode.execute(FindRoleByCodeUseCase.InputValues.of("SUPER_USER")).getEntity()
                .ifPresentOrElse(roles::add,
                        () -> roles.add(createRole.execute(CreateRoleUseCase.InputValues.builder()
                                .code("SUPER_USER")
                                .build()).getEntity()));

        // добавить проверку что вообще доступно создание
        var credential = new CredentialEntity();
        credential.setId(idGenerator.generate());
        credential.setEmail(new Email(inputValues.getEmail(), false));
        credential.setPassword(passwordCryptographer.encrypt(inputValues.getPassword(), credential.getId()));
        credential.setPhoneNumber(new PhoneNumber(inputValues.getPhoneNumber(), inputValues.getIsoTwoLetterCountryCode()));
        credential.setPersonalConfirmationCode(personalCodeResolver.resolve(inputValues.getPersonalConfirmationCode()));
        credential.setRoles(roles
                .stream()
                .map(role -> new CredentialEntity.Role(role, false))
                .collect(Collectors.toList()));
        credential.setBlocked(false);
        credential.setUsername(inputValues.getUsername());

        return SingletonEntityOutputValues.of(repository.save(credential));
    }

    @ValidationDomain(domainName = Domain.CREDENTIAL)
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Data
    public static class InputValues implements UseCase.InputValues {

        @NotBlank
        protected String confirmationCode;

        @NotBlank
        protected String username;

        @NotBlank
        protected String password;

        protected String phoneNumber;
        protected String isoTwoLetterCountryCode;
        protected Locale locale;

        @NotBlank
        protected String email;

        protected String personalConfirmationCode;
    }

    private void validate(InitCredentialsUseCase.InputValues inputValues) {

        Boolean isInitAvailable = checkAvailable.execute(CheckAvailableInitCredentialsUseCase.InputValues.builder().build()).getValue();
        if(isInitAvailable == false)
            throw new InitCredentialAlreadyExistException();

        if (!inputValues.confirmationCode.equals(secretWord))
            throw new IncorrectConfirmationCodeException();

        if (inputValues.getPhoneNumber() != null && StringUtils.hasText(inputValues.phoneNumber))
            if (findCredentialByPhone
                    .execute(FindCredentialByPhoneNumberUseCase.InputValues.of(inputValues.phoneNumber))
                    .getValue()
                    .isPresent())
                throw new CredentialWithPhoneAlreadyExistException();

        Optional credentialWithUsername = findCredentialByUsername.execute(FindCredentialByUsernameUseCase.InputValues.of(inputValues.getUsername()))
                .getValue();
        if (credentialWithUsername.isPresent())
            throw new CredentialWithUsernameAlreadyExistException();

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy