Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permissions loading in standalone and old shell #233

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ export enum CONFIG_KEY {
IS_SHELL = 'IS_SHELL',
AUTH_SERVICE = 'AUTH_SERVICE',
AUTH_SERVICE_CUSTOM_URL = 'AUTH_SERVICE_CUSTOM_URL',
STANDALONE_PERMISSIONS = 'STANDALONE_PERMISSIONS'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, OnDestroy } from '@angular/core'
import { BehaviorSubject, map } from 'rxjs'
import { BehaviorSubject, map, filter, firstValueFrom, skip } from 'rxjs'
import { PermissionsTopic, UserProfile, UserProfileTopic } from '@onecx/integration-interface'
import { DEFAULT_LANG } from '../api/constants'

Expand All @@ -10,6 +10,7 @@ export class UserService implements OnDestroy {
lang$ = new BehaviorSubject(this.determineLanguage() ?? DEFAULT_LANG)

private permissionsTopic$ = new PermissionsTopic()
private oldStylePermissionsInitialized: Promise<string[]>

constructor() {
this.profile$
Expand All @@ -21,7 +22,13 @@ export class UserService implements OnDestroy {
)
.subscribe(this.lang$)

this.profile$.pipe(map((profile) => this.extractPermissions(profile))).subscribe(this.permissions$)
this.oldStylePermissionsInitialized = firstValueFrom(this.permissions$.pipe(skip(1)))
this.profile$
.pipe(
map((profile) => this.extractPermissions(profile)),
filter((permissions): permissions is string[] => !!permissions)
)
.subscribe(this.permissions$)
}

ngOnDestroy(): void {
Expand Down Expand Up @@ -66,9 +73,9 @@ export class UserService implements OnDestroy {
}

private extractPermissions(userProfile: UserProfile) {
const permissions: string[] = []
if (userProfile) {
if (userProfile.memberships) {
const permissions: string[] = []
userProfile.memberships.forEach((m) => {
m.roleMemberships?.forEach((r) => {
r.permissions?.forEach((p) => {
Expand All @@ -80,13 +87,17 @@ export class UserService implements OnDestroy {
})
})
})
return permissions
}
}
return permissions
return null
}

get isInitialized(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return Promise.all([this.permissionsTopic$.isInitialized, this.profile$.isInitialized]).then(() => {})
return Promise.all([
Promise.race([this.permissionsTopic$.isInitialized, this.oldStylePermissionsInitialized]),
this.profile$.isInitialized,
// eslint-disable-next-line @typescript-eslint/no-empty-function
]).then(() => {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CONFIG_KEY,
IAuthService,
} from '@onecx/angular-integration-interface'
import { MfeInfo, PermissionsTopic } from '@onecx/integration-interface'
import { MfeInfo } from '@onecx/integration-interface'
import { PortalApiService } from '../../services/portal-api.service'
import { UserProfileAPIService } from '../../services/userprofile-api.service'

Expand Down Expand Up @@ -69,11 +69,6 @@ export function standaloneInitializer(
workspaceName: portal.portalName,
})

// TODO remove when permissions concept for standalone is there
const permissionsTopic$ = new PermissionsTopic()
await permissionsTopic$.publish((config.getProperty(CONFIG_KEY.STANDALONE_PERMISSIONS) ?? '').split(';'))
permissionsTopic$.destroy()

const standaloneMfeInfo: MfeInfo = {
mountPath: '/',
remoteBaseUrl: '.',
Expand Down
Loading