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

com.cloudimpl.outstack.role.service.CreateOrUpdateRole Maven / Gradle / Ivy

There is a newer version: 4.0.551
Show newest version
/*
 * Copyright 2021 nuwan.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloudimpl.outstack.role.service;

import com.cloudimpl.outstack.runtime.AsyncEntityCommandHandler;
import com.cloudimpl.outstack.runtime.AsyncEntityContext;
import com.cloudimpl.outstack.runtime.EntityContext;
import com.cloudimpl.outstack.runtime.EntityIdHelper;
import com.cloudimpl.outstack.runtime.domain.Policy;
import com.cloudimpl.outstack.runtime.domain.PolicyRef;
import com.cloudimpl.outstack.runtime.domain.PolicyRefCreated;
import com.cloudimpl.outstack.runtime.domain.PolicyStatement;
import com.cloudimpl.outstack.runtime.domain.Role;
import com.cloudimpl.outstack.runtime.domain.RoleCreateRequest;
import com.cloudimpl.outstack.runtime.domain.RoleCreated;
import com.cloudimpl.outstack.runtime.domainspec.QueryByIdRequest;
import java.util.Collection;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 *
 * @author nuwan
 */
public class CreateOrUpdateRole extends AsyncEntityCommandHandler{

    @Override
    protected Mono execute(EntityContext context, RoleCreateRequest command) {
        AsyncEntityContext asyncContext = context.asAsyncEntityContext();
        Role role = asyncContext.getEntity().orElseGet(()->asyncContext.create(command.getRoleName(),new RoleCreated(command.getRoleName(),"system")));
        Collection existRefs  = asyncContext.getAllChildEntitiesByType(PolicyRef.class);
        
        Collection newRefs = command.getPolicyRef();
        return Flux.fromIterable(newRefs).flatMap(ref->validateAndCreatePolicyRef(asyncContext,role, ref,existRefs))
                .thenMany(Flux.fromIterable(existRefs).doOnNext(r->asyncContext.delete(r.id())))
                .then(Mono.just(role));
        
    }

    
    private void createOrUpdatePolicy(AsyncEntityContext asyncContext,Role role,Policy policy,PolicyRef ref)
    {
        asyncContext.create(PolicyRef.class,role.id(),EntityIdHelper.idToRefId(policy.id()), new PolicyRefCreated(ref.getDomainOwner(), ref.getDomainContext(),ref.getVersion(), role.getRoleName(), EntityIdHelper.idToRefId(policy.id())));
    }
    
    private Mono validateAndCreatePolicyRef(AsyncEntityContext context,Role role,PolicyRef ref,Collection existRefs)
    {
        return context.sendRequest(ref.getDomainOwner(), ref.getDomainContext(), ref.getVersion(), "PolicyService", QueryByIdRequest.builder().withId(ref.getPolicyRef()).build())
                .doOnNext(p->createOrUpdatePolicy(context, role, p, ref)).doOnNext(p->existRefs.removeIf(r->r.getPolicyRef().equals(ref.getPolicyRef())));
    }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy