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.command;
018
019import com.echothree.control.user.authentication.common.form.EmployeeLoginForm;
020import com.echothree.model.control.employee.common.workflow.EmployeeStatusConstants;
021import com.echothree.model.control.party.common.PartyRelationshipTypes;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.common.RoleTypes;
024import com.echothree.model.control.party.server.logic.LockoutPolicyLogic;
025import com.echothree.model.control.party.server.logic.PartyLogic;
026import com.echothree.model.control.user.server.logic.UserLoginLogic;
027import com.echothree.model.control.workflow.server.logic.WorkflowStepLogic;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EmployeeLoginCommand
039        extends BaseLoginCommand<EmployeeLoginForm> {
040
041    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("Username", FieldType.STRING, true, 1L, 80L),
047                new FieldDefinition("Password", FieldType.STRING, true, 1L, 40L),
048                new FieldDefinition("RemoteInet4Address", FieldType.INET_4_ADDRESS, false, null, null),
049                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null)
050        );
051    }
052
053    @Inject
054    LockoutPolicyLogic lockoutPolicyLogic;
055
056    @Inject
057    PartyLogic partyLogic;
058
059    @Inject
060    UserLoginLogic userLoginLogic;
061
062    @Inject
063    WorkflowStepLogic workflowStepLogic;
064
065    
066    /** Creates a new instance of EmployeeLoginCommand */
067    public EmployeeLoginCommand() {
068        super(null, FORM_FIELD_DEFINITIONS);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var userLogin = userLoginLogic.getUserLoginByUsername(this, form.getUsername());
074        
075        if(!hasExecutionErrors()) {
076            var party = userLogin.getParty();
077            var partyDetail = party.getLastDetail();
078
079            partyLogic.checkPartyType(this, party, PartyTypes.EMPLOYEE.name());
080
081            if(!hasExecutionErrors()) {
082                var userLoginStatus = userControl.getUserLoginStatusForUpdate(party);
083
084                if(!workflowStepLogic.isEntityInWorkflowSteps(this, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, party,
085                        EmployeeStatusConstants.WorkflowStep_ACTIVE).isEmpty()) {
086                    lockoutPolicyLogic.checkUserLogin(session, this, party, userLoginStatus);
087
088                    if(!hasExecutionErrors()) {
089                        if(checkPasswords(userLoginStatus, form.getPassword(), party, true)) {
090                            var partyCompanyName = form.getCompanyName();
091                            var partyCompany = partyControl.getPartyCompanyByName(partyCompanyName);
092
093                            if(partyCompany != null) {
094                                var partyCompanyParty = partyCompany.getParty();
095                                var partyRelationshipType = partyControl.getPartyRelationshipTypeByName(PartyRelationshipTypes.EMPLOYMENT.name());
096                                var fromRoleType = partyControl.getRoleTypeByName(RoleTypes.EMPLOYER.name());
097                                var toRoleType = partyControl.getRoleTypeByName(RoleTypes.EMPLOYEE.name());
098                                var partyRelationship = partyControl.getPartyRelationship(partyRelationshipType, partyCompanyParty,
099                                        fromRoleType, party, toRoleType);
100
101                                if(partyRelationship != null) {
102                                    var strRemoteInet4Address = form.getRemoteInet4Address();
103                                    var remoteInet4Address = strRemoteInet4Address == null ? null : Integer.valueOf(form.getRemoteInet4Address());
104
105                                    successfulLogin(userLoginStatus, party, partyRelationship, remoteInet4Address);
106                                } else {
107                                    addExecutionError(ExecutionErrors.UnknownPartyRelationship.name(), PartyRelationshipTypes.EMPLOYMENT.name(),
108                                            partyCompanyParty.getLastDetail().getPartyName(), RoleTypes.EMPLOYER.name(), partyDetail.getPartyName(),
109                                            RoleTypes.EMPLOYEE.name());
110                                }
111                            } else {
112                                addExecutionError(ExecutionErrors.UnknownPartyCompanyName.name(), partyCompanyName);
113                            }
114                        }
115                    }
116                } else {
117                    addExecutionError(ExecutionErrors.EmployeeNotActive.name(), partyDetail.getPartyName());
118                }
119                
120                if(hasExecutionErrors()) {
121                    unsuccessfulLogin(userLoginStatus);
122                }
123            }
124        }
125        
126        return null;
127    }
128    
129}