diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-01 18:50:06 +0200 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-01 18:50:06 +0200 |
| commit | aec2553a8ff182353804fc498797dfcb8212bd92 (patch) | |
| tree | 8aa4a490d1d95214b22039d0ff8ea98b202c2091 /database/sqlite | |
| parent | 42a6c774802371482cc008caba1168ad274a8687 (diff) | |
| download | eurephia-aec2553a8ff182353804fc498797dfcb8212bd92.tar.gz eurephia-aec2553a8ff182353804fc498797dfcb8212bd92.tar.xz eurephia-aec2553a8ff182353804fc498797dfcb8212bd92.zip | |
Unified eDBadminGetUserCertsList(...) and eDBadminUpdateUserCertLink(...) into one function
Plus added better XML documentation for the new eDBadminUserCertsLink(...) function
Diffstat (limited to 'database/sqlite')
| -rw-r--r-- | database/sqlite/usercerts.c | 136 |
1 files changed, 97 insertions, 39 deletions
diff --git a/database/sqlite/usercerts.c b/database/sqlite/usercerts.c index 31b72af..a32932d 100644 --- a/database/sqlite/usercerts.c +++ b/database/sqlite/usercerts.c @@ -51,7 +51,7 @@ void xmlReplaceChars(xmlChar *str, char s, char r); -xmlDoc *eDBadminGetUserCertsList(eurephiaCTX *ctx, const char *sortkeys) { +xmlDoc *usercerts_search(eurephiaCTX *ctx, eDBfieldMap *where_m, const char *sortkeys) { xmlDoc *list_xml = NULL; xmlNode *link_root_n = NULL, *link_n = NULL, *tmp_n = NULL; dbresult *res = NULL; @@ -59,7 +59,7 @@ xmlDoc *eDBadminGetUserCertsList(eurephiaCTX *ctx, const char *sortkeys) { char *dbsort = NULL; int i; - DEBUG(ctx, 20, "Function call: eDBadminGetUserCertsList(ctx, '%s')", sortkeys); + DEBUG(ctx, 21, "Function call: usercerts_search(ctx, eDBfieldMap, '%s')", sortkeys); assert( ctx != NULL ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { @@ -72,25 +72,25 @@ xmlDoc *eDBadminGetUserCertsList(eurephiaCTX *ctx, const char *sortkeys) { dbsort = eDBmkSortKeyString(tbl_sqlite_usercerts, sortkeys); } - res = sqlite_query(ctx, - "SELECT uicid, ucs.uid AS uid, certid, ucs.registered AS registered," - " ucs.accessprofile AS accessprofile, access_descr," - " username, " - " common_name, organisation, email, digest, depth " - " FROM openvpn_usercerts ucs" - " LEFT JOIN openvpn_certificates USING(certid)" - " LEFT JOIN openvpn_accesses acc ON(ucs.accessprofile = acc.accessprofile)" - " LEFT JOIN openvpn_users u ON(u.uid = ucs.uid)%s%s", - (dbsort != NULL ? " ORDER BY ":""), - (dbsort != NULL ? dbsort : "")); - + res = sqlite_query_mapped(ctx, SQL_SELECT, + "SELECT uicid, ucs.uid AS uid, certid, ucs.registered AS registered," + " ucs.accessprofile AS accessprofile, access_descr," + " username, " + " common_name, organisation, email, digest, depth " + " FROM openvpn_usercerts ucs" + " LEFT JOIN openvpn_certificates USING(certid)" + " LEFT JOIN openvpn_accesses acc ON(ucs.accessprofile = acc.accessprofile)" + " LEFT JOIN openvpn_users u ON(u.uid = ucs.uid)", + NULL, // values (not used for SELECT) + where_m, // fields and values for the WHERE clause + dbsort); if( res == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not query the certificate table"); + eurephia_log(ctx, LOG_ERROR, 0, "Could not query the usercerts table"); return NULL; } memset(&tmp, 0, 2050); - eurephiaXML_CreateDoc(ctx, 1, "usercerts_links", &list_xml, &link_root_n); + eurephiaXML_CreateDoc(ctx, 1, "usercerts", &list_xml, &link_root_n); xmlStrPrintf(tmp, 64, (xmlChar *) "%i", sqlite_get_numtuples(res)); xmlNewProp(link_root_n, (xmlChar *) "link_count", (xmlChar *) tmp); @@ -127,14 +127,68 @@ xmlDoc *eDBadminGetUserCertsList(eurephiaCTX *ctx, const char *sortkeys) { } -int eDBadminUpdateUserCertLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { - dbresult *res = NULL; - xmlNode *usrcrt_n = NULL, *fmap_n = NULL; +xmlDoc *usercerts_update(eurephiaCTX *ctx, const char *mode, eDBfieldMap *usrcrt_m) { + xmlDoc *res = NULL; + dbresult *dbres = NULL; + + DEBUG(ctx, 21, "Function call: usercerts_update(ctx, xmlDoc)"); + assert( (ctx != NULL) && (usrcrt_m != NULL) ); + + if( strcmp(mode, "register") == 0 ) { + dbres = sqlite_query_mapped(ctx, SQL_INSERT, + "INSERT INTO openvpn_usercerts", usrcrt_m, NULL, NULL); + if( dbres ) { + res = eurephiaXML_ResultMsg(ctx, exmlRESULT, + "Registered new user-cert link with id %i", + dbres->last_insert_id); + } + } else if( strcmp(mode, "remove") == 0 ) { + dbres = sqlite_query_mapped(ctx, SQL_DELETE, + "DELETE FROM openvpn_usercerts", NULL, usrcrt_m, NULL); + if( dbres ) { + int num_rows = sqlite_get_affected_rows(dbres); + if( num_rows > 0 ) { + res = eurephiaXML_ResultMsg(ctx, exmlRESULT, + "Removed %i user-cert %s successfully", + num_rows, (num_rows == 1 ? "link" : "links")); + } else { + res = eurephiaXML_ResultMsg(ctx, exmlERROR, + "No user-cert links where removed"); + } + } + } + + if( dbres == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Failed to %s user-cert link.", mode); + res = eurephiaXML_ResultMsg(ctx, exmlERROR, "Failed to %s user-cert link", mode); + } else { + sqlite_free_results(dbres); + } + + return res; +} + + +// The XML document format: +// <eurephia format="1"> +// <usercerts mode="{search|register|remove|update}" [uicid="{uicid}"]> +// <fieldMapping table="usercerts"> +// <{field name}>{search value}</{field name}> +// </fieldMapping> +// [<sortfields>{field name}[, {field name},...]</sortfields>] <!-- Only for mode='search' --> +// </usercerts +// </eurehpia> +// +// It can be several field name tags to limit the search even more. +// If mode is 'update' the 'uicid' attribute must be present in the usercerts tag. +// +xmlDoc *eDBadminUserCertsLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { + xmlNode *usrcrt_n = NULL, *tmp_n = NULL; + xmlDoc *resxml = NULL; eDBfieldMap *usrcrt_m = NULL; - char *mode = NULL; - int rc = 0; + const char *mode = NULL, *sortfields = NULL, *uicid = NULL; - DEBUG(ctx, 20, "Function call: eDBadminUpdateUserCertLink(ctx, xmlDoc)"); + DEBUG(ctx, 20, "Function call: eDBadminUserCertsLink(ctx, xmlDoc)"); assert( (ctx != NULL) && (usrcrt_xml != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { @@ -143,7 +197,7 @@ int eDBadminUpdateUserCertLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { return 0; } - usrcrt_n = eurephiaXML_getRoot(ctx, usrcrt_xml, "usercerts_link", 1); + usrcrt_n = eurephiaXML_getRoot(ctx, usrcrt_xml, "usercerts", 1); if( usrcrt_n == NULL ) { eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request"); return 0; @@ -154,29 +208,33 @@ int eDBadminUpdateUserCertLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { return 0; } - fmap_n = xmlFindNode(usrcrt_n, "fieldMapping"); - if( fmap_n == NULL ) { + tmp_n = xmlFindNode(usrcrt_n, "sortfields"); + if( tmp_n ) { + sortfields = xmlExtractContent(tmp_n); + } + + tmp_n = xmlFindNode(usrcrt_n, "fieldMapping"); + if( tmp_n == NULL ) { eurephia_log(ctx, LOG_ERROR, 0, "Invalid user-cert link request (2)."); return 0; } - usrcrt_m = eDBxmlMapping(ctx, tbl_sqlite_usercerts, NULL, fmap_n); + usrcrt_m = eDBxmlMapping(ctx, tbl_sqlite_usercerts, NULL, tmp_n); assert(usrcrt_m != NULL); - if( strcmp(mode, "register") == 0 ) { - res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_usercerts", usrcrt_m, NULL, NULL); - rc = res->last_insert_id; - } else if( strcmp(mode, "remove") == 0 ) { - res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_usercerts", NULL, usrcrt_m, NULL); - rc = 1; - } + uicid = xmlGetAttrValue(usrcrt_n->properties, "uicid"); - if( res == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Failed to register user account / certificate"); - rc = -1; - } else { - sqlite_free_results(res); + + if( strcmp(mode, "search") == 0 ) { + resxml = usercerts_search(ctx, usrcrt_m, sortfields); + } else if( strcmp(mode, "register") == 0 ) { + resxml = usercerts_update(ctx, mode, usrcrt_m); + } else if( strcmp(mode, "remove") == 0 ) { + resxml = usercerts_update(ctx, mode, usrcrt_m); } eDBfreeMapping(usrcrt_m); - return rc; + return resxml; } + + + |
