13 changed files with 529 additions and 1 deletions
@ -0,0 +1,46 @@ |
|||||
|
/** |
||||
|
* 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.databind.JsonNode; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
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.Serializable; |
||||
|
|
||||
|
@ApiModel |
||||
|
@Data |
||||
|
@EqualsAndHashCode |
||||
|
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 = 10000) |
||||
|
private transient JsonNode settings; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,78 @@ |
|||||
|
/** |
||||
|
* 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 io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.id.UserAuthSettingsId; |
||||
|
import org.thingsboard.server.common.data.id.UserId; |
||||
|
import org.thingsboard.server.common.data.security.UserAuthSettings; |
||||
|
import org.thingsboard.server.common.data.security.UserSettings; |
||||
|
import org.thingsboard.server.common.data.security.model.mfa.account.AccountTwoFaSettings; |
||||
|
import org.thingsboard.server.common.data.validation.Length; |
||||
|
import org.thingsboard.server.common.data.validation.NoXss; |
||||
|
import org.thingsboard.server.dao.model.BaseEntity; |
||||
|
import org.thingsboard.server.dao.model.BaseSqlEntity; |
||||
|
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; |
||||
|
|
||||
|
@EqualsAndHashCode |
||||
|
@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,65 @@ |
|||||
|
/** |
||||
|
* 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.data.jpa.repository.JpaRepository; |
||||
|
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.UserCredentials; |
||||
|
import org.thingsboard.server.common.data.security.UserSettings; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.model.sql.EntityAlarmEntity; |
||||
|
import org.thingsboard.server.dao.model.sql.RelationEntity; |
||||
|
import org.thingsboard.server.dao.model.sql.UserCredentialsEntity; |
||||
|
import org.thingsboard.server.dao.model.sql.UserSettingsEntity; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; |
||||
|
import org.thingsboard.server.dao.user.UserCredentialsDao; |
||||
|
import org.thingsboard.server.dao.user.UserSettingsDao; |
||||
|
import org.thingsboard.server.dao.util.SqlDao; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/22/2017. |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@SqlDao |
||||
|
public class JpaUserSettingsDao extends JpaAbstractDaoListeningExecutorService implements UserSettingsDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private UserSettingsRepository userSettingsRepository; |
||||
|
|
||||
|
@Override |
||||
|
public UserSettings saveSettings(TenantId tenantId, UserSettings userSettings) { |
||||
|
return DaoUtil.getData(userSettingsRepository.save(new UserSettingsEntity(userSettings))); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserSettings findByUserId(TenantId tenantId, UserId userId) { |
||||
|
return DaoUtil.getData(userSettingsRepository.findById(userId.getId())); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void removeByUserId(TenantId tenantId, UserId userId) { |
||||
|
userSettingsRepository.deleteById(userId.getId()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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.sql.user; |
||||
|
|
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.thingsboard.server.common.data.security.UserSettings; |
||||
|
import org.thingsboard.server.dao.model.sql.UserCredentialsEntity; |
||||
|
import org.thingsboard.server.dao.model.sql.UserSettingsEntity; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/22/2017. |
||||
|
*/ |
||||
|
public interface UserSettingsRepository extends JpaRepository<UserSettingsEntity, UUID> { |
||||
|
|
||||
|
UserSettingsEntity save(UserSettings saveSettings); |
||||
|
|
||||
|
UserSettingsEntity findByUserId(UUID userId); |
||||
|
|
||||
|
UserSettingsEntity deleteByUserId(UUID userId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
/** |
||||
|
* 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.UserAuthSettings; |
||||
|
import org.thingsboard.server.common.data.security.UserSettings; |
||||
|
import org.thingsboard.server.dao.Dao; |
||||
|
|
||||
|
public interface UserSettingsDao { |
||||
|
|
||||
|
UserSettings saveSettings(TenantId tenantId, UserSettings userSettings); |
||||
|
|
||||
|
UserSettings findByUserId(TenantId tenantId, UserId userId); |
||||
|
|
||||
|
void removeByUserId(TenantId tenantId, UserId userId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,105 @@ |
|||||
|
/** |
||||
|
* 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.assertNotNull; |
||||
|
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; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/22/2017. |
||||
|
*/ |
||||
|
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 testFindSettingsByTenantAdminUserId() { |
||||
|
UserSettings userSettings = createUserSettings(user.getId()); |
||||
|
|
||||
|
UserSettings retrievedUserSettings = userSettingsDao.findByUserId(SYSTEM_TENANT_ID, user.getId()); |
||||
|
assertEquals(retrievedUserSettings.getSettings(), userSettings.getSettings()); |
||||
|
|
||||
|
userSettingsDao.removeByUserId(SYSTEM_TENANT_ID, user.getId()); |
||||
|
|
||||
|
UserSettings retrievedUserSettings2 = userSettingsDao.findByUserId(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.saveSettings(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