package performa.form;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import oneit.logging.LogLevel;
import oneit.logging.LogMgr;
import oneit.logging.LoggingArea;
import oneit.objstore.ObjectTransaction;
import oneit.security.LoginProcessor;
import oneit.security.SecUser;
import oneit.servlets.forms.SubmissionDetails;
import oneit.utils.MultiException;
import oneit.utils.parsers.FieldException;
import performa.orm.CompanyUser;
import performa.utils.Utils;

/**
 *
 * @author nilu
 */
public class UserLoginFP extends LoginProcessor
{
    @Override
    protected void checkUserCanLogin(ObjectTransaction trans, SecUser userToCheck, SubmissionDetails submission, Map requestParams) throws FieldException, MultiException
    {
        super.checkUserCanLogin(trans, userToCheck, submission, requestParams);
                
        CompanyUser companyUser =   userToCheck.getExtension(CompanyUser.REFERENCE_CompanyUser);
        
        if(!Utils.checkAdminPortalAccess(userToCheck) || companyUser == null || !companyUser.isTrue(companyUser.getIsAccountVerified()))
        {
            throw new FieldException("You're not an authorised user to access this portal.", SecUser.FIELD_UserName);
        }
        else if(companyUser.getIsEmailChanged()==Boolean.TRUE) 
        {
            //verify email address change
            companyUser.changeEmail(submission.getRequest());
        }
    }
}