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