Skip to content

Commit ce1eea7

Browse files
fix: fixed dynamic field internal names in HAProxyFrontendAction and HAProxyBackendAction #481
1 parent 8782e8f commit ce1eea7

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/HAProxyBackendAction.inc

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use RESTAPI\Fields\StringField;
1212
*/
1313
class HAProxyBackendAction extends Model {
1414
public StringField $action;
15-
public ForeignModelField $acl;
16-
public ForeignModelField $server;
15+
public StringField $acl;
16+
public StringField $server;
1717
public StringField $customaction;
1818
public StringField $deny_status;
1919
public StringField $realm;
@@ -30,7 +30,7 @@ class HAProxyBackendAction extends Model {
3030
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
3131
# Set model attributes
3232
$this->parent_model_class = 'HAProxyBackend';
33-
$this->config_path = 'a_actionsitems/item';
33+
$this->config_path = 'a_actionitems/item';
3434
$this->packages = ['pfSense-pkg-haproxy'];
3535
$this->package_includes = ['haproxy/haproxy.inc', 'haproxy/haproxy_utils.inc'];
3636
$this->many = true;
@@ -86,15 +86,11 @@ class HAProxyBackendAction extends Model {
8686
],
8787
help_text: 'The action to take when an ACL match is found.',
8888
);
89-
$this->acl = new ForeignModelField(
90-
model_name: 'HAProxyBackendACL',
91-
model_field: 'name',
89+
$this->acl = new StringField(
9290
required: true,
9391
help_text: 'The name of the backend ACL this action is associated with.',
9492
);
95-
$this->server = new ForeignModelField(
96-
model_name: 'HAProxyBackendServer',
97-
model_field: 'name',
93+
$this->server = new StringField(
9894
required: true,
9995
conditions: ['action' => 'use_server'],
10096
help_text: 'The backend server to use when an ACL match is found.',
@@ -233,15 +229,16 @@ class HAProxyBackendAction extends Model {
233229
}
234230

235231
/**
236-
* Extends the default `from_internal()` method to automatically set the internal names of fields before calling.
232+
* Extends the default `from_internal_object()` method to automatically set the internal names of fields before calling.
233+
* @param array $internal_object The internal representation of this model to load from.
237234
*/
238-
public function from_internal(): void {
235+
public function from_internal_object(array $internal_object): void {
239236
# Do an initial load of the object to ensure the `action` is obtained first
240-
parent::from_internal();
237+
$this->action->value = $internal_object['action'];
241238

242239
# Set the internal names of all fields before doing another load to include dynamic field names
243240
$this->set_field_internal_names();
244-
parent::from_internal();
241+
parent::from_internal_object($internal_object);
245242
}
246243

247244
/**
@@ -256,9 +253,9 @@ class HAProxyBackendAction extends Model {
256253
# Loop through each field and change it's internal name to be prefixed with the current `action` assigned
257254
foreach ($fields as $field) {
258255
# Don't change exempt field's internal names
259-
if (!in_array($field, $exempt_fields)) {
260-
$field = str_replace('lua_function', 'lua-function', $field);
261-
$this->$field->internal_name = $this->action->value . $field;
256+
if (!in_array($field, $exempt_fields) and $this->action->value and $this->$field) {
257+
$corrected_field = str_replace('lua_function', 'lua-function', $field);
258+
$this->$field->internal_name = $this->action->value . $corrected_field;
262259
}
263260
}
264261
}

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/HAProxyFrontendAction.inc

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ namespace RESTAPI\Models;
44

55
use RESTAPI\Core\Model;
66
use RESTAPI\Dispatchers\HAProxyApplyDispatcher;
7-
use RESTAPI\Fields\ForeignModelField;
87
use RESTAPI\Fields\StringField;
98

109
/**
1110
* Defines a Model for HAProxy Frontend Actions.
1211
*/
1312
class HAProxyFrontendAction extends Model {
1413
public StringField $action;
15-
public ForeignModelField $acl;
16-
public ForeignModelField $backend;
14+
public StringField $acl;
15+
public StringField $backend;
1716
public StringField $customaction;
1817
public StringField $deny_status;
1918
public StringField $realm;
@@ -30,7 +29,7 @@ class HAProxyFrontendAction extends Model {
3029
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
3130
# Set model attributes
3231
$this->parent_model_class = 'HAProxyFrontend';
33-
$this->config_path = 'a_actionsitems/item';
32+
$this->config_path = 'a_actionitems/item';
3433
$this->packages = ['pfSense-pkg-haproxy'];
3534
$this->package_includes = ['haproxy/haproxy.inc', 'haproxy/haproxy_utils.inc'];
3635
$this->many = true;
@@ -86,15 +85,11 @@ class HAProxyFrontendAction extends Model {
8685
],
8786
help_text: 'The action to take when an ACL match is found.',
8887
);
89-
$this->acl = new ForeignModelField(
90-
model_name: 'HAProxyBackendACL',
91-
model_field: 'name',
88+
$this->acl = new StringField(
9289
required: true,
9390
help_text: 'The name of the frontend ACL this action is associated with.',
9491
);
95-
$this->backend = new ForeignModelField(
96-
model_name: 'HAProxyBackend',
97-
model_field: 'name',
92+
$this->backend = new StringField(
9893
required: true,
9994
conditions: ['action' => 'use_backend'],
10095
help_text: 'The backend to use when an ACL match is found.',
@@ -233,15 +228,16 @@ class HAProxyFrontendAction extends Model {
233228
}
234229

235230
/**
236-
* Extends the default `from_internal()` method to automatically set the internal names of fields before calling.
231+
* Extends the default `from_internal_object()` method to automatically set the internal names of fields before calling.
232+
* @param array $internal_object The internal representation of this model to load from.
237233
*/
238-
public function from_internal(): void {
234+
public function from_internal_object(array $internal_object): void {
239235
# Do an initial load of the object to ensure the `action` is obtained first
240-
parent::from_internal();
236+
parent::from_internal_object($internal_object);
241237

242238
# Set the internal names of all fields before doing another load to include dynamic field names
243239
$this->set_field_internal_names();
244-
parent::from_internal();
240+
parent::from_internal_object($internal_object);
245241
}
246242

247243
/**
@@ -256,9 +252,9 @@ class HAProxyFrontendAction extends Model {
256252
# Loop through each field and change it's internal name to be prefixed with the current `action` assigned
257253
foreach ($fields as $field) {
258254
# Don't change exempt field's internal names
259-
if (!in_array($field, $exempt_fields)) {
260-
$field = str_replace('lua_function', 'lua-function', $field);
261-
$this->$field->internal_name = $this->action->value . $field;
255+
if (!in_array($field, $exempt_fields) and $this->action->value and $this->$field) {
256+
$corrected_field = str_replace('lua_function', 'lua-function', $field);
257+
$this->$field->internal_name = $this->action->value . $corrected_field;
262258
}
263259
}
264260
}

0 commit comments

Comments
 (0)