diff options
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 80 |
1 files changed, 80 insertions, 0 deletions
@@ -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); |