Ability to remove version control settings

This commit is contained in:
Igor Kulikov 2022-05-19 13:19:43 +03:00
parent 2eb94fd95e
commit 587066cfd3
12 changed files with 73 additions and 4 deletions

View File

@ -19,13 +19,16 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.thingsboard.rule.engine.api.MailService;
import org.thingsboard.rule.engine.api.SmsService;
import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.UpdateMessage;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.security.model.SecuritySettings;
import org.thingsboard.server.common.data.sms.config.TestSmsRequest;
@ -38,8 +41,8 @@ import org.thingsboard.server.service.security.system.SystemSecurityService;
import org.thingsboard.server.service.sync.vc.EntitiesVersionControlService;
import org.thingsboard.server.service.update.UpdateService;
import static org.thingsboard.server.controller.ControllerConstants.SYSTEM_AUTHORITY_PARAGRAPH;
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH;
import static org.thingsboard.server.controller.ControllerConstants.*;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_ID;
@RestController
@TbCoreComponent
@ -203,7 +206,7 @@ public class AdminController extends BaseController {
@ApiOperation(value = "Creates or Updates the version control settings (saveVersionControlSettings)",
notes = "Creates or Updates the version control settings object. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@PostMapping("/vsSettings")
@PostMapping("/vcSettings")
public void saveVersionControlSettings(@RequestBody EntitiesVersionControlSettings settings) throws ThingsboardException {
try {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.WRITE);
@ -213,6 +216,21 @@ public class AdminController extends BaseController {
}
}
@ApiOperation(value = "Delete version control settings (deleteVersionControlSettings)",
notes = "Deletes the version control settings."
+ TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/vcSettings", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteVersionControlSettings() throws ThingsboardException {
try {
accessControlService.checkPermission(getCurrentUser(), Resource.ADMIN_SETTINGS, Operation.DELETE);
versionControlService.deleteVersionControlSettings(getTenantId());
} catch (Exception e) {
throw handleException(e);
}
}
@ApiOperation(value = "Check version control access (checkVersionControlAccess)",
notes = "Attempts to check version control access. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")

View File

@ -312,6 +312,13 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
return savedVersionControlSettings;
}
@Override
public void deleteVersionControlSettings(TenantId tenantId) {
if (adminSettingsService.deleteAdminSettings(tenantId, SETTINGS_KEY)) {
gitService.clearRepository(tenantId);
}
}
@Override
public void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException {
EntitiesVersionControlSettings storedSettings = getVersionControlSettings(tenantId);

View File

@ -58,6 +58,8 @@ public interface EntitiesVersionControlService {
EntitiesVersionControlSettings saveVersionControlSettings(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings);
void deleteVersionControlSettings(TenantId tenantId);
void checkVersionControlAccess(TenantId tenantId, EntitiesVersionControlSettings settings) throws ThingsboardException;
}

View File

@ -111,6 +111,20 @@ public class LocalGitVersionControlService implements GitVersionControlService {
}
}
@Override
public void clearRepository(TenantId tenantId) {
var lock = getRepoLock(tenantId);
lock.lock();
try {
gitRepositoryService.clearRepository(tenantId);
} catch (Exception e) {
//TODO: analyze and return meaningful exceptions that we can show to the client;
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
@Override
public PendingCommit prepareCommit(TenantId tenantId, VersionCreateRequest request) {
var lock = getRepoLock(tenantId);

View File

@ -27,4 +27,6 @@ public interface AdminSettingsService {
AdminSettings saveAdminSettings(TenantId tenantId, AdminSettings adminSettings);
boolean deleteAdminSettings(TenantId tenantId, String key);
}

View File

@ -224,7 +224,8 @@ public class DefaultGitRepositoryService implements GitRepositoryService {
repositories.put(tenantId, repository);
}
private void clearRepository(TenantId tenantId) throws IOException {
@Override
public void clearRepository(TenantId tenantId) throws IOException {
GitRepository repository = repositories.get(tenantId);
if (repository != null) {
FileUtils.deleteDirectory(new File(repository.getDirectory()));

View File

@ -36,6 +36,8 @@ public interface GitRepositoryService {
void initRepository(TenantId tenantId, EntitiesVersionControlSettings settings) throws Exception;
void clearRepository(TenantId tenantId) throws IOException;
void add(PendingCommit commit, String relativePath, String entityDataJson) throws IOException;
void deleteFolderContent(PendingCommit commit, String relativePath) throws IOException;

View File

@ -34,6 +34,8 @@ public interface GitVersionControlService {
void initRepository(TenantId tenantId, EntitiesVersionControlSettings settings);
void clearRepository(TenantId tenantId);
PendingCommit prepareCommit(TenantId tenantId, VersionCreateRequest request);
void addToCommit(PendingCommit commit, EntityExportData<ExportableEntity<EntityId>> entityData);

View File

@ -39,4 +39,6 @@ public interface AdminSettingsDao extends Dao<AdminSettings> {
*/
AdminSettings findByTenantIdAndKey(UUID tenantId, String key);
boolean removeByTenantIdAndKey(UUID tenantId, String key);
}

View File

@ -62,4 +62,10 @@ public class AdminSettingsServiceImpl implements AdminSettingsService {
return adminSettingsDao.save(tenantId, adminSettings);
}
@Override
public boolean deleteAdminSettings(TenantId tenantId, String key) {
log.trace("Executing deleteAdminSettings, tenantId [{}], key [{}]", tenantId, key);
Validator.validateString(key, "Incorrect key " + key);
return adminSettingsDao.removeByTenantIdAndKey(tenantId.getId(), key);
}
}

View File

@ -27,4 +27,8 @@ public interface AdminSettingsRepository extends JpaRepository<AdminSettingsEnti
AdminSettingsEntity findByTenantIdAndKey(UUID tenantId, String key);
void deleteByTenantIdAndKey(UUID tenantId, String key);
boolean existsByTenantIdAndKey(UUID tenantId, String key);
}

View File

@ -49,4 +49,13 @@ public class JpaAdminSettingsDao extends JpaAbstractDao<AdminSettingsEntity, Adm
public AdminSettings findByTenantIdAndKey(UUID tenantId, String key) {
return DaoUtil.getData(adminSettingsRepository.findByTenantIdAndKey(tenantId, key));
}
@Override
public boolean removeByTenantIdAndKey(UUID tenantId, String key) {
if (adminSettingsRepository.existsByTenantIdAndKey(tenantId, key)) {
adminSettingsRepository.deleteByTenantIdAndKey(tenantId, key);
return true;
}
return false;
}
}