From a481cfb2121094998a291125d79bb5508dab7bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Tue, 27 Sep 2022 12:39:58 +0200 Subject: [PATCH] Q: check for REST API access (user impersonation or remote IP) in workflow #280 --- README.md | 8 +++ app/views/custom_workflows/_form.html.erb | 1 + config/locales/cs.yml | 3 +- config/locales/en.yml | 3 +- config/locales/es.yml | 3 +- config/locales/pt-BR.yml | 3 +- .../attachments_controller_patch.rb | 29 ++++++++-- .../controllers/groups_controller_patch.rb | 29 ++++++++-- .../issue_relations_controller_patch.rb | 29 ++++++++-- .../controllers/issues_controller_patch.rb | 29 ++++++++-- .../controllers/projects_controller_patch.rb | 29 ++++++++-- .../controllers/timelog_controller_patch.rb | 29 ++++++++-- .../controllers/users_controller_patch.rb | 29 ++++++++-- .../controllers/versions_controller_patch.rb | 28 +++++++-- .../controllers/wiki_controller_patch.rb | 39 +++++++++---- .../patches/models/attachment_patch.rb | 5 ++ .../patches/models/group_patch.rb | 5 ++ .../patches/models/issue_patch.rb | 5 ++ .../patches/models/issue_relation_patch.rb | 5 ++ .../patches/models/project_patch.rb | 5 ++ .../patches/models/time_entry_patch.rb | 5 ++ .../patches/models/user_patch.rb | 5 ++ .../patches/models/version_patch.rb | 5 ++ .../patches/models/wiki_content_patch.rb | 5 ++ .../patches/models/wiki_page_patch.rb | 5 ++ test/fixtures/custom_workflows.yml | 57 +++++++++++++++---- test/fixtures/custom_workflows_projects.yml | 4 ++ .../attachments_controller_patch_test.rb | 8 ++- .../groups_controller_patch_test.rb | 7 +++ .../issue_relations_controller_patch_test.rb | 6 ++ .../issues_controller_patch_test.rb | 6 ++ .../projects_controller_patch_test.rb | 6 ++ .../timelog_controller_patch_test.rb | 6 ++ .../functional/users_controller_patch_test.rb | 6 ++ .../versions_controller_patch_test.rb | 6 ++ test/unit/attachment_patch_test.rb | 5 ++ test/unit/group_patch_test.rb | 5 ++ test/unit/issue_patch_test.rb | 5 ++ test/unit/issue_relation_patch_test.rb | 5 ++ test/unit/project_patch_test.rb | 5 ++ test/unit/time_entry_patch_test.rb | 5 ++ test/unit/user_patch_test.rb | 5 ++ test/unit/version_patch_test.rb | 5 ++ test/unit/wiki_content_patch_test.rb | 5 ++ test/unit/wiki_page_patch_test.rb | 5 ++ 45 files changed, 428 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 215dcf9..e05b4cc 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,14 @@ E.g.: self.custom_workflow_messages[:error] = 'Custom workflow error' ``` +Some environmental variables are available in observable objects. + +E.g.: + +```ruby +self.custom_workflow_env[:remote_ip] +``` + Enabling custom workflows for projects -------------------------------------- diff --git a/app/views/custom_workflows/_form.html.erb b/app/views/custom_workflows/_form.html.erb index ed3460f..6f973a0 100644 --- a/app/views/custom_workflows/_form.html.erb +++ b/app/views/custom_workflows/_form.html.erb @@ -130,6 +130,7 @@
<%= f.text_area :before_destroy, cols: 40, rows: 20, wrap: 'off', class: 'custom_workflow_script' %> + <%= l(:text_custom_workflow_before_destroy_note) %>
<%= f.text_area :after_destroy, cols: 40, rows: 20, wrap: 'off', class: 'custom_workflow_script' %> diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 1453811..6a6e9a6 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -66,7 +66,7 @@ cs: text_select_project_custom_workflows: Vyberte uživatelský skript projektu text_custom_workflow_before_save_note: Zde můžete měnit vlastnosti úkolu. Ve skriptu nevytvářejte ani neměňte - související úkoly. Pro ukončení skriptu chybou použijte `raise WorkflowError, 'Zpráva uživateli'`. + související úkoly. Pro ukončení skriptu chybou použijte `raise RedmineCustomWorkflows::Errors::WorkflowError, 'Zpráva uživateli'`. text_custom_workflow_after_save_note: Zde můžete aktualizovat nebo vytvářet souvissející úkoly. Mějte na paměti, že tento skript bude také vykonán pro nově vytvořené úkoly. Takže nezapomeňte udělat vhodné kontroly pro zabránění rekurzivnímu volání. @@ -102,6 +102,7 @@ cs: se maže. Můžete přímo použít metody a vlastnosti objektu. text_custom_workflow_version_code_note: Skripty jsou vykonávány v rámci objektu verze, když se mění nebo se maže. Můžete přímo použít metody a vlastnosti objektu. + text_custom_workflow_before_destroy_note: Zabránit smazání objektu lze pomocí `return false`. text_no_enabled_projects: Žádné projekty text_custom_workflow_author: Bude zahrnuto do exportovaného XML diff --git a/config/locales/en.yml b/config/locales/en.yml index fa31db1..f90c077 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -66,7 +66,7 @@ en: text_select_project_custom_workflows: Select project custom workflows text_custom_workflow_before_save_note: You can change properties of the issues here. Do not create or update related - issues in this script. To finish with error, use `raise WorkflowError, 'Message to user'`. + issues in this script. To finish with error, use `raise RedmineCustomWorkflows::Errors::WorkflowError, 'Message to user'`. text_custom_workflow_after_save_note: You can update or create related issues here. Note that this script will be also executed for the newly created issues. So make appropriate checks to prevent infinite recursion. @@ -102,6 +102,7 @@ en: object changes (destroys). Use methods and properties of the time entry directly (or through `self`) text_custom_workflow_version_code_note: Scripts are executed in the context of Version object when version object changes (destroys). Use methods and properties of the version directly (or through `self`) + text_custom_workflow_before_destroy_note: To prevent destroying of the object use `return false`. text_no_enabled_projects: No projects text_custom_workflow_author: Will be included in exported XML diff --git a/config/locales/es.yml b/config/locales/es.yml index 5f2e122..c8339ac 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -66,7 +66,7 @@ es: text_select_project_custom_workflows: Seleccione un workflow personalizado de proyecto text_custom_workflow_before_save_note: Aquí puede cambiar propiedades de las peticiones. No cree ni actualice peticiones - relacionadas en este script. Para acabar con error, use `raise WorkflowError, 'Message to user'`. + relacionadas en este script. Para acabar con error, use `raise RedmineCustomWorkflows::Errors::WorkflowError, 'Message to user'`. text_custom_workflow_after_save_note: Puede actualizar o crear peticiones relacionadas aquí. Tenga en cuenta que este script también se ejecutará para las peticiones recién creadas. Así que haga las comprobaciones oportunas para evitar la recursión infinita. @@ -102,6 +102,7 @@ es: cambia (destruye). Utilice los métodos y propiedades de la entrada de tiempo directamente (o a través de `self`) text_custom_workflow_version_code_note: Los scripts se ejecutan en el contexto del objeto Version cuando la versión cambia (destruye). Utilizar métodos y propiedades de la versión directamente (o a través de `self`) + text_custom_workflow_before_destroy_note: To prevent destroying of the object use `return false`. text_no_enabled_projects: Sin proyectos text_custom_workflow_author: Se incluirá en el XML exportado diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 055eba4..9ef794c 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -66,7 +66,7 @@ pt-BR: text_select_project_custom_workflows: Selecione o projeto para os fluxos de trabalho text_custom_workflow_before_save_note: Você pode alterar as propriedades das tarefas aqui. Não crie ou atualize - tarefas relacionadas neste script. Para terminar com o erro, utilize a `raise WorkflowError, 'Mensagem para o usuário'`. + tarefas relacionadas neste script. Para terminar com o erro, utilize a `raise RedmineCustomWorkflows::Errors::WorkflowError, 'Mensagem para o usuário'`. text_custom_workflow_after_save_note: Você pode atualizar ou criar tarefas relacionadas aqui. Note que esse script também será executado para as tarefas criadas recentemente. Então, faça as verificações adequadas para evitar que entre em recursividade infinita. @@ -102,6 +102,7 @@ pt-BR: object changes (destroys). Use methods and properties of the time entry directly (or through `self`) text_custom_workflow_version_code_note: Scripts are executed in the context of Version object when version object changes (destroys). Use methods and properties of the version directly (or through `self`) + text_custom_workflow_before_destroy_note: To prevent destroying of the object use `return false`. text_no_enabled_projects: Não há projeto text_custom_workflow_author: Será incluído no XML exportado diff --git a/lib/redmine_custom_workflows/patches/controllers/attachments_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/attachments_controller_patch.rb index 71da15d..fb84d94 100644 --- a/lib/redmine_custom_workflows/patches/controllers/attachments_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/attachments_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @attachments - objects = @attachments - elsif @attachment - objects = [@attachment] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @attachments + objects = @attachments + elsif @attachment + objects = [@attachment] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/groups_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/groups_controller_patch.rb index 78a2977..b60a172 100644 --- a/lib/redmine_custom_workflows/patches/controllers/groups_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/groups_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @groups - objects = @groups - elsif @group - objects = [@group] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @groups + objects = @groups + elsif @group + objects = [@group] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/issue_relations_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/issue_relations_controller_patch.rb index 5d62feb..a27f61a 100644 --- a/lib/redmine_custom_workflows/patches/controllers/issue_relations_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/issue_relations_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @relations - objects = @relations - elsif @relation - objects = [@relation] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @relations + objects = @relations + elsif @relation + objects = [@relation] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/issues_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/issues_controller_patch.rb index 508678f..121974a 100644 --- a/lib/redmine_custom_workflows/patches/controllers/issues_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/issues_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @issues - objects = @issues - elsif @issue - objects = [@issue] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @issues + objects = @issues + elsif @issue + objects = [@issue] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/projects_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/projects_controller_patch.rb index 2a7cb15..9cd01f9 100644 --- a/lib/redmine_custom_workflows/patches/controllers/projects_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/projects_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @projects - objects = @projects - elsif @project - objects = [@project] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @projects + objects = @projects + elsif @project + objects = [@project] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/timelog_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/timelog_controller_patch.rb index 6694dc2..a5f83da 100644 --- a/lib/redmine_custom_workflows/patches/controllers/timelog_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/timelog_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @time_entries - objects = @time_entries - elsif @time_entry - objects = [@time_entry] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @time_entries + objects = @time_entries + elsif @time_entry + objects = [@time_entry] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/users_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/users_controller_patch.rb index e30a689..09e403e 100644 --- a/lib/redmine_custom_workflows/patches/controllers/users_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/users_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @users - objects = @users - elsif @user - objects = [@user] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,17 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @users + objects = @users + elsif @user + objects = [@user] + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/versions_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/versions_controller_patch.rb index 6d74bd4..8e695cd 100644 --- a/lib/redmine_custom_workflows/patches/controllers/versions_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/versions_controller_patch.rb @@ -31,21 +31,27 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end - def display_custom_workflow_messages - if @versions - objects = @versions - elsif @version - objects = [@version] + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end end + end + + def display_custom_workflow_messages + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -57,6 +63,16 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @versions + objects = @versions + elsif @version + objects = [@version] + end + end + end end end diff --git a/lib/redmine_custom_workflows/patches/controllers/wiki_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/wiki_controller_patch.rb index 83ca3c6..be52329 100644 --- a/lib/redmine_custom_workflows/patches/controllers/wiki_controller_patch.rb +++ b/lib/redmine_custom_workflows/patches/controllers/wiki_controller_patch.rb @@ -31,25 +31,29 @@ module RedmineCustomWorkflows def self.prepended(base) base.class_eval do + before_action :set_env after_action :display_custom_workflow_messages end end + def set_env + objects = get_model_objects + if objects&.any? + objects.each do |o| + if request.remote_ip.present? + o.custom_workflow_env[:remote_ip] = request.remote_ip + end + end + end + end + def display_custom_workflow_messages - if @pages - objects = @pages - elsif @page - objects = [@page] - end - if @content - objects ||= [] - objects << @content - end + objects = get_model_objects if objects&.any? objects.each do |o| if o&.custom_workflow_messages&.any? o.custom_workflow_messages.each do |key, value| - if value.empty? + unless value&.present? flash.delete key else flash[key] = value @@ -61,6 +65,21 @@ module RedmineCustomWorkflows end end + private + + def get_model_objects + if @pages + objects = @pages + elsif @page + objects = [@page] + end + if @content + objects ||= [] + objects << @content + end + objects + end + end end end diff --git a/lib/redmine_custom_workflows/patches/models/attachment_patch.rb b/lib/redmine_custom_workflows/patches/models/attachment_patch.rb index 79263be..1800089 100644 --- a/lib/redmine_custom_workflows/patches/models/attachment_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/attachment_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module AttachmentPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/group_patch.rb b/lib/redmine_custom_workflows/patches/models/group_patch.rb index 8aac055..434940e 100644 --- a/lib/redmine_custom_workflows/patches/models/group_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/group_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module GroupPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/issue_patch.rb b/lib/redmine_custom_workflows/patches/models/issue_patch.rb index eab3c32..1351a12 100644 --- a/lib/redmine_custom_workflows/patches/models/issue_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/issue_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module IssuePatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/issue_relation_patch.rb b/lib/redmine_custom_workflows/patches/models/issue_relation_patch.rb index 75df370..062cc44 100644 --- a/lib/redmine_custom_workflows/patches/models/issue_relation_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/issue_relation_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module IssueRelationPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/project_patch.rb b/lib/redmine_custom_workflows/patches/models/project_patch.rb index e3e323b..b08726f 100644 --- a/lib/redmine_custom_workflows/patches/models/project_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/project_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module ProjectPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do has_and_belongs_to_many :custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/time_entry_patch.rb b/lib/redmine_custom_workflows/patches/models/time_entry_patch.rb index 2e5a77d..8ebe5a1 100644 --- a/lib/redmine_custom_workflows/patches/models/time_entry_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/time_entry_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module TimeEntryPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/user_patch.rb b/lib/redmine_custom_workflows/patches/models/user_patch.rb index e92a672..b9bfacd 100644 --- a/lib/redmine_custom_workflows/patches/models/user_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/user_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module UserPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/version_patch.rb b/lib/redmine_custom_workflows/patches/models/version_patch.rb index d59601d..4fcb957 100644 --- a/lib/redmine_custom_workflows/patches/models/version_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/version_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module VersionPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/wiki_content_patch.rb b/lib/redmine_custom_workflows/patches/models/wiki_content_patch.rb index 858dd48..0c86750 100644 --- a/lib/redmine_custom_workflows/patches/models/wiki_content_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/wiki_content_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module WikiContentPatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do before_save :before_save_custom_workflows diff --git a/lib/redmine_custom_workflows/patches/models/wiki_page_patch.rb b/lib/redmine_custom_workflows/patches/models/wiki_page_patch.rb index 8259dcf..1846dc3 100644 --- a/lib/redmine_custom_workflows/patches/models/wiki_page_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/wiki_page_patch.rb @@ -26,11 +26,16 @@ module RedmineCustomWorkflows module WikiPagePatch attr_accessor 'custom_workflow_messages' + attr_accessor 'custom_workflow_env' def custom_workflow_messages @custom_workflow_messages ||= {} end + def custom_workflow_env + @custom_workflow_env ||= {} + end + def self.prepended(base) base.class_eval do def self.attachments_callback(event, page, attachment) diff --git a/test/fixtures/custom_workflows.yml b/test/fixtures/custom_workflows.yml index a0533f1..8a70ff1 100644 --- a/test/fixtures/custom_workflows.yml +++ b/test/fixtures/custom_workflows.yml @@ -1,7 +1,8 @@ --- custom_workflows_001: id: 1 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_save: '' name: 'Issue CW test' description: 'Testing workflow' @@ -23,7 +24,8 @@ custom_workflows_001: custom_workflows_002: id: 2 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_save: '' name: 'Version CW test' description: 'Testing workflow' @@ -44,7 +46,8 @@ custom_workflows_002: custom_workflows_003: id: 3 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_save: '' name: 'User CW test' description: 'Testing workflow' @@ -65,7 +68,8 @@ custom_workflows_003: custom_workflows_004: id: 4 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_save: '' name: 'Project CW test' description: 'Testing workflow' @@ -86,7 +90,8 @@ custom_workflows_004: custom_workflows_005: id: 5 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_save: '' name: 'Group CW test' description: 'Testing workflow' @@ -123,7 +128,8 @@ custom_workflows_006: after_add: NULL before_remove: NULL after_remove: NULL - before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_destroy : '' custom_workflows_007: @@ -144,7 +150,8 @@ custom_workflows_007: after_add: NULL before_remove: NULL after_remove: NULL - before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_destroy : '' custom_workflows_008: @@ -165,14 +172,17 @@ custom_workflows_008: after_add: NULL before_remove: NULL after_remove: NULL - before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" after_destroy : '' custom_workflows_009: id: 9 - before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]\n + Rails.logger.info \">>> #{self.class.name} #{self.id }#{self.custom_workflow_env[:remote_ip]}\"" after_save: '' - name: 'Wiki CW test' + name: 'Wiki content CW test' description: 'Testing workflow' position: 9 is_for_all: 0 @@ -186,5 +196,28 @@ custom_workflows_009: after_add: NULL before_remove: NULL after_remove: NULL - before_destroy: '' - after_destroy : '' \ No newline at end of file + before_destroy: "self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]\n + Rails.logger.info \">>> #{self.class.name} #{self.id }#{self.custom_workflow_env[:remote_ip]}\"" + after_destroy : '' + +custom_workflows_010: + id: 10 + before_save: "self.custom_workflow_messages[:notice] = 'Custom workflow'" + after_save: '' + name: 'Wiki page CW test' + description: 'Testing workflow' + position: 10 + is_for_all: 0 + author: 'karel.picman@kontron.com' + created_at: 2018-12-20 14:01:27 +02:00 + updated_at: 2018-12-20 14:01:27 +02:00 + active: 1 + observable: 'wiki_page' + shared_code: NULL + before_add: NULL + after_add: NULL + before_remove: NULL + after_remove: NULL + before_destroy: "self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]\n + Rails.logger.info \">>> #{self.class.name} #{self.id }#{self.custom_workflow_env[:remote_ip]}\"" + after_destroy: '' \ No newline at end of file diff --git a/test/fixtures/custom_workflows_projects.yml b/test/fixtures/custom_workflows_projects.yml index a7f19c9..4cd1ef3 100644 --- a/test/fixtures/custom_workflows_projects.yml +++ b/test/fixtures/custom_workflows_projects.yml @@ -18,3 +18,7 @@ custom_workflows_projects_004: custom_workflows_projects_005: project_id: 1 custom_workflow_id: 9 # Wiki content + +custom_workflows_projects_006: + project_id: 1 + custom_workflow_id: 10 # Wiki page diff --git a/test/functional/attachments_controller_patch_test.rb b/test/functional/attachments_controller_patch_test.rb index ee8edf1..da3a76f 100644 --- a/test/functional/attachments_controller_patch_test.rb +++ b/test/functional/attachments_controller_patch_test.rb @@ -34,9 +34,15 @@ class AttachmentsControllerPatchTest < RedmineCustomWorkflows::Test::TestCase end def test_delete_with_cw - delete :destroy, params: { id: @attachment8 } + delete :destroy, params: { id: @attachment8.id } assert_response :redirect assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + delete :destroy, params: { id: @attachment8.id } + assert_response :redirect + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/groups_controller_patch_test.rb b/test/functional/groups_controller_patch_test.rb index 0196d04..85eb13b 100644 --- a/test/functional/groups_controller_patch_test.rb +++ b/test/functional/groups_controller_patch_test.rb @@ -41,4 +41,11 @@ class GroupControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + @request.headers['Referer'] = edit_group_path(id: @group10.id) + put :update, params: { id: @group10.id, group: { name: 'Updated name' } } + assert_redirected_to edit_group_path(id: @group10.id) + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/issue_relations_controller_patch_test.rb b/test/functional/issue_relations_controller_patch_test.rb index 8370ae5..a6542d8 100644 --- a/test/functional/issue_relations_controller_patch_test.rb +++ b/test/functional/issue_relations_controller_patch_test.rb @@ -40,4 +40,10 @@ class IssueRelationsControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + delete :destroy, params: { id: @ir1 } + assert_response :redirect + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/issues_controller_patch_test.rb b/test/functional/issues_controller_patch_test.rb index 38840ff..64b52ca 100644 --- a/test/functional/issues_controller_patch_test.rb +++ b/test/functional/issues_controller_patch_test.rb @@ -47,4 +47,10 @@ class IssuesControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert Issue.find_by(id: @issue1.id) end + def test_cw_env + put :update, params: { id: @issue1.id, issue: { subject: 'Updated subject' } } + assert_redirected_to action: 'show', id: @issue1.id + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/projects_controller_patch_test.rb b/test/functional/projects_controller_patch_test.rb index 9da3251..dd6ef7d 100644 --- a/test/functional/projects_controller_patch_test.rb +++ b/test/functional/projects_controller_patch_test.rb @@ -38,4 +38,10 @@ class ProjectsControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + post :update, params: { id: @project1.id, project: { name: 'Updated name' } } + assert_redirected_to settings_project_path(@project1) + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/timelog_controller_patch_test.rb b/test/functional/timelog_controller_patch_test.rb index dac4caf..70cdd4d 100644 --- a/test/functional/timelog_controller_patch_test.rb +++ b/test/functional/timelog_controller_patch_test.rb @@ -40,4 +40,10 @@ class TimelogControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + delete :destroy, params: { id: @te1 } + assert_response :redirect + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/users_controller_patch_test.rb b/test/functional/users_controller_patch_test.rb index 2d9c4e6..aabf700 100644 --- a/test/functional/users_controller_patch_test.rb +++ b/test/functional/users_controller_patch_test.rb @@ -37,4 +37,10 @@ class UsersControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + put :update, params: { id: @jsmith.id, user: { lastname: 'updated_lastname' }} + assert_redirected_to edit_user_path(id: @jsmith.id) + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/functional/versions_controller_patch_test.rb b/test/functional/versions_controller_patch_test.rb index 5352291..2968f2b 100644 --- a/test/functional/versions_controller_patch_test.rb +++ b/test/functional/versions_controller_patch_test.rb @@ -38,4 +38,10 @@ class VersionsControllerPatchTest < RedmineCustomWorkflows::Test::TestCase assert_equal 'Custom workflow', @controller.flash[:notice] end + def test_cw_env + put :update, params: { id: @version1.id, version: { name: 'Updated version' } } + assert_redirected_to settings_project_path(id: @project1, tab: 'versions') + assert_equal request.remote_ip, @controller.flash[:warning] + end + end \ No newline at end of file diff --git a/test/unit/attachment_patch_test.rb b/test/unit/attachment_patch_test.rb index e12de3f..ee285dc 100644 --- a/test/unit/attachment_patch_test.rb +++ b/test/unit/attachment_patch_test.rb @@ -38,4 +38,9 @@ class AttachmentPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @attachment1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @attachment1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @attachment1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/group_patch_test.rb b/test/unit/group_patch_test.rb index 8456755..71f9dcd 100644 --- a/test/unit/group_patch_test.rb +++ b/test/unit/group_patch_test.rb @@ -38,4 +38,9 @@ class GroupPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @group10.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @group10.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @group10.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/issue_patch_test.rb b/test/unit/issue_patch_test.rb index 853d786..97d1922 100644 --- a/test/unit/issue_patch_test.rb +++ b/test/unit/issue_patch_test.rb @@ -38,4 +38,9 @@ class IssuePatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @issue1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @issue1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @issue1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/issue_relation_patch_test.rb b/test/unit/issue_relation_patch_test.rb index 9f04641..f5cc522 100644 --- a/test/unit/issue_relation_patch_test.rb +++ b/test/unit/issue_relation_patch_test.rb @@ -38,4 +38,9 @@ class IssueRelationPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @issue_relation1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @issue_relation1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @issue_relation1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/project_patch_test.rb b/test/unit/project_patch_test.rb index b32df40..5c4d546 100644 --- a/test/unit/project_patch_test.rb +++ b/test/unit/project_patch_test.rb @@ -38,4 +38,9 @@ class ProjectPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @project1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @project1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @project1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/time_entry_patch_test.rb b/test/unit/time_entry_patch_test.rb index 1609713..c2d314f 100644 --- a/test/unit/time_entry_patch_test.rb +++ b/test/unit/time_entry_patch_test.rb @@ -38,4 +38,9 @@ class TimeEntryPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @time_entry1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @time_entry1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @time_entry1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/user_patch_test.rb b/test/unit/user_patch_test.rb index d4e32dd..58a7154 100644 --- a/test/unit/user_patch_test.rb +++ b/test/unit/user_patch_test.rb @@ -38,4 +38,9 @@ class UserPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @user1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @user1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @user1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/version_patch_test.rb b/test/unit/version_patch_test.rb index 688a017..55aa291 100644 --- a/test/unit/version_patch_test.rb +++ b/test/unit/version_patch_test.rb @@ -38,4 +38,9 @@ class VersionPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @version1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @version1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @version1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/wiki_content_patch_test.rb b/test/unit/wiki_content_patch_test.rb index 2d42850..1e943b2 100644 --- a/test/unit/wiki_content_patch_test.rb +++ b/test/unit/wiki_content_patch_test.rb @@ -38,4 +38,9 @@ class WikiContentPatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @wiki_content1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @wiki_content1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @wiki_content1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file diff --git a/test/unit/wiki_page_patch_test.rb b/test/unit/wiki_page_patch_test.rb index 3930381..04e8442 100644 --- a/test/unit/wiki_page_patch_test.rb +++ b/test/unit/wiki_page_patch_test.rb @@ -38,4 +38,9 @@ class WikiPagePatchTest < RedmineCustomWorkflows::Test::UnitTest assert_equal 'Okay', @wiki_page1.custom_workflow_messages[:notice] end + def test_custom_workflow_env + @wiki_page1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @wiki_page1.custom_workflow_env[:remote_ip] + end + end \ No newline at end of file