luci-base: replace uci change pages with client side modal dialog
[oweals/luci.git] / modules / luci-base / luasrc / controller / admin / uci.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2019 Jo-Philipp Wich <jo@mein.io>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.uci", package.seeall)
6
7 function index()
8         local redir = luci.http.formvalue("redir", true)
9                 or table.concat(luci.dispatcher.context.request, "/")
10
11         entry({"admin", "uci"}, nil, _("Configuration"))
12         entry({"admin", "uci", "revert"}, post("action_revert"), nil)
13
14         local node
15         local authen = function(checkpass, allowed_users)
16                 return "root", luci.http.formvalue("sid")
17         end
18
19         node = entry({"admin", "uci", "apply_rollback"}, post("action_apply_rollback"), nil)
20         node.cors = true
21         node.sysauth_authenticator = authen
22
23         node = entry({"admin", "uci", "apply_unchecked"}, post("action_apply_unchecked"), nil)
24         node.cors = true
25         node.sysauth_authenticator = authen
26
27         node = entry({"admin", "uci", "confirm"}, call("action_confirm"), nil)
28         node.cors = true
29         node.sysauth = false
30 end
31
32
33 local function ubus_state_to_http(errstr)
34         local map = {
35                 ["Invalid command"]   = 400,
36                 ["Invalid argument"]  = 400,
37                 ["Method not found"]  = 404,
38                 ["Entry not found"]   = 404,
39                 ["No data"]           = 204,
40                 ["Permission denied"] = 403,
41                 ["Timeout"]           = 504,
42                 ["Not supported"]     = 500,
43                 ["Unknown error"]     = 500,
44                 ["Connection failed"] = 503
45         }
46
47         local code = map[errstr] or 200
48         local msg  = errstr      or "OK"
49
50         luci.http.status(code, msg)
51
52         if code ~= 204 then
53                 luci.http.prepare_content("text/plain")
54                 luci.http.write(msg)
55         end
56 end
57
58 function action_apply_rollback()
59         local uci = require "luci.model.uci"
60         local token, errstr = uci:apply(true)
61         if token then
62                 luci.http.prepare_content("application/json")
63                 luci.http.write_json({ token = token })
64         else
65                 ubus_state_to_http(errstr)
66         end
67 end
68
69 function action_apply_unchecked()
70         local uci = require "luci.model.uci"
71         local _, errstr = uci:apply(false)
72         ubus_state_to_http(errstr)
73 end
74
75 function action_confirm()
76         local uci = require "luci.model.uci"
77         local token = luci.http.formvalue("token")
78         local _, errstr = uci:confirm(token)
79         ubus_state_to_http(errstr)
80 end
81
82 function action_revert()
83         local uci = require "luci.model.uci"
84         local changes = uci:changes()
85
86         -- Collect files to be reverted
87         local _, errstr, r, tbl
88         for r, tbl in pairs(changes) do
89                 _, errstr = uci:revert(r)
90                 if errstr then
91                         break
92                 end
93         end
94
95         ubus_state_to_http(errstr or "OK")
96 end