Skip to content

Commit 23e0675

Browse files
authored
Add [auth|security]:checkRights (#577)
Add [auth|security]:checkRights
1 parent 89cb19f commit 23e0675

File tree

8 files changed

+166
-0
lines changed

8 files changed

+166
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
code: true
3+
type: page
4+
title: checkRights
5+
description: Checks if an API action can be executed by the current user
6+
---
7+
8+
# checkRights
9+
10+
<SinceBadge version="Kuzzle 2.8.0"/>
11+
<SinceBadge version="auto-version"/>
12+
13+
Checks if the provided API request can be executed by the current logged user.
14+
15+
---
16+
17+
```js
18+
checkRights(requestPayload)
19+
```
20+
21+
| Property | Type | Description |
22+
|--- |--- |--- |
23+
| `requestPayload` | <pre>object</pre> | Contains a [RequestPayload](/core/2/api/payloads/request) |
24+
25+
## `requestPayload`
26+
27+
The [RequestPayload](/core/2/api/payloads/request) must contains at least the following properties:
28+
29+
- `controller`: API controller
30+
- `action`: API action
31+
32+
---
33+
34+
## Resolves
35+
36+
A boolean telling whether the provided request would have been allowed or not.
37+
38+
## Usage
39+
40+
<<< ./snippets/check-rights.js
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const requestPayload = {
2+
controller: 'document',
3+
action: 'create',
4+
index: 'nyc-open-data',
5+
collection: 'yellow-taxi',
6+
body: {
7+
name: 'Melis'
8+
}
9+
}
10+
11+
try {
12+
const result = await kuzzle.auth.checkRights(requestPayload);
13+
console.log(result);
14+
/*
15+
true
16+
*/
17+
} catch (error) {
18+
console.error(error.message);
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: auth#checkRights
2+
description: Checks if an API action can be executed by the current user
3+
hooks:
4+
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
5+
after: curl -X DELETE kuzzle:7512/users/foo
6+
template: default
7+
expected: true
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
code: true
3+
type: page
4+
title: checkRights
5+
description: Checks if an API action can be executed by a user
6+
---
7+
8+
# checkRights
9+
10+
<SinceBadge version="2.8.0"/>
11+
<SinceBadge version="auto-version"/>
12+
Checks if the provided API request can be executed by a user.
13+
14+
---
15+
16+
```js
17+
checkRights(kuid, requestPayload)
18+
```
19+
20+
| Property | Type | Description |
21+
|--- |--- |--- |
22+
| `kuid` | <pre>string</pre> | User [kuid](/core/2/guides/main-concepts/authentication#kuzzle-user-identifier-kuid) |
23+
| `requestPayload` | <pre>object</pre> | Contains a [RequestPayload](/core/2/api/payloads/request) |
24+
25+
## `requestPayload`
26+
27+
The [RequestPayload](/core/2/api/payloads/request) must contains at least the following properties:
28+
29+
- `controller`: API controller
30+
- `action`: API action
31+
32+
---
33+
34+
## Resolves
35+
36+
A boolean telling whether the provided request would have been allowed or not
37+
38+
## Usage
39+
40+
<<< ./snippets/check-rights.js
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const requestPayload = {
2+
controller: 'document',
3+
action: 'create',
4+
index: 'nyc-open-data',
5+
collection: 'yellow-taxi',
6+
body: {
7+
name: 'Melis'
8+
}
9+
}
10+
11+
try {
12+
const allowed = await kuzzle.security.checkRights('foo', requestPayload);
13+
console.log(allowed);
14+
/*
15+
true
16+
*/
17+
} catch (error) {
18+
console.error(error.message);
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: security#checkRights
2+
description: Checks if an API action can be executed by a user
3+
hooks:
4+
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
5+
after: curl -X DELETE kuzzle:7512/users/foo
6+
template: default
7+
expected: true

src/controllers/Auth.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ export class AuthController extends BaseController {
9191
.then(response => response.result);
9292
}
9393

94+
/**
95+
* Checks if an API action can be executed by the current user
96+
*
97+
* @see https://docs.kuzzle.io/sdk/js/7/controllers/auth/check-rights
98+
* @param requestPayload Request to check
99+
*/
100+
checkRights (
101+
requestPayload: JSONObject
102+
): Promise<boolean> {
103+
104+
const request = {
105+
body: requestPayload,
106+
action: 'checkRights'
107+
};
108+
return this.query(request)
109+
.then(response => response.result.allowed);
110+
}
111+
94112
/**
95113
* Deletes an API key for the currently loggued user.
96114
*

src/controllers/Security.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ class SecurityController extends BaseController {
3939
.then(response => response.result);
4040
}
4141

42+
/**
43+
* Checks if an API action can be executed by the current user
44+
*
45+
* @param {String} userId - User kuid
46+
* @param {Object} requestPayload - Request to check
47+
*/
48+
checkRights(kuid, requestPayload) {
49+
const request = {
50+
userId: kuid,
51+
body: requestPayload,
52+
action: 'checkRights'
53+
};
54+
return this.query(request)
55+
.then(response => response.result.allowed);
56+
}
57+
4258
/**
4359
* Deletes an user API key.
4460
*

0 commit comments

Comments
 (0)