Skip to content

Commit bf8e9f7

Browse files
Restructured APIUserPrivilegeCreate and APIUserPrivilegeDelete to better match the other user endpoints
1 parent 8fa65eb commit bf8e9f7

4 files changed

Lines changed: 85 additions & 55 deletions

File tree

pfSense-pkg-API/files/etc/inc/api/models/APIUserCreate.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class APIUserCreate extends APIModel {
8989
foreach ($this->initial_data["priv"] as $priv) {
9090
if (array_key_exists($priv, $priv_list)) {
9191
$this->validated_data["priv"][] = $priv;
92+
$this->validated_data["priv"] = array_unique($this->validated_data["priv"]);
9293
} else {
9394
$this->errors[] = APIResponse\get(5006);
9495
break;

pfSense-pkg-API/files/etc/inc/api/models/APIUserPrivilegeCreate.inc

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,68 @@ class APIUserPrivilegeCreate extends APIModel {
2525
}
2626

2727
public function action() {
28-
local_user_set($this->validated_data["user_config"]); // Set user backend parameters
28+
$this->config["system"]["user"][$this->id]["priv"] = $this->validated_data["priv"];
2929
$this->write_config();
30-
return APIResponse\get(0, $this->validated_data["user_config"]["priv"]);
30+
local_user_set($this->validated_data);
31+
return APIResponse\get(0, $this->validated_data["priv"]);
3132
}
3233

3334
public function validate_payload() {
34-
global $priv_list;
35+
$this->__validate_username();
36+
$this->__validate_priv();
37+
}
38+
39+
private function __validate_username() {
40+
# Check for our required `username` payload value
3541
if (isset($this->initial_data['username'])) {
36-
$this->validated_data["username"] = trim($this->initial_data['username']);
37-
$this->validated_data["user_config"] =& getUserEntry($this->validated_data["username"]);
38-
if (!array_key_exists("uid", $this->validated_data["user_config"])) {
42+
# Loop through each configured user and check if this user exists
43+
foreach ($this->config["system"]["user"] as $id=>$user) {
44+
if ($this->initial_data["username"] === $user["name"]) {
45+
$this->validated_data = $user;
46+
$this->id = intval($id);
47+
}
48+
}
49+
# Set an error if no user was found
50+
if (!isset($this->validated_data["uid"])) {
3951
$this->errors[] = APIResponse\get(5001);
4052
}
4153
} else {
4254
$this->errors[] = APIResponse\get(5000);
4355
}
44-
if (isset($this->initial_data['priv'])) {
45-
// Ensure our new priv is array, if it is a string create an array containing the string
46-
if (is_string($this->initial_data["priv"])) {
56+
}
57+
58+
private function __validate_priv() {
59+
global $priv_list;
60+
$this->__init_config();
61+
62+
# Check for our optional `priv` payload value
63+
if ($this->initial_data["priv"]) {
64+
# Ensure value is an array
65+
if (!is_array($this->initial_data["priv"])) {
4766
$this->initial_data["priv"] = array($this->initial_data["priv"]);
4867
}
49-
if (is_array($this->initial_data["priv"])) {
50-
// Loop through our new priv list and check that the privs are valid
51-
foreach ($this->initial_data["priv"] as $np) {
52-
if (!array_key_exists($np, $priv_list)) {
53-
$this->errors[] = APIResponse\get(5006);
54-
}
55-
if (!in_array($np, $this->validated_data["user_config"]["priv"])) {
56-
$this->validated_data["user_config"]["priv"][] = $np;
57-
}
68+
69+
# Loop through each requested privilege and ensure it exists
70+
foreach ($this->initial_data["priv"] as $priv) {
71+
if (array_key_exists($priv, $priv_list)) {
72+
$this->validated_data["priv"][] = $priv;
73+
$this->validated_data["priv"] = array_unique($this->validated_data["priv"]);
74+
} else {
75+
$this->errors[] = APIResponse\get(5006);
76+
break;
5877
}
59-
} else {
60-
$this->errors[] = APIResponse\get(5005);
6178
}
62-
} else {
63-
$this->errors[] = APIResponse\get(5004);
79+
}
80+
}
81+
82+
private function __init_config() {
83+
# Initialize the priv array if the user does not already have one
84+
if (empty($this->validated_data["priv"])) {
85+
$this->validated_data["priv"] = [];
86+
}
87+
# If the user has a priv set, but as a string, convert it to an array
88+
elseif (is_string($this->validated_data["priv"])) {
89+
$this->validated_data["priv"] = array($this->validated_data["priv"]);
6490
}
6591
}
6692
}

pfSense-pkg-API/files/etc/inc/api/models/APIUserPrivilegeDelete.inc

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,52 @@ class APIUserPrivilegeDelete extends APIModel {
2323
$this->privileges = ["page-all", "page-system-usermanager-addprivs"];
2424
$this->change_note = "Deleted privileges for user via API";
2525
}
26-
2726
public function action() {
28-
$user_config =& getUserEntry($this->validated_data["username"]);
29-
$user_id = index_users()[$this->validated_data["username"]]; // Save our user's array index ID
30-
local_user_set($user_config); // Set user backend parameters
31-
$this->config["system"]["user"][$user_id] = $user_config; // Add our new config
32-
$this->write_config(); // Write to config
27+
$this->config["system"]["user"][$this->id]["priv"] = $this->validated_data["priv"];
28+
$this->write_config();
29+
local_user_set($this->validated_data);
3330
return APIResponse\get(0, $this->validated_data["priv"]);
3431
}
3532

3633
public function validate_payload() {
37-
global $priv_list;
34+
$this->__validate_username();
35+
$this->__validate_priv();
36+
}
37+
38+
private function __validate_username() {
39+
# Check for our required `username` payload value
3840
if (isset($this->initial_data['username'])) {
39-
$this->validated_data["username"] = $this->initial_data['username'];
40-
$this->validated_data["username"] = trim($this->validated_data["username"]);
41+
# Loop through each configured user and check if this user exists
42+
foreach ($this->config["system"]["user"] as $id=>$user) {
43+
if ($this->initial_data["username"] === $user["name"]) {
44+
$this->validated_data = $user;
45+
$this->id = intval($id);
46+
}
47+
}
48+
# Set an error if no user was found
49+
if (!isset($this->validated_data["uid"])) {
50+
$this->errors[] = APIResponse\get(5001);
51+
}
4152
} else {
4253
$this->errors[] = APIResponse\get(5000);
4354
}
44-
if (isset($this->initial_data['priv'])) {
45-
$this->validated_data["priv"] = $this->initial_data['priv'];
46-
} else {
47-
$this->errors[] = APIResponse\get(5004);
48-
}
49-
// Check if our user already exists, if so exit on non-zero
50-
$user_config =& getUserEntry($this->validated_data["username"]);
51-
if (!array_key_exists("uid", $user_config)) {
52-
$this->errors[] = APIResponse\get(5002);
53-
}
54-
// Ensure our new priv is array, if it is a string create an array containing the string
55-
if (is_string($this->validated_data["priv"])) {
56-
$this->validated_data["priv"] = array($this->validated_data["priv"]);
57-
}
58-
if (is_array($this->validated_data["priv"])) {
59-
// Loop through our new priv list and check that the privs are valid
60-
foreach ($this->validated_data["priv"] as $dp) {
61-
if (!array_key_exists($dp, $priv_list)) {
62-
$this->errors[] = APIResponse\get(5006);
63-
}
64-
if (in_array($dp, $user_config["priv"])) {
65-
$user_config["priv"] = \array_diff($user_config["priv"], array($dp));
55+
}
56+
57+
private function __validate_priv() {
58+
# Check for our optional `priv` payload value
59+
if ($this->initial_data["priv"]) {
60+
# Ensure value is an array
61+
if (!is_array($this->initial_data["priv"])) {
62+
$this->initial_data["priv"] = array($this->initial_data["priv"]);
63+
}
64+
65+
# Loop through each of the user's stored privileges and remove it if matched
66+
foreach ($this->validated_data["priv"] as $id=>$priv) {
67+
# Check if this privilege is one that is being requested to remove
68+
if (in_array($priv, $this->initial_data["priv"])) {
69+
unset($this->validated_data["priv"][$id]);
6670
}
6771
}
68-
} else {
69-
$this->errors[] = APIResponse\get(5005);
7072
}
7173
}
7274
}

pfSense-pkg-API/files/etc/inc/api/models/APIUserUpdate.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class APIUserUpdate extends APIModel {
7676
foreach ($this->initial_data["priv"] as $priv) {
7777
if (array_key_exists($priv, $priv_list)) {
7878
$this->validated_data["priv"][] = $priv;
79+
$this->validated_data["priv"] = array_unique($this->validated_data["priv"]);
7980
} else {
8081
$this->errors[] = APIResponse\get(5006);
8182
break;

0 commit comments

Comments
 (0)