Add /_lib/get_account.jst, remove env.signed_in_as
[ndcode_site.git] / api / globals / set.json.jst
1 let XDate = require('xdate')
2
3 return async env => {
4   let post_request = await _require('/_lib/post_request.jst')
5   let get_session = await _require('/_lib/get_session.jst')
6   let Problem = await _require('/_lib/Problem.jst')
7
8   await post_request(
9     // env
10     env,
11     // handler
12     async globals => {
13       // coerce and/or validate
14       globals = {
15         site_url: globals.site_url.slice(0, 1024),
16         site_title: globals.site_title.slice(0, 1024),
17         contact_from: globals.contact_from.slice(0, 1024),
18         contact_to: globals.contact_to.slice(0, 1024),
19         feedback_from: globals.feedback_from.slice(0, 1024),
20         feedback_to: globals.feedback_to.slice(0, 1024),
21         noreply_from: globals.noreply_from.slice(0, 1024),
22         noreply_signature: globals.noreply_signature.slice(0, 1024),
23         copyright: globals.copyright.slice(0, 1024)
24       }
25
26       let transaction = await env.site.database.Transaction()
27       try {
28         let account = await get_account(
29           env,
30           transaction,
31           await get_session(env, transaction)
32         )
33         if (account === undefined)
34           throw new Problem('Unauthorized', 'Please sign in first.', 401)
35         if (!await account.get_json('administrator'))
36           throw new Problem('Unauthorized', 'Not administrator.', 401)
37
38         ;(await transaction.get({})).set_json('globals', globals)
39         await transaction.commit()
40       }
41       catch (error) {
42         transaction.rollback()
43         throw error
44       }
45     }
46   )
47 }