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.user.server.logic; 018 019import com.echothree.model.control.user.server.control.UserControl; 020import com.echothree.model.data.party.server.entity.Party; 021import com.echothree.model.data.party.server.entity.PartyRelationship; 022import com.echothree.model.data.user.common.pk.UserVisitPK; 023import com.echothree.model.data.user.server.entity.UserSession; 024import com.echothree.model.data.user.server.entity.UserVisit; 025import com.echothree.model.data.user.server.factory.UserSessionFactory; 026import com.echothree.util.server.persistence.EntityPermission; 027import com.echothree.util.server.persistence.Session; 028import javax.enterprise.context.ApplicationScoped; 029import javax.enterprise.inject.spi.CDI; 030 031@ApplicationScoped 032public class UserSessionLogic { 033 034 protected UserSessionLogic() { 035 super(); 036 } 037 038 public static UserSessionLogic getInstance() { 039 return CDI.current().select(UserSessionLogic.class).get(); 040 } 041 042 /** 043 * Invalidating the UserSession will clear the PasswordVerified time if necessary. 044 * 045 * @param userSession The UserSession that should be invalidated. 046 * @return An invalid UserSession (may be the same as the userSession parameter). 047 */ 048 public UserSession invalidateUserSession(UserSession userSession) { 049 if(userSession.getIdentityVerifiedTime() != null) { 050 var userControl = Session.getModelController(UserControl.class); 051 052 if(!userSession.getEntityPermission().equals(EntityPermission.READ_WRITE)) { 053 userSession = UserSessionFactory.getInstance().getEntityFromPK(EntityPermission.READ_WRITE, userSession.getPrimaryKey()); 054 } 055 056 userControl.deleteUserSession(userSession); 057 058 userSession = userControl.createUserSession(userSession.getUserVisitForUpdate(), userSession.getParty(), userSession.getPartyRelationship(), null); 059 } 060 061 return userSession; 062 } 063 064 /** Sets the Party and PartyRelationship to null when a UserSession contains the specified Party. 065 */ 066 public void deleteUserSessionsByParty(Party party) { 067 var userControl = Session.getModelController(UserControl.class); 068 069 userControl.deleteUserSessions(userControl.getUserSessionsByPartyForUpdate(party)); 070 } 071 072 /** Sets the Party and PartyRelationship to null when a UserSession contains the specified PartyRelationship. 073 */ 074 public void deleteUserSessionsByPartyRelationship(PartyRelationship partyRelationship) { 075 var userControl = Session.getModelController(UserControl.class); 076 077 userControl.deleteUserSessions(userControl.getUserSessionsByPartyRelationshipForUpdate(partyRelationship)); 078 } 079 080 public UserSession deleteUserSessionByUserVisit(UserVisit userVisit) { 081 var userControl = Session.getModelController(UserControl.class); 082 var userSession = userControl.getUserSessionByUserVisitForUpdate(userVisit); 083 084 if(userSession != null) { 085 userControl.deleteUserSession(userSession); 086 } 087 088 return userSession; 089 } 090 091 public UserSession deleteUserSessionByUserVisitPK(UserVisitPK userVisitPK) { 092 var userControl = Session.getModelController(UserControl.class); 093 094 return deleteUserSessionByUserVisit(userControl.getUserVisitByPKForUpdate(userVisitPK)); 095 } 096 097}