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.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.cdi.CommandScopeExtension;
030import javax.ejb.Stateless;
031import javax.enterprise.inject.spi.CDI;
032import javax.inject.Inject;
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    @Inject
058    UserControl userControl;
059
060    @Inject
061    PartyControl partyControl;
062
063    @Override
064    public UserVisitPK getDataLoaderUserVisit() {
065        if(CommandScopeExtension.getCommandScopeContext().isActive()) {
066            CommandScopeExtension.getCommandScopeContext().push();
067        } else {
068            CommandScopeExtension.getCommandScopeContext().activate();
069        }
070
071        UserVisitPK userVisitPK = null;
072        
073        try {
074            var userVisit = userControl.createUserVisit(null, null, null, null, null, null, null, null);
075            var party = partyControl.getPartyByName(PartyNames.DATA_LOADER.name());
076            
077            if(party == null) {
078                var partyType = partyControl.getPartyTypeByName(PartyTypes.UTILITY.name());
079                
080                if(partyType != null) {
081                    party = partyControl.createParty(PartyNames.DATA_LOADER.name(), partyType, null, null, null, null, null);
082                }
083            }
084            
085            if(party == null) {
086                userControl.removeUserVisit(userVisit);
087            } else {
088                userControl.associatePartyToUserVisit(userVisit, party, null, null);
089                userVisitPK = userVisit.getPrimaryKey();
090            }
091        } catch (PersistenceDatabaseException pde) {
092            throw pde;
093        } finally {
094            CommandScopeExtension.getCommandScopeContext().pop();
095        }
096
097        return userVisitPK;
098    }
099    
100    @Override
101    public CommandResult getUserVisit(GetUserVisitForm form) {
102        return CDI.current().select(GetUserVisitCommand.class).get().run(null, form);
103    }
104    
105    @Override
106    public void invalidateUserSession(UserVisitPK userVisitPK) {
107        CDI.current().select(InvalidateUserSessionCommand.class).get().run(userVisitPK);
108    }
109    
110    @Override
111    public void invalidateUserVisit(UserVisitPK userVisitPK) {
112        CDI.current().select(InvalidateUserVisitCommand.class).get().run(userVisitPK);
113    }
114    
115    @Override
116    public CommandResult invalidateAbandonedUserVisits(UserVisitPK userVisitPK, InvalidateAbandonedUserVisitsForm form) {
117        return CDI.current().select(InvalidateAbandonedUserVisitsCommand.class).get().run(userVisitPK, form);
118    }
119    
120    @Override
121    public CommandResult removeInactiveUserKeys(UserVisitPK userVisitPK, RemoveInactiveUserKeysForm form) {
122        return CDI.current().select(RemoveInactiveUserKeysCommand.class).get().run(userVisitPK, form);
123    }
124    
125    @Override
126    public CommandResult removeInvalidatedUserVisits(UserVisitPK userVisitPK) {
127        return CDI.current().select(RemoveInvalidatedUserVisitsCommand.class).get().run(userVisitPK);
128    }
129    
130    // -------------------------------------------------------------------------
131    //   Logins
132    // -------------------------------------------------------------------------
133
134    @Override
135    public CommandResult getCustomerLoginDefaults(UserVisitPK userVisitPK, GetCustomerLoginDefaultsForm form) {
136        return CDI.current().select(GetCustomerLoginDefaultsCommand.class).get().run(userVisitPK, form);
137    }
138
139    @Override
140    public CommandResult customerLogin(UserVisitPK userVisitPK, CustomerLoginForm form) {
141        return CDI.current().select(CustomerLoginCommand.class).get().run(userVisitPK, form);
142    }
143
144    @Override
145    public CommandResult getEmployeeLoginDefaults(UserVisitPK userVisitPK, GetEmployeeLoginDefaultsForm form) {
146        return CDI.current().select(GetEmployeeLoginDefaultsCommand.class).get().run(userVisitPK, form);
147    }
148
149    @Override
150    public CommandResult employeeLogin(UserVisitPK userVisitPK, EmployeeLoginForm form) {
151        return CDI.current().select(EmployeeLoginCommand.class).get().run(userVisitPK, form);
152    }
153
154    @Override
155    public CommandResult getVendorLoginDefaults(UserVisitPK userVisitPK, GetVendorLoginDefaultsForm form) {
156        return CDI.current().select(GetVendorLoginDefaultsCommand.class).get().run(userVisitPK, form);
157    }
158
159    @Override
160    public CommandResult vendorLogin(UserVisitPK userVisitPK, VendorLoginForm form) {
161        return CDI.current().select(VendorLoginCommand.class).get().run(userVisitPK, form);
162    }
163
164    @Override
165    public CommandResult setPassword(UserVisitPK userVisitPK, SetPasswordForm form) {
166        return CDI.current().select(SetPasswordCommand.class).get().run(userVisitPK, form);
167    }
168
169    @Override
170    public CommandResult recoverPassword(UserVisitPK userVisitPK, RecoverPasswordForm form) {
171        return CDI.current().select(RecoverPasswordCommand.class).get().run(userVisitPK, form);
172    }
173
174    @Override
175    public CommandResult idle(UserVisitPK userVisitPK) {
176        return CDI.current().select(IdleCommand.class).get().run(userVisitPK);
177    }
178
179    @Override
180    public CommandResult logout(UserVisitPK userVisitPK) {
181        return CDI.current().select(LogoutCommand.class).get().run(userVisitPK);
182    }
183    
184}