diff --git a/roda-common/roda-common-data/src/main/java/org/roda/core/data/common/RodaConstants.java b/roda-common/roda-common-data/src/main/java/org/roda/core/data/common/RodaConstants.java index 17f751d25a..31530b3a62 100644 --- a/roda-common/roda-common-data/src/main/java/org/roda/core/data/common/RodaConstants.java +++ b/roda-common/roda-common-data/src/main/java/org/roda/core/data/common/RodaConstants.java @@ -112,6 +112,10 @@ public enum DateGranularity { public static final String UI_DATE_TIME_FORMAT_UTC = "ui.dateTime.format.UTC"; public static final String UI_COOKIES_ACTIVE_PROPERTY = "ui.cookies.active"; + + public static final String UI_EXPIRED_SESSION_DETECTOR_ACTIVE = "ui.expired.session.detector.active"; + public static final String UI_EXPIRED_SESSION_DETECTOR_TIME = "ui.expired.session.detector.time"; + public static final String UI_GOOGLE_ANALYTICS_CODE_PROPERTY = "ui.google.analytics.code"; public static final String UI_GOOGLE_RECAPTCHA_CODE_PROPERTY = "ui.google.recaptcha.code"; diff --git a/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/ExpiredSessionDetector.java b/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/ExpiredSessionDetector.java new file mode 100644 index 0000000000..aa58353d22 --- /dev/null +++ b/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/ExpiredSessionDetector.java @@ -0,0 +1,56 @@ +package org.roda.wui.client.main; + +import com.google.gwt.user.client.rpc.AsyncCallback; +import org.roda.core.data.v2.user.User; +import org.roda.wui.client.common.UserLogin; +import com.google.gwt.user.client.Timer; +import org.roda.wui.client.management.RecoverLogin; +import org.roda.wui.client.management.Register; +import org.roda.wui.client.welcome.Welcome; +import org.roda.wui.common.client.tools.HistoryUtils; + +/** + * @author António Lindo + */ +public class ExpiredSessionDetector { + private Timer timer; + + public ExpiredSessionDetector() { + timer = new Timer() { + @Override + public void run() { + checkSessionStatus(); + } + }; + } + + public void setScheduleTime(int time) { + timer.scheduleRepeating(time); + } + + private boolean canGuestAccess() { + String path = HistoryUtils.getCurrentHistoryPath().get(0); + return path.equals(RecoverLogin.RESOLVER.getHistoryToken()) || path.equals(Login.RESOLVER.getHistoryToken()) + || path.equals(Theme.RESOLVER.getHistoryToken()) || path.equals(Register.RESOLVER.getHistoryToken()) + || path.equals(Welcome.RESOLVER.getHistoryToken()); + } + + public void checkSessionStatus() { + UserLogin.getInstance().getAuthenticatedUser(new AsyncCallback() { + @Override + public void onFailure(Throwable caught) { + UserLogin.getInstance().showSuggestLoginDialog(); + } + + @Override + public void onSuccess(User user) { + if (user.isGuest()) { + if (!canGuestAccess()) { + HistoryUtils.newHistory(Welcome.RESOLVER); + UserLogin.getInstance().showSuggestLoginDialog(); + } + } + } + }, true); + } +} diff --git a/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/Main.java b/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/Main.java index ab657b5549..4d6c082dae 100644 --- a/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/Main.java +++ b/roda-ui/roda-wui/src/main/java/org/roda/wui/client/main/Main.java @@ -141,6 +141,12 @@ public void init() { JavascriptUtils.setCookieOptions(messages.cookiesMessage(), messages.cookiesDismisse(), messages.cookiesLearnMore(), "#" + Theme.RESOLVER.getHistoryToken() + "/CookiesPolicy.html"); } + + if (ConfigurationManager.getBoolean(true, RodaConstants.UI_EXPIRED_SESSION_DETECTOR_ACTIVE)) { + ExpiredSessionDetector expiredSessionDetector = new ExpiredSessionDetector(); + expiredSessionDetector.setScheduleTime(ConfigurationManager.getInt(RodaConstants.UI_EXPIRED_SESSION_DETECTOR_TIME)); + } + } private void onHistoryChanged(String historyToken) { diff --git a/roda-ui/roda-wui/src/main/resources/config/roda-wui.properties b/roda-ui/roda-wui/src/main/resources/config/roda-wui.properties index 7639504a8f..85d59913e7 100644 --- a/roda-ui/roda-wui/src/main/resources/config/roda-wui.properties +++ b/roda-ui/roda-wui/src/main/resources/config/roda-wui.properties @@ -58,6 +58,9 @@ ui.sharedProperties.whitelist.configuration.prefix = ui.dateTime # cookie accept ui.sharedProperties.whitelist.configuration.prefix = ui.cookies +# session expired detector +ui.sharedProperties.whitelist.configuration.prefix = ui.expired.session.detector + # google re-captcha and analytics ui.sharedProperties.whitelist.configuration.property = ui.google.analytics.code ui.sharedProperties.whitelist.configuration.property = ui.google.recaptcha.code @@ -108,6 +111,11 @@ ui.sharedProperties.whitelist.configuration.prefix = ui.url ########################################################################## ui.cookies.active = true +########################################################################## +# Expired session detector settings +########################################################################## +ui.expired.session.detector.active = true +ui.expired.session.detector.time = 1800000 ########################################################################## # Google Analytics settings