From 495b7d934107c95639a51fd7be04af1ead773551 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 28 Aug 2023 09:41:37 -0400 Subject: [PATCH] [JS] Add username/id to Loris helper object Currently, you can access the PHP user object from smarty templates, but if you need access the username/userid from javascript you need to jump through hoops (like putting them in a smarty template or an endpoint that returns them.) This adds a `user` object of the form `{username: string, id: number}` to the loris helper object so that you can access the basics from javascript as `loris.user.username` or `loris.user.id` when needed. --- htdocs/js/loris.js | 4 +++- smarty/templates/main.tpl | 2 +- src/Middleware/UserPageDecorationMiddleware.php | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/htdocs/js/loris.js b/htdocs/js/loris.js index b0ddcf438b2..c88c576f5b9 100644 --- a/htdocs/js/loris.js +++ b/htdocs/js/loris.js @@ -1,6 +1,6 @@ /* exported LorisHelper */ -let LorisHelper = function(configParams, userPerms, studyParams) { +let LorisHelper = function(user, configParams, userPerms, studyParams) { 'use strict'; let lorisObj = configParams; @@ -72,6 +72,8 @@ let LorisHelper = function(configParams, userPerms, studyParams) { 'use strict'; return studyParams[param]; }; + + lorisObj.user = user; return lorisObj; }; diff --git a/smarty/templates/main.tpl b/smarty/templates/main.tpl index 7ad1025227b..323deca0e32 100644 --- a/smarty/templates/main.tpl +++ b/smarty/templates/main.tpl @@ -13,7 +13,7 @@ and can access them through the loris global (ie. loris.BaseURL) *} {section name=jsfile loop=$jsfiles} diff --git a/src/Middleware/UserPageDecorationMiddleware.php b/src/Middleware/UserPageDecorationMiddleware.php index 09fc7a1cf43..8c5bc890c39 100644 --- a/src/Middleware/UserPageDecorationMiddleware.php +++ b/src/Middleware/UserPageDecorationMiddleware.php @@ -221,6 +221,10 @@ function ($a, $b) { // Do not show menu item if module not active $tpl_data['my_preferences'] = $loris->hasModule('my_preferences'); + $tpl_data['userjson'] = json_encode([ + 'username' => $user->getUsername(), + 'id' => $user->getId() + ]); // Display the footer links, as specified in the config file $links = $this->Config->getExternalLinks('FooterLink');