diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-18 09:35:01 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-18 09:35:01 +0100 |
commit | 5c1af0cbf682c056f01e0c392321ba648b33def4 (patch) | |
tree | 2e6baae24787474b4f991da254e8ed730ffbe23b /eurephiadm | |
parent | 61f927eacbaa989c5d87fe73d5a4106c46edea33 (diff) | |
download | eurephia-5c1af0cbf682c056f01e0c392321ba648b33def4.tar.gz eurephia-5c1af0cbf682c056f01e0c392321ba648b33def4.tar.xz eurephia-5c1af0cbf682c056f01e0c392321ba648b33def4.zip |
Cleaned up the code and added a lot of comments. Fixed minor display things.
Diffstat (limited to 'eurephiadm')
-rw-r--r-- | eurephiadm/commands/users.c | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c index 54119a0..e2d786a 100644 --- a/eurephiadm/commands/users.c +++ b/eurephiadm/commands/users.c @@ -148,6 +148,7 @@ int list_users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL)); + // Parse arguments for( i = 1; i < argc; i++ ) { switch( eurephia_getopt(&i, argc, argv, listargs) ) { case 'S': @@ -164,16 +165,18 @@ int list_users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } } + // Retrieve the user list - as an XML document userlist = eDBadminGetUserList(ctx, sortkeys); if( userlist == NULL ) { fprintf(stderr, "Error retrieving user list\n"); return 1; } - fprintf(stdout, "%3s %-20.32s %-16.16s %-16.16s %-16.16s\n", + fprintf(stdout, " %3s %-20.32s %-16.16s %-16.16s %-16.16s\n", "ID", "Username", "Activated","Deactivated","Last access"); - fprintf(stdout, "---------------------------------------------------------------------------\n"); + fprintf(stdout, " ----------------------------------------------------------------------------\n"); + // Find the user list node in the XML doc users_n = eurephiaXML_getRoot(ctx, userlist, "userlist", 1); if( users_n == NULL ) { fprintf(stderr, "Could not retrieve valid data\n"); @@ -182,15 +185,16 @@ int list_users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } users_n = users_n->children; + // Loop through the user list for( ; users_n != NULL; users_n = users_n->next ) { - fprintf(stdout, "%3.3s %-20.32s %-16.16s %-16.16s %-16.16s\n", + fprintf(stdout, " %3.3s %-20.32s %-16.16s %-16.16s %-16.16s\n", xmlGetAttrValue(users_n->properties, "uid"), xmlGetNodeContent(users_n, "username"), - defaultValue(xmlGetNodeContent(users_n, "activated"), "(Not active)"), + defaultValue(xmlGetNodeContent(users_n, "activated"), "(Not activated)"), defaultValue(xmlGetNodeContent(users_n, "deactivated"), "-"), defaultValue(xmlGetNodeContent(users_n, "last_accessed"), "-")); } - fprintf(stdout, "---------------------------------------------------------------------------\n"); + fprintf(stdout, " ----------------------------------------------------------------------------\n"); xmlFreeDoc(userlist); return 0; } @@ -202,12 +206,14 @@ char *xmlFlags2str(xmlXPathContext *xpathCTX, const char *xpath) { memset(&flagstr, 0, 8194); + // Find the flag nodes given in the XPath query flagsObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCTX); if( flagsObj == NULL ) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath); return flagstr; } + // Loop through all nodes and build up a string of all flags for( i = 0; i < flagsObj->nodesetval->nodeNr; i++ ) { char *flag = xmlExtractContent(flagsObj->nodesetval->nodeTab[i]); if( flag != NULL ) { @@ -224,19 +230,22 @@ void xmlPrint_certs(xmlXPathContext *certsXP, const char *xpath) { xmlNode *certnode = NULL; int i = 0; + // Find the certificates in the given XPath query certsObj = xmlXPathEvalExpression((xmlChar *)xpath, certsXP); if( certsObj == NULL ) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath); return; } - + // If no certs were found, inform and exit if( certsObj->nodesetval->nodeNr == 0 ) { field_print_str("Associated certificates", "None"); printf("\n"); xmlXPathFreeObject(certsObj); return; } + + // Parse all certificates found field_print_int("Associated certificates", certsObj->nodesetval->nodeNr); printf("\n"); printf(" %3s (D) %-35.35s %33.33s\n %-49.49s %19.19s\n", @@ -288,10 +297,9 @@ void xmlPrint_certs(xmlXPathContext *certsXP, const char *xpath) { // Show account information for a particular user int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { xmlXPathContext *user_XP = NULL; - char *uname = NULL; int i, crit_set = 0; long int show_info = USERINFO_user | USERINFO_certs; - xmlDoc *user = NULL, *srch = NULL; + xmlDoc *user_xml = NULL, *srch_xml = NULL; xmlNode *srch_root = NULL, *user_n = NULL; e_options activargs[] = { @@ -306,9 +314,12 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL)); - eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch, &srch_root); - xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); + // Create a fieldMapping XML document, used for searching the user database + eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_root); + xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); // Which table to search in + + // Argument parsing crit_set = 0; for( i = 1; i < argc; i++ ) { switch( eurephia_getopt(&i, argc, argv, activargs) ) { @@ -348,37 +359,38 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int } } + // Check if we have the needed search criterias if( crit_set == 0 ) { fprintf(stderr, "%s: Missing required parameter. You must provide either user id or user name\n", MODULE); - xmlFreeDoc(srch); + xmlFreeDoc(srch_xml); return 1; } if( crit_set > 1 ) { fprintf(stderr, "%s: You cannot have several search criterias (-i | -u)\n", MODULE); - xmlFreeDoc(srch); + xmlFreeDoc(srch_xml); return 1; } // Search and find the user which was requested - if( (user = eDBadminGetUserInfo(ctx, show_info, srch)) == NULL ) { + if( (user_xml = eDBadminGetUserInfo(ctx, show_info, srch_xml)) == NULL ) { fprintf(stderr, "%s: User not found\n", MODULE); return 1; } - xmlFreeDoc(srch); + xmlFreeDoc(srch_xml); - user_n = eurephiaXML_getRoot(ctx, user, "user", 1); + user_n = eurephiaXML_getRoot(ctx, user_xml, "user", 1); if( user_n == NULL ) { fprintf(stderr, "Could not retrieve valid data\n"); - xmlFreeDoc(user); + xmlFreeDoc(user_xml); return 1; } // Create a XPath context for functions which users XPath to locate info - user_XP = xmlXPathNewContext(user); + user_XP = xmlXPathNewContext(user_xml); if( user_XP == NULL ) { fprintf(stderr,"Error: unable to create new XPath context\n"); return 1; @@ -406,8 +418,7 @@ int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int // Clean up xmlXPathFreeContext(user_XP); - xmlFreeDoc(user); - free_nullsafe(uname); + xmlFreeDoc(user_xml); return 0; } @@ -418,7 +429,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * xmlDoc *user_xml = NULL, *update_xml = NULL, *srch_xml = NULL; xmlNode *user_n = NULL, *update_n = NULL, *srch_root = NULL, *tmp = NULL; char *uid_str = NULL, *activated = NULL, *deactivated = NULL; - int i, actmode = 0, rc = 0, crit_set; + int i, actmode = 0, rc = 0, crit_set = 0; e_options activargs[] = { {"--uid", "-i", 1}, @@ -438,9 +449,11 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * return 1; } + // Create a fieldMapping XML document, used for searching the user database eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_root); - xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); + xmlNewProp(srch_root, (xmlChar *) "table", (xmlChar *)"users"); // Which table to search in + // Argument parsing crit_set = 0; for( i = 1; i < argc; i++ ) { switch( eurephia_getopt(&i, argc, argv, activargs) ) { @@ -471,6 +484,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * } } + // Check if we have the needed search criterias if( crit_set == 0 ) { fprintf(stderr, "%s: Missing required parameter. You must provide either user id or user name\n", @@ -493,9 +507,10 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * } xmlFreeDoc(srch_xml); + // Parse the user information we received user_n = eurephiaXML_getRoot(ctx, user_xml, "user", 1); if( user_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find user node in the XML document"); + eurephia_log(ctx, LOG_ERROR, 0, "%s: Could not find user node in the XML document", MODULE); xmlFreeDoc(user_xml); return 1; } @@ -504,9 +519,12 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * activated = defaultValue(xmlGetNodeContent(user_n, "activated"), NULL); deactivated = defaultValue(xmlGetNodeContent(user_n, "deactivated"), NULL); + // Create a new XML document which will be used to update the user account eurephiaXML_CreateDoc(ctx, 1, "update_user", &update_xml, &update_n); assert( (update_xml != NULL) && (update_n != NULL) ); xmlNewProp(update_n, (xmlChar *) "uid", (xmlChar *) uid_str); + + // Add fieldMapping - to correctly map eurephia fields into the database fields update_n = xmlNewChild(update_n, NULL, (xmlChar *) "fieldMapping", NULL); xmlNewProp(update_n, (xmlChar *) "table", (xmlChar *) "users"); @@ -541,7 +559,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * break; } - // Update the user record + // Do the update of the user account rc = eDBadminUpdateUser(ctx, atoi_nullsafe(uid_str), update_xml); if( rc == 1 ) { printf("User account is %s\n", (actmode == 'd' ? "deactivated" : "activated")); @@ -552,7 +570,7 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES * exit: xmlFreeDoc(user_xml); xmlFreeDoc(update_xml); - return 0; + return (rc != 1); } |