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