Skip to content
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

Add auth:checkRights #36

Merged
merged 13 commits into from
Jan 7, 2021
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ tasks.withType<Jar> {
)
)
}
from(configurations.compileClasspath.get().map { if (it.isDirectory()) it else zipTree(it) })
}
74 changes: 74 additions & 0 deletions doc/1/controllers/auth/check-rights/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
code: true
type: page
title: checkRights
description: Checks if the provided API request can be executed by this network connection, using the current authentication information.
scottinet marked this conversation as resolved.
Show resolved Hide resolved
---

# checkRights

<SinceBadge version="Kuzzle 2.8.0"/>
<SinceBadge version="auto-version"/>

Checks if the provided API request can be executed by this network connection, using the current authentication information.
scottinet marked this conversation as resolved.
Show resolved Hide resolved

:::: tabs
::: tab Java

```java
public CompletableFuture<boolean> checkRights(
ConcurrentHashMap<String, Object> requestPayload) throws NotConnectedException, InternalException
```

| Property | Type | Description |
|--- |--- |--- |
| `requestPayload` | <pre>ConcurrentHashMap<String, Object></pre> | Contains a [RequestPayload](/core/2/api/payloads/request) |

## `requestPayload`

The [RequestPayload](/core/2/api/payloads/request) must contains at least the following properties:
scottinet marked this conversation as resolved.
Show resolved Hide resolved

- `controller`: API controller
- `action`: API action

---

## Returns

Returns a boolean telling whether the provided request would have been allowed or not
scottinet marked this conversation as resolved.
Show resolved Hide resolved

## Usage

<<< ./snippets/check-rights-java.java

:::
::: tab Kotlin

```kotlin
fun checkRights(
requestPayload: ConcurrentHashMap<String, Any?>): CompletableFuture<Boolean>
```

| Property | Type | Description |
|--- |--- |--- |
| `requestPayload` | <pre>ConcurrentHashMap<String, Any?></pre> | Contains a [RequestPayload](/core/2/api/payloads/request) |

## `requestPayload`

The [RequestPayload](/core/2/api/payloads/request) must contains at least the following properties:
scottinet marked this conversation as resolved.
Show resolved Hide resolved

- `controller`: API controller
- `action`: API action

---

## Returns

Returns a boolean telling whether the provided request would have been allowed or not
scottinet marked this conversation as resolved.
Show resolved Hide resolved

## Usage

<<< ./snippets/check-rights-kotlin.kt

:::
::::
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ConcurrentHashMap<String, Object> body = new ConcurrentHashMap<>();
body.put("name", "Melis");

ConcurrentHashMap<String, Object> requestPayload = new ConcurrentHashMap<>();
requestPayload.put("controller", "document");
requestPayload.put("action", "create");
requestPayload.put("index", "nyc-open-data");
requestPayload.put("collection", "yellow-taxi");
requestPayload.put("body", body);

Boolean result =
kuzzle.getAuthController().checkRights(requestPayload).get();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: java-auth#checkRights
description: Checks if an API action can be executed by the current user
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
template: print-result
expected: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
val body = ConcurrentHashMap<String, Any?>().apply {
put("name", "Melis")
}

val requestPayload = ConcurrentHashMap<String, Any?>().apply {
put("controller", "document")
put("action", "create")
put("index", "nyc-open-data")
put("collection", "yellow-taxi")
put("body", body)
}

val result = kuzzle.authController.checkRights(requestPayload).get()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: kotlin-auth#checkRights
description: Checks if an API action can be executed by the current user
hooks:
before: curl -X POST kuzzle:7512/users/foo/_create -H "Content-Type:application/json" --data '{"content":{"profileIds":["default"]},"credentials":{"local":{"username":"foo","password":"bar"}}}'
after: curl -X DELETE kuzzle:7512/users/foo
template: print-result
expected: true
62 changes: 31 additions & 31 deletions doc/1/controllers/document/search/snippets/search-java.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("category", "suv");
query.put("match", match);
searchQuery.put("query", query);
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("category", "suv");
query.put("match", match);
searchQuery.put("query", query);

SearchResult results = kuzzle
.getDocumentController()
.search("nyc-open-data", "yellow-taxi", searchQuery).get();
SearchResult results = kuzzle
.getDocumentController()
.search("nyc-open-data", "yellow-taxi", searchQuery).get();


/*
/*
{
"aggregations"=undefined,
"hits"=[
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
15 changes: 15 additions & 0 deletions src/main/kotlin/io/kuzzle/sdk/controllers/AuthController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import java.util.concurrent.ConcurrentHashMap

class AuthController(kuzzle: Kuzzle) : BaseController(kuzzle) {

fun checkRights(
requestPayload: ConcurrentHashMap<String, Any?>): CompletableFuture<Boolean> {
val query = KuzzleMap().apply {
put("controller", "auth")
put("action", "checkRights")
put("body", requestPayload)
}
return kuzzle
.query(query)
.thenApplyAsync { response -> KuzzleMap
.from(response.result as ConcurrentHashMap<String?, Any?>)
.getBoolean("allowed") as Boolean
}
}

fun checkToken(
token: String): CompletableFuture<ConcurrentHashMap<String, Any?>> {
val query = KuzzleMap().apply {
Expand Down