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

org.xlcloud.console.entitlements.controllers.AccessTokenEntitlementsEditBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.console.entitlements.controllers;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.xlcloud.console.context.EntitlementEngine;
import org.xlcloud.console.context.IdentityContext;
import org.xlcloud.console.controllers.request.RequestParameters;
import org.xlcloud.console.controllers.request.path.ConsolePathParams;
import org.xlcloud.console.controllers.request.path.ConsolePaths;
import org.xlcloud.console.entitlements.EntitlementDictionaryRepository;
import org.xlcloud.console.entitlements.ExplicitEntitlementDictionary;
import org.xlcloud.console.entitlements.nodes.EntitlementNode;
import org.xlcloud.console.entitlements.nodes.predicates.AccessTokenEntitlementOwnershipPredicate;
import org.xlcloud.console.entitlements.nodes.predicates.EntitlementAvailabilityPredicate;
import org.xlcloud.console.entitlements.nodes.predicates.EntitlementNodePredicate;
import org.xlcloud.console.entitlements.scope.predicates.AccountUserPredicate;
import org.xlcloud.console.entitlements.scope.predicates.EntitlementDictionaryPredicate;
import org.xlcloud.console.entitlements.scope.predicates.GlobalUserPredicate;
import org.xlcloud.console.entitlements.utils.EntitlementDictionaryCollectionUtil;
import org.xlcloud.console.extensions.scope.ViewScoped;
import org.xlcloud.service.AccessToken;
import org.xlcloud.service.Entitlement;
import org.xlcloud.service.Entitlements;
import org.xlcloud.service.User;
import org.xlcloud.service.api.AccessTokensApi;
import org.xlcloud.service.api.UsersApi;

/**
 * TODO Documentation
 *
 * @author Konrad Król, AMG.net
 */
@Named
@ViewScoped
public class AccessTokenEntitlementsEditBean {

    private static final String BASE_DEFAULT_REDIRECTION_URL = "/accessToken/accessToken";

    @Inject
    private EntitlementDictionaryRepository repository;

    @Inject
    private EntitlementEngine entitlementEngine;
    
    @Inject
    private IdentityContext identityContext;

    @Inject
    private RequestParameters requestParameters;

    @Inject
    private AccessTokensApi accessTokensApi;
    
    @Inject
    private UsersApi usersApi;
    
    private TreeNode root;

    private TreeNode[] selectedNodes;

    private EntitlementDictionaryPredicate scopePredicate;

    private EntitlementNodePredicate availabilityPredicate;

    private EntitlementNodePredicate selectionPredicate;

    private AccessToken accessToken;
    
    private User user;

    @PostConstruct
    public void initialize() {
        root = new DefaultTreeNode();
        
        String requestedTokenId = getRequestedTokenId();
        accessToken = accessTokensApi.getAccessToken(requestedTokenId);
        accessToken.setEntitlements(accessTokensApi.getAccessTokenEntitlements(requestedTokenId));
        
        user = usersApi.getUser(accessToken.getUserId());
        
        if (user.getAccountId() != null) {
            scopePredicate = new AccountUserPredicate();
        } else {
            scopePredicate = new GlobalUserPredicate();
        }

        availabilityPredicate = new EntitlementAvailabilityPredicate(entitlementEngine, identityContext, scopePredicate);
        selectionPredicate = new AccessTokenEntitlementOwnershipPredicate(accessToken, user, entitlementEngine, identityContext);

        EntitlementNode highLevelNode = new EntitlementNode(repository.getMainEntitlementDictionary(), availabilityPredicate,
                selectionPredicate, root);
        highLevelNode.setExpanded(true);
    }

    public String updateEntitlements() {
        List selectedEntitlementDictionaries = EntitlementDictionaryCollectionUtil
                .collectExplicitEntitlementDictionaries(selectedNodes);

        Entitlements entitlements = new Entitlements();
        for (ExplicitEntitlementDictionary entitlementDictionary : selectedEntitlementDictionaries) {
            Entitlement instantiatedEntitlement = entitlementDictionary.instantiateEntitlement(user.getAccountId(), user.getId());
            entitlements.getEntitlement().add(instantiatedEntitlement);
        }

        accessTokensApi.updateAccessTokenEntitlements(accessToken.getToken(), entitlements);

        return getRedirectionUrl();
    }
    
    /**
     * Gets the value of {@link #selectedNodes}.
     * @return value of {@link #selectedNodes}
     */
    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }
    
    /**
     * Sets the value of {@link #selectedNodes}.
     * @param selectedNodes - value
     */
    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }
    
    /**
     * Gets the value of {@link #root}.
     * @return value of {@link #root}
     */
    public TreeNode getRoot() {
        return root;
    }
    
    /**
     * Sets the value of {@link #repository}.
     * @param repository - value
     */
    public void setRepository(EntitlementDictionaryRepository repository) {
        this.repository = repository;
    }

    
    /**
     * Sets the value of {@link #entitlementEngine}.
     * @param entitlementEngine - value
     */
    public void setEntitlementEngine(EntitlementEngine entitlementEngine) {
        this.entitlementEngine = entitlementEngine;
    }
    
    /**
     * Sets the value of {@link #requestParameters}.
     * @param requestParameters - value
     */
    public void setRequestParameters(RequestParameters requestParameters) {
        this.requestParameters = requestParameters;
    }
    
    /**
     * Sets the value of {@link #accessTokensApi}.
     * @param accessTokensApi - value
     */
    public void setAccessTokensApi(AccessTokensApi accessTokensApi) {
        this.accessTokensApi = accessTokensApi;
    }
    
    /**
     * Sets the value of {@link #usersApi}.
     * @param usersApi - value
     */
    public void setUsersApi(UsersApi usersApi) {
        this.usersApi = usersApi;
    }
    
    /**
     * Gets the value of {@link #accessToken}.
     * @return value of {@link #accessToken}
     */
    public AccessToken getAccessToken() {
        return accessToken;
    }

    private String getRedirectionUrl() {
        StringBuilder url = new StringBuilder();
        url.append(BASE_DEFAULT_REDIRECTION_URL + "?");
        url.append(ConsolePathParams.ACCESS_TOKEN_ID + "=" + accessToken.getToken());
        url.append("&faces-redirect=true");
        return url.toString(); 
    }

    private String getRequestedTokenId() {
        return requestParameters.getParameter(ConsolePathParams.ACCESS_TOKEN_ID);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy