24 changed files with 819 additions and 1 deletions
@ -0,0 +1,43 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserCredentialsId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.security.UserCredentials; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.entity.EntityDaoService; |
|||
|
|||
import java.util.List; |
|||
|
|||
public interface UserSettingsService { |
|||
|
|||
UserSettings updateUserSettings(TenantId tenantId, UserSettings userSettings); |
|||
|
|||
UserSettings saveUserSettings(TenantId tenantId, UserSettings userSettings); |
|||
|
|||
UserSettings findUserSettings(TenantId tenantId, UserId userId); |
|||
|
|||
void deleteUserSettings(TenantId tenantId, UserId userId, List<String> jsonPaths); |
|||
|
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.security; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.validation.Length; |
|||
import org.thingsboard.server.common.data.validation.NoXss; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.io.Serializable; |
|||
import java.util.function.Consumer; |
|||
import java.util.function.Supplier; |
|||
|
|||
import static org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo.getJson; |
|||
import static org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo.setJson; |
|||
|
|||
@ApiModel |
|||
@Data |
|||
public class UserSettings implements Serializable { |
|||
|
|||
private static final long serialVersionUID = 2628320657987010348L; |
|||
|
|||
@ApiModelProperty(position = 1, value = "JSON object with User id.", accessMode = ApiModelProperty.AccessMode.READ_ONLY) |
|||
private UserId userId; |
|||
|
|||
@ApiModelProperty(position = 2, value = "JSON object with user settings.", dataType = "com.fasterxml.jackson.databind.JsonNode") |
|||
@NoXss |
|||
@Length(fieldName = "settings", max = 100000) |
|||
private transient JsonNode settings; |
|||
|
|||
@JsonIgnore |
|||
private byte[] settingsBytes; |
|||
|
|||
public JsonNode getSettings() { |
|||
return getJson(() -> settings, () -> settingsBytes); |
|||
} |
|||
|
|||
public void setSettings(JsonNode settings) { |
|||
setJson(settings, json -> this.settings = json, bytes -> this.settingsBytes = bytes); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.entity; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.ApplicationEventPublisher; |
|||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
public abstract class AbstractCachedService<K extends Serializable, V extends Serializable, E> { |
|||
|
|||
@Autowired |
|||
protected TbTransactionalCache<K, V> cache; |
|||
|
|||
@Autowired |
|||
private ApplicationEventPublisher eventPublisher; |
|||
|
|||
protected void publishEvictEvent(E event) { |
|||
if (TransactionSynchronizationManager.isActualTransactionActive()) { |
|||
eventPublisher.publishEvent(event); |
|||
} else { |
|||
handleEvictEvent(event); |
|||
} |
|||
} |
|||
|
|||
public abstract void handleEvictEvent(E event); |
|||
|
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.model.sql; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.hibernate.annotations.Type; |
|||
import org.hibernate.annotations.TypeDef; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.model.ToData; |
|||
import org.thingsboard.server.dao.util.mapping.JsonStringType; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.Id; |
|||
import javax.persistence.Table; |
|||
import java.util.UUID; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@TypeDef(name = "json", typeClass = JsonStringType.class) |
|||
@Entity |
|||
@Table(name = ModelConstants.USER_SETTINGS_COLUMN_FAMILY_NAME) |
|||
public class UserSettingsEntity implements ToData<UserSettings> { |
|||
|
|||
@Id |
|||
@Column(name = ModelConstants.USER_SETTINGS_USER_ID_PROPERTY) |
|||
private UUID userId; |
|||
@Type(type = "json") |
|||
@Column(name = ModelConstants.USER_SETTINGS_SETTINGS) |
|||
private JsonNode settings; |
|||
|
|||
public UserSettingsEntity(UserSettings userSettings) { |
|||
this.userId = userSettings.getUserId().getId(); |
|||
if (userSettings.getSettings() != null) { |
|||
this.settings= userSettings.getSettings(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public UserSettings toData() { |
|||
UserSettings userSettings = new UserSettings(); |
|||
userSettings.setUserId(new UserId(userId)); |
|||
if (settings != null) { |
|||
userSettings.setSettings(settings); |
|||
} |
|||
return userSettings; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.user; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.DaoUtil; |
|||
import org.thingsboard.server.dao.model.sql.UserSettingsEntity; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; |
|||
import org.thingsboard.server.dao.user.UserSettingsDao; |
|||
import org.thingsboard.server.dao.util.SqlDao; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@SqlDao |
|||
public class JpaUserSettingsDao extends JpaAbstractDaoListeningExecutorService implements UserSettingsDao { |
|||
|
|||
@Autowired |
|||
private UserSettingsRepository userSettingsRepository; |
|||
|
|||
@Override |
|||
public UserSettings save(TenantId tenantId, UserSettings userSettings) { |
|||
return DaoUtil.getData(userSettingsRepository.save(new UserSettingsEntity(userSettings))); |
|||
} |
|||
|
|||
@Override |
|||
public UserSettings findById(TenantId tenantId, UserId userId) { |
|||
return DaoUtil.getData(userSettingsRepository.findById(userId.getId())); |
|||
} |
|||
|
|||
@Override |
|||
public void removeById(TenantId tenantId, UserId userId) { |
|||
userSettingsRepository.deleteById(userId.getId()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.user; |
|||
|
|||
import org.springframework.data.jpa.repository.JpaRepository; |
|||
import org.thingsboard.server.dao.model.sql.UserSettingsEntity; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface UserSettingsRepository extends JpaRepository<UserSettingsEntity, UUID> { |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.CaffeineTbTransactionalCache; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.asset.AssetCacheKey; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) |
|||
@Service("UserSettingsCache") |
|||
public class UserSettingsCaffeineCache extends CaffeineTbTransactionalCache<UserId, UserSettings> { |
|||
|
|||
public UserSettingsCaffeineCache(CacheManager cacheManager) { |
|||
super(cacheManager, CacheConstants.USER_SETTINGS_CACHE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
|
|||
public interface UserSettingsDao { |
|||
|
|||
UserSettings save(TenantId tenantId, UserSettings userSettings); |
|||
|
|||
UserSettings findById(TenantId tenantId, UserId userId); |
|||
|
|||
void removeById(TenantId tenantId, UserId userId); |
|||
|
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
|
|||
@Data |
|||
public class UserSettingsEvictEvent { |
|||
private final UserId userId; |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.CacheSpecsMap; |
|||
import org.thingsboard.server.cache.RedisTbTransactionalCache; |
|||
import org.thingsboard.server.cache.TBRedisCacheConfiguration; |
|||
import org.thingsboard.server.cache.TbFSTRedisSerializer; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.asset.AssetCacheKey; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") |
|||
@Service("UserSettingsCache") |
|||
public class UserSettingsRedisCache extends RedisTbTransactionalCache<UserId, UserSettings> { |
|||
|
|||
public UserSettingsRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { |
|||
super(CacheConstants.USER_SETTINGS_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbFSTRedisSerializer<>()); |
|||
} |
|||
} |
|||
@ -0,0 +1,131 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.user; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import com.jayway.jsonpath.DocumentContext; |
|||
import com.jayway.jsonpath.JsonPath; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.event.TransactionalEventListener; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.entity.AbstractCachedService; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Iterator; |
|||
import java.util.List; |
|||
|
|||
import static org.thingsboard.server.dao.service.Validator.validateId; |
|||
|
|||
@Service("UserSettingsDaoService") |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class UserSettingsServiceImpl extends AbstractCachedService<UserId, UserSettings, UserSettingsEvictEvent> implements UserSettingsService { |
|||
public static final String INCORRECT_USER_ID = "Incorrect userId "; |
|||
private final UserSettingsDao userSettingsDao; |
|||
|
|||
@Override |
|||
public UserSettings saveUserSettings(TenantId tenantId, UserSettings userSettings) { |
|||
log.trace("Executing saveUserSettings for user [{}], [{}]", userSettings.getUserId(), userSettings); |
|||
validateId(userSettings.getUserId(), INCORRECT_USER_ID + userSettings.getUserId()); |
|||
try { |
|||
UserSettings saved = userSettingsDao.save(tenantId, userSettings); |
|||
publishEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
return saved; |
|||
} catch (Exception t) { |
|||
handleEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
throw t; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public UserSettings updateUserSettings(TenantId tenantId, UserSettings userSettings) { |
|||
log.trace("Executing updateUserSettings for user [{}], [{}]", userSettings.getUserId(), userSettings); |
|||
validateId(userSettings.getUserId(), INCORRECT_USER_ID + userSettings.getUserId()); |
|||
UserSettings oldSettings = userSettingsDao.findById(tenantId, userSettings.getUserId()); |
|||
userSettings.setSettings(merge(oldSettings.getSettings(), userSettings.getSettings())); |
|||
|
|||
try { |
|||
UserSettings saved = userSettingsDao.save(tenantId, userSettings); |
|||
publishEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
return saved; |
|||
} catch (Exception t) { |
|||
handleEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
throw t; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public UserSettings findUserSettings(TenantId tenantId, UserId userId) { |
|||
log.trace("Executing findUserSettings for user [{}]", userId); |
|||
validateId(userId, INCORRECT_USER_ID + userId); |
|||
|
|||
return cache.getAndPutInTransaction(userId, |
|||
() -> userSettingsDao.findById(tenantId, userId), true); |
|||
} |
|||
|
|||
@Override |
|||
public void deleteUserSettings(TenantId tenantId, UserId userId, List<String> jsonPaths) { |
|||
log.trace("Executing deleteUserSettings for user [{}]", userId); |
|||
validateId(userId, INCORRECT_USER_ID + userId); |
|||
UserSettings userSettings = userSettingsDao.findById(tenantId, userId); |
|||
DocumentContext docSettings = JsonPath.parse(userSettings.getSettings().toString()); |
|||
try { |
|||
for (String s : jsonPaths) { |
|||
docSettings = docSettings.delete("$." + s); |
|||
} |
|||
userSettings.setSettings(new ObjectMapper().readValue(docSettings.jsonString(), ObjectNode.class)); |
|||
userSettingsDao.save(tenantId, userSettings); |
|||
publishEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
} catch (Exception t) { |
|||
handleEvictEvent(new UserSettingsEvictEvent(userSettings.getUserId())); |
|||
throw new RuntimeException(t); |
|||
} |
|||
} |
|||
|
|||
@TransactionalEventListener(classes = UserSettingsEvictEvent.class) |
|||
@Override |
|||
public void handleEvictEvent(UserSettingsEvictEvent event) { |
|||
List<UserId> keys = new ArrayList<>(); |
|||
keys.add(event.getUserId()); |
|||
cache.evict(keys); |
|||
} |
|||
|
|||
public JsonNode merge(JsonNode mainNode, JsonNode updateNode) { |
|||
|
|||
Iterator<String> fieldNames = updateNode.fieldNames(); |
|||
while (fieldNames.hasNext()) { |
|||
String fieldName = fieldNames.next(); |
|||
JsonNode jsonNode = mainNode.get(fieldName); |
|||
if (jsonNode != null && jsonNode.isObject()) { |
|||
merge(jsonNode, updateNode.get(fieldName)); |
|||
} |
|||
else { |
|||
if (mainNode instanceof ObjectNode) { |
|||
JsonNode value = updateNode.get(fieldName); |
|||
((ObjectNode) mainNode).set(fieldName, value); |
|||
} |
|||
} |
|||
} |
|||
return mainNode; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,101 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.sql.user; |
|||
|
|||
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|||
import org.junit.After; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
import org.thingsboard.server.common.data.security.Authority; |
|||
import org.thingsboard.server.common.data.security.UserSettings; |
|||
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
|||
import org.thingsboard.server.dao.service.AbstractServiceTest; |
|||
import org.thingsboard.server.dao.user.UserDao; |
|||
import org.thingsboard.server.dao.user.UserSettingsDao; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNull; |
|||
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
|||
import static org.thingsboard.server.dao.service.AbstractServiceTest.SYSTEM_TENANT_ID; |
|||
|
|||
public class JpaUserSettingsDaoTest extends AbstractJpaDaoTest { |
|||
|
|||
private UUID tenantId; |
|||
private User user; |
|||
|
|||
@Autowired |
|||
private UserSettingsDao userSettingsDao; |
|||
|
|||
@Autowired |
|||
private UserDao userDao; |
|||
|
|||
@Before |
|||
public void setUp() { |
|||
tenantId = Uuids.timeBased(); |
|||
user = saveUser(tenantId, Uuids.timeBased()); |
|||
} |
|||
|
|||
@After |
|||
public void tearDown() { |
|||
userDao.removeById(user.getTenantId(), user.getUuidId()); |
|||
} |
|||
|
|||
@Test |
|||
public void testFindSettingsByUserId() { |
|||
UserSettings userSettings = createUserSettings(user.getId()); |
|||
|
|||
UserSettings retrievedUserSettings = userSettingsDao.findById(SYSTEM_TENANT_ID, user.getId()); |
|||
assertEquals(retrievedUserSettings.getSettings(), userSettings.getSettings()); |
|||
|
|||
userSettingsDao.removeById(SYSTEM_TENANT_ID, user.getId()); |
|||
|
|||
UserSettings retrievedUserSettings2 = userSettingsDao.findById(SYSTEM_TENANT_ID, user.getId()); |
|||
assertNull(retrievedUserSettings2); |
|||
} |
|||
|
|||
private UserSettings createUserSettings(UserId userId) { |
|||
UserSettings userSettings = new UserSettings(); |
|||
userSettings.setSettings(JacksonUtil.newObjectNode().put("text", RandomStringUtils.randomAlphanumeric(10))); |
|||
userSettings.setUserId(userId); |
|||
return userSettingsDao.save(SYSTEM_TENANT_ID, userSettings); |
|||
} |
|||
|
|||
private User saveUser(UUID tenantId, UUID customerId) { |
|||
User user = new User(); |
|||
UUID id = Uuids.timeBased(); |
|||
user.setId(new UserId(id)); |
|||
user.setTenantId(TenantId.fromUUID(tenantId)); |
|||
user.setCustomerId(new CustomerId(customerId)); |
|||
if (customerId == NULL_UUID) { |
|||
user.setAuthority(Authority.TENANT_ADMIN); |
|||
} else { |
|||
user.setAuthority(Authority.CUSTOMER_USER); |
|||
} |
|||
String idString = id.toString(); |
|||
String email = idString.substring(0, idString.indexOf('-')) + "@thingsboard.org"; |
|||
user.setEmail(email); |
|||
return userDao.save(AbstractServiceTest.SYSTEM_TENANT_ID, user); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue