1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2015 Jo-Philipp Wich <jow@openwrt.org>
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", "changes"}, post_on({ trigger_apply = true }, "action_changes"), _("Changes"), 40).query = {redir=redir}
13 entry({"admin", "uci", "revert"}, post("action_revert"), _("Revert"), 30).query = {redir=redir}
16 local authen = function(checkpass, allowed_users)
17 return "root", luci.http.formvalue("sid")
20 node = entry({"admin", "uci", "apply_rollback"}, post("action_apply_rollback"), nil)
22 node.sysauth_authenticator = authen
24 node = entry({"admin", "uci", "apply_unchecked"}, post("action_apply_unchecked"), nil)
26 node.sysauth_authenticator = authen
28 node = entry({"admin", "uci", "confirm"}, call("action_confirm"), nil)
34 function action_changes()
35 local uci = require "luci.model.uci"
36 local changes = uci:changes()
38 luci.template.render("admin_uci/changes", {
39 changes = next(changes) and changes,
41 trigger_apply = luci.http.formvalue("trigger_apply") and true or false
45 function action_revert()
46 local uci = require "luci.model.uci"
47 local changes = uci:changes()
49 -- Collect files to be reverted
51 for r, tbl in pairs(changes) do
55 luci.template.render("admin_uci/revert", {
56 changes = next(changes) and changes,
62 local function ubus_state_to_http(errstr)
64 ["Invalid command"] = 400,
65 ["Invalid argument"] = 400,
66 ["Method not found"] = 404,
67 ["Entry not found"] = 404,
69 ["Permission denied"] = 403,
71 ["Not supported"] = 500,
72 ["Unknown error"] = 500,
73 ["Connection failed"] = 503
76 local code = map[errstr] or 200
77 local msg = errstr or "OK"
79 luci.http.status(code, msg)
82 luci.http.prepare_content("text/plain")
87 function action_apply_rollback()
88 local uci = require "luci.model.uci"
89 local token, errstr = uci:apply(true)
91 luci.http.prepare_content("application/json")
92 luci.http.write_json({ token = token })
94 ubus_state_to_http(errstr)
98 function action_apply_unchecked()
99 local uci = require "luci.model.uci"
100 local _, errstr = uci:apply(false)
101 ubus_state_to_http(errstr)
104 function action_confirm()
105 local uci = require "luci.model.uci"
106 local token = luci.http.formvalue("token")
107 local _, errstr = uci:confirm(token)
108 ubus_state_to_http(errstr)