1d955dd982deb82a5e4559abec7cf28358aa8183
[oweals/luci.git] / modules / luci-base / luasrc / controller / admin / uci.lua
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.
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", "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}
14
15         local node
16         local authen = function(checkpass, allowed_users)
17                 return "root", luci.http.formvalue("sid")
18         end
19
20         node = entry({"admin", "uci", "apply_rollback"}, post("action_apply_rollback"), nil)
21         node.cors = true
22         node.sysauth_authenticator = authen
23
24         node = entry({"admin", "uci", "apply_unchecked"}, post("action_apply_unchecked"), nil)
25         node.cors = true
26         node.sysauth_authenticator = authen
27
28         node = entry({"admin", "uci", "confirm"}, call("action_confirm"), nil)
29         node.cors = true
30         node.sysauth = false
31 end
32
33
34 function action_changes()
35         local uci  = require "luci.model.uci"
36         local changes = uci:changes()
37
38         luci.template.render("admin_uci/changes", {
39                 changes       = next(changes) and changes,
40                 timeout       = timeout,
41                 trigger_apply = luci.http.formvalue("trigger_apply") and true or false
42         })
43 end
44
45 function action_revert()
46         local uci = require "luci.model.uci"
47         local changes = uci:changes()
48
49         -- Collect files to be reverted
50         local r, tbl
51         for r, tbl in pairs(changes) do
52                 uci:revert(r)
53         end
54
55         luci.template.render("admin_uci/revert", {
56                 changes        = next(changes) and changes,
57                 trigger_revert = true
58         })
59 end
60
61
62 local function ubus_state_to_http(errstr)
63         local map = {
64                 ["Invalid command"]   = 400,
65                 ["Invalid argument"]  = 400,
66                 ["Method not found"]  = 404,
67                 ["Entry not found"]   = 404,
68                 ["No data"]           = 204,
69                 ["Permission denied"] = 403,
70                 ["Timeout"]           = 504,
71                 ["Not supported"]     = 500,
72                 ["Unknown error"]     = 500,
73                 ["Connection failed"] = 503
74         }
75
76         local code = map[errstr] or 200
77         local msg  = errstr      or "OK"
78
79         luci.http.status(code, msg)
80
81         if code ~= 204 then
82                 luci.http.prepare_content("text/plain")
83                 luci.http.write(msg)
84         end
85 end
86
87 function action_apply_rollback()
88         local uci = require "luci.model.uci"
89         local token, errstr = uci:apply(true)
90         if token then
91                 luci.http.prepare_content("application/json")
92                 luci.http.write_json({ token = token })
93         else
94                 ubus_state_to_http(errstr)
95         end
96 end
97
98 function action_apply_unchecked()
99         local uci = require "luci.model.uci"
100         local _, errstr = uci:apply(false)
101         ubus_state_to_http(errstr)
102 end
103
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)
109 end