diff --git a/src/main/java/de/thm/arsnova/aop/UserSessionAspect.java b/src/main/java/de/thm/arsnova/aop/UserSessionAspect.java index bfe0dad441bf1bc57c68f54585aafba56e701feb..690fdd241bf238dfcb89f3009e9af7d4976f1c11 100644 --- a/src/main/java/de/thm/arsnova/aop/UserSessionAspect.java +++ b/src/main/java/de/thm/arsnova/aop/UserSessionAspect.java @@ -14,35 +14,17 @@ public class UserSessionAspect { @Autowired private UserSessionService userSessionService; - /** Sets current user and ARSnova session in session scoped UserSessionService - * + /** Sets current user and ARSnova session in session scoped UserSessionService + * * @param jp * @param keyword * @param session */ @AfterReturning( - pointcut="execution(public * de.thm.arsnova.services.SessionService.joinSession(..)) && args(keyword)", - returning="session" - ) + pointcut="execution(public * de.thm.arsnova.services.SessionService.joinSession(..)) && args(keyword)", + returning="session" + ) public final void joinSessionAdvice(final JoinPoint jp, final String keyword, final Session session) { userSessionService.setSession(session); } - - /** Sets current user, ARSnova session and websocket session ID in session scoped UserSessionService - * - * @param jp - * @param keyword - * @param socketId - * @param session - */ - /* FIXME This is not working because of scoping problems - @AfterReturning( - pointcut="execution(public * de.thm.arsnova.services.SessionService.joinSession(..)) && args(keyword, socketId)", - returning="session" - ) - public final void joinSessionAdviceWithWebsocket(final JoinPoint jp, final String keyword, final UUID socketId, final Session session) { - userSessionService.setSession(session); - userSessionService.setSocketId(socketId); - } - */ } diff --git a/src/main/java/de/thm/arsnova/controller/LoginController.java b/src/main/java/de/thm/arsnova/controller/LoginController.java index 37a47167a39b5877aa33d110afc69f13c65a7b19..a1fee3a1fda43f452836a96f9e3bffc093c3cfda 100644 --- a/src/main/java/de/thm/arsnova/controller/LoginController.java +++ b/src/main/java/de/thm/arsnova/controller/LoginController.java @@ -93,9 +93,9 @@ public class LoginController extends AbstractController { @RequestParam(value = "role", required = false) UserSessionService.Role role, final HttpServletRequest request, final HttpServletResponse response - ) throws IOException, ServletException { + ) throws IOException, ServletException { userSessionService.setRole(role); - + String referer = request.getHeader("referer"); if (null != forcedReferer && null != referer && !UrlUtils.isAbsoluteUrl(referer)) { /* Use a url from a request parameter as referer as long as the url is not absolute (to prevent @@ -107,14 +107,14 @@ public class LoginController extends AbstractController { } request.getSession().setAttribute("ars-login-success-url", - null == successUrl ? referer + "#auth/checkLogin" : successUrl - ); + null == successUrl ? referer + "#auth/checkLogin" : successUrl + ); request.getSession().setAttribute("ars-login-failure-url", - null == failureUrl ? referer : failureUrl - ); + null == failureUrl ? referer : failureUrl + ); View result = null; - + if ("cas".equals(type)) { casEntryPoint.commence(request, response, null); } else if ("twitter".equals(type)) { @@ -133,12 +133,14 @@ public class LoginController extends AbstractController { if (guestName != null && guestName.startsWith("Guest") && guestName.length() == MAX_USERNAME_LENGTH) { username = guestName; } else { - username = "Guest" + Sha512DigestUtils.shaHex(request.getSession().getId()).substring(0, MAX_GUESTHASH_LENGTH); + username = "Guest" + Sha512DigestUtils.shaHex( + request.getSession().getId() + ).substring(0, MAX_GUESTHASH_LENGTH); } org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User( username, "", true, true, true, true, authorities - ); + ); Authentication token = new UsernamePasswordAuthenticationToken(user, null, authorities); SecurityContextHolder.getContext().setAuthentication(token); @@ -146,14 +148,14 @@ public class LoginController extends AbstractController { SecurityContextHolder.getContext()); result = new RedirectView(null == successUrl ? referer + "#auth/checkLogin" : successUrl); } - + return result; } @RequestMapping(value = { "/auth/", "/whoami" }, method = RequestMethod.GET) @ResponseBody public final User whoami() { - userSessionService.setUser(userService.getCurrentUser()); + userSessionService.setUser(userService.getCurrentUser()); return userService.getCurrentUser(); } diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java index b8443784bf35398a46b3845e246b7f97b5ae019e..27e5c8e1f38dabdbb88147978284ad532e10451b 100644 --- a/src/main/java/de/thm/arsnova/controller/SessionController.java +++ b/src/main/java/de/thm/arsnova/controller/SessionController.java @@ -61,8 +61,7 @@ public class SessionController extends AbstractController { @RequestMapping(value = "/{sessionkey}", method = RequestMethod.GET) @ResponseBody public final Session joinSession(@PathVariable final String sessionkey) { - Session session = sessionService.joinSession(sessionkey); - return session; + return sessionService.joinSession(sessionkey); } @RequestMapping(value = "/{sessionkey}", method = RequestMethod.DELETE) @@ -78,7 +77,7 @@ public class SessionController extends AbstractController { public final LoggedIn registerAsOnlineUser( @PathVariable final String sessionkey, final HttpServletResponse response - ) { + ) { response.addHeader("X-Deprecated-API", "1"); User user = userService.getCurrentUser(); @@ -95,7 +94,7 @@ public class SessionController extends AbstractController { public final int countActiveUsers( @PathVariable final String sessionkey, final HttpServletResponse response - ) { + ) { response.addHeader("X-Deprecated-API", "1"); return userService.getUsersInSessionCount(sessionkey); @@ -132,7 +131,7 @@ public class SessionController extends AbstractController { public final Session updateSession( @PathVariable final String sessionkey, @RequestBody final Session session - ) { + ) { return sessionService.updateSession(sessionkey, session); } @@ -143,7 +142,7 @@ public class SessionController extends AbstractController { @RequestParam(value = "visitedonly", defaultValue = "false") final boolean visitedOnly, @RequestParam(value = "sortby", defaultValue = "name") final String sortby, final HttpServletResponse response - ) { + ) { User user = userService.getCurrentUser(); List<Session> sessions = null; @@ -183,7 +182,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionkey, @RequestParam(required = false) final Boolean lock, final HttpServletResponse response - ) { + ) { if (lock != null) { return this.sessionService.setActive(sessionkey, lock); } @@ -196,7 +195,7 @@ public class SessionController extends AbstractController { public final int learningProgress( @PathVariable final String sessionkey, final HttpServletResponse response - ) { + ) { return sessionService.getLearningProgress(sessionkey); } @@ -205,7 +204,7 @@ public class SessionController extends AbstractController { public final int myLearningProgress( @PathVariable final String sessionkey, final HttpServletResponse response - ) { + ) { return sessionService.getMyLearningProgress(sessionkey); } @@ -215,7 +214,7 @@ public class SessionController extends AbstractController { public final String redirectLecturerQuestion( @PathVariable final String sessionKey, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/lecturerquestion/?sessionkey=%s", sessionKey); @@ -226,7 +225,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionKey, @PathVariable final String arg1, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/lecturerquestion/%s/?sessionkey=%s", arg1, sessionKey); @@ -238,7 +237,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg1, @PathVariable final String arg2, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/lecturerquestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); @@ -251,7 +250,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg2, @PathVariable final String arg3, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/lecturerquestion/%s/%s/%s/?sessionkey=%s", arg1, arg2, arg3, sessionKey); @@ -261,7 +260,7 @@ public class SessionController extends AbstractController { public final String redirectAudienceQuestion( @PathVariable final String sessionKey, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/audiencequestion/?sessionkey=%s", sessionKey); @@ -272,7 +271,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionKey, @PathVariable final String arg1, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/audiencequestion/%s/?sessionkey=%s", arg1, sessionKey); @@ -284,7 +283,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg1, @PathVariable final String arg2, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/audiencequestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); @@ -297,7 +296,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg2, @PathVariable final String arg3, final HttpServletResponse response - ) { + ) { response.addHeader("X-Forwarded", "1"); return String.format("forward:/audiencequestion/%s/%s/%s/?sessionkey=%s", arg1, arg2, arg3, sessionKey); diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index 1e3915e9c3d0adec08d012b4351c10b5c648c695..5262cbe9a227ace7d31624f88ecda45514353b55 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -62,7 +62,6 @@ import de.thm.arsnova.entities.VisitedSession; import de.thm.arsnova.exceptions.ForbiddenException; import de.thm.arsnova.exceptions.NotFoundException; import de.thm.arsnova.exceptions.UnauthorizedException; -import de.thm.arsnova.services.IFeedbackService; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; @@ -71,9 +70,6 @@ public class CouchDBDao implements IDatabaseDao { @Autowired private IUserService userService; - @Autowired - private IFeedbackService feedbackService; - @Autowired private ISessionService sessionService; @@ -133,7 +129,7 @@ public class CouchDBDao implements IDatabaseDao { Session session = (Session) JSONObject.toBean( d.getJSONObject().getJSONObject("value"), Session.class - ); + ); session.setCreator(d.getJSONObject().getJSONArray("key").getString(0)); session.setName(d.getJSONObject().getJSONArray("key").getString(1)); session.set_id(d.getId()); @@ -174,7 +170,7 @@ public class CouchDBDao implements IDatabaseDao { return (Session) JSONObject.toBean( results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Session.class - ); + ); } @Override @@ -189,7 +185,7 @@ public class CouchDBDao implements IDatabaseDao { return (Session) JSONObject.toBean( results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Session.class - ); + ); } @Override @@ -243,14 +239,14 @@ public class CouchDBDao implements IDatabaseDao { com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session( databaseHost, databasePort - ); + ); database = session.getDatabase(databaseName); } catch (Exception e) { LOGGER.error( "Cannot connect to CouchDB database '" + databaseName + "' on host '" + databaseHost + "' using port " + databasePort - ); + ); } } @@ -363,14 +359,14 @@ public class CouchDBDao implements IDatabaseDao { Question q = (Question) JSONObject.toBean( results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Question.class - ); + ); JSONArray possibleAnswers = new JSONArray(); possibleAnswers = results.getJSONArray("rows").optJSONObject(0).optJSONObject("value") .getJSONArray("possibleAnswers"); Collection<PossibleAnswer> answers = JSONArray.toCollection( possibleAnswers, PossibleAnswer.class - ); + ); q.setPossibleAnswers(new ArrayList<PossibleAnswer>(answers)); q.setSessionKeyword(this.getSessionKeyword(q.getSessionId())); return q; @@ -461,7 +457,7 @@ public class CouchDBDao implements IDatabaseDao { LOGGER.error("IOException: Could not delete question {}", question.get_id()); } } - + @Override public final void deleteAllQuestionsWithAnswers(Session session) { NovaView view = new NovaView("skill_question/by_session"); @@ -472,7 +468,7 @@ public class CouchDBDao implements IDatabaseDao { view.setStartKeyArray(session.get_id()); view.setEndKey(session.get_id(), "{}"); ViewResults results = this.getDatabase().view(view); - + for (Document d : results.getResults()) { Question q = new Question(); q.set_id(d.getId()); @@ -529,7 +525,7 @@ public class CouchDBDao implements IDatabaseDao { return (Answer) JSONObject.toBean( results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Answer.class - ); + ); } @Override @@ -554,7 +550,7 @@ public class CouchDBDao implements IDatabaseDao { a.setQuestionId(d.getJSONObject().getJSONArray("key").getString(0)); a.setPiRound(piRound); String answerText = d.getJSONObject().getJSONArray("key").getString(2); - a.setAnswerText(answerText == "null" ? null : answerText); + a.setAnswerText("null".equals(answerText) ? null : answerText); answers.add(a); } return answers; @@ -721,7 +717,7 @@ public class CouchDBDao implements IDatabaseDao { InterposedQuestion question = (InterposedQuestion) JSONObject.toBean( document.getJSONObject().getJSONObject("value"), InterposedQuestion.class - ); + ); question.setSessionId(sessionKey); question.set_id(document.getId()); result.add(question); @@ -850,8 +846,8 @@ public class CouchDBDao implements IDatabaseDao { for (int i = 0; i < rows.size(); i++) { JSONObject row = rows.getJSONObject(i); if ( - row.getString("key").equals(key) - ) { + row.getString("key").equals(key) + ) { result += row.getInt("value"); } } @@ -899,9 +895,9 @@ public class CouchDBDao implements IDatabaseDao { if (d.getJSONObject().optJSONArray("value") != null) { @SuppressWarnings("unchecked") Collection<Session> visitedSessions = JSONArray.toCollection( - d.getJSONObject().getJSONArray("value"), - Session.class - ); + d.getJSONObject().getJSONArray("value"), + Session.class + ); allSessions.addAll(visitedSessions); } } @@ -989,7 +985,7 @@ public class CouchDBDao implements IDatabaseDao { Session session = (Session) JSONObject.toBean( d.getJSONObject().getJSONObject("value"), Session.class - ); + ); result.add(session); } return result; @@ -1042,6 +1038,7 @@ public class CouchDBDao implements IDatabaseDao { } } + @Override public String getQueryString() { StringBuilder query = new StringBuilder(); if (super.getQueryString() != null) { @@ -1055,7 +1052,9 @@ public class CouchDBDao implements IDatabaseDao { query.append("keys=" + this.keys); } - if (query.toString().isEmpty()) return null; + if (query.toString().isEmpty()) { + return null; + } return query.toString(); } } @@ -1144,9 +1143,9 @@ public class CouchDBDao implements IDatabaseDao { } } return getQuestions(new NovaView(viewName), session); - + } - + private List<Question> getQuestions(NovaView view, Session session) { view.setStartKeyArray(session.get_id()); view.setEndKeyArray(session.get_id(), "{}"); @@ -1163,12 +1162,12 @@ public class CouchDBDao implements IDatabaseDao { Question question = (Question) JSONObject.toBean( document.getJSONObject().getJSONObject("value"), Question.class - ); + ); @SuppressWarnings("unchecked") Collection<PossibleAnswer> answers = JSONArray.toCollection( document.getJSONObject().getJSONObject("value").getJSONArray("possibleAnswers"), PossibleAnswer.class - ); + ); question.setPossibleAnswers(new ArrayList<PossibleAnswer>(answers)); question.setSessionKeyword(session.getKeyword()); if (!"freetext".equals(question.getQuestionType()) && 0 == question.getPiRound()) { @@ -1194,7 +1193,7 @@ public class CouchDBDao implements IDatabaseDao { public int getPreparationQuestionCount(Session session) { return getQuestionCount(new NovaView("skill_question/preparation_question_count_by_session"), session); } - + private int getQuestionCount(NovaView view, Session session) { view.setKey(session.get_id()); ViewResults results = this.getDatabase().view(view); @@ -1213,7 +1212,7 @@ public class CouchDBDao implements IDatabaseDao { public int countPreparationQuestionAnswers(Session session) { return countQuestionVariantAnswers(session, "preparation"); } - + private int countQuestionVariantAnswers(Session session, String variant) { NovaView view = new NovaView("skill_question/count_answers_by_session_and_question_variant"); view.setKey(session.get_id(), variant); @@ -1308,7 +1307,8 @@ public class CouchDBDao implements IDatabaseDao { } return ids; } - + + @Override public void deleteAllInterposedQuestions(Session session) { NovaView view = new NovaView("interposed_question/by_session"); view.setKey(session.get_id()); diff --git a/src/main/java/de/thm/arsnova/dao/NovaView.java b/src/main/java/de/thm/arsnova/dao/NovaView.java index b9325b598ea5208be3412643ed0c31999ef2dc04..e525f06c32ff9c6dbb6d84a304f1e2f2c3e1d733 100644 --- a/src/main/java/de/thm/arsnova/dao/NovaView.java +++ b/src/main/java/de/thm/arsnova/dao/NovaView.java @@ -100,7 +100,7 @@ public class NovaView extends View { private String quote(String string) { return encode("\"" + string + "\""); } - + private boolean isNumber(String string) { return string.matches("^[0-9]+$"); } diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 205ee49b743cfafe17347bbed23d3c8deca2323e..0d63d14a80ef35230a96756db568389880948199 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -128,8 +128,8 @@ public class SessionService implements ISessionService { } List<Session> courseSessions = databaseDao.getCourseSessions( - connectorClient.getCourses(user.getUsername()).getCourse() - ); + connectorClient.getCourses(user.getUsername()).getCourse() + ); Map<String, Session> allAvailableSessions = new HashMap<String, Session>(); @@ -152,8 +152,8 @@ public class SessionService implements ISessionService { public final Session saveSession(final Session session) { if (connectorClient != null && session.getCourseId() != null) { if (!connectorClient.getMembership( - userService.getCurrentUser().getUsername(), session.getCourseId()).isMember() - ) { + userService.getCurrentUser().getUsername(), session.getCourseId()).isMember() + ) { throw new ForbiddenException(); } } diff --git a/src/main/java/de/thm/arsnova/services/UserService.java b/src/main/java/de/thm/arsnova/services/UserService.java index 99521ec7530b50d628bd9b9555633967da72b3f4..73b5e79d4236370e7f8aaeee212ec028cd1020aa 100644 --- a/src/main/java/de/thm/arsnova/services/UserService.java +++ b/src/main/java/de/thm/arsnova/services/UserService.java @@ -86,17 +86,7 @@ public class UserService implements IUserService { User user = null; if (authentication instanceof OAuthAuthenticationToken) { - OAuthAuthenticationToken token = (OAuthAuthenticationToken) authentication; - if (token.getUserProfile() instanceof Google2Profile) { - Google2Profile profile = (Google2Profile) token.getUserProfile(); - user = new User(profile); - } else if (token.getUserProfile() instanceof TwitterProfile) { - TwitterProfile profile = (TwitterProfile) token.getUserProfile(); - user = new User(profile); - } else if (token.getUserProfile() instanceof FacebookProfile) { - FacebookProfile profile = (FacebookProfile) token.getUserProfile(); - user = new User(profile); - } + user = getOAuthUser(authentication, user); } else if (authentication instanceof CasAuthenticationToken) { CasAuthenticationToken token = (CasAuthenticationToken) authentication; user = new User(token.getAssertion().getPrincipal()); @@ -115,6 +105,21 @@ public class UserService implements IUserService { return user; } + private User getOAuthUser(Authentication authentication, User user) { + OAuthAuthenticationToken token = (OAuthAuthenticationToken) authentication; + if (token.getUserProfile() instanceof Google2Profile) { + Google2Profile profile = (Google2Profile) token.getUserProfile(); + user = new User(profile); + } else if (token.getUserProfile() instanceof TwitterProfile) { + TwitterProfile profile = (TwitterProfile) token.getUserProfile(); + user = new User(profile); + } else if (token.getUserProfile() instanceof FacebookProfile) { + FacebookProfile profile = (FacebookProfile) token.getUserProfile(); + user = new User(profile); + } + return user; + } + @Override public User getUser2SocketId(UUID socketId) { return socketid2user.get(socketId); diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java index d63372fa8b4155a7a0f24d16e70b3f2422db5c24..7fbb17ae61e3f4ec1a63cd14f63cbfb53d866bed 100644 --- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java +++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java @@ -30,7 +30,6 @@ import de.thm.arsnova.entities.User; import de.thm.arsnova.events.ARSnovaEvent; import de.thm.arsnova.exceptions.NoContentException; import de.thm.arsnova.services.IFeedbackService; -import de.thm.arsnova.services.IQuestionService; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; import de.thm.arsnova.socket.message.Feedback; @@ -41,9 +40,6 @@ public class ARSnovaSocketIOServer { @Autowired private IFeedbackService feedbackService; - @Autowired - private IQuestionService questionService; - @Autowired private IUserService userService; @@ -65,14 +61,14 @@ public class ARSnovaSocketIOServer { public ARSnovaSocketIOServer() { config = new Configuration(); } - + @PreDestroy public void closeAllSessions() { LOGGER.info("Close all websockets due to @PreDestroy"); for (SocketIOClient c : server.getAllClients()) { c.disconnect(); } - + int clientCount = 0; for (SocketIOClient c : server.getAllClients()) { c.send(new Packet(PacketType.DISCONNECT)); @@ -135,7 +131,11 @@ public class ARSnovaSocketIOServer { server.addDisconnectListener(new DisconnectListener() { @Override public void onDisconnect(SocketIOClient client) { - if (userService == null || client.getSessionId() == null || userService.getUser2SocketId(client.getSessionId()) == null) { + if ( + userService == null + || client.getSessionId() == null + || userService.getUser2SocketId(client.getSessionId()) == null + ) { LOGGER.warn("NullPointer in ARSnovaSocketIOServer DisconnectListener"); return; } @@ -306,8 +306,8 @@ public class ARSnovaSocketIOServer { broadcastInSession(sessionKey, "lecQuestionAvail", lecturerQuestionId); } - /** Sends event to a websocket connection identified by UUID - * + /** Sends event to a websocket connection identified by UUID + * * @param sessionId The UUID of the websocket ID * @param event The event to be send to client * TODO This method is unimplemented!