luci-app-attendedsysupgrade: lua code for acl.d
[project/luci.git] / applications / luci-app-attendedsysupgrade / luasrc / view / attendedsysupgrade.htm
index ea97e9a..bc3fc6b 100644 (file)
@@ -1,3 +1,78 @@
+<%
+-- all lua code provided by https://github.com/jow-/
+-- thank you very much!
+
+    function apply_acls(filename, session)
+        local json = require "luci.jsonc"
+        local util = require "luci.util"
+        local fs   = require "nixio.fs"
+
+        local grants = { }
+
+        local acl = json.parse(fs.readfile(filename))
+        if type(acl) ~= "table" then
+            return
+        end
+
+        local group, perms
+        for group, perms in pairs(acl) do
+            local perm, scopes
+            for perm, scopes in pairs(perms) do
+                if type(scopes) == "table" then
+                    local scope, objects
+                    for scope, objects in pairs(scopes) do
+                        if type(objects) == "table" then
+                            if not grants[scope] then
+                                grants[scope] = { }
+                            end
+
+                            if next(objects) == 1 then
+                                local _, object
+                                for _, object in ipairs(objects) do
+                                    if not grants[scope][object] then
+                                        grants[scope][object] = { }
+                                    end
+                                    table.insert(grants[scope][object], perm)
+                                end
+                            else
+                                local object, funcs
+                                for object, funcs in pairs(objects) do
+                                    if type(funcs) == "table" then
+                                        local _, func
+                                        for _, func in ipairs(funcs) do
+                                            if not grants[scope][object] then
+                                                grants[scope][object] = { }
+                                            end
+                                            table.insert(grants[scope][object], func)
+                                        end
+                                    end
+                                end
+                            end
+                        end
+                    end
+                end
+            end
+        end
+
+        local _, scope, object, func
+        for scope, _ in pairs(grants) do
+            local objects = { }
+            for object, _ in pairs(_) do
+                for _, func in ipairs(_) do
+                    table.insert(objects, { object, func })
+                end
+            end
+
+            util.ubus("session", "grant", {
+                ubus_rpc_session = session,
+                scope = scope, objects = objects
+            })
+        end
+    end
+
+    apply_acls("/usr/share/rpcd/acl.d/attendedsysupgrade.json", luci.dispatcher.context.authsession)
+    apply_acls("/usr/share/rpcd/acl.d/packagelist.json", luci.dispatcher.context.authsession)
+%>
 <%+header%>
 <h2 name="content"><%:Attended Sysupgrade%></h2>
 <div class="container">
@@ -41,7 +116,11 @@ function server_request(request_dict, path, callback) {
 
 // requests ubus via rpcd
 function ubus_request(command, argument, params, callback) {
-       var request_data = '{ "jsonrpc": "2.0", "id": ' + ubus_counter + ', "method": "call", "params": [ "'+ data["ubus_rpc_session"] +'", "' + command + '", "' + argument + '", ' + params + ' ] }'
+       request_data = {};
+       request_data.jsonrpc = "2.0";
+       request_data.id = ubus_counter;
+       request_data.method = "call";
+       request_data.params = [ data.ubus_rpc_session, command, argument, params ]
        ubus_counter++
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", ubus_url, true);
@@ -56,7 +135,7 @@ function ubus_request(command, argument, params, callback) {
                        ubus_request_callback(xmlhttp, callback)
                }
        });
-       xmlhttp.send(request_data);
+       xmlhttp.send(JSON.stringify(request_data));
 }
 
 // handle ubus_requests, set variables or perform functions
@@ -87,11 +166,11 @@ function ubus_request_callback_uci(response_object, callback) {
 // initial setup, get system information
 function setup() {
        data["ubus_rpc_session"] = "<%=luci.dispatcher.context.authsession%>"
-       ubus_request("packagelist", "list", '{  }', "packagelist")
-       ubus_request("system", "board", '{  }', "release")
-       ubus_request("system", "board", '{  }', "board_name")
-       ubus_request("system", "board", '{  }', "model")
-       ubus_request("uci", "get", '{"config": "attendedsysupgrade", "section": "@settings[0]", "option": "update_server"}', "update_server")
+       ubus_request("packagelist", "list", {}, "packagelist");
+       ubus_request("system", "board", {}, "release");
+       ubus_request("system", "board", {}, "board_name");
+       ubus_request("system", "board", {}, "model");
+       ubus_request("uci", "get", { "config": "attendedsysupgrade", "section": "updateserver", "option": "url" }, "update_server")
 }
 
 // shows notification if update is available
@@ -149,7 +228,7 @@ function update_request_callback(response_object) {
 }
 
 function back_online() {
-       ubus_request("session", "login", '{  }', back_online_callback)
+       ubus_request("session", "login", {}, back_online_callback)
 }
 
 function back_online_callback(response_object) {
@@ -244,7 +323,7 @@ function upload_image(blob) {
                // this checksum should be parsed
                document.getElementById("update_info").innerHTML = "flashing... please wait" // show fancy indicator http://www.ajaxload.info/
 
-               ubus_request("attendedsysupgrade", "sysupgrade", '{ "keep_settings": ' + document.getElementById("keep").checked + ' }', 'done');
+               ubus_request("attendedsysupgrade", "sysupgrade", { "keep_settings": document.getElementById("keep").checked }, 'done');
        });
 
        upload_request.addEventListener('error', function(event) {