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