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