Skip to content

Commit 0d73211

Browse files
Merge pull request #698 from vitspec99/FreeRADIUS-users
create/delete FreeRADIUS user (initial commit)
2 parents 8966314 + ec57b29 commit 0d73211

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace RESTAPI\Endpoints;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
9+
/**
10+
* Defines an Endpoint for interacting with a single OpenVPNExport Model object at
11+
* /api/v2/vpn/openvpn/clientexport.
12+
*/
13+
class FreeRADIUSUser extends Endpoint {
14+
public function __construct() {
15+
/**
16+
* Set Endpoint attributes
17+
*/
18+
$this->url = '/api/v2/services/freeradius/user';
19+
$this->model_name = 'FreeRADIUSUser';
20+
$this->request_method_options = ['GET', 'POST', 'DELETE'];
21+
$this->many = false;
22+
23+
# Construct the parent Endpoint object
24+
parent::__construct();
25+
}
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace RESTAPI\Endpoints;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
9+
/**
10+
* Defines an Endpoint for interacting with a single OpenVPNExport Model object at
11+
* /api/v2/vpn/openvpn/clientexport.
12+
*/
13+
class ServicesFreeRADIUSUserEndpoint extends Endpoint {
14+
public function __construct() {
15+
/**
16+
* Set Endpoint attributes
17+
*/
18+
$this->url = '/api/v2/services/freeradius/user';
19+
$this->model_name = 'FreeRADIUSUser';
20+
$this->many = false;
21+
$this->request_method_options = ['GET', 'POST', 'DELETE'];
22+
23+
# Construct the parent Endpoint object
24+
parent::__construct();
25+
}
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace RESTAPI\Endpoints;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
9+
/**
10+
* Defines an Endpoint for interacting with a single OpenVPNExport Model object at
11+
* /api/v2/vpn/openvpn/clientexport.
12+
*/
13+
class ServicesFreeRADIUSUsersEndpoint extends Endpoint {
14+
public function __construct() {
15+
/**
16+
* Set Endpoint attributes
17+
*/
18+
$this->url = '/api/v2/services/freeradius/users';
19+
$this->model_name = 'FreeRADIUSUser';
20+
$this->many = true;
21+
$this->request_method_options = ['GET', 'DELETE'];
22+
23+
# Construct the parent Endpoint object
24+
parent::__construct();
25+
}
26+
}
27+
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
namespace RESTAPI\Models;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Model;
8+
use RESTAPI\Fields\Base64Field;
9+
use RESTAPI\Fields\BooleanField;
10+
use RESTAPI\Fields\ForeignModelField;
11+
use RESTAPI\Fields\IntegerField;
12+
use RESTAPI\Fields\PortField;
13+
use RESTAPI\Fields\ObjectField;
14+
use RESTAPI\Fields\StringField;
15+
use RESTAPI\Responses\ConflictError;
16+
use RESTAPI\Responses\ValidationError;
17+
use RESTAPI\Responses\ServerError;
18+
use RESTAPI\Validators\HostnameValidator;
19+
use RESTAPI\Validators\IPAddressValidator;
20+
use RESTAPI\Validators\RegexValidator;
21+
22+
/**
23+
* Defines a Model that represents OpenVPN Client config Export.
24+
*/
25+
class FreeRADIUSUser extends Model {
26+
27+
public StringField $username;
28+
public StringField $password;
29+
public StringField $password_encryption;
30+
public StringField $motp_enable;
31+
public StringField $motp_authmethod;
32+
public StringField $motp_secret;
33+
public StringField $motp_pin;
34+
public IntegerField $motp_offset;
35+
public StringField $description;
36+
37+
/**
38+
*
39+
*/
40+
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) {
41+
#
42+
# Set model attributes
43+
#
44+
$this->packages = ['pfSense-pkg-freeradius3'];
45+
$this->package_includes = ['freeradius.inc'];
46+
$this->config_path = 'installedpackages/freeradius/config';
47+
$this->many = true;
48+
$this->always_apply = true;
49+
50+
#
51+
# Set model fields
52+
#
53+
$this->username = new StringField(
54+
required: true,
55+
unique: true,
56+
internal_name: 'varusersusername',
57+
);
58+
59+
$this->password = new StringField(
60+
required: true,
61+
conditions: ['motp_enable' => 'off'],
62+
allow_empty: false,
63+
allow_null: false,
64+
internal_name: 'varuserspassword',
65+
sensitive: true,
66+
);
67+
$this->password_encryption = new StringField(
68+
required: false,
69+
conditions: ['motp_enable' => 'off'],
70+
choices: [ 'Cleartext-Password', 'MD5-Password', 'MD5-Password-hashed', 'NT-Password-hashed' ],
71+
default: 'Cleartext-Password',
72+
internal_name: 'varuserspasswordencryption',
73+
);
74+
75+
$this->motp_enable = new StringField(
76+
required: true,
77+
choices: [ 'on', 'off' ],
78+
internal_name: 'varusersmotpenable',
79+
);
80+
$this->motp_authmethod = new StringField(
81+
required: false,
82+
conditions: ['motp_enable' => 'on'],
83+
choices: [ 'motp', 'googleauth' ],
84+
default: 'googleauth',
85+
internal_name: 'varusersauthmethod',
86+
);
87+
$this->motp_secret = new StringField(
88+
required: true,
89+
conditions: ['motp_enable' => 'on'],
90+
allow_null: false,
91+
internal_name: 'varusersmotpinitsecret',
92+
sensitive: true,
93+
);
94+
$this->motp_pin = new StringField(
95+
required: true,
96+
conditions: ['motp_enable' => 'on'],
97+
allow_null: false,
98+
minimum_length: 4,
99+
maximum_length: 4,
100+
internal_name: 'varusersmotppin',
101+
sensitive: true,
102+
);
103+
$this->motp_offset = new IntegerField(
104+
required: false,
105+
conditions: ['motp_enable' => 'on'],
106+
allow_null: false,
107+
default: 0,
108+
internal_name: 'varusersmotpoffset',
109+
);
110+
111+
$this->description = new StringField(
112+
required: false,
113+
allow_empty: true,
114+
default: "",
115+
validators: [
116+
new RegexValidator(pattern: "/^[a-zA-Z0-9 _,.;:+=()-]*$/", error_msg: 'Value contains invalid characters.'),
117+
],
118+
);
119+
120+
parent::__construct($id, $parent_id, $data, ...$options);
121+
}
122+
123+
124+
/**
125+
*
126+
*/
127+
public function _create() {
128+
$input_errors = [];
129+
130+
if ( $this->motp_enable->value == 'off' ) {
131+
$this->motp_enable->value = '';
132+
}
133+
134+
$user = $this->to_internal();
135+
136+
freeradius_validate_users($user, $input_errors);
137+
138+
if ( ! empty($input_errors) ) {
139+
throw new ServerError(
140+
message: "Some errors occured: input_errors={$input_errors[0]}",
141+
response_id: 'FIELD_INVALID_CHOICE'
142+
);
143+
}
144+
145+
parent::_create();
146+
}
147+
148+
149+
/**
150+
* Apply the creation of this User.
151+
*/
152+
public function apply_create() {
153+
freeradius_users_resync();
154+
}
155+
156+
/**
157+
* Apply the deletion of this User.
158+
*/
159+
public function apply_delete() {
160+
freeradius_users_resync();
161+
}
162+
}
163+

0 commit comments

Comments
 (0)