Fossil SCM

added /json/user/get.

stephan 2011-09-27 00:34 UTC json
Commit 0c7be1fe6c708431d5fb02e2df3b7287b045a56e
1 file changed +76 -16
+76 -16
--- src/json.c
+++ src/json.c
@@ -1173,10 +1173,36 @@
11731173
json_send_response(resp);
11741174
}
11751175
cson_value_free(resp);
11761176
}
11771177
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
+
11781204
/*
11791205
** /json/version implementation.
11801206
**
11811207
** Returns the payload object (owned by the caller).
11821208
*/
@@ -1572,24 +1598,24 @@
15721598
/* Last entry MUST have a NULL name. */
15731599
{NULL,NULL,0}
15741600
};
15751601
15761602
static cson_value * json_user_list();
1603
+static cson_value * json_user_get();
15771604
#if 0
1578
-static cson_value * json_user_detail();
15791605
static cson_value * json_user_create();
15801606
static cson_value * json_user_edit();
15811607
#endif
15821608
15831609
/*
15841610
** Mapping of /json/user/XXX commands/paths to callbacks.
15851611
*/
15861612
static const JsonPageDef JsonPageDefs_User[] = {
1587
-{"list", json_user_list, 0},
1588
-{"detail", json_page_nyi, 0},
15891613
{"create", json_page_nyi, 1},
15901614
{"edit", json_page_nyi, 1},
1615
+{"get", json_user_get, 0},
1616
+{"list", json_user_list, 0},
15911617
/* Last entry MUST have a NULL name. */
15921618
{NULL,NULL,0}
15931619
};
15941620
15951621
@@ -2450,35 +2476,69 @@
24502476
}
24512477
db_finalize(&q);
24522478
return payload;
24532479
}
24542480
2481
+/*
2482
+** Impl of /json/user/list. Requires admin rights.
2483
+*/
24552484
static cson_value * json_user_list(){
24562485
cson_value * payV = NULL;
2457
- cson_array * pay = NULL;
24582486
Stmt q;
2459
- if(! g.perm.Admin ){
2487
+ if(!g.perm.Admin){
24602488
g.json.resultCode = FSL_JSON_E_DENIED;
24612489
return NULL;
24622490
}
2463
- payV = cson_value_new_array();
2464
- pay = cson_value_get_array(payV);
24652491
db_prepare(&q,"SELECT uid AS uid,"
24662492
" login AS name,"
24672493
" cap AS capabilities,"
24682494
" info AS info,"
24692495
" mtime AS mtime"
24702496
" 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;
24802540
}
24812541
db_finalize(&q);
24822542
return payV;
24832543
}
24842544
24852545
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button