001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.vendor.server.graphql;
018
019import com.echothree.control.user.vendor.common.VendorUtil;
020import com.echothree.control.user.vendor.server.command.GetItemPurchasingCategoriesCommand;
021import com.echothree.control.user.vendor.server.command.GetItemPurchasingCategoryCommand;
022import com.echothree.control.user.vendor.server.command.GetVendorCommand;
023import com.echothree.control.user.vendor.server.command.GetVendorItemCommand;
024import com.echothree.control.user.vendor.server.command.GetVendorItemCostCommand;
025import com.echothree.control.user.vendor.server.command.GetVendorItemCostsCommand;
026import com.echothree.control.user.vendor.server.command.GetVendorItemsCommand;
027import com.echothree.control.user.vendor.server.command.GetVendorTypeCommand;
028import com.echothree.control.user.vendor.server.command.GetVendorTypesCommand;
029import com.echothree.control.user.vendor.server.command.GetVendorsCommand;
030import com.echothree.model.control.graphql.server.util.BaseGraphQl;
031import com.echothree.model.data.party.server.entity.Party;
032import com.echothree.util.common.form.BaseForm;
033import graphql.schema.DataFetchingEnvironment;
034import javax.naming.NamingException;
035
036public interface VendorSecurityUtils {
037    
038    static boolean getHasItemPurchasingCategoryAccess(final DataFetchingEnvironment env) {
039        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetItemPurchasingCategoryCommand.class);
040    }
041
042    static boolean getHasItemPurchasingCategoriesAccess(final DataFetchingEnvironment env) {
043        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetItemPurchasingCategoriesCommand.class);
044    }
045
046    static boolean getHasVendorTypeAccess(final DataFetchingEnvironment env) {
047        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorTypeCommand.class);
048    }
049
050    static boolean getHasVendorTypesAccess(final DataFetchingEnvironment env) {
051        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorTypesCommand.class);
052    }
053
054    static boolean getHasVendorAccess(final DataFetchingEnvironment env, final Party party) {
055        var partyDetail = party.getLastDetail();
056        BaseForm baseForm;
057
058        // GetVendorCommand has a security() function that needs the form to be available.
059        try {
060            var commandForm = VendorUtil.getHome().getGetVendorForm();
061
062            commandForm.setPartyName(partyDetail.getPartyName());
063            baseForm = commandForm;
064        } catch (NamingException ex) {
065            throw new RuntimeException(ex);
066        }
067
068        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorCommand.class, baseForm);
069    }
070
071    static boolean getHasVendorsAccess(final DataFetchingEnvironment env) {
072        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorsCommand.class);
073    }
074
075    static boolean getHasVendorItemAccess(final DataFetchingEnvironment env) {
076        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCommand.class);
077    }
078
079    static boolean getHasVendorItemsAccess(final DataFetchingEnvironment env) {
080        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemsCommand.class);
081    }
082
083    static boolean getHasVendorItemCostAccess(final DataFetchingEnvironment env) {
084        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCostCommand.class);
085    }
086
087    static boolean getHasVendorItemCostsAccess(final DataFetchingEnvironment env) {
088        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCostsCommand.class);
089    }
090
091}