modules/admin-full: rework interface add model

This commit is contained in:
Jo-Philipp Wich 2010-11-15 22:11:18 +00:00
parent d26e671b78
commit 125eb141de

View file

@ -1,7 +1,7 @@
--[[ --[[
LuCI - Lua Configuration Interface LuCI - Lua Configuration Interface
Copyright 2009 Jo-Philipp Wich <xm@subsignal.org> Copyright 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -10,96 +10,68 @@ You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
$Id$ $Id$
]]-- ]]--
local nw = require "luci.model.network" local nw = require "luci.model.network".init()
local fw = require "luci.model.firewall" local fw = require "luci.model.firewall".init()
local utl = require "luci.util"
local uci = require "luci.model.uci".cursor() local uci = require "luci.model.uci".cursor()
m = SimpleForm("network", translate("Create Or Attach Network"), m = SimpleForm("network", translate("Create Interface"))
translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
"to the existing interfaces and is covered by the firewall zone of the choosen network.<br />" ..
"Uncheck the attach option to define a new standalone network for this interface."
))
nw.init(uci) newnet = m:field(Value, "_netname", translate("Name of the new interface"),
fw.init(uci)
attachnet = m:field(Flag, "_attach", translate("Attach to existing network"))
attachnet.rmempty = false
attachnet.default = "1"
newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " .. translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
"<code>0-9</code> and <code>_</code>" "<code>0-9</code> and <code>_</code>"
)) ))
newnet:depends("_attach", "") newnet:depends("_attach", "")
newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_") newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
newnet.datatype = "uciname"
addnet = m:field(Value, "_netname_attach", netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
translate("Network to attach interface to"))
addnet.template = "cbi/network_netlist"
addnet.widget = "radio"
addnet.nocreate = true
addnet:depends("_attach", "1")
fwzone = m:field(Value, "_fwzone",
translate("Create / Assign firewall-zone"),
translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
fwzone.template = "cbi/firewall_zonelist"
addnet.widget = "radio"
fwzone:depends("_attach", "")
fwzone.default = arg[1] and "zone_" .. arg[1]:gsub("[^%w_]+", "_")
function attachnet.write(self, section, value) sifname = m:field(Value, "_ifname", translate("Cover the following interface"),
local net, zone translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
if value == "1" then sifname.widget = "radio"
net = nw:get_network(addnet:formvalue(section)) sifname.template = "cbi/network_ifacelist"
if net then sifname.nobridges = true
net:type("bridge") sifname:depends("_bridge", "")
end
else
local zval = fwzone:formvalue(section)
net = nw:add_network(newnet:formvalue(section), { proto = "none" })
zone = fw:get_zone(zval)
if not zone and zval == '-' then mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"),
zval = m:formvalue(fwzone:cbid(section) .. ".newzone") translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
if zval and #zval > 0 then
zone = fw:add_zone(zval) mifname.widget = "checkbox"
else mifname.template = "cbi/network_ifacelist"
fw:del_network(arg[1]) mifname.nobridges = true
end mifname:depends("_bridge", "1")
end
function newnet.write(self, section, value)
local bridge = netbridge:formvalue(section) == "1"
local ifaces = bridge and mifname:formvalue(section) or sifname:formvalue(section)
local nn = nw:add_network(value, { proto = "none" })
if nn then
if bridge then
nn:set("type", "bridge")
end end
if not net then local iface
self.error = { [section] = "missing" } for iface in utl.imatch(ifaces) do
else nn:add_interface(iface)
net:add_interface(arg[1]) if not bridge then
break
if zone then
fw:del_network(net:name())
zone:add_network(net:name())
end
uci:save("wireless")
uci:save("network")
uci:save("firewall")
luci.http.redirect(luci.dispatcher.build_url("admin/network/network", net:name()))
end end
end end
function fwzone.cfgvalue(self, section) nw:save("network")
self.iface = section nw:save("wireless")
local z = fw:get_zone_by_network(section)
return z and z:name() luci.http.redirect(luci.dispatcher.build_url("admin/network/network", nn:name()))
end
end end
return m return m