Skip to content

Commit ab9ff14

Browse files
authored
Merge pull request #588 from kuzzleio/7.5.0-proposal
# [7.5.0](https://github.com/kuzzleio/sdk-javascript/releases/tag/7.5.0) (2021-01-17) #### New features - [ [#577](#577) ] Add [auth|security]:checkRights ([Yoann-Abbes](https://github.com/Yoann-Abbes)) - [ [#576](#576) ] Add document:upsert ([Yoann-Abbes](https://github.com/Yoann-Abbes)) ---
2 parents 7bf900e + fcdb2cb commit ab9ff14

File tree

16 files changed

+1043
-620
lines changed

16 files changed

+1043
-620
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="7.5.0"/>
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
code: true
3+
type: page
4+
title: upsert
5+
description: Applies partial changes to a document. If the document doesn't already exist, a new document is created.
6+
---
7+
8+
# upsert
9+
10+
<SinceBadge version="Kuzzle 2.8.0"/>
11+
<SinceBadge version="7.5.0" />
12+
13+
Applies partial changes to a document. If the document doesn't already exist, a new document is created.
14+
15+
16+
```js
17+
upsert(index, collection, id, changes, [options]);
18+
```
19+
20+
| Argument | Type | Description |
21+
| ------------ | ----------------- | ----------------------------------------- |
22+
| `index` | <pre>string</pre> | Index name |
23+
| `collection` | <pre>string</pre> | Collection name |
24+
| `id` | <pre>string</pre> | Document ID |
25+
| `changes` | <pre>object</pre> | Partial content of the document to update |
26+
27+
### Options
28+
29+
Additional query options
30+
31+
| Options | Type<br/>(default) | Description |
32+
| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- |
33+
| `defaults` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
34+
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
35+
| `retryOnConflict` | <pre>int</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
36+
| `source` | <pre>boolean</pre><br/>(`false`)| If true, returns the updated document inside the response
37+
38+
39+
## Resolves
40+
41+
Resolves to an object containing the document update result.
42+
43+
## Usage
44+
45+
<<< ./snippets/upsert.js
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
try {
2+
await kuzzle.document.create(
3+
'nyc-open-data',
4+
'yellow-taxi',
5+
{ capacity: 4 },
6+
'some-id'
7+
);
8+
9+
const response = await kuzzle.document.upsert(
10+
'nyc-open-data',
11+
'yellow-taxi',
12+
'some-id',
13+
{ changes: { category: 'suv' } }
14+
);
15+
16+
console.log(response);
17+
/*
18+
{
19+
id: 'some-id',
20+
_version: 2
21+
}
22+
*/
23+
} catch (error) {
24+
console.error(error.message);
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: document#upsert
2+
description: Applies a partial update to an existing document.
3+
hooks:
4+
before: |
5+
curl -XDELETE kuzzle:7512/nyc-open-data
6+
curl -XPOST kuzzle:7512/nyc-open-data/_create
7+
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
8+
after:
9+
template: default
10+
expected: "_id: 'some-id'"
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="7.5.0"/>
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

0 commit comments

Comments
 (0)