@@ -1,147 +1,147 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 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 Role < ActiveRecord::Base |
|
19 | 19 | # Built-in roles |
|
20 | 20 | BUILTIN_NON_MEMBER = 1 |
|
21 | 21 | BUILTIN_ANONYMOUS = 2 |
|
22 | 22 | |
|
23 | 23 | named_scope :builtin, lambda { |*args| |
|
24 | 24 | compare = 'not' if args.first == true |
|
25 | 25 | { :conditions => "#{compare} builtin = 0" } |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | before_destroy :check_deletable |
|
29 | 29 | has_many :workflows, :dependent => :delete_all do |
|
30 | 30 | def copy(role) |
|
31 | 31 | raise "Can not copy workflow from a #{role.class}" unless role.is_a?(Role) |
|
32 | 32 | raise "Can not copy workflow from/to an unsaved role" if proxy_owner.new_record? || role.new_record? |
|
33 | 33 | clear |
|
34 |
connection.insert "INSERT INTO |
|
|
34 | connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, old_status_id, new_status_id, role_id)" + | |
|
35 | 35 | " SELECT tracker_id, old_status_id, new_status_id, #{proxy_owner.id}" + |
|
36 |
" FROM |
|
|
36 | " FROM #{Workflow.table_name}" + | |
|
37 | 37 | " WHERE role_id = #{role.id}" |
|
38 | 38 | end |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | has_many :members |
|
42 | 42 | acts_as_list |
|
43 | 43 | |
|
44 | 44 | serialize :permissions, Array |
|
45 | 45 | attr_protected :builtin |
|
46 | 46 | |
|
47 | 47 | validates_presence_of :name |
|
48 | 48 | validates_uniqueness_of :name |
|
49 | 49 | validates_length_of :name, :maximum => 30 |
|
50 | 50 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i |
|
51 | 51 | |
|
52 | 52 | def permissions |
|
53 | 53 | read_attribute(:permissions) || [] |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | def permissions=(perms) |
|
57 | 57 | perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms |
|
58 | 58 | write_attribute(:permissions, perms) |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def add_permission!(*perms) |
|
62 | 62 | self.permissions = [] unless permissions.is_a?(Array) |
|
63 | 63 | |
|
64 | 64 | permissions_will_change! |
|
65 | 65 | perms.each do |p| |
|
66 | 66 | p = p.to_sym |
|
67 | 67 | permissions << p unless permissions.include?(p) |
|
68 | 68 | end |
|
69 | 69 | save! |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def remove_permission!(*perms) |
|
73 | 73 | return unless permissions.is_a?(Array) |
|
74 | 74 | permissions_will_change! |
|
75 | 75 | perms.each { |p| permissions.delete(p.to_sym) } |
|
76 | 76 | save! |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | # Returns true if the role has the given permission |
|
80 | 80 | def has_permission?(perm) |
|
81 | 81 | !permissions.nil? && permissions.include?(perm.to_sym) |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def <=>(role) |
|
85 | 85 | position <=> role.position |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | # Return true if the role is a builtin role |
|
89 | 89 | def builtin? |
|
90 | 90 | self.builtin != 0 |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | # Return true if the role is a project member role |
|
94 | 94 | def member? |
|
95 | 95 | !self.builtin? |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | # Return true if role is allowed to do the specified action |
|
99 | 99 | # action can be: |
|
100 | 100 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
101 | 101 | # * a permission Symbol (eg. :edit_project) |
|
102 | 102 | def allowed_to?(action) |
|
103 | 103 | if action.is_a? Hash |
|
104 | 104 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
105 | 105 | else |
|
106 | 106 | allowed_permissions.include? action |
|
107 | 107 | end |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | # Return all the permissions that can be given to the role |
|
111 | 111 | def setable_permissions |
|
112 | 112 | setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions |
|
113 | 113 | setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER |
|
114 | 114 | setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS |
|
115 | 115 | setable_permissions |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | # Find all the roles that can be given to a project member |
|
119 | 119 | def self.find_all_givable |
|
120 | 120 | find(:all, :conditions => {:builtin => 0}, :order => 'position') |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | # Return the builtin 'non member' role |
|
124 | 124 | def self.non_member |
|
125 | 125 | find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER}) || raise('Missing non-member builtin role.') |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | # Return the builtin 'anonymous' role |
|
129 | 129 | def self.anonymous |
|
130 | 130 | find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS}) || raise('Missing anonymous builtin role.') |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | private |
|
135 | 135 | def allowed_permissions |
|
136 | 136 | @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name} |
|
137 | 137 | end |
|
138 | 138 | |
|
139 | 139 | def allowed_actions |
|
140 | 140 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | def check_deletable |
|
144 | 144 | raise "Can't delete role" if members.any? |
|
145 | 145 | raise "Can't delete builtin role" if builtin? |
|
146 | 146 | end |
|
147 | 147 | end |
@@ -1,56 +1,56 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 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 Tracker < ActiveRecord::Base |
|
19 | 19 | before_destroy :check_integrity |
|
20 | 20 | has_many :issues |
|
21 | 21 | has_many :workflows, :dependent => :delete_all do |
|
22 | 22 | def copy(tracker) |
|
23 | 23 | raise "Can not copy workflow from a #{tracker.class}" unless tracker.is_a?(Tracker) |
|
24 | 24 | raise "Can not copy workflow from/to an unsaved tracker" if proxy_owner.new_record? || tracker.new_record? |
|
25 | 25 | clear |
|
26 |
connection.insert "INSERT INTO |
|
|
26 | connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, old_status_id, new_status_id, role_id)" + | |
|
27 | 27 | " SELECT #{proxy_owner.id}, old_status_id, new_status_id, role_id" + |
|
28 |
" FROM |
|
|
28 | " FROM #{Workflow.table_name}" + | |
|
29 | 29 | " WHERE tracker_id = #{tracker.id}" |
|
30 | 30 | end |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | has_and_belongs_to_many :projects |
|
34 | 34 | has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_trackers#{table_name_suffix}", :association_foreign_key => 'custom_field_id' |
|
35 | 35 | acts_as_list |
|
36 | 36 | |
|
37 | 37 | validates_presence_of :name |
|
38 | 38 | validates_uniqueness_of :name |
|
39 | 39 | validates_length_of :name, :maximum => 30 |
|
40 | 40 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i |
|
41 | 41 | |
|
42 | 42 | def to_s; name end |
|
43 | 43 | |
|
44 | 44 | def <=>(tracker) |
|
45 | 45 | name <=> tracker.name |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def self.all |
|
49 | 49 | find(:all, :order => 'position') |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | private |
|
53 | 53 | def check_integrity |
|
54 | 54 | raise "Can't delete tracker" if Issue.find(:first, :conditions => ["tracker_id=?", self.id]) |
|
55 | 55 | end |
|
56 | 56 | end |
General Comments 0
You need to be logged in to leave comments.
Login now