Changeset 5374

Show
Ignore:
Timestamp:
10/08/09 02:22:21 (4 years ago)
Author:
jow
Message:

libs/core: luci.model.firewall: make top level functions instance methods to keep interface consistent, implement rename_zone()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/core/luasrc/model/firewall.lua

    r5367 r5374  
    3838end 
    3939 
    40 function add_zone(n) 
    41     if n then 
     40function add_zone(self, n) 
     41    if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not self:get_zone(n) then 
    4242        local z = ub.uci:section("firewall", "zone", nil, { 
    4343            name    = n, 
     
    5252end 
    5353 
    54 function get_zone(n) 
     54function get_zone(self, n) 
    5555    local z 
    5656    ub.uci:foreach("firewall", "zone", 
     
    6464end 
    6565 
    66 function get_zones() 
     66function get_zones(self) 
    6767    local zones = { } 
    6868    ub.uci:foreach("firewall", "zone", 
     
    7575end 
    7676 
    77 function get_zones_by_network(net) 
     77function get_zones_by_network(self, net) 
    7878    local zones = { } 
    7979    ub.uci:foreach("firewall", "zone", 
     
    9292end 
    9393 
    94 function del_zone(n) 
     94function del_zone(self, n) 
    9595    local r = false 
    9696    ub.uci:foreach("firewall", "zone", 
     
    124124end 
    125125 
    126 function del_network(net) 
     126function rename_zone(self, old, new) 
     127    local r = false 
     128    if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_zone(new) then 
     129        ub.uci:foreach("firewall", "zone", 
     130            function(s) 
     131                if n and s.name == old then 
     132                    ub.uci:set("firewall", s['.name'], "name", new) 
     133                    r = true 
     134                    return false 
     135                end 
     136            end) 
     137        if r then 
     138            ub.uci:foreach("firewall", "rule", 
     139                function(s) 
     140                    if s.src == old then 
     141                        ub.uci:set("firewall", s['.name'], "src", new) 
     142                    elseif s.dest == old then 
     143                        ub.uci:set("firewall", s['.name'], "dest", new) 
     144                    end 
     145                end) 
     146            ub.uci:foreach("firewall", "redirect", 
     147                function(s) 
     148                    if s.src == old then 
     149                        ub.uci:set("firewall", s['.name'], "src", new) 
     150                    end 
     151                end) 
     152            ub.uci:foreach("firewall", "forwarding", 
     153                function(s) 
     154                    if s.src == old then 
     155                        ub.uci:set("firewall", s['.name'], "src", new) 
     156                    end 
     157                end) 
     158        end 
     159    end 
     160    return r 
     161end 
     162 
     163function del_network(self, net) 
    127164    local z 
    128165    if net then 
    129         for _, z in ipairs(get_zones()) do 
     166        for _, z in ipairs(self:get_zones()) do 
    130167            z:del_network(net) 
    131168        end