Merge pull request #1413 from gaincoder/fixDeleteUser

fix User cannot be deleted #1407
This commit is contained in:
Diego Najar 2022-05-03 15:32:36 +02:00 committed by GitHub
commit 9e07add22d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 124 additions and 1 deletions

View file

@ -224,6 +224,77 @@
}); });
}); });
$('#btnDeleteUserAndKeepContent').on('click', function() {
var username = $('#username').val();
logs('Deleting user. Username: ' + username);
bootbox.confirm({
message: '<?php $L->p('Are you sure you want to delete this user') ?>',
buttons: {
cancel: {
label: '<i class="fa fa-times"></i><?php $L->p('Cancel') ?>',
className: 'btn-sm btn-secondary'
},
confirm: {
label: '<i class="fa fa-check"></i><?php $L->p('Confirm') ?>',
className: 'btn-sm btn-primary'
}
},
closeButton: false,
callback: function(result) {
if (result) {
var args = {
username: $('#username').val()
};
api.deleteUser(args).then(function(response) {
if (response.status == 0) {
logs('User deleted. Username: ' + response.data.key);
window.location.replace(HTML_PATH_ADMIN_ROOT + 'users');
} else {
logs("An error occurred while trying to disable the user.");
showAlertError(response.message);
}
});
}
}
});
});
$('#btnDeleteUserAndContent').on('click', function() {
var username = $('#username').val();
logs('Deleting user and content. Username: ' + username);
bootbox.confirm({
message: '<?php $L->p('Are you sure you want to delete this user') ?>',
buttons: {
cancel: {
label: '<i class="fa fa-times"></i><?php $L->p('Cancel') ?>',
className: 'btn-sm btn-secondary'
},
confirm: {
label: '<i class="fa fa-check"></i><?php $L->p('Confirm') ?>',
className: 'btn-sm btn-primary'
}
},
closeButton: false,
callback: function(result) {
if (result) {
var args = {
username: $('#username').val(),
deleteContent: true
};
api.deleteUser(args).then(function(response) {
if (response.status == 0) {
logs('User and content deleted. Username: ' + response.data.key);
window.location.replace(HTML_PATH_ADMIN_ROOT + 'users');
} else {
logs("An error occurred while trying to disable the user.");
showAlertError(response.message);
}
});
}
}
});
});
}); });
// ============================================================================ // ============================================================================

View file

@ -967,7 +967,7 @@ function deleteUser($args) {
$deleteContent = isset($args['deleteContent']) ? $args['deleteContent'] : false; $deleteContent = isset($args['deleteContent']) ? $args['deleteContent'] : false;
// Only administrators can delete users // Only administrators can delete users
if ($login->role()!=='admin') { if (Session::get('role')!=='admin') {
return false; return false;
} }

View file

@ -377,6 +377,32 @@ class API {
} }
} }
/* Delete user
@args array Arguments can be any of the fields from an user
@return string The username
*/
async deleteUser(args) {
var url = this.apiURL + "users/" + args['username'];
var body = Object.assign({}, this.body, args);
try {
var response = await fetch(url, {
credentials: "same-origin",
method: "DELETE",
body: JSON.stringify(body),
headers: new Headers({
"Content-Type": "application/json"
})
});
var json = await response.json();
return json;
} catch (err) {
console.log(response);
console.log(err);
return {'message': 'Error from API. Open the inspector from the browser for more details.'};
}
}
/* Install and activate a plugin === Bludit v4 /* Install and activate a plugin === Bludit v4
@args array @args array

View file

@ -261,6 +261,11 @@ class pluginAPI extends Plugin {
elseif ( ($method==='POST') && ($parmA==='users') && empty($parmB) && $writePermissions ) { elseif ( ($method==='POST') && ($parmA==='users') && empty($parmB) && $writePermissions ) {
$data = $this->createUser($inputs); $data = $this->createUser($inputs);
} }
// (DELETE) /api/users/:key
elseif ( ($method==='DELETE') && ($parmA==='users') && !empty($parmB) && $writePermissions ) {
$inputs['key'] = $parmB;
$data = $this->deleteUser($inputs);
}
// (POST) /api/users/picture/:username // (POST) /api/users/picture/:username
elseif ( ($method==='POST') && ($parmA==='users') && ($parmB==='picture') && !empty($parmC) && $writePermissions ) { elseif ( ($method==='POST') && ($parmA==='users') && ($parmB==='picture') && !empty($parmC) && $writePermissions ) {
$username = $parmC; $username = $parmC;
@ -815,6 +820,27 @@ class pluginAPI extends Plugin {
); );
} }
/* Delete user === Bludit v4
Referer to the function deleteUser() from functions.php
*/
private function deleteUser($args)
{
$key = deleteUser($args);
if ($key===false) {
return array(
'status'=>'1',
'message'=>'An error occurred while trying to delete the user.'
);
}
return array(
'status'=>'0',
'message'=>'User deleted.',
'data'=>array('key'=>$key)
);
}
/* Upload a profile picture === Bludit v4 /* Upload a profile picture === Bludit v4
Referer to the function uploadProfilePicture() from functions.php Referer to the function uploadProfilePicture() from functions.php
*/ */