From 86beb111d4a5496f9ab2799733421ea75822d1a6 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 13 Jun 2024 18:56:04 +0200 Subject: rest: Add /client/move Add URI to allow a POST request to move clients to a new room/folder. Request POST /client/move: { "clients": [ "192.168.56.11" ], "room": 10, "folder_id": 0, } If folder_id is zero then that means computer is not stored in a folder. --- src/rest.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rest.h | 1 + 2 files changed, 81 insertions(+) diff --git a/src/rest.c b/src/rest.c index 3d881a8..530d667 100644 --- a/src/rest.c +++ b/src/rest.c @@ -2701,6 +2701,71 @@ static int og_cmd_post_client_delete(json_t *element, return 0; } +static int og_cmd_post_client_move(json_t *element, + struct og_msg_params *params) +{ + struct og_computer computer = {}; + const char *key, *msglog; + struct og_dbi *dbi; + dbi_result result; + unsigned int i; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) { + err = og_json_parse_clients(value, params); + } else if (!strcmp(key, "room")) { + err = og_json_parse_uint(value, &computer.room); + } else if (!strcmp(key, "folder_id")) { + err = og_json_parse_uint(value, &computer.folder_id); + if (!err) + computer.folder_id = computer.folder_id & 0x0000ffff; + } + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + for (i = 0; i < params->ips_array_len; i++) { + result = dbi_conn_queryf(dbi->conn, + "UPDATE ordenadores SET " + "idaula=%u, grupoid=%u WHERE ip='%s'", + computer.room, computer.folder_id, + params->ips_array[i]); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + if (dbi_result_get_numrows_affected(result) < 1) { + syslog(LOG_ERR, "client move did not modify any row (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + } + + og_dbi_close(dbi); + return 0; +} + static int og_cmd_get_room_info(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -7559,6 +7624,7 @@ struct { [OG_URI_CLIENT_ADD] = { "client/add", }, [OG_URI_CLIENT_UPDATE] = { "client/update", }, [OG_URI_CLIENT_DELETE] = { "client/delete", }, + [OG_URI_CLIENT_MOVE] = { "client/move", }, [OG_URI_WOL] = { "wol", }, [OG_URI_SHELL_RUN] = { "shell/run", }, [OG_URI_SHELL_OUTPUT] = { "shell/output", }, @@ -7835,6 +7901,20 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_post_client_delete(root, ¶ms); + } else if (!strncmp(cmd, "client/move", strlen("client/move"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command client move with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_post_client_move(root, ¶ms); } else if (!strncmp(cmd, "folder/add", strlen("folder/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); diff --git a/src/rest.h b/src/rest.h index 86cb763..4880c6a 100644 --- a/src/rest.h +++ b/src/rest.h @@ -96,6 +96,7 @@ enum og_rest_uri { OG_URI_CLIENT_ADD, OG_URI_CLIENT_UPDATE, OG_URI_CLIENT_DELETE, + OG_URI_CLIENT_MOVE, OG_URI_WOL, OG_URI_SHELL_RUN, OG_URI_SHELL_OUTPUT, -- cgit v1.2.3-18-g5258