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.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.Arrays;
032import java.util.Collections;
033import java.util.List;
034
035public class VendorLoginCommand
036        extends BaseLoginCommand<VendorLoginForm> {
037
038    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
043                new FieldDefinition("Username", FieldType.STRING, true, 1L, 80L),
044                new FieldDefinition("Password", FieldType.STRING, true, 1L, 40L),
045                new FieldDefinition("RemoteInet4Address", FieldType.INET_4_ADDRESS, false, null, null)
046                ));
047    }
048    
049    /** Creates a new instance of VendorLoginCommand */
050    public VendorLoginCommand(UserVisitPK userVisitPK, VendorLoginForm form) {
051        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var userLogin = UserLoginLogic.getInstance().getUserLoginByUsername(this, form.getUsername());
057
058        if(!hasExecutionErrors()) {
059            var party = userLogin.getParty();
060            var partyDetail = party.getLastDetail();
061
062            PartyLogic.getInstance().checkPartyType(this, party, PartyTypes.VENDOR.name());
063
064            if(!hasExecutionErrors()) {
065                var userControl = getUserControl();
066                var userLoginStatus = userControl.getUserLoginStatusForUpdate(party);
067
068                if(!WorkflowStepLogic.getInstance().isEntityInWorkflowSteps(this, VendorStatusConstants.Workflow_VENDOR_STATUS, party,
069                        VendorStatusConstants.WorkflowStep_ACTIVE).isEmpty()) {
070                    LockoutPolicyLogic.getInstance().checkUserLogin(session, this, party, userLoginStatus);
071
072                    if(!hasExecutionErrors()) {
073                        if(checkPasswords(userLoginStatus, form.getPassword(), party, true)) {
074                            var strRemoteInet4Address = form.getRemoteInet4Address();
075                            var remoteInet4Address = strRemoteInet4Address == null ? null : Integer.valueOf(form.getRemoteInet4Address());
076
077                            successfulLogin(userLoginStatus, party, null, remoteInet4Address);
078                        }
079                    }
080                } else {
081                    addExecutionError(ExecutionErrors.VendorNotActive.name(), partyDetail.getPartyName());
082                }
083
084                if(hasExecutionErrors()) {
085                    unsuccessfulLogin(userLoginStatus);
086                }
087            }
088        }
089
090        return null;
091    }
092    
093}