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