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