Fossil

Check-in [0c7be1fe]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added /json/user/get.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 0c7be1fe6c708431d5fb02e2df3b7287b045a56e
User & Date: stephan 2011-09-27 00:34:02
Context
2011-09-27
00:34
merged in trunk [12272b7ff0]. check-in: a5fd64d3 user: stephan tags: json
00:34
added /json/user/get. check-in: 0c7be1fe user: stephan tags: json
00:14
added /json/user/list. check-in: 59ef1667 user: stephan tags: json
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

1171
1172
1173
1174
1175
1176
1177


























1178
1179
1180
1181
1182
1183
1184
....
1570
1571
1572
1573
1574
1575
1576

1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590


1591
1592
1593
1594
1595
1596
1597
....
2448
2449
2450
2451
2452
2453
2454



2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470




2471































2472
2473
2474
2475
2476
2477

2478
2479


2480
2481
2482
2483
2484
2485
2486
    }
  }else{
    json_send_response(resp);
  }
  cson_value_free(resp);
}



























/*
** /json/version implementation.
**
** Returns the payload object (owned by the caller).
*/
cson_value * json_page_version(){
  cson_value * jval = NULL;
................................................................................
{"wik", json_timeline_wiki, 0},
{"wiki", json_timeline_wiki, 0},
/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};

static cson_value * json_user_list();

#if 0
static cson_value * json_user_detail();
static cson_value * json_user_create();
static cson_value * json_user_edit();
#endif

/*
** Mapping of /json/user/XXX commands/paths to callbacks.
*/
static const JsonPageDef JsonPageDefs_User[] = {
{"list", json_user_list, 0},
{"detail", json_page_nyi, 0},
{"create", json_page_nyi, 1},
{"edit", json_page_nyi, 1},


/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};


/*
** A page/command dispatch helper for fossil_json_f() implementations.
................................................................................
  }else{
    g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
  }
  db_finalize(&q);
  return payload;
}




static cson_value * json_user_list(){
  cson_value * payV = NULL;
  cson_array * pay = NULL;
  Stmt q;
  if(! g.perm.Admin ){
    g.json.resultCode = FSL_JSON_E_DENIED;
    return NULL;
  }
  payV = cson_value_new_array();
  pay = cson_value_get_array(payV);
  db_prepare(&q,"SELECT uid AS uid,"
             " login AS name,"
             " cap AS capabilities,"
             " info AS info,"
             " mtime AS mtime"
             " FROM user ORDER BY login");




































  while( (SQLITE_ROW==db_step(&q)) ){
    cson_value * row = cson_sqlite3_row_to_object(q.pStmt);
    if(!row){
      json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED,
                 "Could not convert at least one user result row to JSON." );
      continue;

    }
    cson_array_append(pay, row);


  }
  db_finalize(&q);
  return payV;  
}


/*







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>

<








<
<


>
>







 







>
>
>


<

|



<
<






>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
<
<
<
>

<
>
>







1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
....
1596
1597
1598
1599
1600
1601
1602
1603
1604

1605
1606
1607
1608
1609
1610
1611
1612


1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
....
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485

2486
2487
2488
2489
2490


2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535



2536
2537

2538
2539
2540
2541
2542
2543
2544
2545
2546
    }
  }else{
    json_send_response(resp);
  }
  cson_value_free(resp);
}


/*
** Iterates through a prepared SELECT statement and converts each row
** to a JSON object. If pTgt is not NULL then it must be-a Array
** object and this function will return pTgt. If pTgt is NULL then a
** new Array object is created and returned (owned by the
** caller). Each row of pStmt is converted to an Object and appended
** to the array.
*/
static cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
                                              cson_value * pTgt){
  cson_value * v = pTgt ? pTgt : cson_value_new_array();
  cson_array * a = cson_value_get_array(pTgt ? pTgt : v);
  assert( NULL != a );
  while( (SQLITE_ROW==db_step(pStmt)) ){
    cson_value * row = cson_sqlite3_row_to_object(pStmt->pStmt);
    if(!row){
      json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED,
                 "Could not convert at least one result row to JSON." );
      continue;
    }
    cson_array_append(a, row);
  }
  return v;  
}

/*
** /json/version implementation.
**
** Returns the payload object (owned by the caller).
*/
cson_value * json_page_version(){
  cson_value * jval = NULL;
................................................................................
{"wik", json_timeline_wiki, 0},
{"wiki", json_timeline_wiki, 0},
/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};

static cson_value * json_user_list();
static cson_value * json_user_get();
#if 0

static cson_value * json_user_create();
static cson_value * json_user_edit();
#endif

/*
** Mapping of /json/user/XXX commands/paths to callbacks.
*/
static const JsonPageDef JsonPageDefs_User[] = {


{"create", json_page_nyi, 1},
{"edit", json_page_nyi, 1},
{"get", json_user_get, 0},
{"list", json_user_list, 0},
/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};


/*
** A page/command dispatch helper for fossil_json_f() implementations.
................................................................................
  }else{
    g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
  }
  db_finalize(&q);
  return payload;
}

/*
** Impl of /json/user/list. Requires admin rights.
*/
static cson_value * json_user_list(){
  cson_value * payV = NULL;

  Stmt q;
  if(!g.perm.Admin){
    g.json.resultCode = FSL_JSON_E_DENIED;
    return NULL;
  }


  db_prepare(&q,"SELECT uid AS uid,"
             " login AS name,"
             " cap AS capabilities,"
             " info AS info,"
             " mtime AS mtime"
             " FROM user ORDER BY login");
  payV = json_stmt_to_array_of_obj(&q, NULL);
  db_finalize(&q);
  if(NULL == payV){
    g.json.resultCode = FSL_JSON_E_UNKNOWN;
  }
  return payV;  
}

/*
** Impl of /json/user/get. Requires admin rights.
*/
static cson_value * json_user_get(){
  cson_value * payV = NULL;
  char const * pUser = NULL;
  Stmt q;
  if(!g.perm.Admin){
    g.json.resultCode = FSL_JSON_E_DENIED;
    return NULL;
  }
  if( g.isHTTP ){
    pUser = json_getenv_cstr("name");
  }else{
    pUser = json_command_arg(g.json.dispatchDepth+1);
  }
  if(!pUser || !*pUser){
    g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
    return NULL;
  }
  db_prepare(&q,"SELECT uid AS uid,"
             " login AS name,"
             " cap AS capabilities,"
             " info AS info,"
             " mtime AS mtime"
             " FROM user"
             " WHERE login=%Q",
             pUser);
  if( (SQLITE_ROW == db_step(&q)) ){
    payV = cson_sqlite3_row_to_object(q.pStmt);
    if(!payV){



      g.json.resultCode = FSL_JSON_E_UNKNOWN;
    }

  }else{
    g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
  }
  db_finalize(&q);
  return payV;  
}


/*