Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev to master #15

Merged
merged 2 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Source/Nebula.API/Controllers/WatershedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public WatershedController(NebulaDbContext dbContext, ILogger<WatershedControlle
}

[HttpGet("/watersheds")]
[AdminFeature]
[DataExplorerFeature]
public ActionResult<List<WatershedDto>> ListAllWatersheds()
{
var watershedDtos = Watershed.List(_dbContext).OrderBy(x => x.WatershedName).ToList();
return watershedDtos;
}

[HttpGet("/watersheds/{watershedID}")]
[AdminFeature]
[DataExplorerFeature]
public ActionResult<WatershedDto> GetWatershedByID([FromRoute] int watershedID)
{
var watershedDto = Watershed.GetByWatershedID(_dbContext, watershedID);
return RequireNotNullThrowNotFound(watershedDto, "Watershed", watershedID);
}

[HttpPost("watersheds/getBoundingBox")]
[AdminFeature]
[DataExplorerFeature]
public ActionResult<BoundingBoxDto> GetBoundingBoxByWatershedIDs([FromBody] WatershedIDListDto watershedIDListDto)
{
if (!ModelState.IsValid)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Nebula.EFModels.Entities;

namespace Nebula.API.Services.Authorization
{
public class DataExplorerFeature : BaseAuthorizationAttribute
{
public DataExplorerFeature() : base(new[] { RoleEnum.Admin, RoleEnum.DataExplorer }) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Nebula.API.Services.Authorization
{
public class UserViewFeature : BaseAuthorizationAttribute
{
public UserViewFeature() : base(new []{RoleEnum.Admin, RoleEnum.LandOwner, RoleEnum.Unassigned})
public UserViewFeature() : base(new []{RoleEnum.Admin, RoleEnum.DataExplorer, RoleEnum.Unassigned})
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Nebula.EFModels/Entities/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static RoleDto GetByRoleID(NebulaDbContext dbContext, int roleID)
public enum RoleEnum
{
Admin = 1,
LandOwner = 2,
DataExplorer = 2,
Unassigned = 3,
Disabled = 4
}
Expand Down
11 changes: 6 additions & 5 deletions Source/Nebula.Web/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ import { FieldDefinitionEditComponent } from './pages/field-definition-edit/fiel
import { TimeSeriesAnalysisComponent } from './pages/time-series-analysis/time-series-analysis.component';
import { PairedRegressionAnalysisComponent } from './pages/paired-regression-analysis/paired-regression-analysis.component';
import { DiversionScenarioComponent } from './pages/diversion-scenario/diversion-scenario.component';
import { DataExplorerGuard } from './shared/guards/unauthenticated-access/data-explorer-guard';

const routes: Routes = [
{ path: "labels-and-definitions/:id", component: FieldDefinitionEditComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "labels-and-definitions", component: FieldDefinitionListComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "watersheds", component: WatershedListComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "watersheds/:id", component: WatershedDetailComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "watersheds", component: WatershedListComponent, canActivate: [UnauthenticatedAccessGuard, DataExplorerGuard, AcknowledgedDisclaimerGuard] },
{ path: "watersheds/:id", component: WatershedDetailComponent, canActivate: [UnauthenticatedAccessGuard, DataExplorerGuard, AcknowledgedDisclaimerGuard] },
{ path: "users", component: UserListComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard]},
{ path: "users/:id", component: UserDetailComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "users/:id/edit", component: UserEditComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "invite-user/:userID", component: UserInviteComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "invite-user", component: UserInviteComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard] },
{ path: "time-series-analysis", component: TimeSeriesAnalysisComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard]},
{ path: "paired-regression-analysis", component: PairedRegressionAnalysisComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard]},
{ path: "diversion-scenario", component: DiversionScenarioComponent, canActivate: [UnauthenticatedAccessGuard, ManagerOnlyGuard, AcknowledgedDisclaimerGuard]},
{ path: "time-series-analysis", component: TimeSeriesAnalysisComponent, canActivate: [UnauthenticatedAccessGuard, DataExplorerGuard, AcknowledgedDisclaimerGuard]},
{ path: "paired-regression-analysis", component: PairedRegressionAnalysisComponent, canActivate: [UnauthenticatedAccessGuard, DataExplorerGuard, AcknowledgedDisclaimerGuard]},
{ path: "diversion-scenario", component: DiversionScenarioComponent, canActivate: [UnauthenticatedAccessGuard, DataExplorerGuard, AcknowledgedDisclaimerGuard]},
{ path: "disclaimer", component: DisclaimerComponent },
{ path: "disclaimer/:forced", component: DisclaimerComponent },
{ path: "help", component: HelpComponent },
Expand Down
13 changes: 13 additions & 0 deletions Source/Nebula.Web/src/app/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ export class AuthenticationService {
public hasCurrentUserAcknowledgedDisclaimer(): boolean {
return this.currentUser != null && this.currentUser.DisclaimerAcknowledgedDate != null;
}

public isUserInRole(user: UserDetailedDto, roles: RoleEnum[]): boolean {
const role = user && user.Role ? user.Role.RoleID : null;
if (role == null) {
return false;
}

return roles.includes(role);
}

public isCurrentUserInRole(roles: RoleEnum[]): boolean {
return this.isUserInRole(this.currentUser, roles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item dropdown" routerLinkActive="active" *ngIf="isAdministrator()">
<li class="nav-item dropdown" routerLinkActive="active" *ngIf="canSeeViewMenu()">
<a href="#" class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AlertService } from '../../services/alert.service';
import { Alert } from '../../models/alert';
import { environment } from 'src/environments/environment';
import { AlertContext } from '../../models/enums/alert-context.enum';
import { RoleEnum } from '../../models/enums/role.enum';

@Component({
selector: 'header-nav',
Expand Down Expand Up @@ -57,6 +58,10 @@ export class HeaderNavComponent implements OnInit, OnDestroy {
return this.authenticationService.isAuthenticated();
}

public canSeeViewMenu(): boolean {
return this.authenticationService.isUserInRole(this.currentUser, [RoleEnum.Admin, RoleEnum.DataExplorer]);
}

public isAdministrator(): boolean {
return this.authenticationService.isUserAnAdministrator(this.currentUser);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { Injectable } from '@angular/core';
import { AlertService } from '../../services/alert.service';
import { AuthenticationService } from 'src/app/services/authentication.service';
import { RoleEnum } from '../../models/enums/role.enum';


@Injectable({
providedIn: 'root'
})
export class DataExplorerGuard implements CanActivate {
constructor(private router: Router, private alertService: AlertService, private authenticationService: AuthenticationService) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (!this.authenticationService.isCurrentUserNullOrUndefined()) {
if (this.authenticationService.isCurrentUserInRole([RoleEnum.Admin, RoleEnum.DataExplorer])) {
return true;
} else {
this.alertService.pushNotFoundUnauthorizedAlert();
this.router.navigate(["/"]);
return false;
}
}

return this.authenticationService.currentUserSetObservable
.pipe(
map(x => {
if (x.Role.RoleID == RoleEnum.Admin || x.Role.RoleID == RoleEnum.DataExplorer) {
return true;
} else {
this.alertService.pushNotFoundUnauthorizedAlert();
this.router.navigate(["/"]);
return false;
}
})
);
}
}