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

ru.foodtechlab.lib.auth.service.domain.role.usecases.GenerateDefaultRoleUseCase Maven / Gradle / Ivy

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

import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.commons.usecase.model.SingleOutput;
import com.rcore.domain.commons.usecase.model.VoidInputValues;
import lombok.RequiredArgsConstructor;
import ru.foodtechlab.lib.auth.service.domain.auth.exceptions.RoleCodeIsRequiredException;
import ru.foodtechlab.lib.auth.service.domain.role.entity.RoleEntity;
import ru.foodtechlab.lib.auth.service.domain.preference.usecase.GetServicePreferenceUseCase;
import ru.foodtechlab.lib.auth.service.domain.roleAccess.usecase.GenerateGodModAccessUseCase;

import java.util.List;

@RequiredArgsConstructor
public class GenerateDefaultRoleUseCase extends UseCase> {

    private final FindRoleByCodeUseCase findRoleByName;
    private final UpdateRoleUseCase updateRoleUseCase;
    private final CreateRoleUseCase createRole;
    private final GetServicePreferenceUseCase getSettings;
    private final GenerateGodModAccessUseCase godModAccessUseCase; // На первое время для хоть какого-то доступа USER-ам

    @Override
    public SingleOutput execute(VoidInputValues voidInputValues) {
        var settings = getSettings.execute(new VoidInputValues()).getValue();
        if (settings.getDefaultRoleCode() == null)
            throw new RoleCodeIsRequiredException();
        var role = findRoleByName.execute(FindRoleByCodeUseCase.InputValues.of(settings.getDefaultRoleCode()))
                .getEntity().orElseGet(() -> createRole.execute(CreateRoleUseCase.InputValues.builder()
                        .code(settings.getDefaultRoleCode())
                        .accessIds(List.of(godModAccessUseCase.execute(new VoidInputValues()).getValue().getId()))
                        .build()).getEntity());
        if (role.getAccesses() == null || role.getAccesses().isEmpty()) {
            role = updateRoleUseCase.execute(
                    UpdateRoleUseCase.InputValues.builder()
                            .id(role.getId())
                            .accessIds(List.of(godModAccessUseCase.execute(new VoidInputValues()).getValue().getId()))
                            .code(role.getCode())
                            .name(role.getName())
                            .isRegistrationAllowed(role.isRegistrationAllowed())
                            .build()
                    ).getEntity();
        }

        return SingleOutput.of(role);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy