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: Add Set Password API using Casdoor Java SDK #101

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 46 additions & 0 deletions src/main/java/org/casbin/casdoor/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
import org.casbin.casdoor.util.Map;
import org.casbin.casdoor.util.UserOperations;
import org.casbin.casdoor.util.http.CasdoorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.io.IOException;
import java.util.List;

public class UserService extends Service {

private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the existing code style. Don't use Logger


public UserService(Config config) {
super(config);
}
Expand Down Expand Up @@ -109,4 +115,44 @@ public java.util.Map<String, Object> getPaginationUsers(int p, int pageSize, jav

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

/**
* Set the password for a user.
* @param owner the owner of the user
* @param name the name of the user
* @param oldPassword the old password of the user (can be empty if not required)
* @param newPassword the new password of the user
* @return true if the password was set successfully, false otherwise
* @throws IOException if there is an error during the operation
*/
public boolean setPassword(String owner, String name, String oldPassword, String newPassword) throws IOException {
HashMap<Object, Object> param = new HashMap<>();
param.put("userOwner", owner);
param.put("userName", name);
param.put("newPassword", newPassword);

if (oldPassword != null && !oldPassword.isEmpty()) {
param.put("oldPassword", oldPassword);
}

String payload = objectMapper.writeValueAsString(param);
CasdoorResponse<String, Object> resp;

try {
resp = doPost("set-password", null, payload, new TypeReference<CasdoorResponse<String, Object>>() {});
} catch (IOException e) {
LOGGER.error("Error setting password for user {}: {}", name, e.getMessage());
throw new IOException("Failed to set password: " + e.getMessage(), e);
}

boolean isSuccess = "ok".equals(resp.getStatus());
if (isSuccess) {
LOGGER.info("Password successfully set for user {}", name);
} else {
LOGGER.warn("Failed to set password for user {}: {}", name, resp.getMsg());
}

return isSuccess;
}

}
Loading