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.
5 module("luci.controller.admin.uci", package.seeall)
8 local redir = luci.http.formvalue("redir", true)
9 or table.concat(luci.dispatcher.context.request, "/")
11 entry({"admin", "uci"}, nil, _("Configuration"))
12 entry({"admin", "uci", "revert"}, post("action_revert"), nil)
15 local authen = function(checkpass, allowed_users)
16 return "root", luci.http.formvalue("sid")
19 node = entry({"admin", "uci", "apply_rollback"}, post("action_apply_rollback"), nil)
21 node.sysauth_authenticator = authen
23 node = entry({"admin", "uci", "apply_unchecked"}, post("action_apply_unchecked"), nil)
25 node.sysauth_authenticator = authen
27 node = entry({"admin", "uci", "confirm"}, call("action_confirm"), nil)
33 local function ubus_state_to_http(errstr)
35 ["Invalid command"] = 400,
36 ["Invalid argument"] = 400,
37 ["Method not found"] = 404,
38 ["Entry not found"] = 404,
40 ["Permission denied"] = 403,
42 ["Not supported"] = 500,
43 ["Unknown error"] = 500,
44 ["Connection failed"] = 503
47 local code = map[errstr] or 200
48 local msg = errstr or "OK"
50 luci.http.status(code, msg)
53 luci.http.prepare_content("text/plain")
58 function action_apply_rollback()
59 local uci = require "luci.model.uci"
60 local token, errstr = uci:apply(true)
62 luci.http.prepare_content("application/json")
63 luci.http.write_json({ token = token })
65 ubus_state_to_http(errstr)
69 function action_apply_unchecked()
70 local uci = require "luci.model.uci"
71 local _, errstr = uci:apply(false)
72 ubus_state_to_http(errstr)
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)
82 function action_revert()
83 local uci = require "luci.model.uci"
84 local changes = uci:changes()
86 -- Collect files to be reverted
87 local _, errstr, r, tbl
88 for r, tbl in pairs(changes) do
89 _, errstr = uci:revert(r)
95 ubus_state_to_http(errstr or "OK")