[kune-commits] r1526 - in trunk/src/main/java/cc/kune: common/client/utils core/client core/client/auth core/client/cookies core/client/state

Vicente J. Ruiz Jurado vjrj_ at ourproject.org
Thu Sep 22 18:08:44 CEST 2011


Author: vjrj_
Date: 2011-09-22 18:08:44 +0200 (Thu, 22 Sep 2011)
New Revision: 1526

Added:
   trunk/src/main/java/cc/kune/core/client/auth/AnonUsersManager.java
Modified:
   trunk/src/main/java/cc/kune/common/client/utils/TextUtils.java
   trunk/src/main/java/cc/kune/core/client/CoreGinModule.java
   trunk/src/main/java/cc/kune/core/client/CoreParts.java
   trunk/src/main/java/cc/kune/core/client/auth/SignInAbstractPresenter.java
   trunk/src/main/java/cc/kune/core/client/cookies/CookiesManager.java
   trunk/src/main/java/cc/kune/core/client/cookies/CookiesManagerImpl.java
   trunk/src/main/java/cc/kune/core/client/state/Session.java
   trunk/src/main/java/cc/kune/core/client/state/SessionDefault.java
Log:
CLOSED - # 102: Highlight Sign-In and/or show "New" button disabled to anonymous user 
http://kune.ourproject.org/issues/ticket/102

Modified: trunk/src/main/java/cc/kune/common/client/utils/TextUtils.java
===================================================================
--- trunk/src/main/java/cc/kune/common/client/utils/TextUtils.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/common/client/utils/TextUtils.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -24,8 +24,10 @@
 import org.apache.commons.lang.WordUtils;
 
 import cc.kune.common.client.errors.NotImplementedException;
+import cc.kune.common.client.errors.UIException;
 
 import com.google.gwt.safehtml.shared.SimpleHtmlSanitizer;
+import com.google.gwt.safehtml.shared.UriUtils;
 
 public class TextUtils {
 
@@ -169,15 +171,25 @@
   }
 
   /**
-   * FIXME: Is this safe? Try to use {@link SimpleHtmlSanitizer} also in the
-   * method
+   * Generates a href link
    * 
    */
-  @Deprecated
   public static String generateHtmlLink(final String href, final String text) {
-    return "<a href=\"" + href + "\" target=\"_blank\">" + text + "</a>";
+    return generateHtmlLink(href, text, true);
   }
 
