Skip to content

Commit

Permalink
QA-15380: Issue with customized preview and aliased user (#18)
Browse files Browse the repository at this point in the history
Fix an issue with customized preview for Javascript modules for content with virtual nodes.

The virtual nodes are correctly created (temporarily) in the session of the aliased user, however the rendering was performed with the current user, causing a ItemNotFoundException. The rendering now uses the aliased user if any, and fallback to the current user otherwise.
  • Loading branch information
baptistegrimaud authored Dec 13, 2024
1 parent 4b1a2f9 commit 9c734a8
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jahia.services.render.RenderException;
import org.jahia.services.render.RenderService;
import org.jahia.services.render.Resource;
import org.jahia.services.usermanager.JahiaUser;
import org.jahia.taglibs.template.include.*;
import org.jahia.taglibs.uicomponents.Functions;
import org.slf4j.Logger;
Expand Down Expand Up @@ -121,7 +122,12 @@ public JCRNodeWrapper getBoundNode(JCRNodeWrapper node, RenderContext context) {
}

public String renderComponent(Map<String, ?> attr, RenderContext renderContext) throws RepositoryException {
return jcrTemplate.doExecuteWithSystemSessionAsUser(jcrSessionFactory.getCurrentUser(), renderContext.getWorkspace(),
// first try to get the aliased user from the session (when using customized preview for instance), otherwise use the current user
JahiaUser user = jcrSessionFactory.getCurrentAliasedUser();
if (user == null) {
user = jcrSessionFactory.getCurrentUser();
}
return jcrTemplate.doExecuteWithSystemSessionAsUser(user, renderContext.getWorkspace(),
renderContext.getMainResource().getLocale(), session -> {

JCRNodeWrapper node = JSNodeMapper.toVirtualNode((Map<String, ?>) attr.get("content"), session, renderContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {addNode, createUser, deleteUser} from '@jahia/cypress';
import {addSimplePage} from '../../utils/Utils';

describe('Test virtual nodes', () => {
const pageName = 'testVirtualNode';

before('Create test page and contents', () => {
addSimplePage('/sites/npmTestSite/home', pageName, pageName, 'en', 'simple', [
{
name: 'pagecontent',
primaryNodeType: 'jnt:contentList'
}
]).then(() => {
addNode({
parentPathOrId: `/sites/npmTestSite/home/${pageName}/pagecontent`,
name: 'test',
primaryNodeType: 'npmExample:testVirtualNode',
properties: [
{name: 'jcr:title', value: 'Test Virtual Node'}
]
});
});
});

it(`${pageName}: Check virtual nodes are correctly rendered in preview mode`, function () {
cy.login();
cy.visit(`/cms/render/default/en/sites/npmTestSite/home/${pageName}.html`);
cy.get('div[data-testid="testVirtualNodeSample_myProperty"]').contains('this is a virtual node property');
cy.get('div[data-testid="virtualNode_aliasedUser"]').should('be.empty'); // logged as root, no alias
cy.logout();
});
it(`${pageName}: Check virtual nodes are correctly rendered in customized preview mode`, function () {
cy.login();
let users = ['fooUser', 'barUser'];
users.forEach(user => {
createUser(user, 'testPassword');
cy.visit(`/cms/render/default/en/sites/npmTestSite/home/${pageName}.html?alias=${user}`);
cy.get('div[data-testid="testVirtualNodeSample_myProperty"]').should('have.text', 'this is a virtual node property');
cy.get('div[data-testid="virtualNode_aliasedUser"]').should('have.text', user);
deleteUser('testUser');
});
cy.logout();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@
[npmExample:testRule] > jnt:content, npmExampleMix:npmExampleComponent, mix:title
- triggerProp (string)
- resultProp (string)

[npmExample:testVirtualNode] > jnt:content, npmExampleMix:npmExampleComponent, mix:title

[npmExample:testVirtualNodeSample] > jnt:content, npmExampleMix:npmExampleComponent, mix:title
- myProperty (string)
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
export * from './contentList/ContentListParameters';
export * from './hydratedNavMenu/HydratedNavMenu';
export * from './navMenu/NavMenu';
export * from './testCurrentContent/TestCurrentContent';
export * from './testAbsoluteAreas/TestAbsoluteAreas';
export * from './testAreaColumns/TestAreaColumns';
export * from './testAreas/TestAreas';
export * from './testAbsoluteAreas/TestAbsoluteAreas';
export * from './testBoundComponent/TestBoundComponent';
export * from './testConfig/TestConfig';
export * from './testFindDisplayableContent/TestFindDisplayableContent';
export * from './testGQL/TestGQL';
export * from './testRender';
export * from './testUrl/TestUrl';
export * from './testGetNodeProps/TestGetNodeProps';
export * from './testGetChildNodes/TestGetChildNodes';
export * from './testContentTemplate/TestContentTemplate';
export * from './testContentTemplate/TestContentTemplateView';
export * from './testContentTemplate/TestContentTemplateOtherView';
export * from './testReactViewRegistration/TestReactViewRegistration';
export * from './testJCRQuery/TestJCRQuery';
export * from './testReactClientSide/TestReactClientSide';
export * from './contentList/ContentListParameters';
export * from './testContentTemplate/TestContentTemplateView';
export * from './testCrashingView/TestCrashingView';
export * from './testCurrentContent/TestCurrentContent';
export * from './testCurrentUser/TestCurrentUser';
export * from './testIsNodeType/TestIsNodeType';
export * from './testFindDisplayableContent/TestFindDisplayableContent';
export * from './testGQL/TestGQL';
export * from './testGetChildNodes/TestGetChildNodes';
export * from './testGetNodeProps/TestGetNodeProps';
export * from './testHasPermission/TestHasPermission';
export * from './testUrlParameters/TestUrlParameters';
export * from './testLocale/TestLocale';
export * from './testCrashingView/TestCrashingView';
export * from './testBoundComponent/TestBoundComponent';
export * from './testI18n/TestI18n';
export * from './testIsNodeType/TestIsNodeType';
export * from './testJCRQuery/TestJCRQuery';
export * from './testLocale/TestLocale';
export * from './testReactClientSide/TestReactClientSide';
export * from './testReactViewRegistration/TestReactViewRegistration';
export * from './testRender';
export * from './testRule/TestRule';
export * from './testUrl/TestUrl';
export * from './testUrlParameters/TestUrlParameters';
export * from './testVirtualNode';

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import {defineJahiaComponent, Render, useServerContext} from '@jahia/javascript-modules-library';


export const TestVirtualNode = () => {
const {currentNode, renderContext} = useServerContext();
const aliasedUser = renderContext.getMainResource().getNode().getSession().getAliasedUser();
// create a virtual node (TestVirtualNodeSample)
const testVirtualNodeSample = {
name: 'testVirtualNodeSample',
nodeType: 'npmExample:testVirtualNodeSample',
properties: {
'myProperty': 'this is a virtual node property'
}
};
return (
<>
<h3>test virtual node</h3>
<div data-testid="virtualNode">
<div data-testid="virtualNode_aliasedUser">{aliasedUser == null ? "" : aliasedUser.getName()}</div>
<Render content={testVirtualNodeSample}/>
</div>
<hr/>
</>
)
}

TestVirtualNode.jahiaComponent = defineJahiaComponent({
nodeType: 'npmExample:testVirtualNode',
name: 'default',
displayName: 'test virtual node',
componentType: 'view'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import {defineJahiaComponent, getNodeProps, useServerContext} from '@jahia/javascript-modules-library';

export const TestVirtualNodeSample = () => {
const {currentNode} = useServerContext();
const props = getNodeProps(currentNode, ['myProperty']);

return (
<div data-testid="testVirtualNodeSample_myProperty">{props['myProperty']}</div>
);
};

TestVirtualNodeSample.jahiaComponent = defineJahiaComponent({
nodeType: 'npmExample:testVirtualNodeSample',
componentType: 'view',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './TestVirtualNode';
export * from './TestVirtualNodeSample';

0 comments on commit 9c734a8

Please sign in to comment.