@@ -51,7 +51,7 @@ class APIFirewallRuleUpdate extends APIModel {
5151 $ this ->errors [] = APIResponse \get (4031 );
5252 }
5353
54- # Check for our required 'type' payload value
54+ # Check for our optional 'type' payload value
5555 if (isset ($ this ->initial_data ["type " ])) {
5656 $ type_options = array ("pass " , "block " , "reject " );
5757 # Ensure our type is a valid option
@@ -62,7 +62,7 @@ class APIFirewallRuleUpdate extends APIModel {
6262 }
6363 }
6464
65- # Check for our required 'interface' payload value
65+ # Check for our optional 'interface' payload value
6666 if (isset ($ this ->initial_data ['interface ' ])) {
6767 $ this ->initial_data ['interface ' ] = APITools \get_pfsense_if_id ($ this ->initial_data ['interface ' ]);
6868 # Check that we found the request pfSense interface ID
@@ -73,7 +73,7 @@ class APIFirewallRuleUpdate extends APIModel {
7373 }
7474 }
7575
76- # Check for our required 'ipprotocol' payload value
76+ # Check for our optional 'ipprotocol' payload value
7777 if (isset ($ this ->initial_data ['ipprotocol ' ])) {
7878 $ ipprotocol_options = array ("inet " , "inet6 " , "inet46 " );
7979 # Check that our ipprotocol value is a support option
@@ -84,13 +84,21 @@ class APIFirewallRuleUpdate extends APIModel {
8484 }
8585 }
8686
87- # Check for our required 'protocol' payload value
87+ # Check for our optional 'protocol' payload value
8888 if (isset ($ this ->initial_data ['protocol ' ])) {
89- $ port_required = (!in_array ($ this ->validated_data ["protocol " ], ["tcp " , "udp " , "tcp/udp " ])) ? true : false ;
89+ $ missing_port = (!in_array ($ this ->validated_data ["protocol " ], ["tcp " , "udp " , "tcp/udp " ])) ? true : false ;
90+ $ requires_port = (in_array ($ this ->initial_data ["protocol " ], ["tcp " , "udp " , "tcp/udp " ])) ? true : false ;
9091 $ protocol_options = [
9192 "any " , "tcp " , "udp " , "tcp/udp " , "icmp " , "esp " , "ah " ,
9293 "gre " , "ipv6 " , "igmp " , "pim " , "ospf " , "carp " , "pfsync "
9394 ];
95+
96+ # If a new protocol was chosen that doesn't require a port, remove existing ports from the rule
97+ if ((!in_array ($ this ->initial_data ["protocol " ], ["tcp " , "udp " , "tcp/udp " ]))) {
98+ unset($ this ->validated_data ["source " ]["port " ]);
99+ unset($ this ->validated_data ["destination " ]["port " ]);
100+ }
101+
94102 # Check that our protocol value is a support option
95103 if (in_array ($ this ->initial_data ["protocol " ], $ protocol_options )) {
96104 # Don't add a specific protocol if any
@@ -102,9 +110,10 @@ class APIFirewallRuleUpdate extends APIModel {
102110 } else {
103111 $ this ->errors [] = APIResponse \get (4042 );
104112 }
105- $ protocol = $ this ->initial_data ['protocol ' ];
106113 } else {
107- $ this ->errors [] = APIResponse \get (4036 );
114+ # If a new protocol was not selected don't validate ports
115+ $ requires_port = false ;
116+ $ missing_port = false ;
108117 }
109118
110119 # Check for our optional 'icmpsubtype' payload value when our protocol is set to ICMP
@@ -130,7 +139,7 @@ class APIFirewallRuleUpdate extends APIModel {
130139 }
131140 }
132141
133- # Check for our required 'src' payload value
142+ # Check for our optional 'src' payload value
134143 if (isset ($ this ->initial_data ['src ' ])) {
135144 // Check if our source and destination values are valid
136145 $ dir_check = APITools \is_valid_rule_addr ($ this ->initial_data ['src ' ], "source " );
@@ -141,7 +150,7 @@ class APIFirewallRuleUpdate extends APIModel {
141150 }
142151 }
143152
144- # Check for our required 'src' payload value
153+ # Check for our optional 'src' payload value
145154 if (isset ($ this ->initial_data ['dst ' ])) {
146155 // Check if our source and destination values are valid
147156 $ dir_check = APITools \is_valid_rule_addr ($ this ->initial_data ['dst ' ], "destination " );
@@ -152,16 +161,16 @@ class APIFirewallRuleUpdate extends APIModel {
152161 }
153162 }
154163
155- # Check for our required 'srcport' and 'dstport' payload values if protocol is TCP, UDP or TCP/UDP
156- if ($ port_required && in_array ( $ this -> initial_data [ " protocol " ], [ " tcp " , " udp " , " tcp/udp " ]) ) {
164+ # Check for our optional 'srcport' and 'dstport' payload values if protocol is TCP, UDP or TCP/UDP
165+ if ($ requires_port ) {
157166 if (isset ($ this ->initial_data ['srcport ' ])) {
158167 $ val = str_replace ("- " , ": " , $ this ->initial_data ['srcport ' ]);
159168 if (!is_port_or_range ($ val ) and $ val !== "any " ) {
160169 $ this ->errors [] = APIResponse \get (4048 );
161170 } elseif ($ val !== "any " ) {
162171 $ this ->validated_data ["source " ]["port " ] = str_replace (": " , "- " , $ val );;
163172 }
164- } else {
173+ } elseif ( $ missing_port ) {
165174 $ this ->errors [] = APIResponse \get (4047 );
166175 }
167176
@@ -172,7 +181,7 @@ class APIFirewallRuleUpdate extends APIModel {
172181 } elseif ($ val !== "any " ) {
173182 $ this ->validated_data ["destination " ]["port " ] = str_replace (": " , "- " , $ val );;
174183 }
175- } else {
184+ } elseif ( $ missing_port ) {
176185 $ this ->errors [] = APIResponse \get (4047 );
177186 }
178187 }
@@ -189,8 +198,10 @@ class APIFirewallRuleUpdate extends APIModel {
189198
190199
191200 # Check for our optional 'disabled' payload value
192- if ($ this ->initial_data ['disabled ' ] !== true ) {
193- $ this ->validated_data ["id " ] = "" ;
201+ if ($ this ->initial_data ['disabled ' ] === true ) {
202+ $ this ->validated_data ["disabled " ] = "" ;
203+ } elseif ($ this ->initial_data ['disabled ' ] === false ) {
204+ unset($ this ->validated_data ["disabled " ]);
194205 }
195206
196207 # Check for our optional 'descr' payload value
0 commit comments