-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/230 x role restriction #278
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c593cd1
Add http interceptor for x-role-restriction header
caebr 4998cbb
Fix tests
caebr ff23040
Update role delimeter in settings
caebr 6bd60ca
Add role for given module
caebr cbf1da4
Refactor url parsing
caebr 98ad4cd
Fix tests
caebr 5be9796
Add updated package-lock after rebasing
caebr 9d28f83
Add tests for url helper function
caebr a5e5027
Add tests for role interceptor
caebr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { HttpTestingController } from '@angular/common/http/testing'; | ||
import { Router } from '@angular/router'; | ||
|
||
import { buildTestModuleMetadata } from 'src/spec-helpers'; | ||
|
||
describe('RestRoleInterceptor', () => { | ||
let http: HttpClient; | ||
let httpTestingController: HttpTestingController; | ||
let successCallback: jasmine.Spy; | ||
let errorCallback: jasmine.Spy; | ||
|
||
const mockRouter = { url: '' }; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule( | ||
buildTestModuleMetadata({ | ||
providers: [{ provide: Router, useValue: mockRouter }], | ||
}) | ||
); | ||
|
||
http = TestBed.inject(HttpClient); | ||
httpTestingController = TestBed.inject(HttpTestingController); | ||
|
||
successCallback = jasmine.createSpy('success'); | ||
errorCallback = jasmine.createSpy('error'); | ||
}); | ||
|
||
describe('.intercept', () => { | ||
it('should not add header on root module', () => { | ||
mockRouter.url = '/'; | ||
http.get('/').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/' && req.headers.get('X-Role-Restriction') === null | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add header on presence control module', () => { | ||
mockRouter.url = '/presence-control'; | ||
http.get('/presence-control').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/presence-control' && | ||
req.headers.get('X-Role-Restriction') === 'LessonTeacherRole' | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add header on my absences module', () => { | ||
mockRouter.url = '/my-absences'; | ||
http.get('/my-absences').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/my-absences' && | ||
req.headers.get('X-Role-Restriction') === 'StudentRole' | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add header on open absences module', () => { | ||
mockRouter.url = '/open-absences'; | ||
http.get('/open-absences').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/open-absences' && | ||
req.headers.get('X-Role-Restriction') === | ||
'LessonTeacherRole;ClassTeacherRole' | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add header on edit absences module', () => { | ||
mockRouter.url = '/edit-absences'; | ||
http.get('/edit-absences').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/edit-absences' && | ||
req.headers.get('X-Role-Restriction') === | ||
'LessonTeacherRole;ClassTeacherRole' | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not add header on evaluate absences module', () => { | ||
mockRouter.url = '/evaluate-absences'; | ||
http.get('/evaluate-absences').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/evaluate-absences' && | ||
req.headers.get('X-Role-Restriction') === null | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not add header on my profile module', () => { | ||
mockRouter.url = '/my-profile'; | ||
http.get('/my-profile').subscribe(successCallback, errorCallback); | ||
httpTestingController | ||
.expectOne( | ||
(req) => | ||
req.url === '/my-profile' && | ||
req.headers.get('X-Role-Restriction') === null | ||
) | ||
.flush('hello', { status: 200, statusText: 'Success' }); | ||
|
||
expect(successCallback).toHaveBeenCalledWith('hello'); | ||
expect(errorCallback).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { | ||
HttpEvent, | ||
HttpHandler, | ||
HttpInterceptor, | ||
HttpRequest, | ||
} from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { SETTINGS, Settings } from './settings'; | ||
import { Router } from '@angular/router'; | ||
import { kebabToCamelCase } from 'codelyzer/util/utils'; | ||
import { getFirstSegment } from './shared/utils/url'; | ||
|
||
@Injectable() | ||
export class RestRoleInterceptor implements HttpInterceptor { | ||
constructor( | ||
private router: Router, | ||
@Inject(SETTINGS) private settings: Settings | ||
) {} | ||
|
||
/** | ||
* Adds the X-Role-Restriction custom HTTP header for the given module to API requests. | ||
*/ | ||
intercept( | ||
req: HttpRequest<any>, | ||
next: HttpHandler | ||
): Observable<HttpEvent<any>> { | ||
if ( | ||
!req.headers.has('X-Role-Restriction') && | ||
this.settings.headerRoleRestriction | ||
) { | ||
const module = this.getCurrentModuleName(); | ||
if (module && this.settings.headerRoleRestriction[module]) { | ||
const headers = req.headers.set( | ||
'X-Role-Restriction', | ||
this.settings.headerRoleRestriction[module] | ||
); | ||
return next.handle(req.clone({ headers })); | ||
} | ||
} | ||
|
||
return next.handle(req); | ||
} | ||
|
||
private getCurrentModuleName(): string | null { | ||
const urlSegment = this.router.url | ||
? getFirstSegment(this.router.url) | ||
: null; | ||
return urlSegment ? kebabToCamelCase(urlSegment) : null; | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Das ist nur der Angular Teil oder? Dann finde ich dies eine gute Variante 👍
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.
Ja, genau. Alles nach dem #
Die ActivatedRoute enhält scheinbar nur in Komponenten die Infos zur Route.