diff options
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 184 |
1 files changed, 182 insertions, 2 deletions
@@ -82,6 +82,7 @@ struct ev_loop *og_loop; #define OG_REST_PARAM_BACKUP (1UL << 44) #define OG_REST_PARAM_ROOM (1UL << 45) #define OG_REST_PARAM_GATEWAY (1UL << 46) +#define OG_REST_PARAM_FOLDER (1UL << 47) static LIST_HEAD(client_list); static LIST_HEAD(client_wol_list); @@ -2038,6 +2039,171 @@ static int add_computer_folder(struct og_dbi *dbi, struct og_folder *folder) return 0; } +static int og_computer_folder_update(struct og_dbi *dbi, struct og_folder *folder) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "SELECT idaula FROM gruposordenadores WHERE idgrupo='%u'", + folder->id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (!dbi_result_next_row(result)) { + syslog(LOG_ERR, "could not find a folder with that id: %u\n", + folder->id); + dbi_result_free(result); + return -1; + } + folder->room = dbi_result_get_uint(result, "idaula"); + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "SELECT idgrupo FROM gruposordenadores " + "WHERE nombregrupoordenador='%s' AND idaula=%u", + folder->name, folder->room); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "Folder with name %s already exists in room " + "with id %d\n", + folder->name, folder->room); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "UPDATE gruposordenadores" + " SET nombregrupoordenador='%s'" + " WHERE idgrupo='%u';", + folder->name, folder->id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to update room in database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + +static int og_room_folder_update(struct og_dbi *dbi, struct og_folder *folder) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "SELECT idcentro FROM grupos WHERE idgrupo='%u'", + folder->id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (!dbi_result_next_row(result)) { + syslog(LOG_ERR, "could not find a folder with that id: %u\n", + folder->id); + dbi_result_free(result); + return -1; + } + folder->center = dbi_result_get_uint(result, "idcentro"); + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "SELECT idgrupo FROM grupos " + "WHERE nombregrupo='%s' AND idcentro=%u", + folder->name, folder->center); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "Folder with name %s already exists in center " + "with id %d\n", + folder->name, folder->center); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "UPDATE grupos" + " SET nombregrupo='%s'" + " WHERE idgrupo='%u';", + folder->name, folder->id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to update room in database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + +#define OG_COMPUTER_FOLDER_MARKER 0x00010000 + +static int og_cmd_post_folder_update(json_t *element, + struct og_msg_params *params) +{ + struct og_folder folder = {}; + struct og_dbi *dbi; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "id")) { + params->flags |= OG_REST_PARAM_FOLDER; + err = og_json_parse_uint(value, &folder.id); + } else if (!strcmp(key, "name")) { + params->flags |= OG_REST_PARAM_NAME; + err = og_json_parse_string_copy(value, + folder.name, + sizeof(folder.name)); + } + + if (err < 0) + return err; + } + if (!og_msg_params_validate(params, OG_REST_PARAM_NAME)) + 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 & OG_COMPUTER_FOLDER_MARKER) { + folder.id = folder.id & 0x0000ffff; + err = og_computer_folder_update(dbi, &folder); + } else { + err = og_room_folder_update(dbi, &folder); + } + og_dbi_close(dbi); + + return err; +} + static int og_cmd_post_folder_add(json_t *element, struct og_msg_params *params) { @@ -4324,8 +4490,6 @@ static int og_dbi_scope_get_computers_from_computers(const struct og_dbi *dbi, char *query, const uint32_t group_id); -#define OG_COMPUTER_FOLDER_MARKER 0x00010000 - static int og_dbi_scope_get_computers(const struct og_dbi *dbi, json_t *array, char *query, bool in_room) { @@ -7120,6 +7284,7 @@ struct { [OG_URI_SERVER] = { "server", }, [OG_URI_STATS] = { "stats", }, [OG_URI_FOLDER_ADD] = { "folder/add", }, + [OG_URI_FOLDER_UPDATE] = { "folder/update", }, [OG_URI_FOLDER_DELETE] = { "folder/delete", }, }; @@ -7361,6 +7526,21 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_cmd_post_folder_add(root, ¶ms); + } else if (!strncmp(cmd, "folder/update", strlen("folder/update"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command folder update with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_post_folder_update(root, ¶ms); + } else if (!strncmp(cmd, "folder/delete", strlen("folder/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); |