Skip to content

Commit

Permalink
add isSecurityContextUsed method
Browse files Browse the repository at this point in the history
  • Loading branch information
mseemann committed May 10, 2016
1 parent 7e21257 commit 9222e5a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/core/descriptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ServiceParser } from './decorator-parser';
import {ServiceDescription, ISecurityContext} from './descriptions';
import {PermitAll, Path, GET, RolesAllowed, SecurityContext} from "./decorators";


@PermitAll()
@Path('/')
class TestServiceA {

@GET()
x(){}
}

@Path('/')
class TestServiceB {
@GET()
x(){}
}

class TestServiceC {

@GET()
@RolesAllowed(['user'])
x(){}
}

class TestServiceD {

@GET()
x(@SecurityContext() conext:ISecurityContext){}
}

describe('ServiceDescription', () => {


it('should need a security context - permitAll at class level', () => {
let serviceDescription = ServiceParser.parse(new TestServiceA());
expect(serviceDescription.isSecurityContextUsed()).toBe(true);
})

it('should not need a security context', () => {
let serviceDescription = ServiceParser.parse(new TestServiceB());
expect(serviceDescription.isSecurityContextUsed()).toBe(false);
})

it('should need a security context - RolesAllowed at method level', () => {
let serviceDescription = ServiceParser.parse(new TestServiceC());
expect(serviceDescription.isSecurityContextUsed()).toBe(true);
})

it('should need a security context - SecurityContextParam', () => {
let serviceDescription = ServiceParser.parse(new TestServiceD());
expect(serviceDescription.isSecurityContextUsed()).toBe(true);
})
})
20 changes: 20 additions & 0 deletions src/core/descriptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {permitAll, rolesAllowed} from "./namings";
/**
* A heigher level description of a service. No need to parse
* the Decortaors by yourself. Just use the @see ServiceParser.
Expand All @@ -19,6 +20,17 @@ export class ServiceDescription {
getMethodDescriptorForMethodName(name:string): MethodDescription {
return this.methodMap[name];
}

isSecurityContextUsed():boolean{
if (this.permitAll || this.rolesAllowed.length>0){
return true;
}

return this.methods.some( (method) => {
return method.isSecurityContextUsed();
})

}
}

/**
Expand Down Expand Up @@ -66,6 +78,14 @@ export class MethodDescription {
this.methodName = name;
this.httpMethod = httpMethod;
}

isSecurityContextUsed():boolean {
if(this.permitAll || this.rolesAllowed.length>0){
return true;
}

return this.securityContextParam ? true : false;
}
}

export interface IUser {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"src/core/decorators.ts",
"src/core/decorators.spec.ts",
"src/core/decoratorUtil.ts",
"src/core/descriptions.spec.ts",
"src/core/descriptions.ts",
"src/core/decorator-parser.ts",
"src/core/decorator-parser.spec.ts",
Expand Down

0 comments on commit 9222e5a

Please sign in to comment.