From 576bffbf78c16c278b27eefd445a8f6368f70884 Mon Sep 17 00:00:00 2001 From: dleadbetter Date: Fri, 27 Dec 2024 06:46:47 -0500 Subject: [PATCH 1/3] BASIRA #294 - Updating base_controller to allow users to update/delete records created by themselves --- app/controllers/api/base_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index 40d58f6..f8bd956 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -23,12 +23,17 @@ def prepare_params private + def is_owned? + item = item_class.find(params[:id]) + item.created_by_id == current_user.id + end + def validate_delete_authorization - render json: { errors: [I18n.t('errors.unauthorized')] }, status: :unauthorized unless current_user.admin? + render json: { errors: [I18n.t('errors.unauthorized')] }, status: :unauthorized unless current_user.admin? || is_owned? end def validate_update_authorization - return if current_user.admin? + return if current_user.admin? || is_owned? unauthorized = false From ba4bf0a1da39422b15964e5cb6fdefac18827a94 Mon Sep 17 00:00:00 2001 From: dleadbetter Date: Fri, 27 Dec 2024 06:47:11 -0500 Subject: [PATCH 2/3] BASIRA #294 - Adding created_by_id to serializers --- app/serializers/artworks_serializer.rb | 6 +++--- app/serializers/documents_serializer.rb | 7 ++++--- app/serializers/physical_components_serializer.rb | 6 +++--- app/serializers/visual_contexts_serializer.rb | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/serializers/artworks_serializer.rb b/app/serializers/artworks_serializer.rb index 2730542..c5cbb40 100644 --- a/app/serializers/artworks_serializer.rb +++ b/app/serializers/artworks_serializer.rb @@ -4,18 +4,18 @@ class ArtworksSerializer < BaseSerializer include LocateableSerializer include NestableSerializer - index_attributes :id, :date_start, :date_end, :date_descriptor, :published, :created_at, :updated_at, + index_attributes :id, :date_start, :date_end, :date_descriptor, :published, :created_at, :updated_at, :created_by_id, primary_title: [:id, :title, qualifications: QualificationsSerializer], updated_by: UsersSerializer, created_by: UsersSerializer show_attributes :id, :date_start, :date_end, :date_descriptor, :published, :height, :width, :depth, :notes_external, :notes_internal, :repository_work_url, :accession_number, - :documents_count, :number_documents_visible, :created_at, :updated_at, + :documents_count, :number_documents_visible, :created_at, :updated_at, :created_by_id, artwork_titles: [:id, :title, :notes, :primary, qualifications: QualificationsSerializer], updated_by: UsersSerializer, created_by: UsersSerializer, participations: ParticipationsSerializer, qualifications: QualificationsSerializer - nested_attributes :id, primary_title: [:id, :title, qualifications: QualificationsSerializer], + nested_attributes :id, :created_by_id, primary_title: [:id, :title, qualifications: QualificationsSerializer], primary_attachment: [:id, :file_url, :primary, :thumbnail_url], children: { physical_components: PhysicalComponentsSerializer } end diff --git a/app/serializers/documents_serializer.rb b/app/serializers/documents_serializer.rb index fb8845f..060d3b9 100644 --- a/app/serializers/documents_serializer.rb +++ b/app/serializers/documents_serializer.rb @@ -3,12 +3,12 @@ class DocumentsSerializer < BaseSerializer include AttachableSerializer include NestableSerializer - index_attributes :id, :name + index_attributes :id, :name, :created_by_id show_attributes :id, :name, :visual_context_id, :notes, :number_sewing_supports, :number_fastenings, :inscriptions_on_binding, :inscription_text, :endband_present, :uncut_fore_edges, :fore_edge_text, :bookmarks_registers, :text_columns, :ruling, :rubrication, :transcription, :transcription_expanded, - :transcription_translation, :identity, :created_at, :updated_at, + :transcription_translation, :identity, :created_at, :updated_at, :created_by_id, qualifications: QualificationsSerializer, actions: [:id, :document_id, :notes, qualifications: QualificationsSerializer] @@ -23,5 +23,6 @@ class DocumentsSerializer < BaseSerializer } end - nested_attributes :id, :visual_context_id, :name, primary_attachment: [:id, :file_url, :primary, :thumbnail_url] + nested_attributes :id, :visual_context_id, :name, :created_by_id, + primary_attachment: [:id, :file_url, :primary, :thumbnail_url] end diff --git a/app/serializers/physical_components_serializer.rb b/app/serializers/physical_components_serializer.rb index 8313540..69637d7 100644 --- a/app/serializers/physical_components_serializer.rb +++ b/app/serializers/physical_components_serializer.rb @@ -3,11 +3,11 @@ class PhysicalComponentsSerializer < BaseSerializer include AttachableSerializer include NestableSerializer - index_attributes :id, :name + index_attributes :id, :name, :created_by_id - show_attributes :id, :artwork_id, :name, :height, :width, :depth, :notes, :created_at, :updated_at + show_attributes :id, :artwork_id, :name, :height, :width, :depth, :notes, :created_at, :updated_at, :created_by_id - nested_attributes :id, :artwork_id, :name, + nested_attributes :id, :artwork_id, :name, :created_by_id, primary_attachment: [:id, :file_url, :primary, :thumbnail_url], children: { visual_contexts: VisualContextsSerializer } end diff --git a/app/serializers/visual_contexts_serializer.rb b/app/serializers/visual_contexts_serializer.rb index 0dea42b..ec1c76f 100644 --- a/app/serializers/visual_contexts_serializer.rb +++ b/app/serializers/visual_contexts_serializer.rb @@ -3,14 +3,14 @@ class VisualContextsSerializer < BaseSerializer include AttachableSerializer include NestableSerializer - index_attributes :id, :name + index_attributes :id, :name, :created_by_id show_attributes :id, :physical_component_id, :name, :height, :width, :depth, :notes, :beta, :created_at, :updated_at, - qualifications: QualificationsSerializer + :created_by_id, qualifications: QualificationsSerializer show_attributes(:artwork_id) { |visual_context| visual_context.physical_component&.artwork_id } - nested_attributes :id, :physical_component_id, :name, + nested_attributes :id, :physical_component_id, :name, :created_by_id, primary_attachment: [:id, :file_url, :primary, :thumbnail_url], children: { documents: DocumentsSerializer } end From a51bc2b4101e552cd2a833a6dfa567a46dd65373 Mon Sep 17 00:00:00 2001 From: dleadbetter Date: Fri, 27 Dec 2024 06:49:37 -0500 Subject: [PATCH 3/3] BASIRA #294 - Removing delete buttons when user does not have permissions --- client/src/components/AdminArtworkMenu.js | 43 +++++++++++------- client/src/components/ArtworkMenu.js | 28 +++++------- client/src/pages/admin/Artworks.js | 4 +- client/src/services/Permissions.js | 55 +++++++++++++++++++++++ client/src/services/Session.js | 15 ++++++- client/src/types/Artwork.js | 1 + client/src/types/Document.js | 3 +- client/src/types/PhysicalComponent.js | 3 +- client/src/types/VisualContext.js | 3 +- 9 files changed, 115 insertions(+), 40 deletions(-) create mode 100644 client/src/services/Permissions.js diff --git a/client/src/components/AdminArtworkMenu.js b/client/src/components/AdminArtworkMenu.js index 6b422f1..89f0931 100644 --- a/client/src/components/AdminArtworkMenu.js +++ b/client/src/components/AdminArtworkMenu.js @@ -17,6 +17,7 @@ import ArtworksService from '../services/Artworks'; import DocumentsService from '../services/Documents'; import { getPhysicalComponents, getVisualContexts } from '../utils/Artwork'; import ItemLabel from './ItemLabel'; +import PermissionsService from '../services/Permissions'; import PhysicalComponentsService from '../services/PhysicalComponents'; import Session from '../services/Session'; import VisualContextsService from '../services/VisualContexts'; @@ -173,7 +174,7 @@ const AdminArtworkMenu = (props: Props) => { to={`/admin${path}`} /> )} - { Session.isAdmin() && onDelete && ( + { onDelete && (