Skip to content

Commit 0fc2898

Browse files
committed
create/delete FreeRADIUS user (initial commit)
1 parent 92d2620 commit 0fc2898

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-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: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 BooleanField $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' => false],
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' => false],
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 BooleanField(
76+
required: false,
77+
default: false,
78+
indicates_true: 'on',
79+
indicates_false: 'off',
80+
internal_name: 'varusersmotpenable',
81+
);
82+
$this->motp_authmethod = new StringField(
83+
required: false,
84+
conditions: ['motp_enable' => true],
85+
choices: [ 'motp', 'googleauth' ],
86+
default: 'googleauth',
87+
internal_name: 'varusersauthmethod',
88+
);
89+
$this->motp_secret = new StringField(
90+
required: true,
91+
conditions: ['motp_enable' => true],
92+
allow_null: false,
93+
internal_name: 'varusersmotpinitsecret',
94+
sensitive: true,
95+
);
96+
$this->motp_pin = new StringField(
97+
required: true,
98+
conditions: ['motp_enable' => true],
99+
allow_null: false,
100+
minimum_length: 4,
101+
maximum_length: 4,
102+
internal_name: 'varusersmotppin',
103+
sensitive: true,
104+
);
105+
$this->motp_offset = new IntegerField(
106+
required: false,
107+
conditions: ['motp_enable' => true],
108+
allow_null: false,
109+
default: 0,
110+
internal_name: 'varusersmotpoffset',
111+
);
112+
113+
$this->description = new StringField(
114+
required: false,
115+
allow_empty: true,
116+
default: "",
117+
validators: [
118+
new RegexValidator(pattern: "/^[a-zA-Z0-9 _,.;:+=()-]*$/", error_msg: 'Value contains invalid characters.'),
119+
],
120+
);
121+
122+
parent::__construct($id, $parent_id, $data, ...$options);
123+
}
124+
125+
126+
/**
127+
*
128+
*/
129+
public function _create() {
130+
$input_errors = [];
131+
132+
$user = $this->to_internal();
133+
freeradius_validate_users($user, $input_errors);
134+
135+
if ( ! empty($input_errors) ) {
136+
throw new ServerError(
137+
message: "Some errors occured: input_errors={$input_errors[0]}",
138+
response_id: 'FIELD_INVALID_CHOICE'
139+
);
140+
}
141+
142+
parent::_create();
143+
}
144+
145+
146+
/**
147+
* Apply the creation of this User.
148+
*/
149+
public function apply_create() {
150+
freeradius_users_resync();
151+
}
152+
153+
/**
154+
* Apply the deletion of this User.
155+
*/
156+
public function apply_delete() {
157+
freeradius_users_resync();
158+
}
159+
}
160+

0 commit comments

Comments
 (0)