選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

workflow_permission.rb 2.6KB

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