-
Notifications
You must be signed in to change notification settings - Fork 98
Prevent unauthorized users (non-registered) from creating surveys #2271
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
base: master
Are you sure you want to change the base?
Changes from all commits
52f4040
a68e454
c37fa5c
a2bb6ac
a71ab4e
4eec2bb
0072a9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
|
|
||
| import {Component, OnDestroy, OnInit} from '@angular/core'; | ||
| import {MatDialog} from '@angular/material/dialog'; | ||
| import {List, Map} from 'immutable'; | ||
| import {Subscription} from 'rxjs'; | ||
|
|
||
|
|
@@ -23,6 +24,12 @@ import { | |
| SurveyGeneralAccess, | ||
| SurveyState, | ||
| } from 'app/models/survey.model'; | ||
| import { | ||
| DialogData, | ||
| DialogType, | ||
| JobDialogComponent, | ||
| } from 'app/pages/edit-survey/job-dialog/job-dialog.component'; | ||
| import {AuthService} from 'app/services/auth/auth.service'; | ||
| import {NavigationService} from 'app/services/navigation/navigation.service'; | ||
| import {SurveyService} from 'app/services/survey/survey.service'; | ||
|
|
||
|
|
@@ -50,6 +57,8 @@ export class SurveyListComponent implements OnInit, OnDestroy { | |
| SurveyGeneralAccess = SurveyGeneralAccess; | ||
|
|
||
| constructor( | ||
| private authService: AuthService, | ||
| public dialog: MatDialog, | ||
| private navigationService: NavigationService, | ||
| private surveyService: SurveyService | ||
| ) {} | ||
|
|
@@ -110,8 +119,24 @@ export class SurveyListComponent implements OnInit, OnDestroy { | |
| } | ||
| } | ||
|
|
||
| createNewSurvey(): void { | ||
| this.navigationService.navigateToCreateSurvey(null); | ||
| async createNewSurvey(): Promise<void> { | ||
| const isPasslisted = await this.authService.isPasslisted(); | ||
|
|
||
| if (!isPasslisted) { | ||
| this.dialog | ||
| .open(JobDialogComponent, { | ||
| data: {dialogType: DialogType.SurveyCreationDenied}, | ||
| panelClass: 'small-width-dialog', | ||
| }) | ||
| .afterClosed() | ||
| .subscribe(async (result: DialogData) => { | ||
| if (!result) return; | ||
|
|
||
| this.navigationService.navigateToSubscriptionForm(); | ||
| }); | ||
| } else { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return early (short circuit) so that the base case doesn't need to be nested in the else statement |
||
| this.navigationService.navigateToCreateSurvey(null); | ||
| } | ||
| } | ||
|
|
||
| private filterSurveys(survey: Survey): boolean { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * Copyright 2025 The Ground Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the 'License'); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an 'AS IS' BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import {Injectable} from '@angular/core'; | ||
|
|
||
| import {AuthService} from 'app/services/auth/auth.service'; | ||
| import {NavigationService} from 'app/services/navigation/navigation.service'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root', | ||
| }) | ||
| export class PasslistGuard { | ||
| constructor( | ||
| private authService: AuthService, | ||
| private navigationService: NavigationService | ||
| ) {} | ||
|
|
||
| async canActivate(): Promise<void> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "activate" is more of an internal term that I think we'd want here... Also, this does more than just check whether the user can activate the survey, it actually redirects to the form. Perhaps something like "checkSurveyAcls" might be more appropriate? |
||
| const isPasslisted = await this.authService.isPasslisted(); | ||
|
|
||
| if (!isPasslisted) this.navigationService.navigateToSubscriptionForm(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import {BehaviorSubject, Observable, Subscription} from 'rxjs'; | |
| import {filter} from 'rxjs/operators'; | ||
|
|
||
| import {UrlParams} from './url-params'; | ||
| import {DataStoreService} from '../data-store/data-store.service'; | ||
|
|
||
| /** | ||
| * Exposes application state in the URL as streams to other services | ||
|
|
@@ -70,6 +71,7 @@ export class NavigationService implements OnDestroy { | |
|
|
||
| constructor( | ||
| @Inject(DOCUMENT) private document: Document, | ||
| private dataStore: DataStoreService, | ||
| private router: Router | ||
| ) { | ||
| this.subscription = this.router.events | ||
|
|
@@ -181,6 +183,18 @@ export class NavigationService implements OnDestroy { | |
| this.router.navigate([SURVEY_SEGMENT, SURVEY_ID_NEW]); | ||
| } | ||
|
|
||
| async getAccessDeniedLink(): Promise<string | undefined> { | ||
| const accessDeniedMessage = await this.dataStore.getAccessDeniedMessage(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do we document the contents of the DB config? Can we create a wiki page or readme for this? Ok to do after merging as well. |
||
|
|
||
| return accessDeniedMessage?.link; | ||
| } | ||
|
|
||
| async navigateToSubscriptionForm() { | ||
| const accessDeniedLink = await this.getAccessDeniedLink(); | ||
|
|
||
| if (accessDeniedLink) window.location.href = accessDeniedLink; | ||
| } | ||
|
|
||
| /** | ||
| * Navigate to the about page | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to uncomment these lines?