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

feat(java-sdk): support list users #355

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ jobs:

- name: Run SDK Tests
run: make test-client-js
env:
OPEN_API_REF: list-users

- name: Check for SDK changes
run: |
Expand Down Expand Up @@ -65,8 +63,6 @@ jobs:

- name: Run SDK Tests
run: make test-client-go
env:
OPEN_API_REF: list-users

- name: Check for SDK changes
run: |
Expand Down Expand Up @@ -97,8 +93,6 @@ jobs:

- name: Run All Tests
run: make test-client-dotnet
env:
OPEN_API_REF: list-users

- name: Check for SDK changes
run: |
Expand Down
7 changes: 7 additions & 0 deletions config/clients/java/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.4.2

### [0.4.2](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.4.1...v0.4.2) (2024-05-02)

- feat: support the [ListUsers](https://github.com/openfga/rfcs/blob/main/20231214-listUsers-api.md) endpoint (#80)
- fix: improve check for validity of token (#76)

## v0.4.1

### [0.4.1](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.4.0...v0.4.1) (2024-04-09)
Expand Down
14 changes: 13 additions & 1 deletion config/clients/java/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"gitRepoId": "java-sdk",
"artifactId": "openfga-sdk",
"groupId": "dev.openfga",
"packageVersion": "0.4.1",
"packageVersion": "0.4.2",
"apiPackage": "dev.openfga.sdk.api",
"authPackage": "dev.openfga.sdk.api.auth",
"clientPackage": "dev.openfga.sdk.api.client",
Expand Down Expand Up @@ -103,6 +103,14 @@
"destinationFilename": "src/main/java/dev/openfga/sdk/api/client/model/ClientListRelationsResponse.java",
"templateType": "SupportingFiles"
},
"client-ClientListUsersRequest.java.mustache": {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/client/model/ClientListUsersRequest.java",
"templateType": "SupportingFiles"
},
"client-ClientListUsersResponse.java.mustache": {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/client/model/ClientListUsersResponse.java",
"templateType": "SupportingFiles"
},
"client-ClientReadAssertionsResponse.java.mustache": {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/client/model/ClientReadAssertionsResponse.java",
"templateType": "SupportingFiles"
Expand Down Expand Up @@ -267,6 +275,10 @@
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientListStoresOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientListUsersOptions.java.mustache": {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientListUsersOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadAssertionsOptions.java.mustache": {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadAssertionsOptions.java",
"templateType": "SupportingFiles"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
cache: gradle

- name: Set up Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
uses: gradle/actions/setup-gradle@db19848a5fa7950289d3668fb053140cf3028d43 # v3.3.2

- name: Test and Build with Gradle
run: |
Expand Down Expand Up @@ -60,10 +60,10 @@ jobs:
distribution: "temurin"

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@b231772637bb498f11fdbc86052b6e8a8dc9fc92 # v2.1.2
uses: gradle/wrapper-validation-action@216d1ad2b3710bf005dc39237337b9673fd8fcd5 # v3.3.2

- name: Publish package
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
uses: gradle/actions/setup-gradle@db19848a5fa7950289d3668fb053140cf3028d43 # v3.3.2
with:
# Tasks created by https://github.com/gradle-nexus/publish-plugin
arguments: publishToSonatype closeAndReleaseSonatypeStagingRepository
Expand Down Expand Up @@ -92,10 +92,10 @@ jobs:
distribution: "temurin"

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@b231772637bb498f11fdbc86052b6e8a8dc9fc92 # v2.1.2
uses: gradle/wrapper-validation-action@216d1ad2b3710bf005dc39237337b9673fd8fcd5 # v3.3.2

- name: Publish package
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
uses: gradle/actions/setup-gradle@db19848a5fa7950289d3668fb053140cf3028d43 # v3.3.2
with:
# Tasks created by https://docs.gradle.org/current/userguide/publishing_maven.html
arguments: publishAllPublicationsToGitHubPackagesRepository
Expand Down
43 changes: 43 additions & 0 deletions config/clients/java/template/README_calling_api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,49 @@ var response = fgaClient.listRelations(request, options).get();
// response.getRelations() = ["can_view", "can_edit"]
```

##### List Users

List the users who have a certain relation to a particular type.

[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/ListUsers)

```java
// Only a single filter is allowed for the time being
var userFilters = new ArrayList<UserTypeFilter>() {
{
add(new UserTypeFilter().type("user"));
// user filters can also be of the form
// add(new UserTypeFilter().type("team").relation("member"));
}
};

var request = new ClientListUsersRequest()
._object(new FgaObject().type("document").id("roadmap"))
.relation("can_read")
.userFilters(userFilters)
.context(Map.of("view_count", 100))
.contextualTupleKeys(List.of(
new ClientTupleKey()
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
.relation("editor")
._object("folder:product"),
new ClientTupleKey()
.user("folder:product")
.relation("parent")
._object("document:roadmap")
));

var options = new ClientListUsersOptions()
.additionalHeaders(Map.of("Some-Http-Header", "Some value"))
// You can rely on the model id set in the configuration or override it for this specific request
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");

var response = fgaClient.listUsers(request, options).get();

// response.getUsers() = [{object: {type: "user", id: "81684243-9356-4421-8fbf-a4f8d36aa31b"}}, {userset: { type: "user" }}, ...]
// response.getExcludedUsers = [ {object: {type: "user", id: "4a455e27-d15a-4434-82e0-136f9c2aa4cf"}}, ... ]
```

#### Assertions

##### Read Assertions
Expand Down
4 changes: 2 additions & 2 deletions config/clients/java/template/build.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
// Publishing
id 'maven-publish'
id 'signing'
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
}

apply from: 'publish.gradle'
Expand Down Expand Up @@ -96,7 +96,7 @@ testing {
implementation "org.junit.jupiter:junit-jupiter:$junit_version"
implementation "org.mockito:mockito-core:5.+"
runtimeOnly "org.junit.platform:junit-platform-launcher"
implementation "org.wiremock:wiremock:3.5.2"
implementation "org.wiremock:wiremock:3.5.4"

// This test-only dependency is convenient but not widely used.
// Review project activity before updating the version here.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{{>licenseInfo}}
package {{clientPackage}}.model;

import java.util.List;
import dev.openfga.sdk.api.model.FgaObject;
import dev.openfga.sdk.api.model.UserTypeFilter;

public class ClientListUsersRequest {
private FgaObject _object;
private String relation;
private List<UserTypeFilter> userFilters;
private List<ClientTupleKey> contextualTupleKeys;
private Object context;

public ClientListUsersRequest _object(FgaObject _object) {
this._object = _object;
return this;
}


/**
* Get _object
* @return _object
**/
public FgaObject getObject() {
return _object;
}

public ClientListUsersRequest relation(String relation) {
this.relation = relation;
return this;
}

/**
* Get relation
* @return relation
**/
public String getRelation() {
return relation;
}

public ClientListUsersRequest userFilters(List<UserTypeFilter> userFilters) {
this.userFilters = userFilters;
return this;
}

/**
* Get userFilters
* @return userFilters
**/
public List<UserTypeFilter> getUserFilters() {
return userFilters;
}

public ClientListUsersRequest contextualTupleKeys(List<ClientTupleKey> contextualTupleKeys) {
this.contextualTupleKeys = contextualTupleKeys;
return this;
}

public List<ClientTupleKey> getContextualTupleKeys() {
return contextualTupleKeys;
}

public ClientListUsersRequest context(Object context) {
this.context = context;
return this;
}

/**
* Get context
* @return context
**/
public Object getContext() {
return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{>licenseInfo}}
package {{clientPackage}}.model;

import {{clientPackage}}.ApiResponse;
import {{modelPackage}}.ListUsersResponse;
import java.util.List;
import java.util.Map;

public class ClientListUsersResponse extends ListUsersResponse {
private final int statusCode;
private final Map<String, List<String>> headers;
private final String rawResponse;

public ClientListUsersResponse(ApiResponse<ListUsersResponse> apiResponse) {
this.statusCode = apiResponse.getStatusCode();
this.headers = apiResponse.getHeaders();
this.rawResponse = apiResponse.getRawResponse();
ListUsersResponse response = apiResponse.getData();
this.setUsers(response.getUsers());
this.setExcludedUsers(response.getExcludedUsers());

}

public int getStatusCode() {
return statusCode;
}

public Map<String, List<String>> getHeaders() {
return headers;
}

public String getRawResponse() {
return rawResponse;
}
}
42 changes: 42 additions & 0 deletions config/clients/java/template/client-OpenFgaClient.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,48 @@ public class OpenFgaClient {
.thenCompose(responses -> call(() -> ClientListRelationsResponse.fromBatchCheckResponses(responses)));
}

/**
* ListUsers - List all users of the given type that the object has a relation with (evaluates)
*/
public CompletableFuture<ClientListUsersResponse> listUsers(ClientListUsersRequest request)
throws FgaInvalidParameterException {
return listUsers(request, null);
}

/**
* ListUsers - List all users of the given type that the object has a relation with (evaluates)
*/
public CompletableFuture<ClientListUsersResponse> listUsers(
ClientListUsersRequest request, ClientListUsersOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

ListUsersRequest body = new ListUsersRequest();

if (request != null) {
body._object(request.getObject()).relation(request.getRelation()).userFilters(request.getUserFilters());
if (request.getContextualTupleKeys() != null) {
var contextualTuples = request.getContextualTupleKeys();
var bodyContextualTuples = ClientTupleKey.asContextualTupleKeys(contextualTuples);
body.contextualTuples(bodyContextualTuples.getTupleKeys());
}
if(request.getContext() != null) {
body.context(request.getContext());
}
}

if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
body.authorizationModelId(options.getAuthorizationModelId());
} else {
String authorizationModelId = configuration.getAuthorizationModelId();
body.authorizationModelId(authorizationModelId);
}

var overrides = new ConfigurationOverride().addHeaders(options);

return call(() -> api.listUsers(storeId, body, overrides)).thenApply(ClientListUsersResponse::new);
}

/* ************
* Assertions *
**************/
Expand Down
Loading
Loading