-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add contributor guard to data entry pages
- Loading branch information
1 parent
a1e6429
commit 3f228a6
Showing
9 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ const fakeUserResponse: UserResponse = { | |
email: "[email protected]", | ||
first_name: "Frodo", | ||
last_name: "Baggins", | ||
is_staff: false | ||
is_staff: false, | ||
is_contributor: true, | ||
} | ||
|
||
const fakeAdminResponse: UserResponse = { | ||
|
@@ -21,7 +22,8 @@ const fakeAdminResponse: UserResponse = { | |
email: "[email protected]", | ||
first_name: "Gandalf", | ||
last_name: "The Grey", | ||
is_staff: true | ||
is_staff: true, | ||
is_contributor: false, | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,52 @@ | ||
import { inject } from "@angular/core"; | ||
import { ActivatedRouteSnapshot, CanActivateFn, Router } from "@angular/router"; | ||
import { AuthService } from "@services/auth.service"; | ||
import { ToastService } from "@services/toast.service"; | ||
import { filter, map } from "rxjs"; | ||
|
||
export const LoggedOnGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => { | ||
const authService = inject(AuthService); | ||
const toastService = inject(ToastService); | ||
const router = inject(Router); | ||
|
||
return authService.currentUser$.pipe( | ||
filter((user) => user !== undefined), | ||
map((user) => { | ||
toastService.show({ | ||
type: 'danger', | ||
header: 'Not signed in', | ||
body: 'You must be signed in to view this page.' | ||
}); | ||
if (user === null) { | ||
router.navigate(["/login"], { queryParams: { next: route.url } }); | ||
return false; | ||
return router.createUrlTree(['/login'], { | ||
queryParams: { next: route.url } | ||
}); | ||
} | ||
return true; | ||
}) | ||
); | ||
}; | ||
|
||
export const ContributorGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => { | ||
const authService = inject(AuthService); | ||
const toastService = inject(ToastService); | ||
const router = inject(Router); | ||
|
||
return authService.currentUser$.pipe( | ||
filter((user) => user !== undefined), | ||
map((user) => { | ||
if (user?.isContributor) { | ||
return true; | ||
} else { | ||
let body = 'You do not have permission to view this page.'; | ||
if (!user) { body += ' Do you need to sign in?'; } | ||
toastService.show({ | ||
type: 'danger', | ||
header: 'Not authorised', | ||
body, | ||
}); | ||
return router.createUrlTree(['/']); | ||
} | ||
}), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ describe("User utils", () => { | |
first_name: "Test", | ||
last_name: "User", | ||
is_staff: true, | ||
is_contributor: true, | ||
}; | ||
const user = parseUserData(result); | ||
expect(user).toBeInstanceOf(User); | ||
|
@@ -48,6 +49,7 @@ describe("User utils", () => { | |
expect(user?.firstName).toBe("Test"); | ||
expect(user?.lastName).toBe("User"); | ||
expect(user?.isStaff).toBe(true); | ||
expect(user?.isContributor).toBe(true); | ||
}); | ||
}); | ||
|
||
|
@@ -59,7 +61,6 @@ describe("User utils", () => { | |
email: "[email protected]", | ||
firstName: "Test", | ||
lastName: "User", | ||
isStaff: true, | ||
}; | ||
const encoded = encodeUserData(data); | ||
expect(encoded).toEqual({ | ||
|
@@ -68,9 +69,20 @@ describe("User utils", () => { | |
email: "[email protected]", | ||
first_name: "Test", | ||
last_name: "User", | ||
is_staff: true, | ||
}); | ||
}); | ||
|
||
it('should omit is_staff and is_contributor', () => { | ||
const data: Partial<User> = { | ||
id: 1, | ||
isStaff: true, | ||
isContributor: true, | ||
}; | ||
const encoded = encodeUserData(data); | ||
expect(encoded).toEqual({ | ||
id: 1, | ||
}); | ||
}) | ||
}); | ||
|
||
describe("setErrors", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters