Fossil SCM
added /json/user/get.
Commit
0c7be1fe6c708431d5fb02e2df3b7287b045a56e
Parent
59ef1667acec853…
1 file changed
+76
-16
+76
-16
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -1173,10 +1173,36 @@ | ||
| 1173 | 1173 | json_send_response(resp); |
| 1174 | 1174 | } |
| 1175 | 1175 | cson_value_free(resp); |
| 1176 | 1176 | } |
| 1177 | 1177 | |
| 1178 | + | |
| 1179 | +/* | |
| 1180 | +** Iterates through a prepared SELECT statement and converts each row | |
| 1181 | +** to a JSON object. If pTgt is not NULL then it must be-a Array | |
| 1182 | +** object and this function will return pTgt. If pTgt is NULL then a | |
| 1183 | +** new Array object is created and returned (owned by the | |
| 1184 | +** caller). Each row of pStmt is converted to an Object and appended | |
| 1185 | +** to the array. | |
| 1186 | +*/ | |
| 1187 | +static cson_value * json_stmt_to_array_of_obj(Stmt *pStmt, | |
| 1188 | + cson_value * pTgt){ | |
| 1189 | + cson_value * v = pTgt ? pTgt : cson_value_new_array(); | |
| 1190 | + cson_array * a = cson_value_get_array(pTgt ? pTgt : v); | |
| 1191 | + assert( NULL != a ); | |
| 1192 | + while( (SQLITE_ROW==db_step(pStmt)) ){ | |
| 1193 | + cson_value * row = cson_sqlite3_row_to_object(pStmt->pStmt); | |
| 1194 | + if(!row){ | |
| 1195 | + json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, | |
| 1196 | + "Could not convert at least one result row to JSON." ); | |
| 1197 | + continue; | |
| 1198 | + } | |
| 1199 | + cson_array_append(a, row); | |
| 1200 | + } | |
| 1201 | + return v; | |
| 1202 | +} | |
| 1203 | + | |
| 1178 | 1204 | /* |
| 1179 | 1205 | ** /json/version implementation. |
| 1180 | 1206 | ** |
| 1181 | 1207 | ** Returns the payload object (owned by the caller). |
| 1182 | 1208 | */ |
| @@ -1572,24 +1598,24 @@ | ||
| 1572 | 1598 | /* Last entry MUST have a NULL name. */ |
| 1573 | 1599 | {NULL,NULL,0} |
| 1574 | 1600 | }; |
| 1575 | 1601 | |
| 1576 | 1602 | static cson_value * json_user_list(); |
| 1603 | +static cson_value * json_user_get(); | |
| 1577 | 1604 | #if 0 |
| 1578 | -static cson_value * json_user_detail(); | |
| 1579 | 1605 | static cson_value * json_user_create(); |
| 1580 | 1606 | static cson_value * json_user_edit(); |
| 1581 | 1607 | #endif |
| 1582 | 1608 | |
| 1583 | 1609 | /* |
| 1584 | 1610 | ** Mapping of /json/user/XXX commands/paths to callbacks. |
| 1585 | 1611 | */ |
| 1586 | 1612 | static const JsonPageDef JsonPageDefs_User[] = { |
| 1587 | -{"list", json_user_list, 0}, | |
| 1588 | -{"detail", json_page_nyi, 0}, | |
| 1589 | 1613 | {"create", json_page_nyi, 1}, |
| 1590 | 1614 | {"edit", json_page_nyi, 1}, |
| 1615 | +{"get", json_user_get, 0}, | |
| 1616 | +{"list", json_user_list, 0}, | |
| 1591 | 1617 | /* Last entry MUST have a NULL name. */ |
| 1592 | 1618 | {NULL,NULL,0} |
| 1593 | 1619 | }; |
| 1594 | 1620 | |
| 1595 | 1621 | |
| @@ -2450,35 +2476,69 @@ | ||
| 2450 | 2476 | } |
| 2451 | 2477 | db_finalize(&q); |
| 2452 | 2478 | return payload; |
| 2453 | 2479 | } |
| 2454 | 2480 | |
| 2481 | +/* | |
| 2482 | +** Impl of /json/user/list. Requires admin rights. | |
| 2483 | +*/ | |
| 2455 | 2484 | static cson_value * json_user_list(){ |
| 2456 | 2485 | cson_value * payV = NULL; |
| 2457 | - cson_array * pay = NULL; | |
| 2458 | 2486 | Stmt q; |
| 2459 | - if(! g.perm.Admin ){ | |
| 2487 | + if(!g.perm.Admin){ | |
| 2460 | 2488 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 2461 | 2489 | return NULL; |
| 2462 | 2490 | } |
| 2463 | - payV = cson_value_new_array(); | |
| 2464 | - pay = cson_value_get_array(payV); | |
| 2465 | 2491 | db_prepare(&q,"SELECT uid AS uid," |
| 2466 | 2492 | " login AS name," |
| 2467 | 2493 | " cap AS capabilities," |
| 2468 | 2494 | " info AS info," |
| 2469 | 2495 | " mtime AS mtime" |
| 2470 | 2496 | " FROM user ORDER BY login"); |
| 2471 | - | |
| 2472 | - while( (SQLITE_ROW==db_step(&q)) ){ | |
| 2473 | - cson_value * row = cson_sqlite3_row_to_object(q.pStmt); | |
| 2474 | - if(!row){ | |
| 2475 | - json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, | |
| 2476 | - "Could not convert at least one user result row to JSON." ); | |
| 2477 | - continue; | |
| 2478 | - } | |
| 2479 | - cson_array_append(pay, row); | |
| 2497 | + payV = json_stmt_to_array_of_obj(&q, NULL); | |
| 2498 | + db_finalize(&q); | |
| 2499 | + if(NULL == payV){ | |
| 2500 | + g.json.resultCode = FSL_JSON_E_UNKNOWN; | |
| 2501 | + } | |
| 2502 | + return payV; | |
| 2503 | +} | |
| 2504 | + | |
| 2505 | +/* | |
| 2506 | +** Impl of /json/user/get. Requires admin rights. | |
| 2507 | +*/ | |
| 2508 | +static cson_value * json_user_get(){ | |
| 2509 | + cson_value * payV = NULL; | |
| 2510 | + char const * pUser = NULL; | |
| 2511 | + Stmt q; | |
| 2512 | + if(!g.perm.Admin){ | |
| 2513 | + g.json.resultCode = FSL_JSON_E_DENIED; | |
| 2514 | + return NULL; | |
| 2515 | + } | |
| 2516 | + if( g.isHTTP ){ | |
| 2517 | + pUser = json_getenv_cstr("name"); | |
| 2518 | + }else{ | |
| 2519 | + pUser = json_command_arg(g.json.dispatchDepth+1); | |
| 2520 | + } | |
| 2521 | + if(!pUser || !*pUser){ | |
| 2522 | + g.json.resultCode = FSL_JSON_E_MISSING_ARGS; | |
| 2523 | + return NULL; | |
| 2524 | + } | |
| 2525 | + db_prepare(&q,"SELECT uid AS uid," | |
| 2526 | + " login AS name," | |
| 2527 | + " cap AS capabilities," | |
| 2528 | + " info AS info," | |
| 2529 | + " mtime AS mtime" | |
| 2530 | + " FROM user" | |
| 2531 | + " WHERE login=%Q", | |
| 2532 | + pUser); | |
| 2533 | + if( (SQLITE_ROW == db_step(&q)) ){ | |
| 2534 | + payV = cson_sqlite3_row_to_object(q.pStmt); | |
| 2535 | + if(!payV){ | |
| 2536 | + g.json.resultCode = FSL_JSON_E_UNKNOWN; | |
| 2537 | + } | |
| 2538 | + }else{ | |
| 2539 | + g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND; | |
| 2480 | 2540 | } |
| 2481 | 2541 | db_finalize(&q); |
| 2482 | 2542 | return payV; |
| 2483 | 2543 | } |
| 2484 | 2544 | |
| 2485 | 2545 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -1173,10 +1173,36 @@ | |
| 1173 | json_send_response(resp); |
| 1174 | } |
| 1175 | cson_value_free(resp); |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | ** /json/version implementation. |
| 1180 | ** |
| 1181 | ** Returns the payload object (owned by the caller). |
| 1182 | */ |
| @@ -1572,24 +1598,24 @@ | |
| 1572 | /* Last entry MUST have a NULL name. */ |
| 1573 | {NULL,NULL,0} |
| 1574 | }; |
| 1575 | |
| 1576 | static cson_value * json_user_list(); |
| 1577 | #if 0 |
| 1578 | static cson_value * json_user_detail(); |
| 1579 | static cson_value * json_user_create(); |
| 1580 | static cson_value * json_user_edit(); |
| 1581 | #endif |
| 1582 | |
| 1583 | /* |
| 1584 | ** Mapping of /json/user/XXX commands/paths to callbacks. |
| 1585 | */ |
| 1586 | static const JsonPageDef JsonPageDefs_User[] = { |
| 1587 | {"list", json_user_list, 0}, |
| 1588 | {"detail", json_page_nyi, 0}, |
| 1589 | {"create", json_page_nyi, 1}, |
| 1590 | {"edit", json_page_nyi, 1}, |
| 1591 | /* Last entry MUST have a NULL name. */ |
| 1592 | {NULL,NULL,0} |
| 1593 | }; |
| 1594 | |
| 1595 | |
| @@ -2450,35 +2476,69 @@ | |
| 2450 | } |
| 2451 | db_finalize(&q); |
| 2452 | return payload; |
| 2453 | } |
| 2454 | |
| 2455 | static cson_value * json_user_list(){ |
| 2456 | cson_value * payV = NULL; |
| 2457 | cson_array * pay = NULL; |
| 2458 | Stmt q; |
| 2459 | if(! g.perm.Admin ){ |
| 2460 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | payV = cson_value_new_array(); |
| 2464 | pay = cson_value_get_array(payV); |
| 2465 | db_prepare(&q,"SELECT uid AS uid," |
| 2466 | " login AS name," |
| 2467 | " cap AS capabilities," |
| 2468 | " info AS info," |
| 2469 | " mtime AS mtime" |
| 2470 | " FROM user ORDER BY login"); |
| 2471 | |
| 2472 | while( (SQLITE_ROW==db_step(&q)) ){ |
| 2473 | cson_value * row = cson_sqlite3_row_to_object(q.pStmt); |
| 2474 | if(!row){ |
| 2475 | json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, |
| 2476 | "Could not convert at least one user result row to JSON." ); |
| 2477 | continue; |
| 2478 | } |
| 2479 | cson_array_append(pay, row); |
| 2480 | } |
| 2481 | db_finalize(&q); |
| 2482 | return payV; |
| 2483 | } |
| 2484 | |
| 2485 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -1173,10 +1173,36 @@ | |
| 1173 | json_send_response(resp); |
| 1174 | } |
| 1175 | cson_value_free(resp); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | /* |
| 1180 | ** Iterates through a prepared SELECT statement and converts each row |
| 1181 | ** to a JSON object. If pTgt is not NULL then it must be-a Array |
| 1182 | ** object and this function will return pTgt. If pTgt is NULL then a |
| 1183 | ** new Array object is created and returned (owned by the |
| 1184 | ** caller). Each row of pStmt is converted to an Object and appended |
| 1185 | ** to the array. |
| 1186 | */ |
| 1187 | static cson_value * json_stmt_to_array_of_obj(Stmt *pStmt, |
| 1188 | cson_value * pTgt){ |
| 1189 | cson_value * v = pTgt ? pTgt : cson_value_new_array(); |
| 1190 | cson_array * a = cson_value_get_array(pTgt ? pTgt : v); |
| 1191 | assert( NULL != a ); |
| 1192 | while( (SQLITE_ROW==db_step(pStmt)) ){ |
| 1193 | cson_value * row = cson_sqlite3_row_to_object(pStmt->pStmt); |
| 1194 | if(!row){ |
| 1195 | json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, |
| 1196 | "Could not convert at least one result row to JSON." ); |
| 1197 | continue; |
| 1198 | } |
| 1199 | cson_array_append(a, row); |
| 1200 | } |
| 1201 | return v; |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | ** /json/version implementation. |
| 1206 | ** |
| 1207 | ** Returns the payload object (owned by the caller). |
| 1208 | */ |
| @@ -1572,24 +1598,24 @@ | |
| 1598 | /* Last entry MUST have a NULL name. */ |
| 1599 | {NULL,NULL,0} |
| 1600 | }; |
| 1601 | |
| 1602 | static cson_value * json_user_list(); |
| 1603 | static cson_value * json_user_get(); |
| 1604 | #if 0 |
| 1605 | static cson_value * json_user_create(); |
| 1606 | static cson_value * json_user_edit(); |
| 1607 | #endif |
| 1608 | |
| 1609 | /* |
| 1610 | ** Mapping of /json/user/XXX commands/paths to callbacks. |
| 1611 | */ |
| 1612 | static const JsonPageDef JsonPageDefs_User[] = { |
| 1613 | {"create", json_page_nyi, 1}, |
| 1614 | {"edit", json_page_nyi, 1}, |
| 1615 | {"get", json_user_get, 0}, |
| 1616 | {"list", json_user_list, 0}, |
| 1617 | /* Last entry MUST have a NULL name. */ |
| 1618 | {NULL,NULL,0} |
| 1619 | }; |
| 1620 | |
| 1621 | |
| @@ -2450,35 +2476,69 @@ | |
| 2476 | } |
| 2477 | db_finalize(&q); |
| 2478 | return payload; |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | ** Impl of /json/user/list. Requires admin rights. |
| 2483 | */ |
| 2484 | static cson_value * json_user_list(){ |
| 2485 | cson_value * payV = NULL; |
| 2486 | Stmt q; |
| 2487 | if(!g.perm.Admin){ |
| 2488 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 2489 | return NULL; |
| 2490 | } |
| 2491 | db_prepare(&q,"SELECT uid AS uid," |
| 2492 | " login AS name," |
| 2493 | " cap AS capabilities," |
| 2494 | " info AS info," |
| 2495 | " mtime AS mtime" |
| 2496 | " FROM user ORDER BY login"); |
| 2497 | payV = json_stmt_to_array_of_obj(&q, NULL); |
| 2498 | db_finalize(&q); |
| 2499 | if(NULL == payV){ |
| 2500 | g.json.resultCode = FSL_JSON_E_UNKNOWN; |
| 2501 | } |
| 2502 | return payV; |
| 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | ** Impl of /json/user/get. Requires admin rights. |
| 2507 | */ |
| 2508 | static cson_value * json_user_get(){ |
| 2509 | cson_value * payV = NULL; |
| 2510 | char const * pUser = NULL; |
| 2511 | Stmt q; |
| 2512 | if(!g.perm.Admin){ |
| 2513 | g.json.resultCode = FSL_JSON_E_DENIED; |
| 2514 | return NULL; |
| 2515 | } |
| 2516 | if( g.isHTTP ){ |
| 2517 | pUser = json_getenv_cstr("name"); |
| 2518 | }else{ |
| 2519 | pUser = json_command_arg(g.json.dispatchDepth+1); |
| 2520 | } |
| 2521 | if(!pUser || !*pUser){ |
| 2522 | g.json.resultCode = FSL_JSON_E_MISSING_ARGS; |
| 2523 | return NULL; |
| 2524 | } |
| 2525 | db_prepare(&q,"SELECT uid AS uid," |
| 2526 | " login AS name," |
| 2527 | " cap AS capabilities," |
| 2528 | " info AS info," |
| 2529 | " mtime AS mtime" |
| 2530 | " FROM user" |
| 2531 | " WHERE login=%Q", |
| 2532 | pUser); |
| 2533 | if( (SQLITE_ROW == db_step(&q)) ){ |
| 2534 | payV = cson_sqlite3_row_to_object(q.pStmt); |
| 2535 | if(!payV){ |
| 2536 | g.json.resultCode = FSL_JSON_E_UNKNOWN; |
| 2537 | } |
| 2538 | }else{ |
| 2539 | g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND; |
| 2540 | } |
| 2541 | db_finalize(&q); |
| 2542 | return payV; |
| 2543 | } |
| 2544 | |
| 2545 |