@@ -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}
0 commit comments