summaryrefslogtreecommitdiffstats
path: root/src/scope.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/scope.c')
-rw-r--r--src/scope.c427
1 files changed, 427 insertions, 0 deletions
diff --git a/src/scope.c b/src/scope.c
new file mode 100644
index 0000000..3f8e172
--- /dev/null
+++ b/src/scope.c
@@ -0,0 +1,427 @@
+/*
+ * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Affero General Public License as published by the
+ * Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <syslog.h>
+#include "dbi.h"
+#include "utils.h"
+#include "list.h"
+#include "rest.h"
+
+#define OG_QUERY_MAXLEN 4096
+
+static int og_dbi_scope_get_computer(const struct og_dbi *dbi, json_t *array,
+ const char* query)
+{
+ const char *computer_name, *computer_ip;
+ uint32_t computer_id;
+ const char *msglog;
+ dbi_result result;
+ json_t *computer;
+
+ result = dbi_conn_queryf(dbi->conn, query);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ computer_id = dbi_result_get_uint(result, "idordenador");
+ computer_name = dbi_result_get_string(result, "nombreordenador");
+ computer_ip = dbi_result_get_string(result, "ip");
+
+ computer = json_object();
+ if (!computer) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ json_object_set_new(computer, "name", json_string(computer_name));
+ json_object_set_new(computer, "type", json_string("computer"));
+ json_object_set_new(computer, "id", json_integer(computer_id));
+ json_object_set_new(computer, "scope", json_array());
+ json_object_set_new(computer, "ip", json_string(computer_ip));
+ json_array_append(array, computer);
+ json_decref(computer);
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_dbi_scope_get_computer_from_room(const struct og_dbi *dbi,
+ json_t *array, char *query,
+ const uint32_t room_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idordenador, nombreordenador, ip "
+ "FROM ordenadores WHERE idaula=%d and grupoid=0",
+ room_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_computer(dbi, array, query);
+}
+
+static int og_dbi_scope_get_computer_from_computers(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t computers_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idordenador, nombreordenador, ip "
+ "FROM ordenadores WHERE grupoid=%d",
+ computers_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_computer(dbi, array, query);
+}
+
+#define OG_COMPUTER_FOLDER_MARKER 0x00010000
+
+static int og_dbi_scope_get_computers_from_computers(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t group_id);
+
+static int og_dbi_scope_get_computers(const struct og_dbi *dbi, json_t *array,
+ char *query, bool in_room)
+{
+ const char *msglog, *computers_name;
+ json_t *computers, *scope_array;
+ uint32_t computers_id;
+ uint32_t marker = 0;
+ dbi_result result;
+
+ if (in_room)
+ marker = OG_COMPUTER_FOLDER_MARKER;
+
+ result = dbi_conn_queryf(dbi->conn, query);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ computers_id = dbi_result_get_uint(result, "idgrupo");
+ computers_name = dbi_result_get_string(result,
+ "nombregrupoordenador");
+
+ computers = json_object();
+ if (!computers) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ json_object_set_new(computers, "name",
+ json_string(computers_name));
+ json_object_set_new(computers, "type", json_string("folder"));
+ json_object_set_new(computers, "id",
+ json_integer(computers_id | marker));
+ json_object_set_new(computers, "scope", json_array());
+ json_array_append(array, computers);
+ json_decref(computers);
+
+ scope_array = json_object_get(computers, "scope");
+ if (!scope_array) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_computers_from_computers(dbi,
+ scope_array,
+ query,
+ computers_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_computer_from_computers(dbi,
+ scope_array,
+ query,
+ computers_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_dbi_scope_get_computers_from_room(const struct og_dbi *dbi,
+ json_t *array, char *query,
+ const uint32_t room_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idgrupo, nombregrupoordenador "
+ "FROM gruposordenadores "
+ "WHERE idaula=%d AND grupoid=0",
+ room_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_computers(dbi, array, query, true);
+}
+
+static int og_dbi_scope_get_computers_from_computers(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t group_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idgrupo, nombregrupoordenador "
+ "FROM gruposordenadores WHERE grupoid=%d",
+ group_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_computers(dbi, array, query, false);
+}
+
+static int og_dbi_scope_get_room(const struct og_dbi *dbi, json_t *array,
+ char *query)
+{
+ const char *msglog, *room_name;
+ json_t *room, *scope_array;
+ dbi_result result;
+ uint32_t room_id;
+
+ result = dbi_conn_queryf(dbi->conn, query);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ room_id = dbi_result_get_uint(result, "idaula");
+ room_name = dbi_result_get_string(result, "nombreaula");
+
+ room = json_object();
+ if (!room) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ json_object_set_new(room, "name", json_string(room_name));
+ json_object_set_new(room, "type", json_string("room"));
+ json_object_set_new(room, "id", json_integer(room_id));
+ json_object_set_new(room, "scope", json_array());
+ json_array_append(array, room);
+ json_decref(room);
+
+ scope_array = json_object_get(room, "scope");
+ if (!scope_array) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_computers_from_room(dbi, scope_array,
+ query, room_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_computer_from_room(dbi, scope_array,
+ query, room_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_dbi_scope_get_room_from_group(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t group_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idaula, nombreaula "
+ "FROM aulas WHERE grupoid=%d", group_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_room(dbi, array, query);
+}
+
+static int og_dbi_scope_get_room_from_center(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t center_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idaula, nombreaula "
+ "FROM aulas WHERE idcentro=%d AND grupoid=0",
+ center_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_room(dbi, array, query);
+}
+
+static int og_dbi_scope_get_group_from_group(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t group_id);
+
+static int og_dbi_scope_get_group(const struct og_dbi *dbi,
+ json_t *array,
+ char *query)
+{
+ const char *msglog, *group_name;
+ json_t *group, *scope_array;
+ dbi_result result;
+ uint32_t group_id;
+
+ result = dbi_conn_queryf(dbi->conn, query);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ group_id = dbi_result_get_uint(result, "idgrupo");
+ group_name = dbi_result_get_string(result, "nombregrupo");
+
+ group = json_object();
+ if (!group) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ json_object_set_new(group, "name", json_string(group_name));
+ json_object_set_new(group, "type", json_string("folder"));
+ json_object_set_new(group, "id", json_integer(group_id));
+ json_object_set_new(group, "scope", json_array());
+ json_array_append(array, group);
+ json_decref(group);
+
+ scope_array = json_object_get(group, "scope");
+ if (!scope_array) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_group_from_group(dbi, scope_array, query,
+ group_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_room_from_group(dbi, scope_array, query,
+ group_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+ }
+ dbi_result_free(result);
+
+ return 0;
+}
+
+static int og_dbi_scope_get_group_from_group(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t group_id)
+{
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idgrupo, nombregrupo "
+ "FROM grupos WHERE grupoid=%d", group_id);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_group(dbi, array, query);
+}
+
+static int og_dbi_scope_get_group_from_center(const struct og_dbi *dbi,
+ json_t *array,
+ char *query,
+ const uint32_t center_id)
+{
+ int group_type_room = 2;
+ int ret = snprintf(query, OG_QUERY_MAXLEN,
+ "SELECT idgrupo, nombregrupo "
+ "FROM grupos "
+ "WHERE idcentro=%d AND grupoid=0 AND tipo=%d",
+ center_id, group_type_room);
+ if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
+ return -1;
+
+ return og_dbi_scope_get_group(dbi, array, query);
+}
+
+int og_dbi_scope_get(struct og_dbi *dbi, json_t *array)
+{
+ const char *msglog, *center_name;
+ json_t *center, *scope_array;
+ char query[OG_QUERY_MAXLEN];
+ uint32_t center_id;
+ dbi_result result;
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT nombrecentro, idcentro FROM centros");
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ center_id = dbi_result_get_uint(result, "idcentro");
+ center_name = dbi_result_get_string(result, "nombrecentro");
+
+ center = json_object();
+ if (!center) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ scope_array = json_array();
+ if (!scope_array) {
+ dbi_result_free(result);
+ json_decref(center);
+ return -1;
+ }
+
+ json_object_set_new(center, "name", json_string(center_name));
+ json_object_set_new(center, "type", json_string("center"));
+ json_object_set_new(center, "id", json_integer(center_id));
+ json_object_set_new(center, "scope", scope_array);
+ json_array_append(array, center);
+ json_decref(center);
+
+ if (og_dbi_scope_get_group_from_center(dbi, scope_array, query,
+ center_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+
+ if (og_dbi_scope_get_room_from_center(dbi, scope_array, query,
+ center_id)) {
+ dbi_result_free(result);
+ return -1;
+ }
+ }
+
+ dbi_result_free(result);
+
+ return 0;
+}