From 2162de61120d0145d72cccacf63ddd7fca51bd86 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Fri, 13 Oct 2023 12:53:37 -0400 Subject: [PATCH] [JS] Add username/id to Loris helper object (#8876) 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 | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/htdocs/js/loris.js b/htdocs/js/loris.js index b0ddcf438b2..a882209c1a4 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; @@ -73,5 +73,7 @@ let LorisHelper = function(configParams, userPerms, studyParams) { 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..567e913b2ac 100644 --- a/src/Middleware/UserPageDecorationMiddleware.php +++ b/src/Middleware/UserPageDecorationMiddleware.php @@ -221,7 +221,12 @@ 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');