Fix user edits | retain crafty perms

Move password/username edit to bootbox
This commit is contained in:
amcmanu3 2023-10-08 22:13:55 -04:00
parent 5547720a8f
commit 2d32e3ad2a
4 changed files with 68 additions and 38 deletions

View File

@ -214,14 +214,14 @@ class UsersController:
limit_server_creation = 0
limit_user_creation = 0
limit_role_creation = 0
PermissionsCrafty.add_or_update_user(
user_id,
permissions_mask,
limit_server_creation,
limit_user_creation,
limit_role_creation,
)
if user_crafty_data:
PermissionsCrafty.add_or_update_user(
user_id,
permissions_mask,
limit_server_creation,
limit_user_creation,
limit_role_creation,
)
self.users_helper.delete_user_roles(user_id, removed_roles)

View File

@ -247,31 +247,25 @@ class ApiUsersUserIndexHandler(BaseApiHandler):
or data["manager"] == 0
):
data["manager"] = None
crafty_perms = None
if "permissions" in data:
permissions: t.List[UsersController.ApiPermissionDict] = data.pop(
"permissions"
)
permissions_mask = "0" * len(EnumPermissionsCrafty)
limit_server_creation = 0
limit_user_creation = 0
limit_role_creation = 0
for permission in permissions:
permissions_mask = self.controller.crafty_perms.set_permission(
permissions_mask,
EnumPermissionsCrafty.__members__[permission["name"]],
"1" if permission["enabled"] else "0",
)
PermissionsCrafty.add_or_update_user(
user_id,
permissions_mask,
limit_server_creation,
limit_user_creation,
limit_role_creation,
)
if permissions is not None:
server_quantity = {}
permissions_mask = list(permissions_mask)
for permission in permissions:
server_quantity[permission["name"]] = permission["quantity"]
permissions_mask[
EnumPermissionsCrafty[permission["name"]].value
] = ("1" if permission["enabled"] else "0")
permissions_mask = "".join(permissions_mask)
crafty_perms = {
"permissions_mask": permissions_mask,
"server_quantity": server_quantity,
}
# TODO: make this more efficient
if len(data) != 0:
for key in data:
@ -280,7 +274,11 @@ class ApiUsersUserIndexHandler(BaseApiHandler):
if key == "password":
value = self.helper.encode_pass(value)
setattr(user_obj, key, value)
self.controller.users.update_user(auth_data[4]["user_id"], data)
self.controller.users.update_user(
user_id,
data,
crafty_perms,
)
self.controller.management.add_to_audit_log(
user["user_id"],

View File

@ -79,7 +79,7 @@
<tbody>
{% for user in data['users'] %}
<tr>
<td><i class="fas fa-user"></i> {{ user.username }}</td>
<td><i class="fas fa-user"></i><span id="user_{{user.user_id}}">{{ user.username }}</span></td>
<td>
{% if user.enabled %}
<span class="text-success">
@ -106,7 +106,7 @@
{% end %}
</ul>
</td>
<td><a class="edit_user" data-name="{{user.username}}" data-id="{{user.user_id}}"><i class="fa-solid fa-user"></i></a>&nbsp;&nbsp;<a class="edit_password" data-id="{{user.user_id}}"><i class="fa-solid fa-lock"></i></a>&nbsp;&nbsp;<a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
<td><span id="username_{{user.user_id}}" class="edit_user clickable" data-name="{{user.username}}" data-id="{{user.user_id}}"><i class="fa-solid fa-user"></i></span>&nbsp;&nbsp;<span class="edit_password clickable" data-id="{{user.user_id}}"><i class="fa-solid fa-lock"></i></span>&nbsp;&nbsp;<a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr>
{% end %}
{% for user in data['managed_users'] %}
@ -138,7 +138,7 @@
{% end %}
</ul>
</td>
<td><a class="edit_user" data-name="{{user.username}}" data-id="{{user.user_id}}"><i class="fa-solid fa-user"></i></a>&nbsp;&nbsp;<a class="edit_password" data-id="{{user.user_id}}"><i class="fa-solid fa-lock"></i></a>&nbsp;&nbsp;<a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
<td><span id="username_{{user.user_id}}" class="edit_user clickable" data-name="{{user.username}}" data-id="{{user.user_id}}"><i class="fa-solid fa-user"></i></span>&nbsp;&nbsp;<span class="edit_password clickable" data-id="{{user.user_id}}"><i class="fa-solid fa-lock"></i></span>&nbsp;&nbsp;<a href="/panel/edit_user?id={{user.user_id}}"><i class="fas fa-pencil-alt"></i></a></td>
</tr>
{% end %}
</tbody>
@ -274,6 +274,12 @@
</div>
<style>
.clickable {
color: #007bff;
}
.clickable:hover {
cursor: pointer;
}
.custom-picker {
border: 1px solid var(--outline);
}
@ -367,6 +373,11 @@
}
});
});
$(document).on("submit", ".bootbox form", function(e) {
e.preventDefault();
$(".bootbox .btn-primary").click();
});
$(".edit_user").on("click", function(){
const token = getCookie("_xsrf");
let username = $(this).data('name');
@ -374,11 +385,30 @@
bootbox.confirm(`<form class="form" id='infos' action=''>\
<div class="form-group">
<label for="username">Username</label>
<input class="form-control" type='text' name='username' value=${username} /><br/>\
<input class="form-control" type='text' name='username' id="username_field" value=${username} /><br/>\
</div>
</form>`, function(result) {
if(result)
$('#infos').submit();
</form>`, async function(result) {
if(result){
let new_username = $("#username_field").val();
let res = await fetch(`/api/v2/users/${user_id}`, {
method: 'PATCH',
headers: {
'X-XSRFToken': token
},
body: JSON.stringify({"username": new_username}),
});
let responseData = await res.json();
if (responseData.status === "ok") {
$(`#user_${user_id}`).html(` ${new_username}`)
$(`#username_${user_id}`).data('name', new_username);
} else {
bootbox.alert({
title: responseData.status,
message: responseData.error
});
}
}
});
});
if (webSocket) {

View File

@ -451,10 +451,12 @@ data['lang']) }}{% end %}
permissions.push({"name": avail_permissions[i], "quantity": $(`#quantity_${avail_permissions[i]}`).val(), "enabled": $(`#permission_${avail_permissions[i]}`).is(':checked')})
}
}
let formData = new FormData(userForm);
//Create an object from the form data entries
let formDataObject = Object.fromEntries(formData.entries());
if(userId){
delete formDataObject.username
}
if (superuser || userId != edit_id){
if (!disabled_flag){
formDataObject.roles = roles;
@ -462,7 +464,7 @@ data['lang']) }}{% end %}
if ($("#permissions").length){
formDataObject.permissions = permissions;
}
if(userId){
if(!userId){
if(typeof password === "string"){
formDataObject.password = password;
}