static GList * dm_ldap_ent_get_values(GList *entlist) { GList *fldlist, *attlist; GList *values = NULL; entlist = g_list_first(entlist); while (entlist) { fldlist = g_list_first(entlist->data); while (fldlist) { attlist = g_list_first(fldlist->data); while (attlist) { char *t = (gchar *)attlist->data; TRACE(TRACE_DEBUG,"value [%s]", t); values = g_list_append_printf(values,"%s", t); if (! g_list_next(attlist)) break; attlist = g_list_next(attlist); } if (! g_list_next(fldlist)) break; fldlist = g_list_next(fldlist); } if (! g_list_next(entlist)) break; entlist = g_list_next(entlist); } return values; }
END_TEST START_TEST(test_g_list_slices_u64) { unsigned i=0; unsigned j=98; unsigned s=11; uint64_t *l; GList *list = NULL; GList *sub = NULL; for (i=0; i< j; i++) { l = g_new0(uint64_t,1); *l = i; list = g_list_append(list, l); } list = g_list_slices_u64(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==9, "number of slices incorrect"); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==s,"Slice length incorrect"); g_list_foreach(list,(GFunc)g_free,NULL); g_list_foreach(sub,(GFunc)g_free,NULL); i=0; j=17; s=100; list = NULL; sub = NULL; for (i=0; i< j; i++) list = g_list_append_printf(list, "ELEM_%d", i); list = g_list_slices(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==1, "number of slices incorrect [%d]", g_list_length(list)); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==j,"Slice length incorrect"); }
//--------------------------------------------------------------------------------------// void Http_getUsers(T R) { struct evbuffer *buf; char *username = NULL; uint64_t id = 0; if (Request_getId(R)) { /* * id can be specified both by name and number * * C < /users/testuser1 * C < /users/123 * */ if ((id = strtoull(Request_getId(R), NULL, 10))) username = auth_get_userid(id); else if (auth_user_exists(Request_getId(R), &id)) username = g_strdup(Request_getId(R)); if (! (username && id)) Request_error(R, HTTP_NOTFOUND, "User not found"); } buf = evbuffer_new(); if (Request_getMethod(R) == NULL) { GList *users = NULL; if (username) { MailboxState_T M; const char *mailbox; uint64_t mboxid; /* * retrieve user meta-data * C < /users/testuser1 * create/delete mailbox for user * POST C < /users/testuser1 */ if ((mailbox = evhttp_find_header(Request_getPOST(R),"create"))) { const char *message; if (db_mailbox_create_with_parents(mailbox, BOX_COMMANDLINE, id, &mboxid, &message)) { Request_error(R, HTTP_BADREQUEST, message); evbuffer_free(buf); return; } } if ((mailbox = evhttp_find_header(Request_getPOST(R),"delete"))) { int access; /* check if there is an attempt to delete inbox */ if (MATCH(mailbox, "INBOX")) { Request_error(R, HTTP_BADREQUEST, "NO cannot delete special mailbox INBOX"); evbuffer_free(buf); return; } if (! (db_findmailbox(mailbox, id, &mboxid)) ) { Request_error(R, HTTP_NOTFOUND, "NO mailbox doesn't exists"); evbuffer_free(buf); return; } /* Check if the user has ACL delete rights to this mailbox */ M = MailboxState_new(mboxid); access = acl_has_right(M, id, ACL_RIGHT_DELETE); if (access != 1) { Request_error(R, HTTP_BADREQUEST, "NO permission denied"); evbuffer_free(buf); return; } /* ok remove mailbox */ if (db_delete_mailbox(mboxid, 0, 1)) { Request_error(R, HTTP_SERVUNAVAIL, "NO delete failed"); evbuffer_free(buf); return; } } users = g_list_append_printf(users, "%s", username); } else { /* * list all users * C < /users/ * * create,edit,delete user * POST C < /users/ */ const char *user = NULL; if ((user = evhttp_find_header(Request_getPOST(R),"create"))) { const char *password, *encoding, *quota; password = evhttp_find_header(Request_getPOST(R), "password"); encoding = evhttp_find_header(Request_getPOST(R), "encoding"); quota = evhttp_find_header(Request_getPOST(R), "quota"); TRACE(TRACE_DEBUG, "create user: [%s] password: [%s] encoding [%s] quota [%s]", user, password, encoding, quota); } else if ((user = evhttp_find_header(Request_getPOST(R),"edit"))) { TRACE(TRACE_DEBUG, "edit user: [%s]", user); } else if ((user = evhttp_find_header(Request_getPOST(R),"delete"))) { TRACE(TRACE_DEBUG, "delete user: [%s]", user); } users = auth_get_known_users(); } Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"users\": {\n"); while(users->data) { uint64_t id; if (auth_user_exists((char *)users->data, &id)) evbuffer_add_printf(buf, " \"%lu\":{\"name\":\"%s\"}", id, (char *)users->data); if (! g_list_next(users)) break; users = g_list_next(users); evbuffer_add_printf(buf,",\n"); } evbuffer_add_printf(buf, "\n}}\n"); g_list_destroy(users); } else if (MATCH(Request_getMethod(R),"mailboxes")) { GList *mailboxes = NULL; if (! username) { Request_error(R, HTTP_NOTFOUND, "User not found"); evbuffer_free(buf); return; } /* * list mailboxes for user * GET C < /users/testuser1/mailboxes * */ db_findmailbox_by_regex(id, "*", &mailboxes, FALSE); Request_setContentType(R,"application/json; charset=utf-8"); evbuffer_add_printf(buf, "{\"mailboxes\": {\n"); while (mailboxes->data) { MailboxState_T b = MailboxState_new(*((uint64_t *)mailboxes->data)); MailboxState_setOwner(b, id); //if (MailboxState_reload(b) == DM_SUCCESS) evbuffer_add_printf(buf, " \"%lu\":{\"name\":\"%s\",\"exists\":%u}", MailboxState_getId(b), MailboxState_getName(b), MailboxState_getExists(b)); MailboxState_free(&b); if (! g_list_next(mailboxes)) break; mailboxes = g_list_next(mailboxes); evbuffer_add_printf(buf,",\n"); } evbuffer_add_printf(buf, "\n}}\n"); } if (EVBUFFER_LENGTH(buf)) Request_send(R, HTTP_OK, "OK", buf); else Request_error(R, HTTP_SERVUNAVAIL, "Server error"); if (username) g_free(username); evbuffer_free(buf); }