2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
2019-02-07 18:10:34 +00:00
|
|
|
-- Copyright 2010-2019 Jo-Philipp Wich <jo@mein.io>
|
2015-01-16 22:38:38 +00:00
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2010-10-15 21:53:28 +00:00
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
module("luci.controller.admin.uci", package.seeall)
|
2008-04-11 18:24:25 +00:00
|
|
|
|
2018-04-26 06:52:55 +00:00
|
|
|
local function ubus_state_to_http(errstr)
|
|
|
|
local map = {
|
|
|
|
["Invalid command"] = 400,
|
|
|
|
["Invalid argument"] = 400,
|
|
|
|
["Method not found"] = 404,
|
|
|
|
["Entry not found"] = 404,
|
|
|
|
["No data"] = 204,
|
|
|
|
["Permission denied"] = 403,
|
|
|
|
["Timeout"] = 504,
|
|
|
|
["Not supported"] = 500,
|
|
|
|
["Unknown error"] = 500,
|
|
|
|
["Connection failed"] = 503
|
|
|
|
}
|
2008-09-06 13:51:51 +00:00
|
|
|
|
2018-04-26 06:52:55 +00:00
|
|
|
local code = map[errstr] or 200
|
|
|
|
local msg = errstr or "OK"
|
|
|
|
|
|
|
|
luci.http.status(code, msg)
|
|
|
|
|
|
|
|
if code ~= 204 then
|
|
|
|
luci.http.prepare_content("text/plain")
|
|
|
|
luci.http.write(msg)
|
2008-04-11 18:24:25 +00:00
|
|
|
end
|
2018-04-26 06:52:55 +00:00
|
|
|
end
|
2010-10-15 21:53:28 +00:00
|
|
|
|
2018-04-26 06:52:55 +00:00
|
|
|
function action_apply_rollback()
|
|
|
|
local uci = require "luci.model.uci"
|
2018-07-26 20:12:45 +00:00
|
|
|
local token, errstr = uci:apply(true)
|
|
|
|
if token then
|
|
|
|
luci.http.prepare_content("application/json")
|
|
|
|
luci.http.write_json({ token = token })
|
|
|
|
else
|
|
|
|
ubus_state_to_http(errstr)
|
|
|
|
end
|
2018-04-26 06:52:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function action_apply_unchecked()
|
|
|
|
local uci = require "luci.model.uci"
|
|
|
|
local _, errstr = uci:apply(false)
|
|
|
|
ubus_state_to_http(errstr)
|
|
|
|
end
|
|
|
|
|
|
|
|
function action_confirm()
|
|
|
|
local uci = require "luci.model.uci"
|
2018-07-26 20:12:45 +00:00
|
|
|
local token = luci.http.formvalue("token")
|
|
|
|
local _, errstr = uci:confirm(token)
|
2018-04-26 06:52:55 +00:00
|
|
|
ubus_state_to_http(errstr)
|
2008-06-03 22:42:01 +00:00
|
|
|
end
|
2019-02-07 18:10:34 +00:00
|
|
|
|
|
|
|
function action_revert()
|
|
|
|
local uci = require "luci.model.uci"
|
|
|
|
local changes = uci:changes()
|
|
|
|
|
|
|
|
-- Collect files to be reverted
|
|
|
|
local _, errstr, r, tbl
|
|
|
|
for r, tbl in pairs(changes) do
|
|
|
|
_, errstr = uci:revert(r)
|
|
|
|
if errstr then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ubus_state_to_http(errstr or "OK")
|
|
|
|
end
|