[ofbiz-framework] branch trunk updated: Fixed: setUserTimeZone should ran only once based on error (OFBIZ-11329)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Fixed: setUserTimeZone should ran only once based on error (OFBIZ-11329)

jleroux@apache.org
This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 350c71f  Fixed: setUserTimeZone should ran only once based on error (OFBIZ-11329)
350c71f is described below

commit 350c71f4df45cbe5671b54e61f74f9a352d78e05
Author: Jacques Le Roux <[hidden email]>
AuthorDate: Wed Feb 5 09:30:55 2020 +0100

    Fixed: setUserTimeZone should ran only once based on error
    (OFBIZ-11329)
   
    This will be notably useful when committing CSRF solution as explained in
    OFBIZ-11306:
   
    SetTimeZoneFromBrowser when starting gives a  RequestHandlerException:
    Invalid or missing CSRF token for AJAX call to path '/SetTimeZoneFromBrowser'.
    Also not only when starting.
   
    Thanks: James Yong for review
---
 framework/common/groovyScripts/SetTimeZoneFromBrowser.groovy | 5 +++--
 themes/common-theme/webapp/common/js/util/setUserTimeZone.js | 9 ++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/framework/common/groovyScripts/SetTimeZoneFromBrowser.groovy b/framework/common/groovyScripts/SetTimeZoneFromBrowser.groovy
index 9a0201a..21f9607 100644
--- a/framework/common/groovyScripts/SetTimeZoneFromBrowser.groovy
+++ b/framework/common/groovyScripts/SetTimeZoneFromBrowser.groovy
@@ -20,13 +20,14 @@
 import org.apache.ofbiz.service.ServiceUtil
 
 public Map SetTimeZoneFromBrowser() {
-    Map results = ServiceUtil.returnSuccess()
     userLogin = from("UserLogin").where("userLoginId", parameters.userLogin.userLoginId).queryFirst();
     if (userLogin) {
         if (!userLogin.lastTimeZone || "null".equals(userLogin.lastTimeZone)) {
             userLogin.lastTimeZone = parameters.localeName
             userLogin.store()
+            return ServiceUtil.returnSuccess()
         }
+    } else {
+        return ServiceUtil.returnError()
     }
-    return results
 }
diff --git a/themes/common-theme/webapp/common/js/util/setUserTimeZone.js b/themes/common-theme/webapp/common/js/util/setUserTimeZone.js
index 4c29928..b74504b 100644
--- a/themes/common-theme/webapp/common/js/util/setUserTimeZone.js
+++ b/themes/common-theme/webapp/common/js/util/setUserTimeZone.js
@@ -17,18 +17,17 @@ specific language governing permissions and limitations
 under the License.
 */
 
-// Only once by session
+// Only once by session (ref https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)
 if (sessionStorage.getItem("SetTimeZoneFromBrowser") === null || sessionStorage.getItem("SetTimeZoneFromBrowser") !== "done") {
-    sessionStorage.setItem("SetTimeZoneFromBrowser", "done");
     var timezone = moment.tz.guess();
     $.ajax({
         url: "SetTimeZoneFromBrowser",
         type: "POST",
         async: false,
         data: "localeName=" + timezone,
-        error: function(error) {
-            if (error != "") {
-                console.error("Error while setting user locale: ", error);
+        success: function(success) {
+            if (success._ERROR_MESSAGE_ === undefined && success._ERROR_MESSAGE_LIST_ === undefined) {
+                sessionStorage.setItem("SetTimeZoneFromBrowser", "done");
             }
         }
     });