diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/ProgramProcessor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/ProgramProcessor.java index 7410e0be1..f9ae00154 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/ProgramProcessor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/ProgramProcessor.java @@ -16,6 +16,7 @@ import de.fhg.iais.roberta.persistence.util.DbSession; import de.fhg.iais.roberta.persistence.util.HttpSessionState; import de.fhg.iais.roberta.util.Key; +import de.fhg.iais.roberta.util.Pair; import de.fhg.iais.roberta.util.Util; public class ProgramProcessor extends AbstractProcessor { @@ -24,8 +25,8 @@ public ProgramProcessor(DbSession dbSession, HttpSessionState httpSessionState) } /** - * load a program from the data base. Either the program is owned by the user with the id given or the program is shared - * by the user given to the user requesting the program + * load a program from the data base. Either the program is owned by the user with the id given or the program is shared by the user given to the user + * requesting the program * * @param programName the program to load * @param ownerId the owner (either the user logged in or an owner who shared the program R/W with the user logged in @@ -173,10 +174,10 @@ private Program getProgramWithAccessRight(String programName, int ownerId) { * @param mayExist true, if an existing program may be changed; false if a program may be stored only, if it does not exist in the database * @param isOwner true, if the owner updates a program; false if a user with access right WRITE updates a program */ - public void updateProgram(String programName, int userId, int robotId, String programText, Timestamp programTimestamp, boolean isOwner) { + public Program updateProgram(String programName, int userId, int robotId, String programText, Timestamp programTimestamp, boolean isOwner) { if ( !Util.isValidJavaIdentifier(programName) ) { setError(Key.PROGRAM_ERROR_ID_INVALID, programName); - return; + return null; } if ( this.httpSessionState.isUserLoggedIn() ) { UserDao userDao = new UserDao(this.dbSession); @@ -184,15 +185,17 @@ public void updateProgram(String programName, int userId, int robotId, String pr ProgramDao programDao = new ProgramDao(this.dbSession); User user = userDao.get(userId); Robot robot = robotDao.get(robotId); - Key result = programDao.persistProgramText(programName, user, robot, programText, programTimestamp, isOwner); + Pair result = programDao.persistProgramText(programName, user, robot, programText, programTimestamp, isOwner); // a bit strange, but necessary to distinguish between success and failure - if ( result == Key.PROGRAM_SAVE_SUCCESS ) { + if ( result.getFirst() == Key.PROGRAM_SAVE_SUCCESS ) { setSuccess(Key.PROGRAM_SAVE_SUCCESS); } else { - setError(result); + setError(result.getFirst()); } + return result.getSecond(); } else { setError(Key.USER_ERROR_NOT_LOGGED_IN); + return null; } } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/dao/ProgramDao.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/dao/ProgramDao.java index 8b66aa84a..46a7a13e3 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/dao/ProgramDao.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/dao/ProgramDao.java @@ -15,6 +15,7 @@ import de.fhg.iais.roberta.persistence.bo.User; import de.fhg.iais.roberta.persistence.util.DbSession; import de.fhg.iais.roberta.util.Key; +import de.fhg.iais.roberta.util.Pair; import de.fhg.iais.roberta.util.dbc.Assert; /** @@ -46,7 +47,7 @@ public ProgramDao(DbSession session) { * @param isOwner true, if the owner updates a program; false if a user with access right WRITE updates a program * @return true if the program could be persisted successfully */ - public Key persistProgramText(String name, User user, Robot robot, String programText, Timestamp timestamp, boolean isOwner) { + public Pair persistProgramText(String name, User user, Robot robot, String programText, Timestamp timestamp, boolean isOwner) { Assert.notNull(name); Assert.notNull(user); Assert.notNull(robot); @@ -60,30 +61,30 @@ public Key persistProgramText(String name, User user, Robot robot, String progra program = new Program(name, user, robot); program.setProgramText(programText); this.session.save(program); - return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success + return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success } else { // otherwise ... ProgramDao.LOG.error("update was requested, but no program with matching timestamp was found"); - return Key.PROGRAM_SAVE_ERROR_NO_PROGRAM_TO_UPDATE_FOUND; + return Pair.of(Key.PROGRAM_SAVE_ERROR_NO_PROGRAM_TO_UPDATE_FOUND, null); } } else if ( !timestamp.equals(program.getLastChanged()) ) { ProgramDao.LOG.error("update was requested, timestamps don't match. Has another user updated the program in the meantime?"); - return Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING; + return Pair.of(Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING, null); } else { program.setProgramText(programText); - return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success + return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success } } else { Program program = loadSharedForUpdate(name, user, robot); if ( program == null ) { ProgramDao.LOG.error("update was requested, but no shared program was found"); - return Key.PROGRAM_SAVE_ERROR_NO_WRITE_PERMISSION; + return Pair.of(Key.PROGRAM_SAVE_ERROR_NO_WRITE_PERMISSION, null); } else if ( !timestamp.equals(program.getLastChanged()) ) { ProgramDao.LOG.error("update was requested, timestamps don't match. Has another user updated the program in the meantime?"); - return Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING; + return Pair.of(Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING, null); } else { program.setProgramText(programText); - return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success + return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success } } } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/util/HttpSessionState.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/util/HttpSessionState.java index 736239ff8..b40ee1643 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/util/HttpSessionState.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/persistence/util/HttpSessionState.java @@ -3,7 +3,9 @@ import de.fhg.iais.roberta.util.dbc.Assert; public class HttpSessionState { - private int userId = -1; + public final static int NO_USER = -1; + + private int userId = HttpSessionState.NO_USER; private int robotId = 42; private String token = "1Q2W3E4R"; private String programName; @@ -28,20 +30,11 @@ public boolean isUserLoggedIn() { return this.userId >= 1; } - public void rememberLogin(int userId) { - Assert.isTrue(userId >= 1); + public void setUserClearDataKeepTokenAndRobotId(int userId) { + Assert.isTrue(userId >= 1 || userId == HttpSessionState.NO_USER); // token is not cleared. This would annoy the user. + // robotId is not cleared. This would annoy the user. this.userId = userId; - // robotId is not cleared. - this.programName = null; - this.program = null; - this.configurationName = null; - this.configuration = null; - } - - public void rememberLogout() { - this.userId = -1; - // token is not cleared. This would annoy the user. this.programName = null; this.program = null; this.configurationName = null; diff --git a/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientProgram.java b/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientProgram.java index cf151519f..4fbd64d1b 100644 --- a/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientProgram.java +++ b/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientProgram.java @@ -92,11 +92,12 @@ public Response command(@OraData HttpSessionState httpSessionState, JSONObject f Long timestamp = request.getLong("timestamp"); Timestamp programTimestamp = new Timestamp(timestamp); boolean isShared = request.optBoolean("shared", false); - pp.updateProgram(programName, userId, robotId, programText, programTimestamp, !isShared); + Program program = pp.updateProgram(programName, userId, robotId, programText, programTimestamp, !isShared); if ( pp.isOk() ) { - Program program = pp.getProgram(programName, userId, robotId); if ( program != null ) { response.put("lastChanged", program.getLastChanged()); + } else { + ClientProgram.LOG.error("TODO: check potential error: the saved program should never be null"); } } Util.addResultInfo(response, pp); diff --git a/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientUser.java b/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientUser.java index 8a2b45cbd..ef7c63700 100644 --- a/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientUser.java +++ b/OpenRobertaServer/src/main/java/de/fhg/iais/roberta/javaServer/restServices/all/ClientUser.java @@ -15,7 +15,6 @@ import com.google.inject.Inject; import de.fhg.iais.roberta.javaServer.provider.OraData; -import de.fhg.iais.roberta.persistence.AccessRightProcessor; import de.fhg.iais.roberta.persistence.UserProcessor; import de.fhg.iais.roberta.persistence.bo.User; import de.fhg.iais.roberta.persistence.util.DbSession; @@ -51,9 +50,15 @@ public Response command(@OraData HttpSessionState httpSessionState, @OraData DbS ClientUser.LOG.info("command is: " + cmd); response.put("cmd", cmd); UserProcessor up = new UserProcessor(dbSession, httpSessionState); - AccessRightProcessor upp = new AccessRightProcessor(dbSession, httpSessionState); - if ( cmd.equals("login") && !httpSessionState.isUserLoggedIn() ) { + if ( cmd.equals("clear") ) { + httpSessionState.setUserClearDataKeepTokenAndRobotId(HttpSessionState.NO_USER); + response.put("rc", "ok"); + if ( userId != HttpSessionState.NO_USER ) { + ClientUser.LOG.info("clear for (logged in) user " + userId + ". Has the user reloaded the page?"); + } + + } else if ( cmd.equals("login") && !httpSessionState.isUserLoggedIn() ) { String userAccountName = request.getString("accountName"); String password = request.getString("password"); User user = up.getUser(userAccountName, password); @@ -61,7 +66,7 @@ public Response command(@OraData HttpSessionState httpSessionState, @OraData DbS if ( user != null ) { int id = user.getId(); String account = user.getAccount(); - httpSessionState.rememberLogin(id); + httpSessionState.setUserClearDataKeepTokenAndRobotId(id); user.setLastLogin(); response.put("userId", id); response.put("userRole", user.getRole()); @@ -71,7 +76,7 @@ public Response command(@OraData HttpSessionState httpSessionState, @OraData DbS } } else if ( cmd.equals("logout") && httpSessionState.isUserLoggedIn() ) { - httpSessionState.rememberLogout(); + httpSessionState.setUserClearDataKeepTokenAndRobotId(HttpSessionState.NO_USER); response.put("rc", "ok"); response.put("message", Key.USER_LOGOUT_SUCCESS.getKey()); ClientUser.LOG.info("logout of user " + userId);