1
0

uci.lua 2.8 KB

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