modules: Split luci-mod-full
[project/luci.git] / modules / luci-base / luasrc / controller / admin / index.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.admin.index", package.seeall)
5
6 function index()
7 function toplevel_page(page, preflookup, preftarget)
8 if preflookup and preftarget then
9 if lookup(preflookup) then
10 page.target = preftarget
11 end
12 end
13
14 if not page.target then
15 page.target = firstchild()
16 end
17 end
18
19 local uci = require("luci.model.uci").cursor()
20
21 local root = node()
22 if not root.target then
23 root.target = alias("admin")
24 root.index = true
25 end
26
27 local page = node("admin")
28
29 page.title = _("Administration")
30 page.order = 10
31 page.sysauth = "root"
32 page.sysauth_authenticator = "htmlauth"
33 page.ucidata = true
34 page.index = true
35 toplevel_page(page, "admin/status/overview", alias("admin", "status"))
36
37 -- Empty menu tree to be populated by addons and modules
38
39 page = node("admin", "status")
40 page.title = _("Status")
41 page.order = 10
42 page.index = true
43 -- overview is from mod-admin-full
44 toplevel_page(page, "admin/status/overview", alias("admin", "status", "overview"))
45
46 page = node("admin", "system")
47 page.title = _("System")
48 page.order = 20
49 page.index = true
50 -- system/system is from mod-admin-full
51 toplevel_page(page, "admin/system/system", alias("admin", "system", "system"))
52
53 -- Only used if applications add items
54 page = node("admin", "services")
55 page.title = _("Services")
56 page.order = 40
57 page.index = true
58 toplevel_page(page, false, false)
59
60 -- Even for mod-admin-full network just uses first submenu item as landing
61 page = node("admin", "network")
62 page.title = _("Network")
63 page.order = 50
64 page.index = true
65 toplevel_page(page, false, false)
66
67 if nixio.fs.access("/etc/config/dhcp") then
68 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
69 page.leaf = true
70 end
71
72 local has_wifi = false
73
74 uci:foreach("wireless", "wifi-device",
75 function(s)
76 has_wifi = true
77 return false
78 end)
79
80 if has_wifi then
81 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
82 page.leaf = true
83 end
84
85 -- Logout is last
86 entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
87 end
88
89 function action_logout()
90 local dsp = require "luci.dispatcher"
91 local utl = require "luci.util"
92 local sid = dsp.context.authsession
93
94 if sid then
95 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
96
97 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s/" %{
98 sid, 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
99 })
100 end
101
102 luci.http.redirect(dsp.build_url())
103 end
104
105
106 function lease_status()
107 local s = require "luci.tools.status"
108
109 luci.http.prepare_content("application/json")
110 luci.http.write('[')
111 luci.http.write_json(s.dhcp_leases())
112 luci.http.write(',')
113 luci.http.write_json(s.dhcp6_leases())
114 luci.http.write(']')
115 end
116
117 function wifi_assoclist()
118 local s = require "luci.tools.status"
119
120 luci.http.prepare_content("application/json")
121 luci.http.write_json(s.wifi_assoclist())
122 end