Merge pull request #1922 from umbraco/bugfix/initial-section

Bugfix: Initial section
This commit is contained in:
Lee Kelleher
2024-05-27 13:52:11 +01:00
committed by GitHub
3 changed files with 58 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ import type { ManifestSection } from '@umbraco-cms/backoffice/extension-registry
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { UmbExtensionManifestInitializer } from '@umbraco-cms/backoffice/extension-api';
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user';
export class UmbBackofficeContext extends UmbContextBase<UmbBackofficeContext> {
#activeSectionAlias = new UmbStringState(undefined);
@@ -23,9 +24,6 @@ export class UmbBackofficeContext extends UmbContextBase<UmbBackofficeContext> {
constructor(host: UmbControllerHost) {
super(host, UMB_BACKOFFICE_CONTEXT);
new UmbExtensionsManifestInitializer(this, umbExtensionsRegistry, 'section', null, (sections) => {
this.#allowedSections.setValue([...sections]);
});
// TODO: We need to ensure this request is called every time the user logs in, but this should be done somewhere across the app and not here [JOV]
this.consumeContext(UMB_AUTH_CONTEXT, (authContext) => {
@@ -34,6 +32,29 @@ export class UmbBackofficeContext extends UmbContextBase<UmbBackofficeContext> {
this.#getVersion();
});
});
this.#init();
}
async #init() {
const userContext = await this.getContext(UMB_CURRENT_USER_CONTEXT);
this.observe(
userContext.allowedSections,
(allowedSections) => {
if (!allowedSections) return;
new UmbExtensionsManifestInitializer(
this,
umbExtensionsRegistry,
'section',
(manifest) => allowedSections.includes(manifest.alias),
async (sections) => {
this.#allowedSections.setValue([...sections]);
},
'umbAllowedSectionsManifestInitializer',
);
},
'umbAllowedSectionsObserver',
);
}
async #getVersion() {

View File

@@ -64,22 +64,6 @@ export class UmbBackofficeMainElement extends UmbLitElement {
}
});
if (this._sections.length > 0) {
const fallbackSectionPath = UMB_SECTION_PATH_PATTERN.generateLocal({
sectionName: this._sections[0].manifest!.meta.pathname,
});
this._routes.push({
alias: '__redirect',
path: '/',
redirectTo: fallbackSectionPath,
});
this._routes.push({
alias: '__redirect',
path: '/section/',
redirectTo: fallbackSectionPath,
});
}
this.requestUpdate('_routes', oldValue);
}

View File

@@ -7,11 +7,13 @@ import { firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import { umbLocalizationRegistry } from '@umbraco-cms/backoffice/localization';
import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';
import { UMB_SECTION_PATH_PATTERN } from '@umbraco-cms/backoffice/section';
export class UmbCurrentUserContext extends UmbContextBase<UmbCurrentUserContext> {
#currentUser = new UmbObjectState<UmbCurrentUserModel | undefined>(undefined);
readonly currentUser = this.#currentUser.asObservable();
readonly allowedSections = this.#currentUser.asObservablePart((user) => user?.allowedSections);
readonly unique = this.#currentUser.asObservablePart((user) => user?.unique);
readonly languageIsoCode = this.#currentUser.asObservablePart((user) => user?.languageIsoCode);
readonly hasDocumentRootAccess = this.#currentUser.asObservablePart((user) => user?.hasDocumentRootAccess);
@@ -43,6 +45,7 @@ export class UmbCurrentUserContext extends UmbContextBase<UmbCurrentUserContext>
if (asObservable) {
this.observe(asObservable(), (currentUser) => {
this.#currentUser?.setValue(currentUser);
this.#redirectToFirstAllowedSectionIfNeeded();
});
}
}
@@ -75,6 +78,36 @@ export class UmbCurrentUserContext extends UmbContextBase<UmbCurrentUserContext>
}
});
}
async #redirectToFirstAllowedSectionIfNeeded() {
const url = new URL(window.location.href);
if (url.pathname === '/') {
const sectionManifest = await this.#firstAllowedSection();
if (!sectionManifest) return;
const fallbackSectionPath = UMB_SECTION_PATH_PATTERN.generateLocal({
sectionName: sectionManifest.meta.pathname,
});
history.pushState(null, '', fallbackSectionPath);
}
}
async #firstAllowedSection() {
const currentUser = this.#currentUser.getValue();
if (!currentUser) return;
/* TODO: this solution is not bullet proof as we still rely on the "correct" section to be registered at this point in time so we can get the path.
It probably would have been better if we used the section alias instead as the path.
Then we would have it available at all times and it also ensured a unique path. */
const sections = await this.observe(
umbExtensionsRegistry.byTypeAndAliases('section', currentUser.allowedSections),
() => {},
).asPromise();
return sections[0];
}
}
export default UmbCurrentUserContext;