Skip to content

Commit

Permalink
Zeitwerk
Browse files Browse the repository at this point in the history
  • Loading branch information
picman committed Jan 29, 2025
1 parent a29574e commit c7fd1c0
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 116 deletions.
56 changes: 26 additions & 30 deletions app/controllers/dmsf_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ def entries_operation
else
download_entries @selected_folders, @selected_files
end
rescue RedmineDmsf::Errors::DmsfFileNotFoundError
rescue DmsfFileNotFoundError
render_404
rescue RedmineDmsf::Errors::DmsfAccessError
rescue DmsfAccessError
render_403
rescue StandardError => e
flash[:error] = e.message
Expand Down Expand Up @@ -497,16 +497,14 @@ def users_for_new_users
end

def email_entries(selected_folders, selected_files)
raise RedmineDmsf::Errors::DmsfAccessError unless User.current.allowed_to?(:email_documents, @project)
raise DmsfAccessError unless User.current.allowed_to?(:email_documents, @project)

zip = Zip.new
zip_entries(zip, selected_folders, selected_files)
zipped_content = zip.finish

max_filesize = RedmineDmsf.dmsf_max_email_filesize
if max_filesize.positive? && File.size(zipped_content) > max_filesize * 1_048_576
raise RedmineDmsf::Errors::DmsfEmailMaxFileSizeError
end
raise DmsfEmailMaxFileSizeError if max_filesize.positive? && File.size(zipped_content) > max_filesize * 1_048_576

zip.dmsf_files.each do |f|
# Action
Expand Down Expand Up @@ -569,23 +567,22 @@ def zip_entries(zip, selected_folders, selected_files)
member = Member.find_by(user_id: User.current.id, project_id: @project.id)
selected_folders.each do |selected_folder_id|
folder = DmsfFolder.visible.find_by(id: selected_folder_id)
raise RedmineDmsf::Errors::DmsfFileNotFoundError unless folder
raise DmsfFileNotFoundError unless folder

zip.add_dmsf_folder folder, member, folder&.dmsf_folder&.dmsf_path_str
end
selected_files.each do |selected_file_id|
file = DmsfFile.visible.find_by(id: selected_file_id)
unless file&.last_revision && File.exist?(file.last_revision&.disk_file)
raise RedmineDmsf::Errors::DmsfFileNotFoundError
end
raise DmsfFileNotFoundError unless file&.last_revision && File.exist?(file.last_revision&.disk_file)

unless (file.project == @project) || User.current.allowed_to?(:view_dmsf_files, file.project)
raise RedmineDmsf::Errors::DmsfAccessError
raise DmsfAccessError
end

zip.add_dmsf_file file, member, file.dmsf_folder&.dmsf_path_str
end
max_files = RedmineDmsf.dmsf_max_file_download
raise RedmineDmsf::Errors::DmsfZipMaxFilesError if max_files.positive? && zip.dmsf_files.length > max_files
raise DmsfZipMaxFilesError if max_files.positive? && zip.dmsf_files.length > max_files

zip
end
Expand All @@ -595,19 +592,19 @@ def restore_entries(selected_folders, selected_files, selected_links)
selected_folders.each do |id|
folder = DmsfFolder.find_by(id: id)

raise RedmineDmsf::Errors::DmsfFileNotFoundError unless folder
raise DmsfFileNotFoundError unless folder
end
# Files
selected_files.each do |id|
file = DmsfFile.find_by(id: id)
raise RedmineDmsf::Errors::DmsfFileNotFoundError unless file
raise DmsfFileNotFoundError unless file

flash[:error] = file.errors.full_messages.to_sentence unless file.restore
end
# Links
selected_links.each do |id|
link = DmsfLink.find_by(id: id)
raise RedmineDmsf::Errors::DmsfFileNotFoundError unless link
raise DmsfFileNotFoundError unless link

flash[:error] = link.errors.full_messages.to_sentence unless link.restore
end
Expand All @@ -616,20 +613,20 @@ def restore_entries(selected_folders, selected_files, selected_links)
def delete_entries(selected_folders, selected_files, selected_links, commit)
# Folders
selected_folders.each do |id|
raise RedmineDmsf::Errors::DmsfAccessError unless User.current.allowed_to?(:folder_manipulation, @project)
raise DmsfAccessError unless User.current.allowed_to?(:folder_manipulation, @project)

folder = DmsfFolder.find_by(id: id)
if folder
raise StandardError, folder.errors.full_messages.to_sentence unless folder.delete(commit: commit)
elsif !commit
raise RedmineDmsf::Errors::DmsfFileNotFoundError
raise DmsfFileNotFoundError
end
end
# Files
deleted_files = []
not_deleted_files = []
if selected_files.any?
raise RedmineDmsf::Errors::DmsfAccessError unless User.current.allowed_to?(:file_delete, @project)
raise DmsfAccessError unless User.current.allowed_to?(:file_delete, @project)