+  /**
+   * Generates a href link
+   * 
+   */
+  public static String generateHtmlLink(final String href, final String text, final boolean targetBlank) {
+    if (!UriUtils.isSafeUri(href)) {
+      throw new UIException("Unsafe href");
+    }
+    return "<a href=\"" + UriUtils.sanitizeUri(href) + "\"" + (targetBlank ? "target=\"_blank\"" : "")
+        + ">" + text + "</a>";
+  }
+
   private static boolean isDelimiter(final char ch, final char[] delimiters) {
     if (delimiters == null) {
       throw new NotImplementedException();

Modified: trunk/src/main/java/cc/kune/core/client/CoreGinModule.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/CoreGinModule.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/CoreGinModule.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -31,6 +31,7 @@
 import cc.kune.common.client.ui.MaskWidgetView;
 import cc.kune.core.client.actions.ActionRegistryByType;
 import cc.kune.core.client.actions.xml.XMLActionsParser;
+import cc.kune.core.client.auth.AnonUsersManager;
 import cc.kune.core.client.auth.Register;
 import cc.kune.core.client.auth.RegisterPanel;
 import cc.kune.core.client.auth.RegisterPresenter;
@@ -137,6 +138,7 @@
     s(I18nUITranslationService.class);
     bind(I18nTranslationService.class).to(I18nUITranslationService.class).in(Singleton.class);
     bind(GlobalShortcutRegister.class).to(DefaultGlobalShortcutRegister.class).in(Singleton.class);
+    s(AnonUsersManager.class);
 
     // DnD
     s(KuneDragController.class);

Modified: trunk/src/main/java/cc/kune/core/client/CoreParts.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/CoreParts.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/CoreParts.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -19,6 +19,7 @@
  */
 package cc.kune.core.client;
 
+import cc.kune.core.client.auth.AnonUsersManager;
 import cc.kune.core.client.init.AppStartEvent;
 import cc.kune.core.client.init.AppStartEvent.AppStartHandler;
 import cc.kune.core.client.sitebar.SiteUserOptionsPresenter;
@@ -37,7 +38,7 @@
   public CoreParts(final Session session, final Provider<GroupSNPresenter> groupMembersPresenter,
       final Provider<UserSNPresenter> buddiesAndParticipationPresenter,
       final Provider<GroupSNConfActions> groupMembersConfActions,
-      final Provider<UserSNConfActions> userSNConfActions,
+      final Provider<UserSNConfActions> userSNConfActions, final Provider<AnonUsersManager> anonUsers,
       final Provider<SiteUserOptionsPresenter> userOptions) {
     session.onAppStart(true, new AppStartHandler() {
       @Override
@@ -47,6 +48,7 @@
         groupMembersPresenter.get();
         buddiesAndParticipationPresenter.get();
         userOptions.get();
+        anonUsers.get();
       }
     });
   }

Added: trunk/src/main/java/cc/kune/core/client/auth/AnonUsersManager.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/auth/AnonUsersManager.java	                        (rev 0)
+++ trunk/src/main/java/cc/kune/core/client/auth/AnonUsersManager.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -0,0 +1,57 @@
+package cc.kune.core.client.auth;
+
+import cc.kune.common.client.notify.NotifyUser;
+import cc.kune.common.client.utils.TextUtils;
+import cc.kune.core.client.cookies.CookiesManager;
+import cc.kune.core.client.init.AppStartEvent;
+import cc.kune.core.client.init.AppStartEvent.AppStartHandler;
+import cc.kune.core.client.state.Session;
+import cc.kune.core.client.state.SiteTokens;
+import cc.kune.core.client.state.UserSignInEvent;
+import cc.kune.core.client.state.UserSignInEvent.UserSignInHandler;
+import cc.kune.core.shared.i18n.I18nTranslationService;
+
+import com.google.inject.Inject;
+
+public class AnonUsersManager {
+
+  @Inject
+  public AnonUsersManager(final Session session, final CookiesManager cookiesManager,
+      final I18nTranslationService i18n) {
+    session.onAppStart(true, new AppStartHandler() {
+      @Override
+      public void onAppStart(final AppStartEvent event) {
+        if (session.isNotLogged()) {
+          final String anonCookie = cookiesManager.getAnonCookie();
+          if (anonCookie == null) {
+            // First access, set cookie to short period (1day), and show message
+            cookiesManager.setAnonCookie(false);
+            final String register = TextUtils.generateHtmlLink("#" + SiteTokens.REGISTER,
+                i18n.tWithNT("register", "register, in lowercase"), false);
+            final String signin = TextUtils.generateHtmlLink("#" + SiteTokens.SIGNIN,
+                i18n.tWithNT("sign in", "register, in lowercase"), false);
+            NotifyUser.info(
+                i18n.tWithNT(
+                    "You did not sign-in, so you can just see some public contents in this website, "
+                        + "but not edit or collaborate with others. Please [%s] or [%s] in order to get full access to this site tools and contents",
+                    "This will be something like 'Please register or sign in', but instead of %s some links",
+                    register, signin), true);
+          } else {
+            if (Boolean.valueOf(anonCookie)) {
+              // Registered already: we set the cookie for some big period again
+              cookiesManager.setAnonCookie(true);
+            } else {
+              // Non registered yet: but we show the message already today
+            }
+          }
+        }
+      }
+    });
+    session.onUserSignIn(true, new UserSignInHandler() {
+      @Override
+      public void onUserSignIn(final UserSignInEvent event) {
+        cookiesManager.setAnonCookie(true);
+      }
+    });
+  }
+}

Modified: trunk/src/main/java/cc/kune/core/client/auth/SignInAbstractPresenter.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/auth/SignInAbstractPresenter.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/auth/SignInAbstractPresenter.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -76,7 +76,7 @@
 
   protected void onSignIn(final UserInfoDTO userInfoDTO) {
     final String userHash = userInfoDTO.getUserHash();
-    cookiesManager.setCookie(userHash);
+    cookiesManager.setAuthCookie(userHash);
     getView().reset();
     session.setUserHash(userHash);
     session.setCurrentUserInfo(userInfoDTO);

Modified: trunk/src/main/java/cc/kune/core/client/cookies/CookiesManager.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/cookies/CookiesManager.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/cookies/CookiesManager.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -20,9 +20,16 @@
 package cc.kune.core.client.cookies;
 
 public interface CookiesManager {
-    String getCurrentCookie();
+  String getAnonCookie();
 
-    void removeCookie();
+  String getAuthCookie();
 
-    void setCookie(String userHash);
+  void removeAnonCookie();
+
+  void removeAuthCookie();
+
+  void setAnonCookie(Boolean userRegister);
+
+  void setAuthCookie(String userHash);
+
 }

Modified: trunk/src/main/java/cc/kune/core/client/cookies/CookiesManagerImpl.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/cookies/CookiesManagerImpl.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/cookies/CookiesManagerImpl.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -28,31 +28,50 @@
 
 public class CookiesManagerImpl implements CookiesManager {
 
-    public CookiesManagerImpl() {
-    }
+  private static final String ANON = "annon";
 
-    @Override
-    public String getCurrentCookie() {
-        return Cookies.getCookie(Session.USERHASH);
-    }
+  public CookiesManagerImpl() {
+  }
 
-    @Override
-    public void removeCookie() {
-        // FIXME: Remove cookie doesn't works in all browsers, know
-        // issue:
-        // http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/ded86778ee56690/515dc513c7d085eb?lnk=st&q=remove+cookie#515dc513c7d085eb
-        // http://code.google.com/p/google-web-toolkit/issues/detail?id=1735&q=removeCookie
-        Cookies.removeCookie(Session.USERHASH);
-        // Workaround:
-        Cookies.setCookie(Session.USERHASH, null, new Date(0), null, "/", false);
-    }
+  @Override
+  public String getAnonCookie() {
+    return Cookies.getCookie(ANON);
+  }
 
-    @Override
-    public void setCookie(final String userHash) {
-        // http://code.google.com/p/google-web-toolkit-incubator/wiki/LoginSecurityFAQ
-        final long duration = Session.SESSION_DURATION;
-        final Date expires = new Date(System.currentTimeMillis() + duration);
-        Cookies.setCookie(Session.USERHASH, userHash, expires, null, "/", false);
-        Log.info("Received hash: " + userHash, null);
-    }
+  @Override
+  public String getAuthCookie() {
+    return Cookies.getCookie(Session.USERHASH);
+  }
+
+  @Override
+  public void removeAnonCookie() {
+    Cookies.removeCookie(ANON);
+    Cookies.setCookie(ANON, null, new Date(0), null, "/", false);
+  }
+
+  @Override
+  public void removeAuthCookie() {
+    // FIXME: Remove cookie doesn't works in all browsers, know
+    // issue:
+    // http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/ded86778ee56690/515dc513c7d085eb?lnk=st&q=remove+cookie#515dc513c7d085eb
+    // http://code.google.com/p/google-web-toolkit/issues/detail?id=1735&q=removeCookie
+    Cookies.removeCookie(Session.USERHASH);
+    // Workaround:
+    Cookies.setCookie(Session.USERHASH, null, new Date(0), null, "/", false);
+  }
+
+  @Override
+  public void setAnonCookie(final Boolean userRegister) {
+    final Date expires = new Date(System.currentTimeMillis()
+        + (userRegister ? Session.ANON_SESSION_DURATION_AFTER_REG : Session.ANON_SESSION_DURATION));
+    Cookies.setCookie(ANON, userRegister.toString(), expires, null, "/", false);
+  }
+
+  @Override
+  public void setAuthCookie(final String userHash) {
+    // http://code.google.com/p/google-web-toolkit-incubator/wiki/LoginSecurityFAQ
+    final Date expires = new Date(System.currentTimeMillis() + Session.SESSION_DURATION);
+    Cookies.setCookie(Session.USERHASH, userHash, expires, null, "/", false);
+    Log.info("Received hash: " + userHash, null);
+  }
 }

Modified: trunk/src/main/java/cc/kune/core/client/state/Session.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/state/Session.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/state/Session.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -61,11 +61,11 @@
 
 public interface Session {
 
-  /**
-   * Duration remembering login: 2 weeks
-   */
-  int SESSION_DURATION = 1000 * 60 * 60 * 24 * 14;
-  // int SESSION_DURATION = 1000;
+  long A_DAY = 1000 * 60 * 60 * 24;
+  long ANON_SESSION_DURATION = A_DAY;
+  long ANON_SESSION_DURATION_AFTER_REG = A_DAY * 365;
+  long SESSION_DURATION = A_DAY * 14; // two weeks login session duration
+  // long SESSION_DURATION = 1000;
 
   String USERHASH = "k007userHash";
 

Modified: trunk/src/main/java/cc/kune/core/client/state/SessionDefault.java
===================================================================
--- trunk/src/main/java/cc/kune/core/client/state/SessionDefault.java	2011-09-20 23:21:27 UTC (rev 1525)
+++ trunk/src/main/java/cc/kune/core/client/state/SessionDefault.java	2011-09-22 16:08:44 UTC (rev 1526)
@@ -71,7 +71,7 @@
       final Provider<UserServiceAsync> userServiceProvider, final EventBus eventBus) {
     this.cookieManager = cookieManager;
     this.eventBus = eventBus;
-    this.userHash = cookieManager.getCurrentCookie();
+    this.userHash = cookieManager.getAuthCookie();
     this.userHash = userHash == null || userHash.equals("null") ? null : userHash;
     this.userServiceProvider = userServiceProvider;
     languagesArray = null;
@@ -391,7 +391,7 @@
 
   @Override
   public void signOut() {
-    cookieManager.removeCookie();
+    cookieManager.removeAuthCookie();
     setUserHash(null);
     setCurrentUserInfo(null);
   }




More information about the kune-commits mailing list