Search in sources :

Example 1 with Login

use of BasicCommonClasses.Login in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method forgetPasswordSendAnswerWithNewPassword.

private void forgetPasswordSendAnswerWithNewPassword(SQLDatabaseConnection c) {
    Login login;
    log.info("Get question for forget password send answer command called with from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Get question for forget password send answer command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    boolean goodAnswer;
    try {
        goodAnswer = inCommandWrapper.getSenderID() == 0 ? /* Command sent from employee */
        c.verifySecurityAnswerWorker(login.getUserName(), login.getForgetPassword().getAnswer()) : /* Command sent from customer */
        c.verifySecurityAnswerCustomer(login.getUserName(), login.getForgetPassword().getAnswer());
        if (!goodAnswer) {
            log.info("the anwser is incorrect.");
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_FOROGT_PASSWORD_WRONG_ANSWER, Serialization.serialize(false));
        } else {
            log.info("the anwser is correct");
            if (inCommandWrapper.getSenderID() == 0)
                /* Command sent from employee */
                c.setPasswordWorker(login.getUserName(), login.getPassword());
            else
                /* Command sent from customer */
                c.setPasswordCustomer(login.getUserName(), login.getPassword());
            outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK, Serialization.serialize(true));
            log.info("the anwser is correct. password changed succesfully.");
        }
    } catch (CriticalError e) {
        log.fatal("Get question for forget password send answer command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientNotExist e) {
        log.info("Get question for forget password send answer command failed, client is not exist");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST);
    }
    log.info("Get question for forget password send answer command system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) ClientNotExist(SQLDatabase.SQLDatabaseException.ClientNotExist)

Example 2 with Login

use of BasicCommonClasses.Login in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method registerNewWorker.

private void registerNewWorker(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Register new worker from serderID " + inCommandWrapper.getSenderID() + " command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for Register New Worker command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    log.info("Trying to register new worker " + login.getUserName() + " to system");
    try {
        c.addWorker(inCommandWrapper.getSenderID(), login, login.getForgetPassword());
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
    } catch (CriticalError e) {
        log.fatal("Register new worker command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyExist e) {
        log.info("Register new worker command failed, worker already exists");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_ALREADY_EXISTS);
    } catch (ClientNotConnected e) {
        log.info("Register new worker command failed, manager is not connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_NOT_CONNECTED);
    }
    log.info("Register new worker " + login.getUserName() + " to system finished");
}
Also used : CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) ClientAlreadyExist(SQLDatabase.SQLDatabaseException.ClientAlreadyExist) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login)

Example 3 with Login

use of BasicCommonClasses.Login in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method loginEmployeeCommand.

private void loginEmployeeCommand(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Login employee command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for login command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    if (!login.isValid()) {
        log.info("Login command failed, username and password can't be empty");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        return;
    }
    try {
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
        outCommandWrapper.setSenderID(c.loginWorker(login.getUserName(), login.getPassword()));
        try {
            outCommandWrapper.setData(c.getClientType(outCommandWrapper.getSenderID()));
        } catch (ClientNotConnected e) {
            log.fatal("Client is not connected for sender ID " + outCommandWrapper.getSenderID());
        }
        log.info("Login command succeded with sender ID " + outCommandWrapper.getSenderID() + " with client type " + outCommandWrapper.getData());
    } catch (AuthenticationError e) {
        log.info("Login command failed, username dosen't exist or wrong password received");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST_WRONG_PASSWORD);
    } catch (CriticalError e) {
        log.fatal("Login command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyConnected e) {
        log.info("Login command failed, user already connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_ALREADY_CONNECTED);
    } catch (NumberOfConnectionsExceeded e) {
        log.fatal("Login command failed, too much connections (try to increase TRYS_NUMBER)");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    }
    log.info("Login employee with User " + login.getUserName() + " finished");
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) ClientNotConnected(SQLDatabase.SQLDatabaseException.ClientNotConnected) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)

Example 4 with Login

use of BasicCommonClasses.Login in project SmartCity-Market by TechnionYP5777.

the class CommandExecuter method loginClientCommand.