selected_files.each do |id|
file = DmsfFile.find_by(id: id)
Expand All @@ -640,7 +637,7 @@ def delete_entries(selected_folders, selected_files, selected_links, commit)
not_deleted_files << file
end
elsif !commit
raise RedmineDmsf::Errors::DmsfFileNotFoundError
raise DmsfFileNotFoundError
end
end
end
Expand All @@ -665,7 +662,7 @@ def delete_entries(selected_folders, selected_files, selected_links, commit)
end
# Links
if selected_links.any?
raise RedmineDmsf::Errors::DmsfAccessError unless User.current.allowed_to?(:folder_manipulation, @project)
raise DmsfAccessError unless User.current.allowed_to?(:folder_manipulation, @project)

selected_links.each do |id|
link = DmsfLink.find_by(id: id)
Expand Down Expand Up @@ -699,11 +696,10 @@ def copy_entries(selected_folders, selected_files, selected_links)

def move_entries(selected_folders, selected_files, selected_links)
# Permissions
if selected_folders.any? && !User.current.allowed_to?(:folder_manipulation, @project)
raise RedmineDmsf::Errors::DmsfAccessError
end
raise DmsfAccessError if selected_folders.any? && !User.current.allowed_to?(:folder_manipulation, @project)

if (selected_folders.any? || selected_links.any?) && !User.current.allowed_to?(:file_manipulation, @project)
raise RedmineDmsf::Errors::DmsfAccessError
raise DmsfAccessError
end

# Folders
Expand Down Expand Up @@ -826,27 +822,27 @@ def check_target_folder
links = DmsfLink.where(id: @selected_links).to_a
(folders + files + links).each do |entry|
if entry.dmsf_folder
raise RedmineDmsf::Errors::DmsfParentError if entry.dmsf_folder == @target_folder || entry == @target_folder
raise DmsfParentError if entry.dmsf_folder == @target_folder || entry == @target_folder
elsif @target_folder.nil?
raise RedmineDmsf::Errors::DmsfParentError if entry.project == @target_project
raise DmsfParentError if entry.project == @target_project
end
end
# Prevent recursion
if params[:move_entries].present?
folders.each do |entry|
raise RedmineDmsf::Errors::DmsfParentError if entry.any_child?(@target_folder)
raise DmsfParentError if entry.any_child?(@target_folder)
end
end
# Check permissions
if (@target_folder && (@target_folder.locked_for_user? ||
!DmsfFolder.permissions?(@target_folder, allow_system: false))) ||
!@target_project.allows_to?(:folder_manipulation)
raise RedmineDmsf::Errors::DmsfAccessError
raise DmsfAccessError
end
rescue RedmineDmsf::Errors::DmsfParentError
rescue DmsfParentError
flash[:error] = l(:error_target_folder_same)
redirect_back_or_default dmsf_folder_path(id: @project, folder_id: @folder)
rescue RedmineDmsf::Errors::DmsfAccessError
rescue DmsfAccessError
render_403
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/dmsf_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def view
@revision = @file.last_revision
else
@revision = DmsfFileRevision.find(params[:download].to_i)
raise RedmineDmsf::Errors::DmsfAccessError if @revision.dmsf_file != @file
raise DmsfAccessError if @revision.dmsf_file != @file
end

check_project @revision.dmsf_file
Expand Down Expand Up @@ -93,7 +93,7 @@ def view
type: @revision.detect_content_type,
disposition: params[:disposition].presence || @revision.dmsf_file.disposition
end
rescue RedmineDmsf::Errors::DmsfAccessError => e
rescue DmsfAccessError => e
Rails.logger.error e.message
render_403
rescue StandardError => e
Expand Down Expand Up @@ -378,6 +378,6 @@ def find_folder
def check_project(entry)
return unless entry && entry.project != @project

raise RedmineDmsf::Errors::DmsfAccessError, l(:error_entry_project_does_not_match_current_project)
raise DmsfAccessError, l(:error_entry_project_does_not_match_current_project)
end
end
4 changes: 2 additions & 2 deletions app/controllers/dmsf_folder_permissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ def users_for_new_users

def find_project
@project = Project.visible.find_by_param(params[:project_id])
rescue RedmineDmsf::Errors::DmsfAccessError
rescue DmsfAccessError
render_403
rescue ActiveRecord::RecordNotFound
render_404
end

