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.control.user.authentication.server;
018
019import com.echothree.control.user.authentication.common.AuthenticationRemote;
020import com.echothree.control.user.authentication.common.form.*;
021import com.echothree.control.user.authentication.server.command.*;
022import com.echothree.model.control.party.common.PartyNames;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.user.server.control.UserControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.exception.PersistenceDatabaseException;
028import com.echothree.util.common.command.CommandResult;
029import com.echothree.util.server.persistence.Session;
030import com.echothree.util.server.persistence.ThreadUtils;
031import javax.ejb.Stateless;
032import javax.enterprise.inject.spi.CDI;
033
034@Stateless
035public class AuthenticationBean
036        extends AuthenticationFormsImpl
037        implements AuthenticationRemote, AuthenticationLocal {
038    
039    // -------------------------------------------------------------------------
040    //   Testing
041    // -------------------------------------------------------------------------
042    
043    @Override
044    public String ping() {
045        return "AuthenticationBean is alive!";
046    }
047    
048    // -------------------------------------------------------------------------
049    //   User Visits and Sessions
050    // -------------------------------------------------------------------------
051    
052    @Override
053    public CommandResult getJobUserVisit(GetJobUserVisitForm form) {
054        return CDI.current().select(GetJobUserVisitCommand.class).get().run(null, form);
055    }
056    
057    @Override
058    public UserVisitPK getDataLoaderUserVisit() {
059        var preservedState = ThreadUtils.preserveState();
060
061        UserVisitPK userVisitPK = null;
062        
063        try {
064            var userControl = Session.getModelController(UserControl.class);
065            var userVisit = userControl.createUserVisit(null, null, null, null, null, null, null, null);
066            var partyControl = Session.getModelController(PartyControl.class);
067            var party = partyControl.getPartyByName(PartyNames.DATA_LOADER.name());
068            
069            if(party == null) {
070                var partyType = partyControl.getPartyTypeByName(PartyTypes.UTILITY.name());
071                
072                if(partyType != null) {
073                    party = partyControl.createParty(PartyNames.DATA_LOADER.name(), partyType, null, null, null, null, null);
074                }
075            }
076            
077            if(party == null) {
078                userControl.removeUserVisit(userVisit);
079            } else {
080                userControl.associatePartyToUserVisit(userVisit, party, null, null);
081                userVisitPK = userVisit.getPrimaryKey();
082            }
083        } catch (PersistenceDatabaseException pde) {
084            throw pde;
085        } finally {
086            ThreadUtils.close();
087        }
088
089        ThreadUtils.restoreState(preservedState);
090
091        return userVisitPK;
092    }
093    
094    @Override
095    public CommandResult getUserVisit(GetUserVisitForm form) {
096        return CDI.current().select(GetUserVisitCommand.class).get().run(null, form);
097    }
098    
099    @Override
100    public void invalidateUserSession(UserVisitPK userVisitPK) {
101        CDI.current().select(InvalidateUserSessionCommand.class).get().run(userVisitPK);
102    }
103    
104    @Override
105    public void invalidateUserVisit(UserVisitPK userVisitPK) {
106        CDI.current().select(InvalidateUserVisitCommand.class).get().run(userVisitPK);
107    }
108    
109    @Override
110    public CommandResult invalidateAbandonedUserVisits(UserVisitPK userVisitPK, InvalidateAbandonedUserVisitsForm form) {
111        return CDI.current().select(InvalidateAbandonedUserVisitsCommand.class).get().run(userVisitPK, form);
112    }
113    
114    @Override
115    public CommandResult removeInactiveUserKeys(UserVisitPK userVisitPK, RemoveInactiveUserKeysForm form) {
116        return CDI.current().select(RemoveInactiveUserKeysCommand.class).get().run(userVisitPK, form);
117    }
118    
119    @Override
120    public CommandResult removeInvalidatedUserVisits(UserVisitPK userVisitPK) {
121        return CDI.current().select(RemoveInvalidatedUserVisitsCommand.class).get().run(userVisitPK);
122    }
123    
124    // -------------------------------------------------------------------------
125    //   Logins
126    // -------------------------------------------------------------------------
127
128    @Override
129    public CommandResult getCustomerLoginDefaults(UserVisitPK userVisitPK, GetCustomerLoginDefaultsForm form) {
130        return CDI.current().select(GetCustomerLoginDefaultsCommand.class).get().run(userVisitPK, form);
131    }
132
133    @Override
134    public CommandResult customerLogin(UserVisitPK userVisitPK, CustomerLoginForm form) {
135        return CDI.current().select(CustomerLoginCommand.class).get().run(userVisitPK, form);
136    }
137
138    @Override
139    public CommandResult getEmployeeLoginDefaults(UserVisitPK userVisitPK, GetEmployeeLoginDefaultsForm form) {
140        return CDI.current().select(GetEmployeeLoginDefaultsCommand.class).get().run(userVisitPK, form);
141    }
142
143    @Override
144    public CommandResult employeeLogin(UserVisitPK userVisitPK, EmployeeLoginForm form) {
145        return CDI.current().select(EmployeeLoginCommand.class).get().run(userVisitPK, form);
146    }
147
148    @Override
149    public CommandResult getVendorLoginDefaults(UserVisitPK userVisitPK, GetVendorLoginDefaultsForm form) {
150        return CDI.current().select(GetVendorLoginDefaultsCommand.class).get().run(userVisitPK, form);
151    }
152
153    @Override
154    public CommandResult vendorLogin(UserVisitPK userVisitPK, VendorLoginForm form) {
155        return CDI.current().select(VendorLoginCommand.class).get().run(userVisitPK, form);
156    }
157
158    @Override
159    public CommandResult setPassword(UserVisitPK userVisitPK, SetPasswordForm form) {
160        return CDI.current().select(SetPasswordCommand.class).get().run(userVisitPK, form);
161    }
162
163    @Override
164    public CommandResult recoverPassword(UserVisitPK userVisitPK, RecoverPasswordForm form) {
165        return CDI.current().select(RecoverPasswordCommand.class).get().run(userVisitPK, form);
166    }
167
168    @Override
169    public CommandResult idle(UserVisitPK userVisitPK) {
170        return CDI.current().select(IdleCommand.class).get().run(userVisitPK);
171    }
172
173    @Override
174    public CommandResult logout(UserVisitPK userVisitPK) {
175        return CDI.current().select(LogoutCommand.class).get().run(userVisitPK);
176    }
177    
178}