summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJavier Hernandez <jhernandez@soleta.eu>2024-01-10 11:47:51 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-01-10 13:06:47 +0100
commitbc787a9a68de88cd534f8cb8541ecec0861a2053 (patch)
tree36e05ddc7af31bc7889d4ea2c89cb5bca2874729 /src
parent1fb425694c6d9fbf5b9c0ad7282a68fa588fa506 (diff)
rest: Add support for deleting folders
Add support for deleting folders containing rooms. Delete rooms that are in folder. Computers inside rooms are automatically deleted in cascade when room is deleted (implemented in sql) Add support for deleting folders containing clients. Delete clients that are in folder.
Diffstat (limited to 'src')
-rw-r--r--src/rest.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index 51121fc..bf2e1e5 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -1995,6 +1995,123 @@ static int og_cmd_post_folder_add(json_t *element,
return err;
}
+static int og_delete_computer_folder(struct og_dbi *dbi, uint32_t folder_id)
+{
+ const char *msglog;
+ dbi_result result;
+
+ folder_id = folder_id & 0x0000ffff;
+
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM gruposordenadores "
+ "WHERE idgrupo=%u",
+ folder_id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to delete computer-group from database "
+ "(%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+ if (dbi_result_get_numrows_affected(result) < 1) {
+ syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM ordenadores "
+ "WHERE grupoid=%u",
+ folder_id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to delete computers from database "
+ "(%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_delete_room_folder(struct og_dbi *dbi, uint32_t folder_id)
+{
+ const char *msglog;
+ dbi_result result;
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM grupos "
+ "WHERE idgrupo=%u",
+ folder_id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to delete room-group from database "
+ "(%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+ if (dbi_result_get_numrows_affected(result) < 1) {
+ syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ return -1;
+ }
+ dbi_result_free(result);
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM aulas "
+ "WHERE grupoid=%u",
+ folder_id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to delete rooms from database "
+ "(%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_cmd_post_folder_delete(json_t *element,
+ struct og_msg_params *params)
+{
+ struct og_dbi *dbi;
+ uint32_t folder_id;
+ const char *key;
+ json_t *value;
+ int err = 0;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "id")) {
+ err = og_json_parse_uint(value, &folder_id);
+ params->flags |= OG_REST_PARAM_ID;
+ }
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_ID))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ if (folder_id & 0x00010000) {
+ err = og_delete_computer_folder(dbi, folder_id);
+ } else {
+ err = og_delete_room_folder(dbi, folder_id);
+ }
+
+ og_dbi_close(dbi);
+ return err;
+}
+
static int og_cmd_post_client_add(json_t *element,
struct og_msg_params *params,
char *buffer_reply)
@@ -7005,6 +7122,21 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_post_folder_add(root, &params);
+ } else if (!strncmp(cmd, "folder/delete", strlen("folder/delete"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command folder delete with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+
+ err = og_cmd_post_folder_delete(root, &params);
+
} else if (!strncmp(cmd, "wol", strlen("wol"))) {
if (method != OG_METHOD_POST) {
err = og_client_method_not_found(cli);