diff options
Diffstat (limited to 'database/sqlite/administration/useraccount.c')
-rw-r--r-- | database/sqlite/administration/useraccount.c | 101 |
1 files changed, 88 insertions, 13 deletions
diff --git a/database/sqlite/administration/useraccount.c b/database/sqlite/administration/useraccount.c index e867615..e7d1da7 100644 --- a/database/sqlite/administration/useraccount.c +++ b/database/sqlite/administration/useraccount.c @@ -556,18 +556,23 @@ int eDBadminDeleteUser(eurephiaCTX *ctx, const int uid, xmlDoc *userinfo) { /** - * @copydoc eDBadminGetAdminAccess() + * Internal function. Retrieves all administrator access levels granted. + * + * @param ctx eurephiaCTX + * @param fmap eDBfieldMap with field values to narrow the SQL query + * + * @return Returns an eurephia XML document on success or with a failure message. NULL is returned + * on fatal errors. */ -xmlDoc *eDBadminGetAdminAccess(eurephiaCTX *ctx, xmlDoc *srch_xml) { +xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) { dbresult *res = NULL; - eDBfieldMap *fmap = NULL; int last_uid = -1, i = 0; xmlDoc *doc = NULL; - xmlNode *root_n = NULL, *fieldmap_n = NULL, *rec_n = NULL, *acl_n = NULL, *tmp_n; + xmlNode *root_n = NULL, *rec_n = NULL, *acl_n = NULL, *tmp_n; - DEBUG(ctx, 20, "Function call: eDBadminGetAdminAccess(ctx, {xmlDoc})"); - assert( (ctx != NULL) && (srch_xml != NULL) ); + DEBUG(ctx, 21, "Function call: adminacclvl_Get(ctx, {fieldMapping})"); + assert( (ctx != NULL) && (fmap != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, @@ -575,10 +580,6 @@ xmlDoc *eDBadminGetAdminAccess(eurephiaCTX *ctx, xmlDoc *srch_xml) { return 0; } - tmp_n = eurephiaXML_getRoot(ctx, srch_xml, "admin_access", 1); - fieldmap_n = xmlFindNode(tmp_n, "fieldMapping"); - fmap = eDBxmlMapping(ctx, tbl_sqlite_eurephiaadmacc, "eac", fieldmap_n); - // Query the database, find the user defined in the user map res = sqlite_query_mapped(ctx, SQL_SELECT, "SELECT eac.uid, username, interface, access" @@ -587,12 +588,11 @@ xmlDoc *eDBadminGetAdminAccess(eurephiaCTX *ctx, xmlDoc *srch_xml) { NULL, fmap, "uid, interface, access"); if( res == NULL ) { eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a access levels"); - return 0; + return eurephiaXML_ResultMsg(ctx, exmlERROR, + "Error querying the database for a access levels"); } - eDBfreeMapping(fmap); eurephiaXML_CreateDoc(ctx, 1, "admin_access_list", &doc, &root_n); - for( i = 0; i < sqlite_get_numtuples(res); i++ ) { if( last_uid != atoi_nullsafe(sqlite_get_value(res, i, 0)) ) { // Create a new block element when we get a new uid @@ -612,3 +612,78 @@ xmlDoc *eDBadminGetAdminAccess(eurephiaCTX *ctx, xmlDoc *srch_xml) { sqlite_free_results(res); return doc; } + +/** + * @copydoc eDBadminAccessLevel() + */ +xmlDoc *eDBadminAccessLevel(eurephiaCTX *ctx, xmlDoc *qryxml) { + dbresult *sqlres = NULL; + xmlDoc *res_d = NULL; + xmlNode *qry_n = NULL, *fmap_n = NULL; + eDBfieldMap *fmap_m = NULL; + char *mode = NULL; + + DEBUG(ctx, 20, "Function call: eDBadminAccessLevel(ctx, xmlDoc)"); + assert( (ctx != NULL) && (qryxml != NULL) ); + + if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "eurephia admin function call attempted with wrong context type"); + return 0; + } + + qry_n = eurephiaXML_getRoot(ctx, qryxml, "admin_access", 1); + if( qry_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request"); + return 0; + } + mode = xmlGetAttrValue(qry_n->properties, "mode"); + if( mode == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (1)."); + return 0; + } + + fmap_n = xmlFindNode(qry_n, "fieldMapping"); + if( fmap_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (2)."); + return 0; + } + + fmap_m = eDBxmlMapping(ctx, tbl_sqlite_eurephiaadmacc, NULL, fmap_n); + assert(fmap_m != NULL); + + if( strcmp(mode, "grant") == 0 ) { + sqlres = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO eurephia_adminaccess", + fmap_m, NULL, NULL); + if( sqlres && (sqlite_get_affected_rows(sqlres) > 0) ) { + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, + "Access level %s (%s) was granted to uid %s", + eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL), + eDBmappingGetValue(fmap_m, FIELD_INTERFACE), + eDBmappingGetValue(fmap_m, FIELD_UID)); + } + } else if( strcmp(mode, "revoke") == 0 ) { + sqlres = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM eurephia_adminaccess", + NULL, fmap_m, NULL); + if( sqlres && (sqlite_get_affected_rows(sqlres) > 0) ) { + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, + "Access level %s (%s) was revoked from uid %s", + eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL), + eDBmappingGetValue(fmap_m, FIELD_INTERFACE), + eDBmappingGetValue(fmap_m, FIELD_UID)); + } + } else if( strcmp(mode, "list") == 0 ) { + res_d = adminacclvl_Get(ctx, fmap_m); + } + + if( res_d == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Failed to update admin access"); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, "Failed to complete %s operation", mode); + } + if( sqlres ) { + sqlite_free_results(sqlres); + } + eDBfreeMapping(fmap_m); + + return res_d; +} |