IsAdmin is temporary is admin or is manager

This commit is contained in:
Timo 2020-12-02 13:29:55 +02:00
parent e0152b93e6
commit 3ab53b59fa
6 changed files with 25 additions and 67 deletions

View File

@ -1,53 +0,0 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { UserService } from '../user/user.service';
import { AppLoadStateService } from '../utils/app-load-state.service';
import { Observable } from 'rxjs';
import { AppStateService } from '../state/app-state.service';
import { NotificationService, NotificationType } from '../notification/notification.service';
import { TranslateService } from '@ngx-translate/core';
@Injectable({
providedIn: 'root'
})
export class ProjectMemberGuard implements CanActivate {
constructor(
private readonly _router: Router,
private readonly _appLoadStateService: AppLoadStateService,
private readonly _appStateService: AppStateService,
private readonly _userService: UserService,
private readonly _notificationService: NotificationService,
private readonly _translateService: TranslateService
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return new Observable((obs) => {
if (this._userService.isManager(this._userService.user)) {
obs.next(true);
obs.complete();
return;
}
const projectId = route.params.projectId;
this._appStateService.loadAllProjects().then(() => {
const isProjectMember = this._appStateService.allProjects
.find((pw) => pw.project.projectId === projectId)
?.project.memberIds.includes(this._userService.user.id);
if (!isProjectMember) {
this._router.navigate(['ui', 'projects']);
this._appLoadStateService.pushLoadingEvent(false);
this._notificationService.showToastNotification(
this._translateService.instant('project-member-guard.access-denied'),
null,
NotificationType.ERROR
);
obs.next(false);
obs.complete();
} else {
obs.next(true);
obs.complete();
}
});
});
}
}

View File

@ -11,12 +11,7 @@ class UrlTree {}
providedIn: 'root'
})
export class RedRoleGuard implements CanActivate {
constructor(
protected readonly _router: Router,
protected readonly _keycloak: KeycloakService,
private readonly _appLoadStateService: AppLoadStateService,
private readonly _userService: UserService
) {}
constructor(protected readonly _router: Router, private readonly _appLoadStateService: AppLoadStateService, private readonly _userService: UserService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return new Observable((obs) => {
@ -26,6 +21,11 @@ export class RedRoleGuard implements CanActivate {
obs.next(false);
obs.complete();
} else {
if (!this._userService.isUser() && state.url.startsWith('/ui/projects')) {
this._router.navigate(['/ui/admin-dictionaries']);
obs.next(false);
obs.complete();
}
obs.next(true);
obs.complete();
}

View File

@ -208,6 +208,14 @@ export class PermissionsService {
if (!user) {
user = this._userService.user;
}
return user.isAdmin;
// TODO temporary
return user.isAdmin || user.isManager;
}
isUser(user?: UserWrapper) {
if (!user) {
user = this._userService.user;
}
return user.isUser;
}
}

View File

@ -62,6 +62,7 @@
[mode]="'text'"
[theme]="'tomorrow'"
[options]="aceOptions"
[readOnly]="!permissionsService.isAdmin()"
(textChanged)="textChanged($event)"
[autoUpdateContent]="true"
[text]="dictionaryEntriesAsText"

View File

@ -1,6 +1,6 @@
<div class="red-top-bar">
<div class="top-bar-row">
<div class="menu visible-lt-lg">
<div class="menu visible-lt-lg" *ngIf="permissionsService.isUser()">
<button [matMenuTriggerFor]="menuNav" mat-flat-button>
<mat-icon svgIcon="red:menu"></mat-icon>
</button>
@ -18,7 +18,7 @@
</button>
</mat-menu>
</div>
<div class="menu flex-2 visible-lg breadcrumbs-container">
<div class="menu flex-2 visible-lg breadcrumbs-container" *ngIf="permissionsService.isUser()">
<a class="breadcrumb" routerLink="/ui/projects" translate="top-bar.navigation-items.projects"></a>
<mat-icon *ngIf="appStateService.activeProject" svgIcon="red:arrow-right"></mat-icon>
<a *ngIf="appStateService.activeProject" class="breadcrumb" [routerLink]="'/ui/projects/' + appStateService.activeProjectId">

View File

@ -7,14 +7,16 @@ import { UserService } from '../user/user.service';
providedIn: 'root'
})
export class AppStateGuard implements CanActivate {
constructor(
private readonly _appStateService: AppStateService,
private readonly _userService: UserService
) {}
constructor(private readonly _appStateService: AppStateService, private readonly _userService: UserService) {}
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
await this._userService.loadAllUsersIfNecessary();
await this._appStateService.loadAllProjectsIfNecessary();
if (this._userService.isUser()) {
console.log('jere');
await this._appStateService.loadAllProjectsIfNecessary();
}
await this._appStateService.loadDictionaryDataIfNecessary();
await this._appStateService.updateDictionaryVersion();