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// -------------------------------------------------------------------------------- 016package com.echothree.model.control.graphql.server.util; 017 018import com.echothree.model.data.user.common.pk.UserVisitPK; 019import com.echothree.model.data.user.server.entity.UserSession; 020import com.echothree.model.data.user.server.entity.UserVisit; 021import com.echothree.util.common.form.BaseForm; 022import com.echothree.util.server.control.GraphQlSecurityCommand; 023import java.util.HashMap; 024import java.util.Map; 025 026public class GraphQlExecutionContext { 027 028 private final UserVisitPK userVisitPK; 029 private final UserVisit userVisit; 030 private final UserSession userSession; 031 private final String remoteInet4Address; 032 033 private final Map<Class<? extends GraphQlSecurityCommand>, Boolean> securityCache = new HashMap<>(); 034 035 public GraphQlExecutionContext(UserVisitPK userVisitPK, UserVisit userVisit, UserSession userSession, String remoteInet4Address) { 036 this.userVisitPK = userVisitPK; 037 this.userVisit = userVisit; 038 this.userSession = userSession; 039 this.remoteInet4Address = remoteInet4Address; 040 } 041 042 public UserVisitPK getUserVisitPK() { 043 return userVisitPK; 044 } 045 046 public UserVisit getUserVisit() { 047 return userVisit; 048 } 049 050 public UserSession getUserSession() { 051 return userSession; 052 } 053 054 public String getRemoteInet4Address() { 055 return remoteInet4Address; 056 } 057 058 public boolean hasAccess(final Class<? extends GraphQlSecurityCommand> command) { 059 return hasAccess(command, null); 060 } 061 062 public boolean hasAccess(final Class<? extends GraphQlSecurityCommand> command, final BaseForm form) { 063 var canCache = form == null; 064 var hasAccess = canCache ? securityCache.get(command) : null; // Bypass if it cannot be cached. 065 066 if(hasAccess == null) { 067 hasAccess = GraphQlSecurityUtils.hasAccess(this, command, form); 068 069 if(canCache) { 070 securityCache.put(command, hasAccess); 071 } 072 } 073 074 return hasAccess; 075 } 076 077}