|
20 | 20 | import org.casbin.casdoor.util.Map;
|
21 | 21 | import org.casbin.casdoor.util.UserOperations;
|
22 | 22 | import org.casbin.casdoor.util.http.CasdoorResponse;
|
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
23 | 25 |
|
| 26 | +import java.util.HashMap; |
24 | 27 | import java.io.IOException;
|
25 | 28 | import java.util.List;
|
26 | 29 |
|
27 | 30 | public class UserService extends Service {
|
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); |
| 33 | + |
28 | 34 | public UserService(Config config) {
|
29 | 35 | super(config);
|
30 | 36 | }
|
@@ -109,4 +115,44 @@ public java.util.Map<String, Object> getPaginationUsers(int p, int pageSize, jav
|
109 | 115 |
|
110 | 116 | return Map.of("casdoorUsers", resp.getData(), "data2", resp.getData2());
|
111 | 117 | }
|
| 118 | + |
| 119 | + /** |
| 120 | + * Set the password for a user. |
| 121 | + * @param owner the owner of the user |
| 122 | + * @param name the name of the user |
| 123 | + * @param oldPassword the old password of the user (can be empty if not required) |
| 124 | + * @param newPassword the new password of the user |
| 125 | + * @return true if the password was set successfully, false otherwise |
| 126 | + * @throws IOException if there is an error during the operation |
| 127 | + */ |
| 128 | + public boolean setPassword(String owner, String name, String oldPassword, String newPassword) throws IOException { |
| 129 | + HashMap<Object, Object> param = new HashMap<>(); |
| 130 | + param.put("userOwner", owner); |
| 131 | + param.put("userName", name); |
| 132 | + param.put("newPassword", newPassword); |
| 133 | + |
| 134 | + if (oldPassword != null && !oldPassword.isEmpty()) { |
| 135 | + param.put("oldPassword", oldPassword); |
| 136 | + } |
| 137 | + |
| 138 | + String payload = objectMapper.writeValueAsString(param); |
| 139 | + CasdoorResponse<String, Object> resp; |
| 140 | + |
| 141 | + try { |
| 142 | + resp = doPost("set-password", null, payload, new TypeReference<CasdoorResponse<String, Object>>() {}); |
| 143 | + } catch (IOException e) { |
| 144 | + LOGGER.error("Error setting password for user {}: {}", name, e.getMessage()); |
| 145 | + throw new IOException("Failed to set password: " + e.getMessage(), e); |
| 146 | + } |
| 147 | + |
| 148 | + boolean isSuccess = "ok".equals(resp.getStatus()); |
| 149 | + if (isSuccess) { |
| 150 | + LOGGER.info("Password successfully set for user {}", name); |
| 151 | + } else { |
| 152 | + LOGGER.warn("Failed to set password for user {}: {}", name, resp.getMsg()); |
| 153 | + } |
| 154 | + |
| 155 | + return isSuccess; |
| 156 | + } |
| 157 | + |
112 | 158 | }
|
0 commit comments