mirror of
https://github.com/bitwarden/web
synced 2026-01-04 17:43:47 +00:00
org route guards
This commit is contained in:
23
src/app/services/organization-guard.service.ts
Normal file
23
src/app/services/organization-guard.service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
ActivatedRouteSnapshot,
|
||||
CanActivate,
|
||||
Router,
|
||||
} from '@angular/router';
|
||||
|
||||
import { UserService } from 'jslib/abstractions/user.service';
|
||||
|
||||
@Injectable()
|
||||
export class OrganizationGuardService implements CanActivate {
|
||||
constructor(private userService: UserService, private router: Router) { }
|
||||
|
||||
async canActivate(route: ActivatedRouteSnapshot) {
|
||||
const org = await this.userService.getOrganization(route.params.organizationId);
|
||||
if (org == null) {
|
||||
this.router.navigate(['/']);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
26
src/app/services/organization-type-guard.service.ts
Normal file
26
src/app/services/organization-type-guard.service.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
ActivatedRouteSnapshot,
|
||||
CanActivate,
|
||||
Router,
|
||||
} from '@angular/router';
|
||||
|
||||
import { UserService } from 'jslib/abstractions/user.service';
|
||||
|
||||
import { OrganizationUserType } from 'jslib/enums/organizationUserType';
|
||||
|
||||
@Injectable()
|
||||
export class OrganizationTypeGuardService implements CanActivate {
|
||||
constructor(private userService: UserService, private router: Router) { }
|
||||
|
||||
async canActivate(route: ActivatedRouteSnapshot) {
|
||||
const org = await this.userService.getOrganization(route.parent.params.organizationId);
|
||||
const allowedTypes = route.data['allowedTypes'] as OrganizationUserType[];
|
||||
if (allowedTypes == null || allowedTypes.indexOf(org.type) === -1) {
|
||||
this.router.navigate(['/organizations', org.id]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import { I18nService } from '../../services/i18n.service';
|
||||
import { MemoryStorageService } from '../../services/memoryStorage.service';
|
||||
import { WebPlatformUtilsService } from '../../services/webPlatformUtils.service';
|
||||
|
||||
import { OrganizationGuardService } from './organization-guard.service';
|
||||
import { OrganizationTypeGuardService } from './organization-type-guard.service';
|
||||
import { RouterService } from './router.service';
|
||||
import { UnauthGuardService } from './unauth-guard.service';
|
||||
|
||||
@@ -142,6 +144,8 @@ export function initFactory(): Function {
|
||||
providers: [
|
||||
ValidationService,
|
||||
AuthGuardService,
|
||||
OrganizationGuardService,
|
||||
OrganizationTypeGuardService,
|
||||
UnauthGuardService,
|
||||
RouterService,
|
||||
{ provide: AuditServiceAbstraction, useValue: auditService },
|
||||
|
||||
Reference in New Issue
Block a user