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

io.github.factoryfx.microservice.rest.MicroserviceResource Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
package io.github.factoryfx.microservice.rest;

import java.util.*;
import java.util.function.Function;

import io.github.factoryfx.factory.merge.MergeDiffInfo;
import io.github.factoryfx.factory.storage.DataUpdate;
import io.github.factoryfx.factory.FactoryBase;
import io.github.factoryfx.factory.storage.StoredDataMetadata;
import io.github.factoryfx.factory.log.FactoryUpdateLog;
import io.github.factoryfx.server.Microservice;
import io.github.factoryfx.server.user.AuthorizedUser;
import io.github.factoryfx.server.user.UserManagement;
import io.github.factoryfx.microservice.common.*;

public class MicroserviceResource,S> implements MicroserviceResourceApi {

    protected final Microservice microservice;
    private final UserManagement userManagement;

    public MicroserviceResource(Microservice microservice, UserManagement userManagement) {
        this.microservice = microservice;
        this.userManagement = userManagement;
    }

    private Optional authenticate(UserAwareRequest request){
        if (userManagement.authorisationRequired()){
            final Optional authorizedUser = userManagement.authenticate(request.user, request.passwordHash);
            if (!authorizedUser.isPresent()){
                throw new IllegalStateException("invalid user");
            }
            return authorizedUser;
        }
        return Optional.empty();
    }

    @Override
    public FactoryUpdateLog updateCurrentFactory(UserAwareRequest> update) {
        update.request.permissionChecker = authenticateAndGetPermissionChecker(update);
        update.request.root.internal().finalise();
        return microservice.updateCurrentFactory(update.request);
    }

    @Override
    public FactoryUpdateLog revert(UserAwareRequest> historyFactory) {
        authenticate(historyFactory);
        return microservice.revertTo(historyFactory.request,historyFactory.user);
    }

    private Function authenticateAndGetPermissionChecker(UserAwareRequest request) {
        final Optional authenticate = authenticate(request);
        Function permissionChecker = (permission)->true;
        if (authenticate.isPresent()){
            permissionChecker = (permission)->authenticate.get().checkPermissionValid(permission);
        }
        return permissionChecker;
    }

    @Override
    public MergeDiffInfo simulateUpdateCurrentFactory(UserAwareRequest> request) {
        request.request.permissionChecker= authenticateAndGetPermissionChecker(request);
        request.request.root.internal().finalise();
        return microservice.simulateUpdateCurrentFactory(request.request);
    }

    @Override
    public MergeDiffInfo getDiff(UserAwareRequest> request) {
        authenticate(request);
        return microservice.getDiffToPreviousVersion(request.request);
    }

    @Override
    public DataUpdate prepareNewFactory(VoidUserAwareRequest request) {
        authenticate(request);
        return microservice.prepareNewFactory();
    }

    @Override
    public ResponseWorkaround getHistoryFactory(UserAwareRequest request) {
        authenticate(request);
        return new ResponseWorkaround<>(microservice.getHistoryFactory(request.request));
    }

    @Override
    public Collection> getHistoryFactoryList(VoidUserAwareRequest request) {
        authenticate(request);
        return microservice.getHistoryFactoryList();
    }

    @Override
    public CheckUserResponse checkUser(VoidUserAwareRequest request){
        return new CheckUserResponse(userManagement.authenticate(request.user,request.passwordHash).isPresent());
    }

    @Override
    public UserLocaleResponse getUserLocale(VoidUserAwareRequest request){
        final Optional authenticate = authenticate(request);
        return authenticate.map(authorizedUser -> new UserLocaleResponse(authorizedUser.getLocale())).orElseGet(() -> new UserLocaleResponse(Locale.ENGLISH));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy