@@ -5,17 +5,9 @@ namespace RESTAPI\Models;
55require_once 'RESTAPI/autoloader.inc ' ;
66
77use RESTAPI \Core \Model ;
8- use RESTAPI \Fields \Base64Field ;
9- use RESTAPI \Fields \BooleanField ;
10- use RESTAPI \Fields \ForeignModelField ;
11- use RESTAPI \Fields \IntegerField ;
128use RESTAPI \Fields \PortField ;
13- use RESTAPI \Fields \ObjectField ;
149use RESTAPI \Fields \StringField ;
15- use RESTAPI \Responses \ConflictError ;
1610use RESTAPI \Responses \ValidationError ;
17- use RESTAPI \Responses \ServerError ;
18- use RESTAPI \Validators \HostnameValidator ;
1911use RESTAPI \Validators \IPAddressValidator ;
2012use RESTAPI \Validators \RegexValidator ;
2113
@@ -26,59 +18,50 @@ class FreeRADIUSInterface extends Model {
2618 public StringField $ addr ;
2719 public PortField $ port ;
2820 public StringField $ type ;
29- public StringField $ ipv ;
21+ public StringField $ ip_version ;
3022 public StringField $ description ;
3123
32- /**
33- *
34- */
3524 public function __construct (mixed $ id = null , mixed $ parent_id = null , mixed $ data = [], mixed ...$ options ) {
36- #
3725 # Set model attributes
38- #
3926 $ this ->packages = ['pfSense-pkg-freeradius3 ' ];
4027 $ this ->package_includes = ['freeradius.inc ' ];
4128 $ this ->config_path = 'installedpackages/freeradiusinterfaces/config ' ;
4229 $ this ->many = true ;
4330 $ this ->always_apply = true ;
44- $ this ->unique_together_fields = ['addr ' , 'port ' , 'ipv ' ];
31+ $ this ->unique_together_fields = ['addr ' , 'port ' , 'ip_version ' ];
4532
46- #
4733 # Set model fields
48- #
4934 $ this ->addr = new StringField (
50- internal_name: 'varinterfaceip ' ,
5135 required: true ,
36+ internal_name: 'varinterfaceip ' ,
5237 validators: [new IPAddressValidator (allow_ipv4: true , allow_ipv6: true , allow_keywords: ['* ' ])],
5338 help_text: 'The IP address of the listening interface. If you choose * then it means all interfaces. ' ,
5439 );
5540 $ this ->port = new PortField (
56- internal_name: 'varinterfaceport ' ,
5741 required: false ,
42+ default: '1812 ' ,
5843 allow_alias: false ,
5944 allow_range: false ,
60- default : '1812 ' ,
45+ internal_name : 'varinterfaceport ' ,
6146 help_text: 'The port number of the listening interface. Different interface types need different ports. ' ,
6247 );
48+ $ this ->ip_version = new StringField (
49+ required: true ,
50+ choices: ['ipaddr ' , 'ipv6addr ' ],
51+ internal_name: 'varinterfaceipversion ' ,
52+ help_text: 'The IP version of the listening interface. ' ,
53+ );
6354 $ this ->type = new StringField (
64- internal_name: 'varinterfacetype ' ,
6555 required: false ,
66- choices: ['auth ' , 'acct ' ],
6756 default: 'auth ' ,
57+ choices: ['auth ' , 'acct ' , 'proxy ' , 'detail ' , 'status ' , 'coa ' ],
58+ internal_name: 'varinterfacetype ' ,
6859 help_text: 'The type of the listening interface: Authentication/Accounting. ' ,
6960 );
70- $ this ->ipv = new StringField (
71- internal_name: 'varinterfaceipversion ' ,
72- choices: ['ipaddr ' , 'ipv6addr ' ],
73- allow_empty: true ,
74- required: true ,
75- conditions: ['addr ' => '* ' ],
76- help_text: 'The IP version of the listening interface. ' ,
77- );
7861 $ this ->description = new StringField (
7962 required: false ,
80- allow_empty: true ,
8163 default: '' ,
64+ allow_empty: true ,
8265 validators: [
8366 new RegexValidator (
8467 pattern: "/^[a-zA-Z0-9 _,.;:+=()-]*$/ " ,
@@ -92,29 +75,44 @@ class FreeRADIUSInterface extends Model {
9275 }
9376
9477 /**
95- * Perform additional validation on the Model's fields and data.
78+ * Perform extra validation on the Model's 'addr' field.
79+ * @param string $value The value to validate.
80+ * @returns string The validated value.
81+ * @throws ValidationError If the value does not match IP version specified in the 'ip_version' field.
9682 */
97- public function validate_extra (): void {
98- $ input_errors = [];
83+ public function validate_addr (string $ value ): string {
84+ # Asterisk (*) is always a valid value for the addr field, so return it without further validation
85+ if ($ value === '* ' ) {
86+ return $ value ;
87+ }
9988
100- $ iface_addr = $ this ->addr ->value ;
101- if ($ iface_addr != '* ' ) {
102- if (is_ipaddrv4 ($ iface_addr )) {
103- $ this ->ipv ->value = 'ipaddr ' ;
104- } elseif (is_ipaddrv6 ($ iface_addr )) {
105- $ this ->ipv ->value = 'ipv6addr ' ;
106- } else {
107- // we don't must be here because Model validator for $this->addr
108- $ input_errors [] = "Cann't recognize IP-address= {$ iface_addr }" ;
109- }
89+ # Do not allow the value to be an IPv4 address if ip_version is 'ipv6addr'
90+ if ($ this ->addr ->has_label ('is_ipaddrv4 ' ) and $ this ->ip_version ->value === 'ipv6addr ' ) {
91+ throw new ValidationError (
92+ message: "Field `addr` cannot be an IPv4 address when `ip_version` is set to `ipv6addr`, received ` $ value`. " ,
93+ response_id: 'FREERADIUS_INTERFACE_ADDR_IPV4_NOT_ALLOWED ' ,
94+ );
11095 }
11196
112- # Run service level validations
113- $ iface = $ this ->to_internal ();
114- freeradius_validate_interfaces ($ iface , $ input_errors );
97+ # Do not allow the value to be an IPv6 address if ip_version is 'ipaddr'
98+ if ($ this ->addr ->has_label ('is_ipaddrv6 ' ) and $ this ->ip_version ->value === 'ipaddr ' ) {
99+ throw new ValidationError (
100+ message: "Field `addr` cannot be an IPv6 address when `ip_version` is set to `ipaddr`, received ` $ value`. " ,
101+ response_id: 'FREERADIUS_INTERFACE_ADDR_IPV6_NOT_ALLOWED ' ,
102+ );
103+ }
115104
105+ return $ value ;
106+ }
107+
108+ /**
109+ * Perform additional validation on the Model's fields and data.
110+ */
111+ public function validate_extra (): void {
116112 # If there were validation errors that were not caught by the model fields, throw a ValidationError.
117113 # Ideally the Model should catch all validation errors itself so prompt the user to report this error
114+ $ input_errors = [];
115+ freeradius_validate_interfaces ($ this ->to_internal (), $ input_errors );
118116 if (!empty ($ input_errors )) {
119117 throw new ValidationError (
120118 message: "An unexpected validation error has occurred: $ input_errors [0 ]. Please report this issue at " .
@@ -127,7 +125,7 @@ class FreeRADIUSInterface extends Model {
127125 /**
128126 * Apply the action on Interface(s)
129127 */
130- public function apply () {
128+ public function apply (): void {
131129 freeradius_settings_resync ();
132130 }
133131}
0 commit comments