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