93cdecac78518de8f3632cf4877bffee31031207
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local nw  = require "luci.model.network"
16 local fw  = require "luci.model.firewall"
17 local uci = require "luci.model.uci".cursor()
18
19 m = SimpleForm("network", translate("a_n_create", "Create Or Attach Network"),
20         translate("a_n_c_desc",
21                 "If the interface is attached to an existing network it will be <em>bridged</em> " ..
22                 "to the existing interfaces and is covered by the firewall zone of the choosen network.<br />" ..
23                 "Uncheck the attach option to define a new standalone network for this interface."
24         ))
25
26 nw.init(uci)
27 fw.init(uci)
28
29 attachnet = m:field(Flag, "_attach", translate("a_n_c_attach", "Attach to existing network"))
30 attachnet.rmempty = false
31 attachnet.default = "1"
32
33 newnet = m:field(Value, "_netname_new", translate("a_n_c_netname", "Name of the new network"),
34         translate("a_n_c_netname_desc",
35                 "The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
36                 "<code>0-9</code> and <code>_</code>"
37         ))
38
39 newnet:depends("_attach", "")
40 newnet.default = "net_" .. arg[1]:gsub("[^%w_]+", "_")
41
42 addnet = m:field(Value, "_netname_attach",
43         translate("a_n_c_network", "Network to attach interface to"))
44
45 addnet.template = "cbi/network_netlist"
46 addnet.widget = "radio"
47 addnet.nocreate = true
48 addnet:depends("_attach", "1")
49
50 fwzone = m:field(Value, "_fwzone",
51         translate("network_interface_fwzone"),
52         translate("network_interface_fwzone_desc"))
53
54 fwzone.template = "cbi/firewall_zonelist"
55 addnet.widget = "radio"
56 fwzone:depends("_attach", "")
57 fwzone.default = "zone_" .. arg[1]:gsub("[^%w_]+", "_")
58
59
60 function attachnet.write(self, section, value)
61         local net, zone
62
63         if value == "1" then
64                 net = nw:get_network(addnet:formvalue(section))
65                 if net then
66                         net:type("bridge")
67                 end
68         else
69                 local zval = fwzone:formvalue(section)
70                 
71                 net = nw:add_network(newnet:formvalue(section), { proto  = "none" })
72                 zone = fw:get_zone(zval)
73
74                 if not zone and zval == '-' then
75                         zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
76                         if zval and #zval > 0 then
77                                 zone = fw:add_zone(zval)
78                         else
79                                 fw:del_network(arg[1])
80                         end
81                 end
82         end
83
84         if not net then
85                 self.error = { [section] = "missing" }
86         else
87                 net:add_interface(arg[1])
88
89                 if zone then
90                         fw:del_network(net:name())
91                         zone:add_network(net:name())
92                 end
93
94                 uci:save("wireless")
95                 uci:save("network")
96                 uci:save("firewall")
97                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", net:name()))
98         end
99 end
100
101 function fwzone.cfgvalue(self, section)
102         self.iface = section
103         local z = fw:get_zone_by_network(section)
104         return z and z:name()
105 end
106
107 return m