You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflow_permission.rb 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class WorkflowPermission < WorkflowRule
  18. validates_inclusion_of :rule, :in => %w(readonly required)
  19. validates_presence_of :old_status
  20. validate :validate_field_name
  21. # Returns the workflow permissions for the given trackers and roles
  22. # grouped by status_id
  23. #
  24. # Example:
  25. # WorkflowPermission.rules_by_status_id trackers, roles
  26. # # => {1 => {'start_date' => 'required', 'due_date' => 'readonly'}}
  27. def self.rules_by_status_id(trackers, roles)
  28. WorkflowPermission.where(:tracker_id => trackers.map(&:id), :role_id => roles.map(&:id)).inject({}) do |h, w|
  29. h[w.old_status_id] ||= {}
  30. h[w.old_status_id][w.field_name] ||= []
  31. h[w.old_status_id][w.field_name] << w.rule
  32. h
  33. end
  34. end
  35. # Replaces the workflow permissions for the given trackers and roles
  36. #
  37. # Example:
  38. # WorkflowPermission.replace_permissions trackers, roles, {'1' => {'start_date' => 'required', 'due_date' => 'readonly'}}
  39. def self.replace_permissions(trackers, roles, permissions)
  40. trackers = Array.wrap trackers
  41. roles = Array.wrap roles
  42. transaction do
  43. permissions.each { |status_id, rule_by_field|
  44. rule_by_field.each { |field, rule|
  45. where(:tracker_id => trackers.map(&:id), :role_id => roles.map(&:id), :old_status_id => status_id, :field_name => field).destroy_all
  46. if rule.present?
  47. trackers.each do |tracker|
  48. roles.each do |role|
  49. WorkflowPermission.create(:role_id => role.id, :tracker_id => tracker.id, :old_status_id => status_id, :field_name => field, :rule => rule)
  50. end
  51. end
  52. end
  53. }
  54. }
  55. end
  56. end
  57. protected
  58. def validate_field_name
  59. unless Tracker::CORE_FIELDS_ALL.include?(field_name) || field_name.to_s.match(/^\d+$/)
  60. errors.add :field_name, :invalid
  61. end
  62. end
  63. end