Skip to content

Commit

Permalink
[JS] Add username/id to Loris helper object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
driusan committed Aug 28, 2023
1 parent e1df7cb commit 495b7d9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion htdocs/js/loris.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* exported LorisHelper */

let LorisHelper = function(configParams, userPerms, studyParams) {
let LorisHelper = function(user, configParams, userPerms, studyParams) {
'use strict';
let lorisObj = configParams;

Expand Down Expand Up @@ -72,6 +72,8 @@ let LorisHelper = function(configParams, userPerms, studyParams) {
'use strict';
return studyParams[param];
};

lorisObj.user = user;

return lorisObj;
};
2 changes: 1 addition & 1 deletion smarty/templates/main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
and can access them through the loris global (ie. loris.BaseURL) *}
<script src="{$baseurl}/js/loris.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
let loris = new LorisHelper({$jsonParams}, {$userPerms|json_encode}, {$studyParams|json_encode});
let loris = new LorisHelper({$userjson}, {$jsonParams}, {$userPerms|json_encode}, {$studyParams|json_encode});
</script>
{section name=jsfile loop=$jsfiles}
<script src="{$jsfiles[jsfile]}" type="text/javascript"></script>
Expand Down
4 changes: 4 additions & 0 deletions src/Middleware/UserPageDecorationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 495b7d9

Please # to comment.