@DisableDelete WIP

This commit is contained in:
Francesco
2023-10-06 12:46:11 +02:00
parent 5fdf7e18e7
commit 7e7089ea72
9 changed files with 153 additions and 5 deletions

View File

@@ -0,0 +1,33 @@
/*
* Spring Boot Database Admin - An automatically generated CRUD admin UI for Spring Boot apps
* Copyright (C) 2023 Ailef (http://ailef.tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tech.ailef.dbadmin.external.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Disables create actions on the Entity class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DisableCreate {
}

View File

@@ -0,0 +1,33 @@
/*
* Spring Boot Database Admin - An automatically generated CRUD admin UI for Spring Boot apps
* Copyright (C) 2023 Ailef (http://ailef.tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tech.ailef.dbadmin.external.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Disables delete actions on the Entity class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DisableDelete {
}

View File

@@ -0,0 +1,33 @@
/*
* Spring Boot Database Admin - An automatically generated CRUD admin UI for Spring Boot apps
* Copyright (C) 2023 Ailef (http://ailef.tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tech.ailef.dbadmin.external.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Disables edit actions on the Entity class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface DisableEdit {
}

View File

@@ -309,6 +309,12 @@ public class DefaultDbAdminController {
public String delete(@PathVariable String className, @PathVariable String id, RedirectAttributes attr) {
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
if (!schema.isDeleteEnabled()) {
attr.addFlashAttribute("errorTitle", "Unable to DELETE row");
attr.addFlashAttribute("error", "DELETE operations have been disabled on this table.");
return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
}
try {
repository.delete(schema, id);
} catch (DataIntegrityViolationException e) {
@@ -332,6 +338,12 @@ public class DefaultDbAdminController {
public String delete(@PathVariable String className, @RequestParam String[] ids, RedirectAttributes attr) {
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
if (!schema.isDeleteEnabled()) {
attr.addFlashAttribute("errorTitle", "Unable to DELETE rows");
attr.addFlashAttribute("error", "DELETE operations have been disabled on this table.");
return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
}
int countDeleted = 0;
for (String id : ids) {
try {

View File

@@ -38,6 +38,9 @@ import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import tech.ailef.dbadmin.external.DbAdmin;
import tech.ailef.dbadmin.external.annotations.ComputedColumn;
import tech.ailef.dbadmin.external.annotations.DisableCreate;
import tech.ailef.dbadmin.external.annotations.DisableDelete;
import tech.ailef.dbadmin.external.annotations.DisableEdit;
import tech.ailef.dbadmin.external.annotations.HiddenColumn;
import tech.ailef.dbadmin.external.dto.MappingError;
import tech.ailef.dbadmin.external.exceptions.DbAdminException;
@@ -335,6 +338,18 @@ public class DbObjectSchema {
}).toList();
}
public boolean isDeleteEnabled() {
return entityClass.getAnnotation(DisableDelete.class) == null;
}
public boolean isEditEnabled() {
return entityClass.getAnnotation(DisableEdit.class) == null;
}
public boolean isCreateEnabled() {
return entityClass.getAnnotation(DisableCreate.class) == null;
}
/**
* Returns all the data in this schema, as `DbObject`s
* @return