13 changed files with 276 additions and 2 deletions
@ -0,0 +1,80 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.controller; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.security.access.prepost.PreAuthorize; |
||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.config.annotations.ApiOperation; |
||||
|
import org.thingsboard.server.dao.trendz.TrendzSettingsService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.security.model.SecurityUser; |
||||
|
import org.thingsboard.server.service.security.permission.Operation; |
||||
|
import org.thingsboard.server.service.security.permission.Resource; |
||||
|
|
||||
|
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_END; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_START; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.NEW_LINE; |
||||
|
import static org.thingsboard.server.controller.ControllerConstants.SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH; |
||||
|
|
||||
|
@RestController |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
@RequestMapping("/api") |
||||
|
public class TrendzController extends BaseController { |
||||
|
|
||||
|
private final TrendzSettingsService trendzSettingsService; |
||||
|
|
||||
|
@ApiOperation(value = "Save Trendz settings (saveTrendzSettings)", |
||||
|
notes = "Saves Trendz settings for this tenant or sysadmin.\n" + NEW_LINE + |
||||
|
"Here is an example of the Trendz settings:\n" + |
||||
|
MARKDOWN_CODE_BLOCK_START + |
||||
|
"{\n" + |
||||
|
" \"enabled\": true,\n" + |
||||
|
" \"trendzUrl\": \"https://some.domain.com:18888/also_necessary_prefix\"\n" + |
||||
|
"}" + |
||||
|
MARKDOWN_CODE_BLOCK_END + |
||||
|
SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH) |
||||
|
@PostMapping("/trendz/settings") |
||||
|
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
||||
|
public TrendzSettings saveTrendzSettings(@RequestBody TrendzSettings trendzSettings, |
||||
|
@AuthenticationPrincipal SecurityUser user) throws ThingsboardException { |
||||
|
accessControlService.checkPermission(user, Resource.TRENDZ_SETTINGS, Operation.WRITE); |
||||
|
TenantId tenantId = user.isSystemAdmin() ? TenantId.SYS_TENANT_ID : user.getTenantId(); |
||||
|
trendzSettingsService.saveTrendzSettings(tenantId, trendzSettings); |
||||
|
return trendzSettings; |
||||
|
} |
||||
|
|
||||
|
@ApiOperation(value = "Get Trendz Settings (getTrendzSettings)", |
||||
|
notes = "Retrieves trendz settings for this tenant or sysadmin." + |
||||
|
SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH) |
||||
|
@GetMapping("/trendz/settings") |
||||
|
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
public TrendzSettings getTrendzSettings(@AuthenticationPrincipal SecurityUser user) throws ThingsboardException { |
||||
|
accessControlService.checkPermission(user, Resource.TRENDZ_SETTINGS, Operation.READ); |
||||
|
TenantId tenantId = user.isSystemAdmin() ? TenantId.SYS_TENANT_ID : user.getTenantId(); |
||||
|
return trendzSettingsService.findTrendzSettings(tenantId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.controller; |
||||
|
|
||||
|
import org.junit.Test; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||
|
|
||||
|
@DaoSqlTest |
||||
|
public class TrendzControllerTest extends AbstractControllerTest { |
||||
|
|
||||
|
private final String trendzUrl = "https://some.domain.com:18888/also_necessary_prefix"; |
||||
|
|
||||
|
@Test |
||||
|
public void testTrendzSettingsWhenTenant() throws Exception { |
||||
|
loginTenantAdmin(); |
||||
|
|
||||
|
TrendzSettings trendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
|
||||
|
assertThat(trendzSettings).isNotNull(); |
||||
|
assertThat(trendzSettings.isEnabled()).isFalse(); |
||||
|
assertThat(trendzSettings.getTrendzUrl()).isNull(); |
||||
|
|
||||
|
trendzSettings.setEnabled(true); |
||||
|
trendzSettings.setTrendzUrl(trendzUrl); |
||||
|
|
||||
|
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk()); |
||||
|
|
||||
|
TrendzSettings updatedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
assertThat(updatedTrendzSettings).isEqualTo(trendzSettings); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testTrendzSettingsWhenCustomer() throws Exception { |
||||
|
loginCustomerUser(); |
||||
|
|
||||
|
TrendzSettings trendzSettings = new TrendzSettings(); |
||||
|
trendzSettings.setEnabled(true); |
||||
|
trendzSettings.setTrendzUrl("https://some.domain.com:18888/customer_trendz"); |
||||
|
|
||||
|
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isForbidden()); |
||||
|
|
||||
|
TrendzSettings fetchedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
||||
|
assertThat(fetchedTrendzSettings).isNotNull(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.trendz; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
|
||||
|
public interface TrendzSettingsService { |
||||
|
|
||||
|
void saveTrendzSettings(TenantId tenantId, TrendzSettings settings); |
||||
|
|
||||
|
TrendzSettings findTrendzSettings(TenantId tenantId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.trendz; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class TrendzSettings { |
||||
|
|
||||
|
private boolean enabled; |
||||
|
private String trendzUrl; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 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.trendz; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.cache.annotation.CacheEvict; |
||||
|
import org.springframework.cache.annotation.Cacheable; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.AdminSettings; |
||||
|
import org.thingsboard.server.common.data.CacheConstants; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
||||
|
import org.thingsboard.server.dao.settings.AdminSettingsService; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
public class DefaultTrendzSettingsService implements TrendzSettingsService { |
||||
|
|
||||
|
private final AdminSettingsService adminSettingsService; |
||||
|
|
||||
|
private static final String SETTINGS_KEY = "trendz"; |
||||
|
|
||||
|
@CacheEvict(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
||||
|
@Override |
||||
|
public void saveTrendzSettings(TenantId tenantId, TrendzSettings settings) { |
||||
|
AdminSettings adminSettings = Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
||||
|
.orElseGet(() -> { |
||||
|
AdminSettings newAdminSettings = new AdminSettings(); |
||||
|
newAdminSettings.setTenantId(tenantId); |
||||
|
newAdminSettings.setKey(SETTINGS_KEY); |
||||
|
return newAdminSettings; |
||||
|
}); |
||||
|
adminSettings.setJsonValue(JacksonUtil.valueToTree(settings)); |
||||
|
adminSettingsService.saveAdminSettings(tenantId, adminSettings); |
||||
|
} |
||||
|
|
||||
|
@Cacheable(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
||||
|
@Override |
||||
|
public TrendzSettings findTrendzSettings(TenantId tenantId) { |
||||
|
return Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
||||
|
.map(adminSettings -> JacksonUtil.treeToValue(adminSettings.getJsonValue(), TrendzSettings.class)) |
||||
|
.orElseGet(TrendzSettings::new); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue