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.VendorLoginForm;
020import com.echothree.model.control.party.common.PartyTypes;
021import com.echothree.model.control.party.server.logic.LockoutPolicyLogic;
022import com.echothree.model.control.party.server.logic.PartyLogic;
023import com.echothree.model.control.user.server.logic.UserLoginLogic;
024import com.echothree.model.control.vendor.common.workflow.VendorStatusConstants;
025import com.echothree.model.control.workflow.server.logic.WorkflowStepLogic;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
036public class VendorLoginCommand
037        extends BaseLoginCommand<VendorLoginForm> {
038
039    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("Username", FieldType.STRING, true, 1L, 80L),
045                new FieldDefinition("Password", FieldType.STRING, true, 1L, 40L),
046                new FieldDefinition("RemoteInet4Address", FieldType.INET_4_ADDRESS, false, null, null)
047        );
048    }
049
050    @Inject
051    LockoutPolicyLogic lockoutPolicyLogic;
052
053    @Inject
054    PartyLogic partyLogic;
055
056    @Inject
057    UserLoginLogic userLoginLogic;
058
059    @Inject
060    WorkflowStepLogic workflowStepLogic;
061
062    
063    /** Creates a new instance of VendorLoginCommand */
064    public VendorLoginCommand() {
065        super(null, FORM_FIELD_DEFINITIONS);
066    }
067    
068    @Override
069    protected BaseResult execute() {
070        var userLogin = userLoginLogic.getUserLoginByUsername(this, form.getUsername());
071
072        if(!hasExecutionErrors()) {
073            var party = userLogin.getParty();
074            var partyDetail = party.getLastDetail();
075
076            partyLogic.checkPartyType(this, party, PartyTypes.VENDOR.name());
077
078            if(!hasExecutionErrors()) {
079                var userLoginStatus = userControl.getUserLoginStatusForUpdate(party);
080
081                if(!workflowStepLogic.isEntityInWorkflowSteps(this, VendorStatusConstants.Workflow_VENDOR_STATUS, party,
082                        VendorStatusConstants.WorkflowStep_ACTIVE).isEmpty()) {
083                    lockoutPolicyLogic.checkUserLogin(session, this, party, userLoginStatus);
084
085                    if(!hasExecutionErrors()) {
086                        if(checkPasswords(userLoginStatus, form.getPassword(), party, true)) {
087                            var strRemoteInet4Address = form.getRemoteInet4Address();
088                            var remoteInet4Address = strRemoteInet4Address == null ? null : Integer.valueOf(form.getRemoteInet4Address());
089
090                            successfulLogin(userLoginStatus, party, null, remoteInet4Address);
091                        }
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.VendorNotActive.name(), partyDetail.getPartyName());
095                }
096
097                if(hasExecutionErrors()) {
098                    unsuccessfulLogin(userLoginStatus);
099                }
100            }
101        }
102
103        return null;
104    }
105    
106}