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.party.server.graphql;
018
019import com.echothree.control.user.customer.common.CustomerUtil;
020import com.echothree.control.user.customer.server.command.GetCustomerCommand;
021import com.echothree.control.user.employee.common.EmployeeUtil;
022import com.echothree.control.user.employee.server.command.GetEmployeeCommand;
023import com.echothree.control.user.party.common.PartyUtil;
024import com.echothree.control.user.party.server.command.GetCompanyCommand;
025import com.echothree.control.user.party.server.command.GetDateTimeFormatCommand;
026import com.echothree.control.user.party.server.command.GetDepartmentCommand;
027import com.echothree.control.user.party.server.command.GetDepartmentsCommand;
028import com.echothree.control.user.party.server.command.GetDivisionCommand;
029import com.echothree.control.user.party.server.command.GetDivisionsCommand;
030import com.echothree.control.user.party.server.command.GetLanguageCommand;
031import com.echothree.control.user.party.server.command.GetPartyAliasCommand;
032import com.echothree.control.user.party.server.command.GetPartyAliasTypeCommand;
033import com.echothree.control.user.party.server.command.GetPartyAliasTypesCommand;
034import com.echothree.control.user.party.server.command.GetPartyAliasesCommand;
035import com.echothree.control.user.party.server.command.GetPartyRelationshipCommand;
036import com.echothree.control.user.party.server.command.GetPartyRelationshipsCommand;
037import com.echothree.control.user.party.server.command.GetPartyTypeCommand;
038import com.echothree.control.user.party.server.command.GetPartyTypesCommand;
039import com.echothree.control.user.party.server.command.GetTimeZoneCommand;
040import com.echothree.control.user.vendor.common.VendorUtil;
041import com.echothree.control.user.vendor.server.command.GetVendorCommand;
042import com.echothree.control.user.warehouse.server.command.GetWarehouseCommand;
043import com.echothree.model.control.graphql.server.util.BaseGraphQl;
044import com.echothree.model.control.party.common.PartyTypes;
045import com.echothree.model.data.party.server.entity.Party;
046import graphql.schema.DataFetchingEnvironment;
047import javax.naming.NamingException;
048
049public interface PartySecurityUtils {
050
051    static boolean getHasPartyTypeAccess(final DataFetchingEnvironment env) {
052        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyTypeCommand.class);
053    }
054
055    static boolean getHasPartyTypesAccess(final DataFetchingEnvironment env) {
056        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyTypesCommand.class);
057    }
058
059    static boolean getHasPartyAliasTypeAccess(final DataFetchingEnvironment env) {
060        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyAliasTypeCommand.class);
061    }
062
063    static boolean getHasPartyAliasTypesAccess(final DataFetchingEnvironment env) {
064        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyAliasTypesCommand.class);
065    }
066
067    static boolean getHasPartyAliasAccess(final DataFetchingEnvironment env, final Party targetParty) {
068        try {
069            var commandForm = PartyUtil.getHome().getGetPartyAliasForm();
070
071            commandForm.setPartyName(targetParty.getLastDetail().getPartyName());
072
073            return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyAliasCommand.class, commandForm);
074        } catch (NamingException ex) {
075            throw new RuntimeException(ex);
076        }
077    }
078
079    static boolean getHasPartyAliasesAccess(final DataFetchingEnvironment env, final Party targetParty) {
080        try {
081            var commandForm = PartyUtil.getHome().getGetPartyAliasesForm();
082
083            commandForm.setPartyName(targetParty.getLastDetail().getPartyName());
084
085            return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyAliasesCommand.class, commandForm);
086        } catch (NamingException ex) {
087            throw new RuntimeException(ex);
088        }
089    }
090
091    static boolean getHasPartyRelationshipAccess(final DataFetchingEnvironment env) {
092        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyRelationshipCommand.class);
093    }
094
095    static boolean getHasPartyRelationshipsAccess(final DataFetchingEnvironment env) {
096        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetPartyRelationshipsCommand.class);
097    }
098
099    static boolean getHasLanguageAccess(final DataFetchingEnvironment env) {
100        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetLanguageCommand.class);
101    }
102
103    static boolean getHasTimeZoneAccess(final DataFetchingEnvironment env) {
104        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetTimeZoneCommand.class);
105    }
106
107    static boolean getHasDateTimeFormatAccess(final DataFetchingEnvironment env) {
108        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetDateTimeFormatCommand.class);
109    }
110
111    static boolean getHasDivisionsAccess(final DataFetchingEnvironment env) {
112        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetDivisionsCommand.class);
113    }
114
115    static boolean getHasDepartmentsAccess(final DataFetchingEnvironment env) {
116        return BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetDepartmentsCommand.class);
117    }
118
119    static boolean getHasPartyAccess(final DataFetchingEnvironment env, final Party targetParty) {
120        var targetPartyDetail = targetParty.getLastDetail();
121        var targetPartyTypeEnum = PartyTypes.valueOf(targetPartyDetail.getPartyType().getPartyTypeName());
122
123        return switch(targetPartyTypeEnum) {
124            case CUSTOMER -> {
125                try {
126                    var commandForm = CustomerUtil.getHome().getGetCustomerForm();
127    
128                    commandForm.setPartyName(targetPartyDetail.getPartyName());
129
130                    yield BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCustomerCommand.class, commandForm);
131                } catch (NamingException ex) {
132                    throw new RuntimeException(ex);
133                }
134            }
135            case EMPLOYEE -> {
136                try {
137                    var commandForm = EmployeeUtil.getHome().getGetEmployeeForm();
138
139                    commandForm.setPartyName(targetPartyDetail.getPartyName());
140
141                    yield BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetEmployeeCommand.class, commandForm);
142                } catch (NamingException ex) {
143                    throw new RuntimeException(ex);
144                }
145            }
146            case VENDOR -> {
147                try {
148                    var commandForm = VendorUtil.getHome().getGetVendorForm();
149
150                    commandForm.setPartyName(targetPartyDetail.getPartyName());
151
152                    yield BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetVendorCommand.class, commandForm);
153                } catch (NamingException ex) {
154                    throw new RuntimeException(ex);
155                }
156            }
157            case COMPANY -> BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetCompanyCommand.class);
158            case DIVISION -> BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetDivisionCommand.class);
159            case DEPARTMENT -> BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetDepartmentCommand.class);
160            case WAREHOUSE -> BaseGraphQl.getGraphQlExecutionContext(env).hasAccess(GetWarehouseCommand.class);
161            default -> throw new RuntimeException("Unhandled PartyType: " + targetPartyTypeEnum);
162        };
163    }
164
165}