Skip to content

Commit

Permalink
feat: fix get-account API (#50)
Browse files Browse the repository at this point in the history
* feat: change type of data2 to a generic type

* test: provide a factory to create common config

* feat: get account by access token

* refactor!: change type of data2 of CasdoorResponse to a generic type
  • Loading branch information
towerhe authored Jul 24, 2023
1 parent bea9ae7 commit a0d7c8e
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

package org.casbin.casdoor.service;

import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;

import org.casbin.casdoor.config.CasdoorConfig;
import org.casbin.casdoor.entity.CasdoorOrganization;
import org.casbin.casdoor.entity.CasdoorUser;
import org.casbin.casdoor.util.Map;
import org.casbin.casdoor.util.http.CasdoorResponse;

import java.io.IOException;

import com.fasterxml.jackson.core.type.TypeReference;

/**
* Service Related to Account API
Expand All @@ -46,7 +48,21 @@ public CasdoorResponse setPassword(String userName, String oldPassword, String n
"userOwner", casdoorConfig.getOrganizationName(),
"userName", userName,
"oldPassword", oldPassword,
"newPassword", newPassword
), new TypeReference<CasdoorResponse<Object>>() {});
"newPassword", newPassword),
new TypeReference<CasdoorResponse<Object, Object>>() {
});
}

/**
* Get current user
*
* @param accessToken access token of current user
* @return user and organization info of current user
* @throws IOException when JSON unmarshalling fails or HTTP requests fails
*/
public CasdoorResponse<CasdoorUser, CasdoorOrganization> getAccount(String accessToken) throws IOException {
return doGet("get-account", Map.of("access_token", accessToken),
new TypeReference<CasdoorResponse<CasdoorUser, CasdoorOrganization>>() {
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public CasdoorResponse sendEmail(String title, String content, String sender, St
CasdoorEmailForm casdoorEmailForm = new CasdoorEmailForm(title, content, sender, receivers);
String emailFormStr = objectMapper.writeValueAsString(casdoorEmailForm);

return doPost("send-email", null, emailFormStr, new TypeReference<CasdoorResponse<Object>>() {});
return doPost("send-email", null, emailFormStr, new TypeReference<CasdoorResponse<Object, Object>>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public boolean enforce(String permissionId, String modelId, String resourceId, O
if (postBytes == null) {
throw new CasdoorException("Failed to get bytes from URL");
}
CasdoorResponse<Boolean[]> response = doPost("enforce",
CasdoorResponse<Boolean[], Object> response = doPost("enforce",
Map.of(
"permissionId", casdoorConfig.getOrganizationName() + "/" + permissionId,
"modelId", modelId,
"resourceId", resourceId
),
new String(postBytes, StandardCharsets.UTF_8),
new TypeReference<CasdoorResponse<Boolean[]>>() {}
new TypeReference<CasdoorResponse<Boolean[], Object>>() {}
);

// All true
Expand All @@ -52,14 +52,14 @@ public Boolean[][] batchEnforce(String permissionId, String modelId, String reso
if (postBytes == null) {
throw new CasdoorException("Failed to get bytes from URL");
}
CasdoorResponse<Boolean[][]> response = doPost("batch-enforce",
CasdoorResponse<Boolean[][], Object> response = doPost("batch-enforce",
Map.of(
"permissionId", casdoorConfig.getOrganizationName() + "/" + permissionId,
"modelId", modelId,
"resourceId", resourceId
),
new String(postBytes, StandardCharsets.UTF_8),
new TypeReference<CasdoorResponse<Boolean[][]>>() {}
new TypeReference<CasdoorResponse<Boolean[][], Object>>() {}
);

return response.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,57 +33,57 @@ public CasdoorPermissionService(CasdoorConfig casdoorConfig) {
}

public CasdoorPermission getPermission(String name) throws IOException {
CasdoorResponse<CasdoorPermission> response = doGet("get-permission",
Map.of("id", casdoorConfig.getOrganizationName() + "/" + name), new TypeReference<CasdoorResponse<CasdoorPermission>>() {});
CasdoorResponse<CasdoorPermission, Object> response = doGet("get-permission",
Map.of("id", casdoorConfig.getOrganizationName() + "/" + name), new TypeReference<CasdoorResponse<CasdoorPermission, Object>>() {});
return response.getData();
}

public List<CasdoorPermission> getPermissions() throws IOException {
CasdoorResponse<List<CasdoorPermission>> resp = doGet("get-permissions",
Map.of("owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorPermission>>>() {});
CasdoorResponse<List<CasdoorPermission>, Object> resp = doGet("get-permissions",
Map.of("owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorPermission>, Object>>() {});
return resp.getData();
}

public List<CasdoorPermission> getPermissionsByRole(String name) throws IOException {
CasdoorResponse<List<CasdoorPermission>> resp = doGet("get-permissions-by-role",
CasdoorResponse<List<CasdoorPermission>, Object> resp = doGet("get-permissions-by-role",
Map.of("id", casdoorConfig.getOrganizationName() + "/" + name,
"owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorPermission>>>() {});
"owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorPermission>, Object>>() {});

return resp.getData();
}
public java.util.Map<String, Object> getPaginationPermissions(int p, int pageSize, @Nullable java.util.Map<String, String> queryMap) throws IOException {
CasdoorResponse<CasdoorPermission[]> resp = doGet("get-permissions",
CasdoorResponse<CasdoorPermission[], Object> resp = doGet("get-permissions",
Map.mergeMap(Map.of("owner", casdoorConfig.getOrganizationName(),
"p", Integer.toString(p),
"pageSize", Integer.toString(pageSize)), queryMap), new TypeReference<CasdoorResponse<CasdoorPermission[]>>() {});
"pageSize", Integer.toString(pageSize)), queryMap), new TypeReference<CasdoorResponse<CasdoorPermission[], Object>>() {});

return Map.of("casdoorPermissions", resp.getData(), "data2", resp.getData2());
}


public CasdoorResponse<String> updatePermission(CasdoorPermission permission) throws IOException {
public CasdoorResponse<String, Object> updatePermission(CasdoorPermission permission) throws IOException {
return modifyPermission(PermissionOperations.UPDATE_PERMISSION, permission);
}

public CasdoorResponse<String> updatePermissionForColumns(CasdoorPermission permission, String... columns) throws IOException {
public CasdoorResponse<String, Object> updatePermissionForColumns(CasdoorPermission permission, String... columns) throws IOException {
return modifyPermission(PermissionOperations.UPDATE_PERMISSION, permission);
}

public CasdoorResponse<String> addPermission(CasdoorPermission permission) throws IOException {
public CasdoorResponse<String, Object> addPermission(CasdoorPermission permission) throws IOException {
return modifyPermission(PermissionOperations.ADD_PERMISSION, permission);
}

public CasdoorResponse<String> deletePermission(CasdoorPermission permission) throws IOException {
public CasdoorResponse<String, Object> deletePermission(CasdoorPermission permission) throws IOException {
return modifyPermission(PermissionOperations.DELETE_PERMISSION, permission);
}

/**
* modifyPermission is an encapsulation of permission CUD(Create, Update, Delete) operations.
* possible actions are `add-permission`, `update-permission`, `delete-permission`,
*/
private <T> CasdoorResponse<T> modifyPermission(PermissionOperations method, CasdoorPermission permission) throws IOException {
private <T1, T2> CasdoorResponse<T1, T2> modifyPermission(PermissionOperations method, CasdoorPermission permission) throws IOException {
return doPost(method.getOperation(),
Map.of("id", permission.getOwner() + "/" + permission.getName()),
objectMapper.writeValueAsString(permission), new TypeReference<CasdoorResponse<T>>() {});
objectMapper.writeValueAsString(permission), new TypeReference<CasdoorResponse<T1, T2>>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public CasdoorResourceService(CasdoorConfig casdoorConfig) {
super(casdoorConfig);
}

public CasdoorResponse<String> uploadResource(String user, String tag, String parent, String fullFilePath, File file) throws IOException {
public CasdoorResponse<String, Object> uploadResource(String user, String tag, String parent, String fullFilePath, File file) throws IOException {
return doPost("upload-resource",
Map.of("owner", casdoorConfig.getOrganizationName(),
"user", user,
"application", casdoorConfig.getApplicationName(),
"tag", tag,
"parent", parent,
"fullFilePath", fullFilePath),
file, new TypeReference<CasdoorResponse<String>>() {});
file, new TypeReference<CasdoorResponse<String, Object>>() {});
}

public CasdoorResponse<String> deleteResource(String name) throws IOException {
public CasdoorResponse<String, Object> deleteResource(String name) throws IOException {
CasdoorResource casdoorResource = new CasdoorResource(casdoorConfig.getOrganizationName(), name);
String userStr = objectMapper.writeValueAsString(casdoorResource);
return doPost("delete-resource", null, userStr, new TypeReference<CasdoorResponse<String>>() {});
return doPost("delete-resource", null, userStr, new TypeReference<CasdoorResponse<String, Object>>() {});
}
}
24 changes: 12 additions & 12 deletions src/main/java/org/casbin/casdoor/service/CasdoorRoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ public CasdoorRoleService(CasdoorConfig casdoorConfig) {
}

public CasdoorRole getRole(String name) throws IOException {
CasdoorResponse<CasdoorRole> resp = doGet("get-role",
Map.of("id", casdoorConfig.getOrganizationName() + "/" + name), new TypeReference<CasdoorResponse<CasdoorRole>>() {});
CasdoorResponse<CasdoorRole, Object> resp = doGet("get-role",
Map.of("id", casdoorConfig.getOrganizationName() + "/" + name), new TypeReference<CasdoorResponse<CasdoorRole, Object>>() {});
return resp.getData();
}

public List<CasdoorRole> getRoles() throws IOException {
CasdoorResponse<List<CasdoorRole>> resp = doGet("get-roles",
Map.of("owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorRole>>>() {});
CasdoorResponse<List<CasdoorRole>, Object> resp = doGet("get-roles",
Map.of("owner", casdoorConfig.getOrganizationName()), new TypeReference<CasdoorResponse<List<CasdoorRole>, Object>>() {});
return resp.getData();
}

public java.util.Map<String, Object> getPaginationRoles(int p, int pageSize, @Nullable java.util.Map<String, String> queryMap) throws IOException {
CasdoorResponse<CasdoorRole[]> casdoorResponse = doGet("get-roles",
CasdoorResponse<CasdoorRole[], Object> casdoorResponse = doGet("get-roles",
Map.mergeMap(Map.of("owner", casdoorConfig.getOrganizationName(),
"p", Integer.toString(p),
"pageSize", Integer.toString(pageSize)), queryMap), new TypeReference<CasdoorResponse<CasdoorRole[]>>() {});
"pageSize", Integer.toString(pageSize)), queryMap), new TypeReference<CasdoorResponse<CasdoorRole[], Object>>() {});

return Map.of("casdoorRoles", casdoorResponse.getData(), "data2", casdoorResponse.getData2());
}
public CasdoorResponse<String> updateRole(CasdoorRole role) throws IOException {
public CasdoorResponse<String, Object> updateRole(CasdoorRole role) throws IOException {
return modifyRole(RoleOperations.UPDATE_ROLE, role);
}

public CasdoorResponse<String> updateRoleForColumns(CasdoorRole role, String... columns) throws IOException {
public CasdoorResponse<String, Object> updateRoleForColumns(CasdoorRole role, String... columns) throws IOException {
return modifyRole(RoleOperations.UPDATE_ROLE, role);
}

public CasdoorResponse<String> addRole(CasdoorRole role) throws IOException {
public CasdoorResponse<String, Object> addRole(CasdoorRole role) throws IOException {
return modifyRole(RoleOperations.ADD_ROLE, role);
}

public CasdoorResponse<String> deleteRole(CasdoorRole role) throws IOException {
public CasdoorResponse<String, Object> deleteRole(CasdoorRole role) throws IOException {
return modifyRole(RoleOperations.DELETE_ROLE, role);
}
private <T> CasdoorResponse<T> modifyRole(RoleOperations method, CasdoorRole role) throws IOException {
private <T1, T2> CasdoorResponse<T1, T2> modifyRole(RoleOperations method, CasdoorRole role) throws IOException {
return doPost(method.getOperation(),
Map.of("id", role.getOwner() + "/" + role.getName()),
objectMapper.writeValueAsString(role), new TypeReference<CasdoorResponse<T>>() {});
objectMapper.writeValueAsString(role), new TypeReference<CasdoorResponse<T1, T2>>() {});
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/casbin/casdoor/service/CasdoorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@ protected CasdoorService(CasdoorConfig casdoorConfig) {
this.credential = Credentials.basic(casdoorConfig.getClientId(), casdoorConfig.getClientSecret());
}

protected <T> CasdoorResponse<T> doGet(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, TypeReference<CasdoorResponse<T>> typeReference) throws IOException {
protected <T1, T2> CasdoorResponse<T1, T2> doGet(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, TypeReference<CasdoorResponse<T1, T2>> typeReference) throws IOException {
String url = String.format("%s/api/%s?%s", casdoorConfig.getEndpoint(), action, Map.mapToUrlParams(queryParams));
String response = HttpClient.syncGet(url, credential);
CasdoorResponse<T> resp = objectMapper.readValue(response, typeReference);
CasdoorResponse<T1, T2> resp = objectMapper.readValue(response, typeReference);
if (!Objects.equals(resp.getStatus(), "ok")) {
throw new CasdoorException(String.format("Failed fetching %s : %s", url, resp.getMsg()));
}

return resp;
}

protected <T> CasdoorResponse<T> doPost(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, java.util.Map<String, String> postForm, TypeReference<CasdoorResponse<T>> typeReference) throws IOException {
protected <T1, T2> CasdoorResponse<T1, T2> doPost(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, java.util.Map<String, String> postForm, TypeReference<CasdoorResponse<T1, T2>> typeReference) throws IOException {
String url = String.format("%s/api/%s?%s", casdoorConfig.getEndpoint(), action, Map.mapToUrlParams(queryParams));
String response = HttpClient.postForm(url, postForm, credential);
CasdoorResponse<T> resp = objectMapper.readValue(response, typeReference);
CasdoorResponse<T1, T2> resp = objectMapper.readValue(response, typeReference);
if (!Objects.equals(resp.getStatus(), "ok")) {
throw new CasdoorException(String.format("Failed fetching %s : %s", url, resp.getMsg()));
}

return resp;
}

protected <T> CasdoorResponse<T> doPost(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, String postString, TypeReference<CasdoorResponse<T>> typeReference) throws IOException {
protected <T1, T2> CasdoorResponse<T1, T2> doPost(@NotNull String action, @Nullable java.util.Map<String, String> queryParams, String postString, TypeReference<CasdoorResponse<T1, T2>> typeReference) throws IOException {
String url = String.format("%s/api/%s?%s", casdoorConfig.getEndpoint(), action, Map.mapToUrlParams(queryParams));
String response = HttpClient.postString(url, postString, credential);
CasdoorResponse<T> resp = objectMapper.readValue(response, typeReference);
CasdoorResponse<T1, T2> resp = objectMapper.readValue(response, typeReference);
if (!Objects.equals(resp.getStatus(), "ok")) {
throw new CasdoorException(String.format("Failed fetching %s : %s", url, resp.getMsg()));
}

return resp;
}

protected <T> CasdoorResponse<T> doPost(String action, @Nullable java.util.Map<String, String> queryParams, File postFile, TypeReference<CasdoorResponse<T>> typeReference) throws IOException {
protected <T1, T2> CasdoorResponse<T1, T2> doPost(String action, @Nullable java.util.Map<String, String> queryParams, File postFile, TypeReference<CasdoorResponse<T1, T2>> typeReference) throws IOException {
String url = String.format("%s/api/%s?%s", casdoorConfig.getEndpoint(), action, Map.mapToUrlParams(queryParams));
String response = HttpClient.postFile(url, postFile, credential);
CasdoorResponse<T> resp = objectMapper.readValue(response, typeReference);
CasdoorResponse<T1, T2> resp = objectMapper.readValue(response, typeReference);
if (!Objects.equals(resp.getStatus(), "ok")) {
throw new CasdoorException(String.format("Failed fetching %s : %s", url, resp.getMsg()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public CasdoorResponse sendSms(String content, String... receivers) throws IOExc
CasdoorSmsForm casdoorSmsForm = new CasdoorSmsForm("admin/" + casdoorConfig.getOrganizationName(), content, receivers);
String smsFormStr = objectMapper.writeValueAsString(casdoorSmsForm);

return doPost("send-sms", Map.of(), smsFormStr, new TypeReference<CasdoorResponse<Object>>() {});
return doPost("send-sms", Map.of(), smsFormStr, new TypeReference<CasdoorResponse<Object, Object>>() {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public CasdoorTokenService(CasdoorConfig casdoorConfig) {
* @return the list of tokens
* @throws IOException if fails.
*/
public CasdoorResponse<List<CasdoorToken>> getTokens(int p, int pageSize) throws IOException {
public CasdoorResponse<List<CasdoorToken>, Object> getTokens(int p, int pageSize) throws IOException {
return doGet("get-tokens", p > -1 ? Map.of(
"owner", casdoorConfig.getOrganizationName(),
"p", Integer.toString(p),
"pageSize", Integer.toString(pageSize)
) : Map.of(
"owner", casdoorConfig.getOrganizationName()
), new TypeReference<CasdoorResponse<List<CasdoorToken>>>() {});
), new TypeReference<CasdoorResponse<List<CasdoorToken>, Object>>() {});
}

public CasdoorResponse<Boolean> deleteToken(CasdoorToken casdoorToken) throws IOException {
return doPost("delete-token", Map.of(), objectMapper.writeValueAsString(casdoorToken), new TypeReference<CasdoorResponse<Boolean>>() {});
public CasdoorResponse<Boolean, Object> deleteToken(CasdoorToken casdoorToken) throws IOException {
return doPost("delete-token", Map.of(), objectMapper.writeValueAsString(casdoorToken), new TypeReference<CasdoorResponse<Boolean, Object>>() {});
}
}
Loading

0 comments on commit a0d7c8e

Please sign in to comment.