Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Merge remote-tracking branch 'origin/dev' into feature/hide-bcf-if-not-allowed

Oliver Günther2b6730271d85
diff --cc frontend/src/app/modules/bim/bcf/api/bcf-authorization.service.ts
index fb1fee62e7c,00000000000..6c9708496a8
mode 100644,000000..100644
--- a/frontend/src/app/modules/bim/bcf/api/bcf-authorization.service.ts
+++ b/frontend/src/app/modules/bim/bcf/api/bcf-authorization.service.ts
@@@ -1,65 -1,0 +1,66 @@@
+import {multiInput} from "reactivestates";
+import {BcfExtensionResource} from "core-app/modules/bim/bcf/api/extensions/bcf-extension.resource";
+import {BcfApiService} from "core-app/modules/bim/bcf/api/bcf-api.service";
+import {Observable} from "rxjs";
+import {map, take} from "rxjs/operators";
+import {Injectable} from "@angular/core";
+
+export type AllowedExtensionKey = keyof BcfExtensionResource;
+
+@Injectable({ providedIn: 'root' })
+export class BcfAuthorizationService {
+
+ // Poor mans caching to avoid repeatedly fetching from the backend.
+ protected authorizationMap = multiInput<BcfExtensionResource>();
+
+ constructor(readonly bcfApi:BcfApiService) {
+ }
+
+ /**
+ * Returns an observable boolean whether the given action
+ * is authorized in the project by using the project extensions.
+ *
+ * Ensures the extension is loaded only once per project
+ *
+ * @param projectIdentifier Project identifier to check permission in
+ * @param extension The extension key to check for
+ * @param action The desired action
+ */
+ public authorized$(projectIdentifier:string, extension:AllowedExtensionKey, action:string):Observable<boolean> {
+ const state = this.authorizationMap.get(projectIdentifier);
+
+ state.putFromPromiseIfPristine(() =>
+ this.bcfApi
+ .projects.id(projectIdentifier)
+ .extensions
+ .get()
+ .toPromise()
+ );
+
+ return state
+ .values$()
+ .pipe(
+ map(
+ resource => resource[extension] && resource[extension].includes(action)
+ )
+ );
+ }
+
+ /**
+ * One-time check to determine current allowed state.
+ *
+ * @param projectIdentifier Project identifier to check permission in
+ * @param extension The extension key to check for
+ * @param action The desired action
+ */
+ public isAllowedTo(projectIdentifier:string, extension:AllowedExtensionKey, action:string):Promise<boolean> {
+ return this
+ .authorized$(projectIdentifier, extension, action)
+ .pipe(
+ take(1)
+ )
- .toPromise();
++ .toPromise()
++ .catch(() => false);
+ }
+}
+