001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.customer.server.graphql;
018
019import com.echothree.control.user.customer.common.CustomerUtil;
020import com.echothree.control.user.customer.common.form.GetCustomerForm;
021import com.echothree.control.user.customer.server.command.GetCustomerCommand;
022import com.echothree.control.user.customer.server.command.GetCustomerTypeCommand;
023import com.echothree.control.user.customer.server.command.GetCustomerTypePaymentMethodCommand;
024import com.echothree.control.user.customer.server.command.GetCustomerTypePaymentMethodsCommand;
025import com.echothree.control.user.customer.server.command.GetCustomerTypeShippingMethodCommand;
026import com.echothree.control.user.customer.server.command.GetCustomerTypeShippingMethodsCommand;
027import com.echothree.control.user.customer.server.command.GetCustomerTypesCommand;
028import com.echothree.control.user.customer.server.command.GetCustomersCommand;
029import com.echothree.model.control.graphql.server.util.BaseGraphQl;
030import com.echothree.model.data.party.server.entity.Party;
031import graphql.schema.DataFetchingEnvironment;
032import javax.naming.NamingException;
033
034public interface CustomerSecurityUtils {
035
036    static boolean getHasCustomerTypeAccess(final DataFetchingEnvironment env) {
037        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypeCommand.class);
038    }
039
040    static boolean getHasCustomerTypesAccess(final DataFetchingEnvironment env) {
041        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypesCommand.class);
042    }
043
044    static boolean getHasCustomerTypePaymentMethodAccess(final DataFetchingEnvironment env) {
045        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypePaymentMethodCommand.class);
046    }
047
048    static boolean getHasCustomerTypePaymentMethodsAccess(final DataFetchingEnvironment env) {
049        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypePaymentMethodsCommand.class);
050    }
051
052    static boolean getHasCustomerTypeShippingMethodAccess(final DataFetchingEnvironment env) {
053        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypeShippingMethodCommand.class);
054    }
055
056    static boolean getHasCustomerTypeShippingMethodsAccess(final DataFetchingEnvironment env) {
057        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerTypeShippingMethodsCommand.class);
058    }
059
060    static boolean getHasCustomerAccess(final DataFetchingEnvironment env, final Party party) {
061        var partyDetail = party.getLastDetail();
062        GetCustomerForm commandForm;
063
064        // GetCustomerCommand has a security() function that needs the form to be available.
065        try {
066            commandForm = CustomerUtil.getHome().getGetCustomerForm();
067            commandForm.setPartyName(partyDetail.getPartyName());
068        } catch (NamingException ex) {
069            throw new RuntimeException(ex);
070        }
071
072        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerCommand.class, commandForm);
073    }
074
075    static boolean getHasCustomersAccess(final DataFetchingEnvironment env) {
076        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomersCommand.class);
077    }
078
079}