Merge pull request #1452 from mkresin/master
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_edit.lua
1 -- Copyright 2017 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs   = require("nixio.fs")
5 local uci  = require("luci.model.uci").cursor()
6 local http = require("luci.http")
7 local val  = ""
8
9 m = SimpleForm("edit", translate("Edit Wireless Uplink Configuration"))
10 m.submit = translate("Save")
11 m.cancel = translate("Back to overview")
12 m.reset = false
13
14 function m.on_cancel()
15         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
16 end
17
18 m.hidden = {
19         cfg = http.formvalue("cfg")
20 }
21
22 local s = uci:get_all("wireless", m.hidden.cfg)
23 if s ~= nil then
24         wssid = m:field(Value, "ssid", translate("SSID"))
25         wssid.datatype = "rangelength(1,32)"
26         wssid.default = s.ssid
27         bssid = m:field(Value, "bssid", translate("BSSID"))
28         bssid.datatype = "macaddr"
29         bssid.default = s.bssid
30         if s.identity then
31                 ident = m:field(Value, "identity", translate("Identity"))
32                 ident.default = s.identity
33         end
34         if s.encryption and s.key then
35                 wkey = m:field(Value, "key", translatef("Passphrase (%s)", s.encryption))
36         elseif s.encryption and s.password then
37                 wkey = m:field(Value, "password", translatef("Passphrase (%s)", s.encryption))
38         end
39         if s.encryption and (s.key or s.password) then
40                 wkey.password = true
41                 wkey.default = s.key or s.password
42                 if s.encryption == "wep" then
43                         wkey.datatype = "wepkey"
44                 else
45                         wkey.datatype = "wpakey"
46                 end
47         end
48 else
49         m.on_cancel()
50 end
51
52 function wssid.write(self, section, value)
53         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
54         uci:set("wireless", m.hidden.cfg, "bssid", bssid:formvalue(section))
55         if s.identity then
56                 val = ident:formvalue(section)
57                 if val == "" then
58                         val = "changeme"
59                 end
60                 uci:set("wireless", m.hidden.cfg, "identity", val)
61         end
62
63         if s.encryption and s.encryption ~= "none" then
64                 val = wkey:formvalue(section)
65                 if val == "" then
66                         val = "changeme"
67                 end
68                 if s.key then
69                         uci:set("wireless", m.hidden.cfg, "key", val)
70                 elseif s.password then
71                         uci:set("wireless", m.hidden.cfg, "password", val)
72                 end
73         end
74         uci:save("wireless")
75         uci:commit("wireless")
76         m.on_cancel()
77 end
78
79 return m