18 changed files with 433 additions and 102 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,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,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,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.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); |
|||
ObjectNode settings = (ObjectNode) userSettings.getSettings(); |
|||
|
|||
try { |
|||
for (String s : jsonPaths) { |
|||
settings = new ObjectMapper().readValue(JsonPath.parse(settings.toString()).delete("$." + s).jsonString(), ObjectNode.class); |
|||
userSettings.setSettings(settings); |
|||
} |
|||
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; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue