Skip to content

Commit

Permalink
Merge ef81430 into 894d95f
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Sep 19, 2018
2 parents 894d95f + ef81430 commit 3cf522f
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/lib/y2firewall/widgets/pages/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Zone < CWM::Page
# @param zone [Y2Firewall::Firewalld::Zone]
# @param pager [CWM::TreePager]
def initialize(zone, pager)
Yast.import "Popup"
textdomain "firewall"
@zone = zone
@pager = pager
Expand Down Expand Up @@ -106,10 +107,19 @@ def contents
VBox(* fields)
end

def valid_port_description
format(
_("Enter ports or port ranges, separated by spaces and/or commas.\n" \
"A port is an integer.\n" \
"A port range is port-dash-port (with no spaces).\n" \
"For example:\n" \
"%s"),
"16001-16009, 18080"
)
end

def help
"FIXME: ports or port ranges, separated by spaces and/or commas <br>" \
"a port is an integer <br>" \
"a port range is port-dash-port (with no spaces)"
valid_port_description.gsub("\n", "<br>\n")
end

def init
Expand All @@ -119,16 +129,37 @@ def init
end
end

# FIXME: validation, cleanup, error reporting
def store
by_proto = PROTOCOLS.map do |sym, _label|
by_proto = values
@zone.ports = ports_to_array(by_proto.to_h)
end

def validate
by_proto = values
by_proto.each do |sym, ranges|
invalid_range = ranges.find { |r| !valid_range?(r) }
next unless invalid_range
Yast::UI.SetFocus(Id(sym))
err_msg = format(_("Invalid port range: %s"), invalid_range)
Yast::Popup.Error(err_msg + "\n" + valid_port_description)
return false
end
true
end

private

# @return [Hash{Symbol => Array<String>}]
def values
PROTOCOLS.map do |sym, _label|
line = Yast::UI.QueryWidget(Id(sym), :Value)
[sym, items_from_ui(line)]
end
@zone.ports = ports_to_array(by_proto.to_h)
end

private
def valid_range?(r)
r =~ /\d+/ || r =~ /\d+-\d+/
end

def items_from_ui(s)
# the separator is at least one comma or space, surrounded by optional spaces
Expand Down

0 comments on commit 3cf522f

Please sign in to comment.