private void loginClientCommand(SQLDatabaseConnection c) {
    Login login = null;
    log.info("Login client command called");
    try {
        login = Serialization.deserialize(inCommandWrapper.getData(), Login.class);
    } catch (java.lang.RuntimeException e) {
        log.fatal("Failed to parse data for login command");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
        return;
    }
    if (!login.isValid()) {
        log.info("Login command failed, username and password can't be empty");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_INVALID_PARAMETER);
        return;
    }
    try {
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_OK);
        int senderID = c.loginCustomer(login.getUserName(), login.getPassword());
        outCommandWrapper.setSenderID(senderID);
        try {
            outCommandWrapper.setData(c.getCustomerProfile(login.getUserName()));
        } catch (ClientNotExist e) {
            log.fatal("Client is not exist with username: " + outCommandWrapper.getSenderID());
        }
        log.info("Login command succeded with sender ID " + outCommandWrapper.getSenderID() + " with client type " + outCommandWrapper.getData());
    } catch (AuthenticationError e) {
        log.info("Login command failed, username dosen't exist or wrong password received");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_USERNAME_DOES_NOT_EXIST_WRONG_PASSWORD);
    } catch (CriticalError e) {
        log.fatal("Login command failed, critical error occured from SQL Database connection");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    } catch (ClientAlreadyConnected e) {
        log.info("Login command failed, user already connected");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_SENDER_IS_ALREADY_CONNECTED);
    } catch (NumberOfConnectionsExceeded e) {
        log.fatal("Login command failed, too much connections (try to increase TRYS_NUMBER)");
        outCommandWrapper = new CommandWrapper(ResultDescriptor.SM_ERR);
    }
    log.info("Login client with User " + login.getUserName() + " finished");
}
Also used : AuthenticationError(SQLDatabase.SQLDatabaseException.AuthenticationError) CriticalError(SQLDatabase.SQLDatabaseException.CriticalError) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) NumberOfConnectionsExceeded(SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded) ClientAlreadyConnected(SQLDatabase.SQLDatabaseException.ClientAlreadyConnected) ClientNotExist(SQLDatabase.SQLDatabaseException.ClientNotExist)

Example 5 with Login

use of BasicCommonClasses.Login in project SmartCity-Market by TechnionYP5777.

the class ForgotPasswordTest method sendWrongAnswerWithNewPassword.

@Test
public void sendWrongAnswerWithNewPassword() {
    ForgotPasswordData forgotPassData = new ForgotPasswordData(null, authQuestionAnswer + "bla");
    Login ansAndPassContainer = new Login(worker.getUsername(), newPass, forgotPassData);
    try {
        Mockito.when(clientRequestHandler.sendRequestWithRespond(new CommandWrapper(WorkerDefs.loginCommandSenderId, CommandDescriptor.FORGOT_PASSWORD_SEND_ANSWER_WITH_NEW_PASSWORD, Serialization.serialize(ansAndPassContainer)).serialize())).thenReturn(new CommandWrapper(ResultDescriptor.SM_FOROGT_PASSWORD_WRONG_ANSWER, null).serialize());
    } catch (IOException ยข) {
        fail();
    }
    try {
        ((IForgotPasswordHandler) worker).sendAnswerAndNewPassword(authQuestionAnswer + "bla", newPass);
    } catch (WrongAnswer e) {
        //success
        return;
    } catch (NoSuchUserName e) {
        fail();
    }
    fail();
}
Also used : WrongAnswer(UtilsImplementations.ForgotPasswordHandler.WrongAnswer) IForgotPasswordHandler(UtilsContracts.IForgotPasswordHandler) NoSuchUserName(UtilsImplementations.ForgotPasswordHandler.NoSuchUserName) ForgotPasswordData(BasicCommonClasses.ForgotPasswordData) CommandWrapper(ClientServerApi.CommandWrapper) Login(BasicCommonClasses.Login) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Login (BasicCommonClasses.Login)20 CommandWrapper (ClientServerApi.CommandWrapper)15 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)14 Test (org.junit.Test)12 AuthenticationError (SQLDatabase.SQLDatabaseException.AuthenticationError)9 ClientAlreadyConnected (SQLDatabase.SQLDatabaseException.ClientAlreadyConnected)9 NumberOfConnectionsExceeded (SQLDatabase.SQLDatabaseException.NumberOfConnectionsExceeded)9 ForgotPasswordData (BasicCommonClasses.ForgotPasswordData)8 CommandExecuter (CommandHandler.CommandExecuter)6 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)6 ClientNotExist (SQLDatabase.SQLDatabaseException.ClientNotExist)6 Gson (com.google.gson.Gson)6 ClientAlreadyExist (SQLDatabase.SQLDatabaseException.ClientAlreadyExist)5 SQLDatabaseConnection (SQLDatabase.SQLDatabaseConnection)4 CriticalError (SMExceptions.CommonExceptions.CriticalError)3 EmployeeNotConnected (EmployeeDefs.AEmployeeException.EmployeeNotConnected)2 WorkerAlreadyExists (EmployeeDefs.AEmployeeException.WorkerAlreadyExists)2 IForgotPasswordHandler (UtilsContracts.IForgotPasswordHandler)2 NoSuchUserName (UtilsImplementations.ForgotPasswordHandler.NoSuchUserName)2 WrongAnswer (UtilsImplementations.ForgotPasswordHandler.WrongAnswer)2