def find_folder
@dmsf_folder = DmsfFolder.visible.find(params[:dmsf_folder_id])
rescue RedmineDmsf::Errors::DmsfAccessError
rescue DmsfAccessError
render_403
rescue ActiveRecord::RecordNotFound
render_404
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dmsf_upload_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def commit_files_internal(committed_files)

def find_folder
@folder = DmsfFolder.visible.find(params[:folder_id]) if params.key?('folder_id')
rescue RedmineDmsf::Errors::DmsfAccessError
rescue DmsfAccessError
render_403
end
end
4 changes: 2 additions & 2 deletions app/controllers/dmsf_workflows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def new_action
if revision.dmsf_file
begin
revision.dmsf_file.unlock!(force_file_unlock_allowed: true) unless RedmineDmsf.dmsf_keep_documents_locked?
rescue RedmineDmsf::Errors::DmsfLockError => e
rescue DmsfLockError => e
flash[:info] = e.message
end
end
Expand Down Expand Up @@ -225,7 +225,7 @@ def assignment
if file
begin
file.lock!
rescue RedmineDmsf::Errors::DmsfLockError => e
rescue DmsfLockError => e
Rails.logger.warn e.message
end
flash[:notice] = l(:notice_successful_update)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# Access error
class DmsfAccessError < StandardError
# Nothing to do
end
end
# Access error
class DmsfAccessError < StandardError
# Nothing to do
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# max file size error
class DmsfEmailMaxFileSizeError < StandardError
include Redmine::I18n
# Max file size error
class DmsfEmailMaxFileSizeError < StandardError
include Redmine::I18n

def initialize(message = nil)
if message.present?
super
else
super(l(:error_max_email_filesize_exceeded, number: RedmineDmsf.dmsf_max_email_filesize))
end
end
def initialize(message = nil)
if message.present?
super
else
super(l(:error_max_email_filesize_exceeded, number: RedmineDmsf.dmsf_max_email_filesize))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# Not found error
class DmsfFileNotFoundError < StandardError
# nothing to do
end
end
# Not found error
class DmsfFileNotFoundError < StandardError
# nothing to do
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# Lock error
class DmsfLockError < StandardError
# Nothing to do
end
end
# Lock error
class DmsfLockError < StandardError
# Nothing to do
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# Parent error
class DmsfParentError < StandardError
# Nothing to do
end
end
# Parent error
class DmsfParentError < StandardError
# Nothing to do
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

module RedmineDmsf
module Errors
# File count exceeded
class DmsfZipMaxFilesError < StandardError
include Redmine::I18n
# File count exceeded
class DmsfZipMaxFilesError < StandardError
include Redmine::I18n

def initialize(message = nil)
if message.present?
super
else
super(l(:error_max_files_exceeded, number: RedmineDmsf.dmsf_max_file_download))
end
end
def initialize(message = nil)
if message.present?
super
else
super(l(:error_max_files_exceeded, number: RedmineDmsf.dmsf_max_file_download))
end
end
end
2 changes: 1 addition & 1 deletion app/helpers/dmsf_upload_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def self.commit_files_internal(committed_files, project, folder, controller = ni
wf.notify_users project, new_revision, controller
begin
file.lock!
rescue RedmineDmsf::Errors::DmsfLockError => e
rescue DmsfLockError => e
Rails.logger.warn e.message
end
else
Expand Down
2 changes: 1 addition & 1 deletion app/models/dmsf_file_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def disk_file(search_if_not_exists: true)
end

def new_storage_filename
raise RedmineDmsf::Errors::DmsfAccessError, 'File id is not set' unless dmsf_file&.id
raise DmsfAccessError, 'File id is not set' unless dmsf_file&.id

filename = DmsfHelper.sanitize_filename(name)
timestamp = DateTime.current.strftime('%y%m%d%H%M%S')
Expand Down
9 changes: 0 additions & 9 deletions lib/redmine_dmsf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,6 @@ def after_easy_init(&block)
require "#{File.dirname(__FILE__)}/redmine_dmsf/webdav/resource_proxy"
end

# Errors
after_easy_init do
require "#{File.dirname(__FILE__)}/redmine_dmsf/errors/dmsf_access_error"
require "#{File.dirname(__FILE__)}/redmine_dmsf/errors/dmsf_email_max_file_size_error"
require "#{File.dirname(__FILE__)}/redmine_dmsf/errors/dmsf_file_not_found_error"
require "#{File.dirname(__FILE__)}/redmine_dmsf/errors/dmsf_lock_error"
require "#{File.dirname(__FILE__)}/redmine_dmsf/errors/dmsf_zip_max_files_error"
end

# Hooks
def require_hooks
require "#{File.dirname(__FILE__)}/redmine_dmsf/hooks/controllers/account_controller_hooks"
Expand Down
Loading

0 comments on commit c7fd1c0

Please sign in to comment.