-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathownership.service.ts
46 lines (45 loc) · 1.4 KB
/
ownership.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Injectable } from "@angular/core";
import { Router } from "@angular/router";
import { Store } from "@ngrx/store";
import { combineLatest, Observable } from "rxjs";
import { map } from "rxjs/operators";
import { Dataset } from "shared/sdk";
import {
selectIsAdmin,
selectProfile,
} from "state-management/selectors/user.selectors";
@Injectable({
providedIn: "root",
})
export class OwnershipService {
checkDatasetAccess(
dataset: Dataset | undefined,
store: Store,
router: Router,
) {
if (dataset) {
const userProfile$: Observable<any> = store.select(selectProfile);
const isAdmin$ = store.select(selectIsAdmin);
const accessGroups$: Observable<string[]> = userProfile$.pipe(
map((profile) => (profile ? profile.accessGroups : [])),
);
combineLatest([accessGroups$, isAdmin$])
.subscribe(([groups, isAdmin]) => {
const isInOwnerGroup =
groups.indexOf(dataset.ownerGroup) !== -1 || isAdmin;
const hasAccessToDataset =
isInOwnerGroup ||
dataset.accessGroups.some((g) => groups.includes(g));
if (!isInOwnerGroup && !hasAccessToDataset) {
router.navigate(["/401"], {
skipLocationChange: true,
queryParams: {
url: router.routerState.snapshot.url,
},
});
}
})
.unsubscribe();
}
}
}