Skip to content

Commit 6c09d98

Browse files
committed
updated to v0.6
1 parent ef45f93 commit 6c09d98

6 files changed

Lines changed: 91 additions & 13 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: node_js
22
node_js:
33
- "12"
4+
- "13"
45
cache:
56
directories:
67
- "node_modules"
@@ -15,7 +16,7 @@ before_install:
1516
- ./cc-test-reporter before-build
1617
install:
1718
- npm install
18-
- npm install @rxstack/core@^0.5 @rxstack/async-event-dispatcher@^0.5 @rxstack/platform@^0.5 @rxstack/exceptions@^0.5 @rxstack/query-filter@^0.5 @rxstack/security@^0.5 @rxstack/utils@^0.5 @rxstack/service-registry@^0.5 winston@^3.2.1
19+
- npm install @rxstack/core@^0.6 @rxstack/async-event-dispatcher@^0.5 @rxstack/platform@^0.6 @rxstack/exceptions@^0.5 @rxstack/query-filter@^0.5 @rxstack/security@^0.6 @rxstack/utils@^0.5 @rxstack/service-registry@^0.5 winston@^3.2.1
1920
script:
2021
- npm test
2122
after_script:

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [associateWithCurrentUser](#callbacks-associate-with-current-user)
1616
- [queryWithCurrentUser](#callbacks-query-with-current-user)
1717
- [restrictToAuthenticatedUser](#callbacks-restrict-to-authenticated-user)
18+
- [restrictToAnonymousUser](#callbacks-restrict-to-anonymous-user)
1819
- [restrictToOwner](#callbacks-restrict-to-owner)
1920
- [restrictToRole](#callbacks-restrict-to-role)
2021
- [objectExists](#callbacks-object-exists)
@@ -33,7 +34,7 @@
3334
npm install @rxstack/platform-callbacks --save
3435
3536
// peer depencencies
36-
npm install @rxstack/core@^0.5 @rxstack/async-event-dispatcher@^0.5 @rxstack/platform@^0.5 @rxstack/exceptions@^0.5 @rxstack/query-filter@^0.5 @rxstack/security@^0.5 @rxstack/utils@^0.5 @rxstack/service-registry@^0.5 winston@^3.2.1
37+
npm install @rxstack/core@^0.6 @rxstack/async-event-dispatcher@^0.5 @rxstack/platform@^0.6 @rxstack/exceptions@^0.5 @rxstack/query-filter@^0.5 @rxstack/security@^0.6 @rxstack/utils@^0.5 @rxstack/service-registry@^0.5 winston@^3.2.1
3738
```
3839

3940
## <a name="callbacks"></a> Callbacks
@@ -193,6 +194,29 @@ import {restrictToAuthenticatedUser} from '@rxstack/platform-callbacks';
193194
})
194195
```
195196

197+
### <a name="callbacks-restrict-to-anonymous-user"></a> restrictToAnonymousUser
198+
199+
Restricts resource to anonymous user `request.token`.
200+
201+
Available on:
202+
203+
- `preExecute`
204+
205+
Example:
206+
207+
```typescript
208+
// ...
209+
import {restrictToAnonymousUser} from '@rxstack/platform-callbacks';
210+
211+
@Operation<ResourceOperationMetadata<Task>>({
212+
// ...
213+
onPreExecute: [
214+
// ...
215+
restrictToAnonymousUser()
216+
]
217+
})
218+
```
219+
196220
### <a name="callbacks-restrict-to-owner"></a> restrictToOwner
197221

198222
Restricts resource to current user `request.token.getUser()`.
@@ -594,9 +618,9 @@ import {validateUnique} from '@rxstack/platform-callbacks';
594618
validateUnique({
595619
service: TaskService,
596620
properties: ['name'],
597-
propertyPath: 'name',
621+
errorPath: 'name',
598622
method: 'findMany',
599-
mesage: 'Property name should be unique'
623+
message: 'Property name should be unique'
600624
})
601625
]
602626
})

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rxstack/platform-callbacks",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "RxStack platform specific middleware",
55
"private": false,
66
"author": "Nikolay Georgiev <symfonist@gmail.com>",
@@ -28,32 +28,32 @@
2828
"test": "npm run lint && npm run coverage"
2929
},
3030
"peerDependencies": {
31-
"@rxstack/core": "^0.5",
32-
"@rxstack/platform": "^0.5",
33-
"@rxstack/security": "^0.5",
31+
"@rxstack/core": "^0.6",
32+
"@rxstack/platform": "^0.6",
33+
"@rxstack/security": "^0.6",
3434
"@rxstack/utils": "^0.5"
3535
},
3636
"dependencies": {
3737
"class-transformer": "^0.1.10",
3838
"class-validator": "^0.9.1",
39-
"injection-js": "^2.2.2",
39+
"injection-js": "^2.3.0",
4040
"lodash": "^4.17.11",
4141
"reflect-metadata": "^0.1.13"
4242
},
4343
"devDependencies": {
44-
"@types/chai": "^4.2.6",
44+
"@types/chai": "^4.2.7",
4545
"@types/lodash": "^4.14.149",
4646
"@types/mocha": "^2.2.48",
47-
"@types/node": "^12.12.14",
47+
"@types/node": "^12.12.25",
4848
"chai": "3.5.0",
4949
"mocha": "^5.2.0",
5050
"nyc": "^14.1.1",
5151
"should": "^13.2.3",
5252
"sinon": "^6.3.4",
5353
"source-map-support": "^0.4.15",
54-
"ts-node": "^8.5.4",
54+
"ts-node": "^8.6.2",
5555
"tslint": "^5.20.1",
56-
"typescript": "^3.7.3"
56+
"typescript": "^3.7.5"
5757
},
5858
"bugs": {
5959
"url": "https://github.com/rxstack/platform-callbacks/issues"

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './soft-delete';
1212
export * from './restrict-to-role';
1313
export * from './query-with-current-user';
1414
export * from './restrict-to-owner';
15+
export * from './restrict-to-anonymous-user';
1516
export * from './associate-with-current-user';
1617
export * from './restrict-to-authenticated-user';
1718
export * from './query-filter';

src/restrict-to-anonymous-user.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {OperationCallback, OperationEvent, OperationEventsEnum} from '@rxstack/platform';
2+
import {UnauthorizedException} from '@rxstack/exceptions';
3+
import {AnonymousToken} from '@rxstack/security';
4+
import {restrictToOperations} from './utils';
5+
6+
export const restrictToAnonymousUser = (): OperationCallback => {
7+
return async (event: OperationEvent): Promise<void> => {
8+
restrictToOperations(event.eventType, [OperationEventsEnum.PRE_EXECUTE]);
9+
const token = event.request.token;
10+
if (false === (!token || token instanceof AnonymousToken)) {
11+
throw new UnauthorizedException();
12+
}
13+
};
14+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'reflect-metadata';
2+
import {Injector} from 'injection-js';
3+
import {Request} from '@rxstack/core';
4+
import {OperationEvent, OperationEventsEnum} from '@rxstack/platform';
5+
import {app_get_metadata} from './mocks/shared/app.metadata';
6+
import {restrictToAnonymousUser} from '../src';
7+
import {AnonymousToken} from '@rxstack/security';
8+
import {Token} from './mocks/shared/token';
9+
import {UnauthorizedException} from '@rxstack/exceptions';
10+
11+
const sinon = require('sinon');
12+
const injector = sinon.createStubInstance(Injector);
13+
14+
describe('PlatformCallbacks:restrict-to-anonymous-user', () => {
15+
16+
it('should pass anonymous user', async () => {
17+
const request = new Request('HTTP');
18+
request.token = new AnonymousToken();
19+
const apiEvent = new OperationEvent(request, injector, app_get_metadata);
20+
apiEvent.eventType = OperationEventsEnum.PRE_EXECUTE;
21+
await restrictToAnonymousUser()(apiEvent); // do nothing
22+
});
23+
24+
it('should throw an exception', async () => {
25+
const request = new Request('HTTP');
26+
request.token = new Token();
27+
const apiEvent = new OperationEvent(request, injector, app_get_metadata);
28+
apiEvent.eventType = OperationEventsEnum.PRE_EXECUTE;
29+
let exception: UnauthorizedException;
30+
try {
31+
await restrictToAnonymousUser()(apiEvent);
32+
} catch (e) {
33+
exception = e;
34+
}
35+
exception.should.be.instanceOf(UnauthorizedException);
36+
});
37+
38+
});

0 commit comments

Comments
 (0)