mirror of
https://github.com/bitwarden/web
synced 2026-01-07 11:03:16 +00:00
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { Injectable } from "@angular/core";
|
|
import { ActivatedRouteSnapshot, CanActivate, Router } from "@angular/router";
|
|
|
|
import { BaseGuard } from "jslib-angular/guards/base.guard";
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
|
import { OrganizationService } from "jslib-common/abstractions/organization.service";
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
import { SyncService } from "jslib-common/abstractions/sync.service";
|
|
import { Permissions } from "jslib-common/enums/permissions";
|
|
|
|
@Injectable()
|
|
export class PermissionsGuard extends BaseGuard implements CanActivate {
|
|
constructor(
|
|
router: Router,
|
|
private organizationService: OrganizationService,
|
|
private platformUtilsService: PlatformUtilsService,
|
|
private i18nService: I18nService,
|
|
private syncService: SyncService
|
|
) {
|
|
super(router);
|
|
}
|
|
|
|
async canActivate(route: ActivatedRouteSnapshot) {
|
|
// TODO: We need to fix this issue once and for all.
|
|
if ((await this.syncService.getLastSync()) == null) {
|
|
await this.syncService.fullSync(false);
|
|
}
|
|
|
|
const org = await this.organizationService.get(route.params.organizationId);
|
|
if (org == null) {
|
|
return this.router.createUrlTree(["/"]);
|
|
}
|
|
|
|
if (!org.isOwner && !org.enabled) {
|
|
this.platformUtilsService.showToast(
|
|
"error",
|
|
null,
|
|
this.i18nService.t("organizationIsDisabled")
|
|
);
|
|
return this.router.createUrlTree(["/"]);
|
|
}
|
|
|
|
const permissions = route.data == null ? [] : (route.data.permissions as Permissions[]);
|
|
if (permissions != null && !org.hasAnyPermission(permissions)) {
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("accessDenied"));
|
|
return this.router.createUrlTree(["/"]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|