2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 19:46:46 +00:00

Fix API endpoint permission for the "AttachmentMixin" class (#3218)

* Fix API endpoint permission for the "AttachmentMixin" class

- Any authenticated user could perform CREATE and UPDATE operations on attachments
- Could be performed via the browsable DRF API
- Could also be performed via the front-end (with some advaned jiggering of OPTIONS code)

* Show or hide buttons depending on the permissions of the user

* Add shortcut for table permission check
This commit is contained in:
Oliver 2022-06-18 14:48:09 +10:00 committed by GitHub
parent 18cf92ec8b
commit 12fcccb5a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 44 deletions

View File

@ -11,6 +11,7 @@ from rest_framework.response import Response
from rest_framework.serializers import ValidationError from rest_framework.serializers import ValidationError
from InvenTree.mixins import ListCreateAPI from InvenTree.mixins import ListCreateAPI
from InvenTree.permissions import RolePermission
from .status import is_worker_running from .status import is_worker_running
from .version import (inventreeApiVersion, inventreeInstanceName, from .version import (inventreeApiVersion, inventreeInstanceName,
@ -182,7 +183,10 @@ class APIDownloadMixin:
class AttachmentMixin: class AttachmentMixin:
"""Mixin for creating attachment objects, and ensuring the user information is saved correctly.""" """Mixin for creating attachment objects, and ensuring the user information is saved correctly."""
permission_classes = [permissions.IsAuthenticated] permission_classes = [
permissions.IsAuthenticated,
RolePermission,
]
filter_backends = [ filter_backends = [
DjangoFilterBackend, DjangoFilterBackend,

View File

@ -2,7 +2,7 @@
<div id='attachment-buttons'> <div id='attachment-buttons'>
<div class='btn-group' role='group'> <div class='btn-group' role='group'>
<div class='btn-group'> <div class='btn-group' id='multi-attachment-actions'>
<button class='btn btn-primary dropdown-toggle' type='button' data-bs-toggle='dropdown' title='{% trans "Actions" %}'> <button class='btn btn-primary dropdown-toggle' type='button' data-bs-toggle='dropdown' title='{% trans "Actions" %}'>
<span class='fas fa-tools'></span> <span class='caret'></span> <span class='fas fa-tools'></span> <span class='caret'></span>
</button> </button>

View File

@ -136,19 +136,56 @@ function loadAttachmentTable(url, options) {
var table = options.table || '#attachment-table'; var table = options.table || '#attachment-table';
setupFilterList('attachments', $(table), '#filter-list-attachments'); var permissions = {};
addAttachmentButtonCallbacks(url, options.fields || {}); // First we determine which permissions the user has for this attachment table
$.ajax({
url: url,
async: false,
type: 'OPTIONS',
contentType: 'application/json',
dataType: 'json',
accepts: {
json: 'application/json',
},
success: function(response) {
if (response.actions.DELETE) {
permissions.delete = true;
}
// Add callback for the 'multi delete' button if (response.actions.POST) {
$('#multi-attachment-delete').click(function() { permissions.change = true;
var attachments = getTableData(table); permissions.add = true;
}
if (attachments.length > 0) { },
deleteAttachments(attachments, url, options); error: function(xhr) {
showApiError(xhr, url);
} }
}); });
setupFilterList('attachments', $(table), '#filter-list-attachments');
if (permissions.add) {
addAttachmentButtonCallbacks(url, options.fields || {});
} else {
// Hide the buttons
$('#new-attachment').hide();
$('#new-attachment-link').hide();
}
if (permissions.delete) {
// Add callback for the 'multi delete' button
$('#multi-attachment-delete').click(function() {
var attachments = getTableData(table);
if (attachments.length > 0) {
deleteAttachments(attachments, url, options);
}
});
} else {
$('#multi-attachment-actions').hide();
}
$(table).inventreeTable({ $(table).inventreeTable({
url: url, url: url,
name: options.name || 'attachments', name: options.name || 'attachments',
@ -162,32 +199,36 @@ function loadAttachmentTable(url, options) {
onPostBody: function() { onPostBody: function() {
// Add callback for 'edit' button // Add callback for 'edit' button
$(table).find('.button-attachment-edit').click(function() { if (permissions.change) {
var pk = $(this).attr('pk'); $(table).find('.button-attachment-edit').click(function() {
var pk = $(this).attr('pk');
constructForm(`${url}${pk}/`, { constructForm(`${url}${pk}/`, {
fields: { fields: {
link: {}, link: {},
comment: {}, comment: {},
}, },
processResults: function(data, fields, opts) { processResults: function(data, fields, opts) {
// Remove the "link" field if the attachment is a file! // Remove the "link" field if the attachment is a file!
if (data.attachment) { if (data.attachment) {
delete opts.fields.link; delete opts.fields.link;
} }
}, },
onSuccess: reloadAttachmentTable, onSuccess: reloadAttachmentTable,
title: '{% trans "Edit Attachment" %}', title: '{% trans "Edit Attachment" %}',
});
}); });
}); }
// Add callback for 'delete' button if (permissions.delete) {
$(table).find('.button-attachment-delete').click(function() { // Add callback for 'delete' button
var pk = $(this).attr('pk'); $(table).find('.button-attachment-delete').click(function() {
var pk = $(this).attr('pk');
var attachment = $(table).bootstrapTable('getRowByUniqueId', pk); var attachment = $(table).bootstrapTable('getRowByUniqueId', pk);
deleteAttachments([attachment], url, options); deleteAttachments([attachment], url, options);
}); });
}
}, },
columns: [ columns: [
{ {
@ -261,19 +302,23 @@ function loadAttachmentTable(url, options) {
html = `<div class='btn-group float-right' role='group'>`; html = `<div class='btn-group float-right' role='group'>`;
html += makeIconButton( if (permissions.change) {
'fa-edit icon-blue', html += makeIconButton(
'button-attachment-edit', 'fa-edit icon-blue',
row.pk, 'button-attachment-edit',
'{% trans "Edit attachment" %}', row.pk,
); '{% trans "Edit attachment" %}',
);
}
html += makeIconButton( if (permissions.delete) {
'fa-trash-alt icon-red', html += makeIconButton(
'button-attachment-delete', 'fa-trash-alt icon-red',
row.pk, 'button-attachment-delete',
'{% trans "Delete attachment" %}', row.pk,
); '{% trans "Delete attachment" %}',
);
}
html += `</div>`; html += `</div>`;

View File

@ -222,6 +222,11 @@ class RuleSet(models.Model):
@classmethod @classmethod
def check_table_permission(cls, user, table, permission): def check_table_permission(cls, user, table, permission):
"""Check if the provided user has the specified permission against the table.""" """Check if the provided user has the specified permission against the table."""
# Superuser knows no bounds
if user.is_superuser:
return True
# If the table does *not* require permissions # If the table does *not* require permissions
if table in cls.RULESET_IGNORE: if table in cls.RULESET_IGNORE:
return True return True