liquid_feedback_frontend
diff app/main/api/settings.lua @ 1309:32cc544d5a5b
Cumulative patch for upcoming frontend version 4
author | bsw/jbe |
---|---|
date | Sun Jul 15 14:07:29 2018 +0200 (2018-07-15) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/api/settings.lua Sun Jul 15 14:07:29 2018 +0200 1.3 @@ -0,0 +1,50 @@ 1.4 +slot.set_layout(nil, "application/json") 1.5 + 1.6 +if not app.access_token then 1.7 + return util.api_error(400, "Forbidden", "insufficient_scope", "Scope 'settings' required") 1.8 +end 1.9 + 1.10 +local r = json.object{} 1.11 + 1.12 +if request.is_post() then 1.13 + if not app.scopes.update_settings then 1.14 + return util.api_error(403, "Forbidden", "insufficient_scope", "Scope update_settings required") 1.15 + end 1.16 + local settings = app.access_token.member.settings 1.17 + if not settings then 1.18 + settings = MemberSettings:new() 1.19 + settings.member_id = app.access_token.member_id 1.20 + settings.settings = json.object() 1.21 + end 1.22 + local fields = json.import(param.get("update")) 1.23 + if not fields then 1.24 + return util.api_error(400, "Bad Request", "settings_data_expected", "JSON object with updated settings data expected") 1.25 + end 1.26 + for i, field in ipairs(config.member_settings_fields) do 1.27 + if json.type(fields, field.id) ~= "nil" then 1.28 + local value = fields[field.id] 1.29 + if value ~= nil then 1.30 + if (field.type == "string" or field.type == "text") and json.type(value) ~= "string" then 1.31 + return util.api_error(400, "Bad Request", "string_expected", "JSON encoded string value expected") 1.32 + end 1.33 + if (field.type == "boolean") and json.type(value) ~= "boolean" then 1.34 + return util.api_error(400, "Bad Request", "boolean_expected", "JSON encoded boolean value expected") 1.35 + end 1.36 + end 1.37 + settings.settings[field.id] = value 1.38 + end 1.39 + end 1.40 + settings:save() 1.41 + r.status = 'ok' 1.42 + slot.put_into("data", json.export(r)) 1.43 + slot.put_into("data", "\n") 1.44 +else 1.45 + if not app.scopes.settings then 1.46 + return util.api_error(403, "Forbidden", "insufficient_scope", "Scope 'settings' required") 1.47 + end 1.48 + local settings = app.access_token.member.settings or json.object() 1.49 + r = execute.chunk{ module = "api", chunk = "_settings", params = { settings = settings } } 1.50 + slot.put_into("data", json.export(json.object{ result = r })) 1.51 + slot.put_into("data", "\n") 1.52 +end 1.53 +