001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.common.form.GetVendorForm;
021import com.echothree.control.user.vendor.server.command.GetItemPurchasingCategoriesCommand;
022import com.echothree.control.user.vendor.server.command.GetItemPurchasingCategoryCommand;
023import com.echothree.control.user.vendor.server.command.GetVendorCommand;
024import com.echothree.control.user.vendor.server.command.GetVendorItemCommand;
025import com.echothree.control.user.vendor.server.command.GetVendorItemCostCommand;
026import com.echothree.control.user.vendor.server.command.GetVendorItemCostsCommand;
027import com.echothree.control.user.vendor.server.command.GetVendorItemsCommand;
028import com.echothree.control.user.vendor.server.command.GetVendorTypeCommand;
029import com.echothree.control.user.vendor.server.command.GetVendorTypesCommand;
030import com.echothree.control.user.vendor.server.command.GetVendorsCommand;
031import com.echothree.model.control.graphql.server.util.BaseGraphQl;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.util.common.form.BaseForm;
034import graphql.schema.DataFetchingEnvironment;
035import javax.naming.NamingException;
036
037public interface VendorSecurityUtils {
038    
039    static boolean getHasItemPurchasingCategoryAccess(final DataFetchingEnvironment env) {
040        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetItemPurchasingCategoryCommand.class);
041    }
042
043    static boolean getHasItemPurchasingCategoriesAccess(final DataFetchingEnvironment env) {
044        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetItemPurchasingCategoriesCommand.class);
045    }
046
047    static boolean getHasVendorTypeAccess(final DataFetchingEnvironment env) {
048        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorTypeCommand.class);
049    }
050
051    static boolean getHasVendorTypesAccess(final DataFetchingEnvironment env) {
052        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorTypesCommand.class);
053    }
054
055    static boolean getHasVendorAccess(final DataFetchingEnvironment env, final Party party) {
056        var partyDetail = party.getLastDetail();
057        GetVendorForm commandForm;
058
059        // GetVendorCommand has a security() function that needs the form to be available.
060        try {
061            commandForm = VendorUtil.getHome().getGetVendorForm();
062            commandForm.setPartyName(partyDetail.getPartyName());
063        } catch (NamingException ex) {
064            throw new RuntimeException(ex);
065        }
066
067        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorCommand.class, commandForm);
068    }
069
070    static boolean getHasVendorsAccess(final DataFetchingEnvironment env) {
071        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorsCommand.class);
072    }
073
074    static boolean getHasVendorItemAccess(final DataFetchingEnvironment env) {
075        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCommand.class);
076    }
077
078    static boolean getHasVendorItemsAccess(final DataFetchingEnvironment env) {
079        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemsCommand.class);
080    }
081
082    static boolean getHasVendorItemCostAccess(final DataFetchingEnvironment env) {
083        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCostCommand.class);
084    }
085
086    static boolean getHasVendorItemCostsAccess(final DataFetchingEnvironment env) {
087        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorItemCostsCommand.class);
088    }
089
090}