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 29, 2023
1 parent e1df7cb commit f943f04
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 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 @@ -73,5 +73,7 @@ let LorisHelper = function(configParams, userPerms, studyParams) {
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
7 changes: 6 additions & 1 deletion src/Middleware/UserPageDecorationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit f943f04

Please # to comment.