155fab42ce08e53b0c33d17e4709e954643bcd1e
[ndcode_site.git] / api / globals / get.json.jst
1 let XDate = require('xdate')
2
3 return async env => {
4   let get_account = await _require('/_lib/get_account.jst')
5   let get_session = await _require('/_lib/get_session.jst')
6   let post_request = await _require('/_lib/post_request.jst')
7   let Problem = await _require('/_lib/Problem.jst')
8
9   await post_request(
10     // env
11     env,
12     // handler
13     async () => {
14       let transaction = await env.site.database.Transaction()
15       try {
16         let root = await transaction.get({})
17         let session = await get_session(env, root)
18
19         let account = await get_account(root, session)
20         if (account === undefined)
21           throw new Problem('Unauthorized', 'Please sign in first.', 401)
22         if (!await account.get_json('administrator'))
23           throw new Problem('Unauthorized', 'Not administrator.', 401)
24
25         return /*await*/ root.get_json('globals', {})
26       }
27       finally {
28         transaction.rollback()
29       }
30     }
31   )
32 }