diff --git a/lib/tasks/access_and_permissions.rake b/lib/tasks/access_and_permissions.rake index b72dcd3a3..a332fba4d 100644 --- a/lib/tasks/access_and_permissions.rake +++ b/lib/tasks/access_and_permissions.rake @@ -6,26 +6,27 @@ namespace :permissions do document = Artefact.find_by(id: args[:document_content_id]) if document.nil? - message = "Document ID #{args[:document_content_id]} not found, no permissions changed." + message = "Document ID #{args[:document_content_id]} not found, no permissions added for organisation with ID: #{args[:org_content_id]}" elsif document.latest_edition.owning_org_content_ids.include?(args[:org_content_id]) - message = "Organisation already has permission to access the document with ID - #{document.id}" + message = "Organisation with ID: #{args[:org_content_id]} already has permission to access the document with ID: #{document.id}" else Edition.where(panopticon_id: document.id).each do |edition| edition.owning_org_content_ids << args[:org_content_id] edition.save!(validate: false) end document.save_as_task!("PermissionsAddition") - message = "Access permission successfully assigned to document with ID - #{document.id}" + message = "Access permission for organisation ID: #{args[:org_content_id]}, successfully assigned to document with ID: #{document.id}" end args[:log_file] ? args[:log_file].puts(message) : puts(message) - rescue StandardError => e + rescue Mongoid::Errors::DocumentNotFound => e error_message = "An error occurred while processing document ID #{args[:document_content_id]}: #{e.message}" args[:log_file] ? args[:log_file].puts(error_message) : puts(error_message) end - desc "Bulk process access permissions from CSV of URLs - See doc" + desc "Bulk process access permissions from CSV of URLs" task :bulk_process_access_flags, %i[csv_filename organisation_id] => :environment do |_, args| - log_file = File.new("/tmp/permissions_rake_log.txt", "w") + log_file = File.open("permissions_rake_log.txt", "w") + log_file.puts("Adding access permissions for the organisation with ID - #{args[:organisation_id]}") begin CSV.foreach(args[:csv_filename], headers: true) do |row| @@ -40,8 +41,8 @@ namespace :permissions do Rake::Task["permissions:add_organisation_access"].reenable Rake::Task["permissions:add_organisation_access"].invoke(document.id, args[:organisation_id], log_file) - rescue Mongoid::Errors::DocumentNotFound => e - log_file.puts "--- Document not found error ---" + rescue StandardError => e + log_file.puts "--- Error occurred ---" log_file.puts e.detailed_message log_file.puts "------" end