Skip to content

Commit

Permalink
Schema API is used outside of the admin context #1555
Browse files Browse the repository at this point in the history
  • Loading branch information
alansemenov committed Jan 2, 2025
1 parent 428c158 commit d4e6432
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
compileOnly "com.enonic.xp:portal-api:${xpVersion}"
compileOnly "com.enonic.xp:script-api:${xpVersion}"

implementation "com.enonic.xp:lib-context:${xpVersion}"
implementation "com.enonic.xp:lib-io:${xpVersion}"
implementation "com.enonic.xp:lib-portal:${xpVersion}"
implementation "com.enonic.xp:lib-schema:${xpVersion}"
Expand Down
15 changes: 8 additions & 7 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@babel/register": "^7.25.9",
"@enonic-types/global": "^7.15.0-A2",
"@enonic-types/lib-content": "^7.15.0-A2",
"@enonic-types/lib-context": "^7.15.0-A2",
"@enonic-types/lib-io": "^7.15.0-A2",
"@enonic-types/lib-node": "^7.15.0-A2",
"@enonic-types/lib-portal": "^7.15.0-A2",
Expand Down
67 changes: 47 additions & 20 deletions src/main/resources/lib/enonic/react4xp/DataFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import type {
RenderableWarning,
XpRunMode,
} from '@enonic/react-components/dist/nashorn';

import type {
ContextParams
} from '@enonic-types/lib-context';

import {getIn} from '@enonic/js-utils/object/getIn';
import {setIn} from '@enonic/js-utils/object/setIn';
Expand All @@ -53,6 +55,11 @@ import {
processHtml,
// pageUrl as getPageUrl,
} from '/lib/xp/portal';

import {
run as runInContext
} from '/lib/xp/context';

import {
REQUEST_METHOD,
REQUEST_MODE,
Expand Down Expand Up @@ -208,6 +215,10 @@ export type ShortcutContent = Content<{

const RUN_MODE = IS_DEV_MODE ? 'development' : 'production';

const ADMIN_CONTEXT: ContextParams = {
principals: ['role:system.admin']
}

export class DataFetcher {
private content: PageContent;
private contentTypes: Record<PageDescriptor, PageComponentProcessorFunction> = {};
Expand Down Expand Up @@ -273,10 +284,14 @@ export class DataFetcher {

if (!mixin) {
const [application] = name.split(':');
const mixinsList = listSchemas({
application,
type: 'MIXIN'
}) as MixinSchema[];
const mixinsList = runInContext(
ADMIN_CONTEXT,
() =>
listSchemas({
application,
type: 'MIXIN'
}) as MixinSchema[]
);
// log.debug('findHtmlAreasInFormItemArray mixinsList', mixinsList);

for (let j = 0; j < mixinsList.length; j++) {
Expand Down Expand Up @@ -441,19 +456,19 @@ export class DataFetcher {
const {type} = fragment;
// log.info('processFragment fragment:', fragment);

if(type === 'part') {
if (type === 'part') {
return this.processPart({
component: fragment,
...passAlong
});
}
if(type === 'layout') {
if (type === 'layout') {
return this.processLayout({
component: fragment,
...passAlong
});
}
if(type === 'text') {
if (type === 'text') {
return this.processTextComponent({
component: fragment as TextComponent,
});
Expand Down Expand Up @@ -493,10 +508,14 @@ export class DataFetcher {
};
}

const {form} = getComponentSchema({
key: descriptor,
type: 'LAYOUT',
}) as GetComponentReturnType;
const {form} = runInContext(
ADMIN_CONTEXT,
() =>
getComponentSchema({
key: descriptor,
type: 'LAYOUT',
}) as GetComponentReturnType
);

const processedLayoutComponent = this.processWithRegions({
component,
Expand Down Expand Up @@ -640,10 +659,14 @@ export class DataFetcher {
};
}

const {form} = getComponentSchema({
key: descriptor,
type: 'PAGE',
}) as GetComponentReturnType;
const {form} = runInContext(
ADMIN_CONTEXT,
() =>
getComponentSchema({
key: descriptor,
type: 'PAGE',
}) as GetComponentReturnType
);

const processedPageComponent = this.processWithRegions({
component,
Expand Down Expand Up @@ -700,10 +723,14 @@ export class DataFetcher {
};
}

const {form} = getComponentSchema({
key: descriptor,
type: 'PART',
}) as GetComponentReturnType;
const {form} = runInContext(
ADMIN_CONTEXT,
() =>
getComponentSchema({
key: descriptor,
type: 'PART',
}) as GetComponentReturnType
);

const htmlAreas = this.getHtmlAreas({
ancestor: 'config',
Expand Down
1 change: 1 addition & 0 deletions types/package.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"dependencies": {
"@enonic-types/core": "%XP_TYPES_VERSION%",
"@enonic-types/lib-context": "%XP_TYPES_VERSION%",
"@enonic-types/lib-io": "%XP_TYPES_VERSION%",
"@enonic-types/lib-portal": "%XP_TYPES_VERSION%",
"@enonic-types/lib-schema": "%XP_TYPES_VERSION%",
Expand Down

0 comments on commit d4e6432

Please sign in to comment.