Fix edge user update handling
This commit is contained in:
parent
486fed375c
commit
dee62dfad2
@ -181,7 +181,8 @@ public class EdgeEventSourcingListener {
|
||||
return false;
|
||||
}
|
||||
if (oldEntity != null) {
|
||||
User oldUser = (User) oldEntity;
|
||||
user = JacksonUtil.clone(user);
|
||||
User oldUser = JacksonUtil.clone((User) oldEntity);
|
||||
cleanUpUserAdditionalInfo(oldUser);
|
||||
cleanUpUserAdditionalInfo(user);
|
||||
return !user.equals(oldUser);
|
||||
@ -225,6 +226,7 @@ public class EdgeEventSourcingListener {
|
||||
user.setAdditionalInfo(additionalInfo);
|
||||
}
|
||||
}
|
||||
user.setVersion(null);
|
||||
}
|
||||
|
||||
private EdgeEventType getEdgeEventTypeForEntityEvent(Object entity) {
|
||||
|
||||
@ -47,7 +47,7 @@ public class UserEdgeTest extends AbstractEdgeTest {
|
||||
@Test
|
||||
public void testCreateUpdateDeleteTenantUser() throws Exception {
|
||||
// create user
|
||||
edgeImitator.expectMessageAmount(3);
|
||||
edgeImitator.expectMessageAmount(4);
|
||||
User newTenantAdmin = new User();
|
||||
newTenantAdmin.setAuthority(Authority.TENANT_ADMIN);
|
||||
newTenantAdmin.setTenantId(tenantId);
|
||||
@ -55,7 +55,7 @@ public class UserEdgeTest extends AbstractEdgeTest {
|
||||
newTenantAdmin.setFirstName("Boris");
|
||||
newTenantAdmin.setLastName("Johnson");
|
||||
User savedTenantAdmin = createUser(newTenantAdmin, "tenant");
|
||||
Assert.assertTrue(edgeImitator.waitForMessages()); // wait 3 messages - user update msg and x2 user credentials update msgs
|
||||
Assert.assertTrue(edgeImitator.waitForMessages()); // wait 4 messages - x2 user update msg and x2 user credentials update msgs
|
||||
Optional<UserUpdateMsg> userUpdateMsgOpt = edgeImitator.findMessageByType(UserUpdateMsg.class);
|
||||
Assert.assertTrue(userUpdateMsgOpt.isPresent());
|
||||
UserUpdateMsg userUpdateMsg = userUpdateMsgOpt.get();
|
||||
@ -131,7 +131,7 @@ public class UserEdgeTest extends AbstractEdgeTest {
|
||||
Assert.assertTrue(edgeImitator.waitForMessages());
|
||||
|
||||
// create user
|
||||
edgeImitator.expectMessageAmount(3);
|
||||
edgeImitator.expectMessageAmount(4);
|
||||
User customerUser = new User();
|
||||
customerUser.setAuthority(Authority.CUSTOMER_USER);
|
||||
customerUser.setTenantId(tenantId);
|
||||
@ -140,7 +140,7 @@ public class UserEdgeTest extends AbstractEdgeTest {
|
||||
customerUser.setFirstName("John");
|
||||
customerUser.setLastName("Edwards");
|
||||
User savedCustomerUser = createUser(customerUser, "customer");
|
||||
Assert.assertTrue(edgeImitator.waitForMessages()); // wait 3 messages - user update msg and x2 user credentials update msgs
|
||||
Assert.assertTrue(edgeImitator.waitForMessages()); // wait 4 messages - x2 user update msg and x2 user credentials update msgs
|
||||
Optional<UserUpdateMsg> userUpdateMsgOpt = edgeImitator.findMessageByType(UserUpdateMsg.class);
|
||||
Assert.assertTrue(userUpdateMsgOpt.isPresent());
|
||||
UserUpdateMsg userUpdateMsg = userUpdateMsgOpt.get();
|
||||
|
||||
@ -17,6 +17,9 @@ package org.thingsboard.server.dao.user;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.BooleanNode;
|
||||
import com.fasterxml.jackson.databind.node.IntNode;
|
||||
import com.fasterxml.jackson.databind.node.LongNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -411,16 +414,11 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
||||
saveUserCredentials(tenantId, userCredentials);
|
||||
|
||||
User user = findUserById(tenantId, userId);
|
||||
JsonNode additionalInfo = user.getAdditionalInfo();
|
||||
if (!(additionalInfo instanceof ObjectNode)) {
|
||||
additionalInfo = JacksonUtil.newObjectNode();
|
||||
}
|
||||
((ObjectNode) additionalInfo).put(USER_CREDENTIALS_ENABLED, enabled);
|
||||
user.setAdditionalInfo(additionalInfo);
|
||||
user.setAdditionalInfoField(USER_CREDENTIALS_ENABLED, BooleanNode.valueOf(enabled));
|
||||
if (enabled) {
|
||||
resetFailedLoginAttempts(user);
|
||||
}
|
||||
userDao.save(user.getTenantId(), user);
|
||||
saveUser(tenantId, user);
|
||||
}
|
||||
|
||||
|
||||
@ -433,23 +431,13 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
||||
}
|
||||
|
||||
private void resetFailedLoginAttempts(User user) {
|
||||
JsonNode additionalInfo = user.getAdditionalInfo();
|
||||
if (!(additionalInfo instanceof ObjectNode)) {
|
||||
additionalInfo = JacksonUtil.newObjectNode();
|
||||
}
|
||||
((ObjectNode) additionalInfo).put(FAILED_LOGIN_ATTEMPTS, 0);
|
||||
user.setAdditionalInfo(additionalInfo);
|
||||
user.setAdditionalInfoField(FAILED_LOGIN_ATTEMPTS, IntNode.valueOf(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastLoginTs(TenantId tenantId, UserId userId) {
|
||||
User user = findUserById(tenantId, userId);
|
||||
JsonNode additionalInfo = user.getAdditionalInfo();
|
||||
if (!(additionalInfo instanceof ObjectNode)) {
|
||||
additionalInfo = JacksonUtil.newObjectNode();
|
||||
}
|
||||
((ObjectNode) additionalInfo).put(LAST_LOGIN_TS, System.currentTimeMillis());
|
||||
user.setAdditionalInfo(additionalInfo);
|
||||
user.setAdditionalInfoField(LAST_LOGIN_TS, new LongNode(System.currentTimeMillis()));
|
||||
saveUser(tenantId, user);
|
||||
}
|
||||
|
||||
@ -499,17 +487,9 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
|
||||
}
|
||||
|
||||
private int increaseFailedLoginAttempts(User user) {
|
||||
JsonNode additionalInfo = user.getAdditionalInfo();
|
||||
if (!(additionalInfo instanceof ObjectNode)) {
|
||||
additionalInfo = JacksonUtil.newObjectNode();
|
||||
}
|
||||
int failedLoginAttempts = 0;
|
||||
if (additionalInfo.has(FAILED_LOGIN_ATTEMPTS)) {
|
||||
failedLoginAttempts = additionalInfo.get(FAILED_LOGIN_ATTEMPTS).asInt();
|
||||
}
|
||||
failedLoginAttempts = failedLoginAttempts + 1;
|
||||
((ObjectNode) additionalInfo).put(FAILED_LOGIN_ATTEMPTS, failedLoginAttempts);
|
||||
user.setAdditionalInfo(additionalInfo);
|
||||
int failedLoginAttempts = user.getAdditionalInfoField(FAILED_LOGIN_ATTEMPTS, JsonNode::asInt, 0);
|
||||
failedLoginAttempts++;
|
||||
user.setAdditionalInfoField(FAILED_LOGIN_ATTEMPTS, new IntNode(failedLoginAttempts));
|
||||
return failedLoginAttempts;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user