##// END OF EJS Templates
Fixed that r15668 prevents from setting managed roles....
Jean-Philippe Lang -
r15530:a2b17dae6e89
parent child
Show More
@@ -1,304 +1,305
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 Role < ActiveRecord::Base
18 class Role < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 include Redmine::SafeAttributes
20
20
21 # Custom coder for the permissions attribute that should be an
21 # Custom coder for the permissions attribute that should be an
22 # array of symbols. Rails 3 uses Psych which can be *unbelievably*
22 # array of symbols. Rails 3 uses Psych which can be *unbelievably*
23 # slow on some platforms (eg. mingw32).
23 # slow on some platforms (eg. mingw32).
24 class PermissionsAttributeCoder
24 class PermissionsAttributeCoder
25 def self.load(str)
25 def self.load(str)
26 str.to_s.scan(/:([a-z0-9_]+)/).flatten.map(&:to_sym)
26 str.to_s.scan(/:([a-z0-9_]+)/).flatten.map(&:to_sym)
27 end
27 end
28
28
29 def self.dump(value)
29 def self.dump(value)
30 YAML.dump(value)
30 YAML.dump(value)
31 end
31 end
32 end
32 end
33
33
34 # Built-in roles
34 # Built-in roles
35 BUILTIN_NON_MEMBER = 1
35 BUILTIN_NON_MEMBER = 1
36 BUILTIN_ANONYMOUS = 2
36 BUILTIN_ANONYMOUS = 2
37
37
38 ISSUES_VISIBILITY_OPTIONS = [
38 ISSUES_VISIBILITY_OPTIONS = [
39 ['all', :label_issues_visibility_all],
39 ['all', :label_issues_visibility_all],
40 ['default', :label_issues_visibility_public],
40 ['default', :label_issues_visibility_public],
41 ['own', :label_issues_visibility_own]
41 ['own', :label_issues_visibility_own]
42 ]
42 ]
43
43
44 TIME_ENTRIES_VISIBILITY_OPTIONS = [
44 TIME_ENTRIES_VISIBILITY_OPTIONS = [
45 ['all', :label_time_entries_visibility_all],
45 ['all', :label_time_entries_visibility_all],
46 ['own', :label_time_entries_visibility_own]
46 ['own', :label_time_entries_visibility_own]
47 ]
47 ]
48
48
49 USERS_VISIBILITY_OPTIONS = [
49 USERS_VISIBILITY_OPTIONS = [
50 ['all', :label_users_visibility_all],
50 ['all', :label_users_visibility_all],
51 ['members_of_visible_projects', :label_users_visibility_members_of_visible_projects]
51 ['members_of_visible_projects', :label_users_visibility_members_of_visible_projects]
52 ]
52 ]
53
53
54 scope :sorted, lambda { order(:builtin, :position) }
54 scope :sorted, lambda { order(:builtin, :position) }
55 scope :givable, lambda { order(:position).where(:builtin => 0) }
55 scope :givable, lambda { order(:position).where(:builtin => 0) }
56 scope :builtin, lambda { |*args|
56 scope :builtin, lambda { |*args|
57 compare = (args.first == true ? 'not' : '')
57 compare = (args.first == true ? 'not' : '')
58 where("#{compare} builtin = 0")
58 where("#{compare} builtin = 0")
59 }
59 }
60
60
61 before_destroy :check_deletable
61 before_destroy :check_deletable
62 has_many :workflow_rules, :dependent => :delete_all do
62 has_many :workflow_rules, :dependent => :delete_all do
63 def copy(source_role)
63 def copy(source_role)
64 WorkflowRule.copy(nil, source_role, nil, proxy_association.owner)
64 WorkflowRule.copy(nil, source_role, nil, proxy_association.owner)
65 end
65 end
66 end
66 end
67 has_and_belongs_to_many :custom_fields, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "role_id"
67 has_and_belongs_to_many :custom_fields, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "role_id"
68
68
69 has_and_belongs_to_many :managed_roles, :class_name => 'Role',
69 has_and_belongs_to_many :managed_roles, :class_name => 'Role',
70 :join_table => "#{table_name_prefix}roles_managed_roles#{table_name_suffix}",
70 :join_table => "#{table_name_prefix}roles_managed_roles#{table_name_suffix}",
71 :association_foreign_key => "managed_role_id"
71 :association_foreign_key => "managed_role_id"
72
72
73 has_many :member_roles, :dependent => :destroy
73 has_many :member_roles, :dependent => :destroy
74 has_many :members, :through => :member_roles
74 has_many :members, :through => :member_roles
75 acts_as_positioned :scope => :builtin
75 acts_as_positioned :scope => :builtin
76
76
77 serialize :permissions, ::Role::PermissionsAttributeCoder
77 serialize :permissions, ::Role::PermissionsAttributeCoder
78 store :settings, :accessors => [:permissions_all_trackers, :permissions_tracker_ids]
78 store :settings, :accessors => [:permissions_all_trackers, :permissions_tracker_ids]
79 attr_protected :builtin
79 attr_protected :builtin
80
80
81 validates_presence_of :name
81 validates_presence_of :name
82 validates_uniqueness_of :name
82 validates_uniqueness_of :name
83 validates_length_of :name, :maximum => 30
83 validates_length_of :name, :maximum => 30
84 validates_inclusion_of :issues_visibility,
84 validates_inclusion_of :issues_visibility,
85 :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
85 :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
86 :if => lambda {|role| role.respond_to?(:issues_visibility) && role.issues_visibility_changed?}
86 :if => lambda {|role| role.respond_to?(:issues_visibility) && role.issues_visibility_changed?}
87 validates_inclusion_of :users_visibility,
87 validates_inclusion_of :users_visibility,
88 :in => USERS_VISIBILITY_OPTIONS.collect(&:first),
88 :in => USERS_VISIBILITY_OPTIONS.collect(&:first),
89 :if => lambda {|role| role.respond_to?(:users_visibility) && role.users_visibility_changed?}
89 :if => lambda {|role| role.respond_to?(:users_visibility) && role.users_visibility_changed?}
90 validates_inclusion_of :time_entries_visibility,
90 validates_inclusion_of :time_entries_visibility,
91 :in => TIME_ENTRIES_VISIBILITY_OPTIONS.collect(&:first),
91 :in => TIME_ENTRIES_VISIBILITY_OPTIONS.collect(&:first),
92 :if => lambda {|role| role.respond_to?(:time_entries_visibility) && role.time_entries_visibility_changed?}
92 :if => lambda {|role| role.respond_to?(:time_entries_visibility) && role.time_entries_visibility_changed?}
93
93
94 safe_attributes 'name',
94 safe_attributes 'name',
95 'assignable',
95 'assignable',
96 'position',
96 'position',
97 'issues_visibility',
97 'issues_visibility',
98 'users_visibility',
98 'users_visibility',
99 'time_entries_visibility',
99 'time_entries_visibility',
100 'all_roles_managed',
100 'all_roles_managed',
101 'managed_role_ids',
101 'permissions',
102 'permissions',
102 'permissions_all_trackers',
103 'permissions_all_trackers',
103 'permissions_tracker_ids'
104 'permissions_tracker_ids'
104
105
105 # Copies attributes from another role, arg can be an id or a Role
106 # Copies attributes from another role, arg can be an id or a Role
106 def copy_from(arg, options={})
107 def copy_from(arg, options={})
107 return unless arg.present?
108 return unless arg.present?
108 role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
109 role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
109 self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
110 self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
110 self.permissions = role.permissions.dup
111 self.permissions = role.permissions.dup
111 self
112 self
112 end
113 end
113
114
114 def permissions=(perms)
115 def permissions=(perms)
115 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
116 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
116 write_attribute(:permissions, perms)
117 write_attribute(:permissions, perms)
117 end
118 end
118
119
119 def add_permission!(*perms)
120 def add_permission!(*perms)
120 self.permissions = [] unless permissions.is_a?(Array)
121 self.permissions = [] unless permissions.is_a?(Array)
121
122
122 permissions_will_change!
123 permissions_will_change!
123 perms.each do |p|
124 perms.each do |p|
124 p = p.to_sym
125 p = p.to_sym
125 permissions << p unless permissions.include?(p)
126 permissions << p unless permissions.include?(p)
126 end
127 end
127 save!
128 save!
128 end
129 end
129
130
130 def remove_permission!(*perms)
131 def remove_permission!(*perms)
131 return unless permissions.is_a?(Array)
132 return unless permissions.is_a?(Array)
132 permissions_will_change!
133 permissions_will_change!
133 perms.each { |p| permissions.delete(p.to_sym) }
134 perms.each { |p| permissions.delete(p.to_sym) }
134 save!
135 save!
135 end
136 end
136
137
137 # Returns true if the role has the given permission
138 # Returns true if the role has the given permission
138 def has_permission?(perm)
139 def has_permission?(perm)
139 !permissions.nil? && permissions.include?(perm.to_sym)
140 !permissions.nil? && permissions.include?(perm.to_sym)
140 end
141 end
141
142
142 def consider_workflow?
143 def consider_workflow?
143 has_permission?(:add_issues) || has_permission?(:edit_issues)
144 has_permission?(:add_issues) || has_permission?(:edit_issues)
144 end
145 end
145
146
146 def <=>(role)
147 def <=>(role)
147 if role
148 if role
148 if builtin == role.builtin
149 if builtin == role.builtin
149 position <=> role.position
150 position <=> role.position
150 else
151 else
151 builtin <=> role.builtin
152 builtin <=> role.builtin
152 end
153 end
153 else
154 else
154 -1
155 -1
155 end
156 end
156 end
157 end
157
158
158 def to_s
159 def to_s
159 name
160 name
160 end
161 end
161
162
162 def name
163 def name
163 case builtin
164 case builtin
164 when 1; l(:label_role_non_member, :default => read_attribute(:name))
165 when 1; l(:label_role_non_member, :default => read_attribute(:name))
165 when 2; l(:label_role_anonymous, :default => read_attribute(:name))
166 when 2; l(:label_role_anonymous, :default => read_attribute(:name))
166 else; read_attribute(:name)
167 else; read_attribute(:name)
167 end
168 end
168 end
169 end
169
170
170 # Return true if the role is a builtin role
171 # Return true if the role is a builtin role
171 def builtin?
172 def builtin?
172 self.builtin != 0
173 self.builtin != 0
173 end
174 end
174
175
175 # Return true if the role is the anonymous role
176 # Return true if the role is the anonymous role
176 def anonymous?
177 def anonymous?
177 builtin == 2
178 builtin == 2
178 end
179 end
179
180
180 # Return true if the role is a project member role
181 # Return true if the role is a project member role
181 def member?
182 def member?
182 !self.builtin?
183 !self.builtin?
183 end
184 end
184
185
185 # Return true if role is allowed to do the specified action
186 # Return true if role is allowed to do the specified action
186 # action can be:
187 # action can be:
187 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
188 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
188 # * a permission Symbol (eg. :edit_project)
189 # * a permission Symbol (eg. :edit_project)
189 def allowed_to?(action)
190 def allowed_to?(action)
190 if action.is_a? Hash
191 if action.is_a? Hash
191 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
192 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
192 else
193 else
193 allowed_permissions.include? action
194 allowed_permissions.include? action
194 end
195 end
195 end
196 end
196
197
197 # Return all the permissions that can be given to the role
198 # Return all the permissions that can be given to the role
198 def setable_permissions
199 def setable_permissions
199 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
200 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
200 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
201 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
201 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
202 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
202 setable_permissions
203 setable_permissions
203 end
204 end
204
205
205 def permissions_tracker_ids(*args)
206 def permissions_tracker_ids(*args)
206 if args.any?
207 if args.any?
207 Array(permissions_tracker_ids[args.first.to_s]).map(&:to_i)
208 Array(permissions_tracker_ids[args.first.to_s]).map(&:to_i)
208 else
209 else
209 super || {}
210 super || {}
210 end
211 end
211 end
212 end
212
213
213 def permissions_tracker_ids=(arg)
214 def permissions_tracker_ids=(arg)
214 h = arg.to_hash
215 h = arg.to_hash
215 h.values.each {|v| v.reject!(&:blank?)}
216 h.values.each {|v| v.reject!(&:blank?)}
216 super(h)
217 super(h)
217 end
218 end
218
219
219 # Returns true if tracker_id belongs to the list of
220 # Returns true if tracker_id belongs to the list of
220 # trackers for which permission is given
221 # trackers for which permission is given
221 def permissions_tracker_ids?(permission, tracker_id)
222 def permissions_tracker_ids?(permission, tracker_id)
222 permissions_tracker_ids(permission).include?(tracker_id)
223 permissions_tracker_ids(permission).include?(tracker_id)
223 end
224 end
224
225
225 def permissions_all_trackers
226 def permissions_all_trackers
226 super || {}
227 super || {}
227 end
228 end
228
229
229 def permissions_all_trackers=(arg)
230 def permissions_all_trackers=(arg)
230 super(arg.to_hash)
231 super(arg.to_hash)
231 end
232 end
232
233
233 # Returns true if permission is given for all trackers
234 # Returns true if permission is given for all trackers
234 def permissions_all_trackers?(permission)
235 def permissions_all_trackers?(permission)
235 permissions_all_trackers[permission.to_s].to_s != '0'
236 permissions_all_trackers[permission.to_s].to_s != '0'
236 end
237 end
237
238
238 # Returns true if permission is given for the tracker
239 # Returns true if permission is given for the tracker
239 # (explicitly or for all trackers)
240 # (explicitly or for all trackers)
240 def permissions_tracker?(permission, tracker)
241 def permissions_tracker?(permission, tracker)
241 permissions_all_trackers?(permission) ||
242 permissions_all_trackers?(permission) ||
242 permissions_tracker_ids?(permission, tracker.try(:id))
243 permissions_tracker_ids?(permission, tracker.try(:id))
243 end
244 end
244
245
245 # Sets the trackers that are allowed for a permission.
246 # Sets the trackers that are allowed for a permission.
246 # tracker_ids can be an array of tracker ids or :all for
247 # tracker_ids can be an array of tracker ids or :all for
247 # no restrictions.
248 # no restrictions.
248 #
249 #
249 # Examples:
250 # Examples:
250 # role.set_permission_trackers :add_issues, [1, 3]
251 # role.set_permission_trackers :add_issues, [1, 3]
251 # role.set_permission_trackers :add_issues, :all
252 # role.set_permission_trackers :add_issues, :all
252 def set_permission_trackers(permission, tracker_ids)
253 def set_permission_trackers(permission, tracker_ids)
253 h = {permission.to_s => (tracker_ids == :all ? '1' : '0')}
254 h = {permission.to_s => (tracker_ids == :all ? '1' : '0')}
254 self.permissions_all_trackers = permissions_all_trackers.merge(h)
255 self.permissions_all_trackers = permissions_all_trackers.merge(h)
255
256
256 h = {permission.to_s => (tracker_ids == :all ? [] : tracker_ids)}
257 h = {permission.to_s => (tracker_ids == :all ? [] : tracker_ids)}
257 self.permissions_tracker_ids = permissions_tracker_ids.merge(h)
258 self.permissions_tracker_ids = permissions_tracker_ids.merge(h)
258
259
259 self
260 self
260 end
261 end
261
262
262 # Find all the roles that can be given to a project member
263 # Find all the roles that can be given to a project member
263 def self.find_all_givable
264 def self.find_all_givable
264 Role.givable.to_a
265 Role.givable.to_a
265 end
266 end
266
267
267 # Return the builtin 'non member' role. If the role doesn't exist,
268 # Return the builtin 'non member' role. If the role doesn't exist,
268 # it will be created on the fly.
269 # it will be created on the fly.
269 def self.non_member
270 def self.non_member
270 find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
271 find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
271 end
272 end
272
273
273 # Return the builtin 'anonymous' role. If the role doesn't exist,
274 # Return the builtin 'anonymous' role. If the role doesn't exist,
274 # it will be created on the fly.
275 # it will be created on the fly.
275 def self.anonymous
276 def self.anonymous
276 find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
277 find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
277 end
278 end
278
279
279 private
280 private
280
281
281 def allowed_permissions
282 def allowed_permissions
282 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
283 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
283 end
284 end
284
285
285 def allowed_actions
286 def allowed_actions
286 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
287 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
287 end
288 end
288
289
289 def check_deletable
290 def check_deletable
290 raise "Cannot delete role" if members.any?
291 raise "Cannot delete role" if members.any?
291 raise "Cannot delete builtin role" if builtin?
292 raise "Cannot delete builtin role" if builtin?
292 end
293 end
293
294
294 def self.find_or_create_system_role(builtin, name)
295 def self.find_or_create_system_role(builtin, name)
295 role = where(:builtin => builtin).first
296 role = where(:builtin => builtin).first
296 if role.nil?
297 if role.nil?
297 role = create(:name => name) do |r|
298 role = create(:name => name) do |r|
298 r.builtin = builtin
299 r.builtin = builtin
299 end
300 end
300 raise "Unable to create the #{name} role (#{role.errors.full_messages.join(',')})." if role.new_record?
301 raise "Unable to create the #{name} role (#{role.errors.full_messages.join(',')})." if role.new_record?
301 end
302 end
302 role
303 role
303 end
304 end
304 end
305 end
@@ -1,248 +1,263
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 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class RolesControllerTest < Redmine::ControllerTest
20 class RolesControllerTest < Redmine::ControllerTest
21 fixtures :roles, :users, :members, :member_roles, :workflows, :trackers
21 fixtures :roles, :users, :members, :member_roles, :workflows, :trackers
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 @request.session[:user_id] = 1 # admin
25 @request.session[:user_id] = 1 # admin
26 end
26 end
27
27
28 def test_index
28 def test_index
29 get :index
29 get :index
30 assert_response :success
30 assert_response :success
31
31
32 assert_select 'table.roles tbody' do
32 assert_select 'table.roles tbody' do
33 assert_select 'tr', Role.count
33 assert_select 'tr', Role.count
34 assert_select 'a[href="/roles/1/edit"]', :text => 'Manager'
34 assert_select 'a[href="/roles/1/edit"]', :text => 'Manager'
35 end
35 end
36 end
36 end
37
37
38 def test_new
38 def test_new
39 get :new
39 get :new
40 assert_response :success
40 assert_response :success
41 assert_select 'input[name=?]', 'role[name]'
41 assert_select 'input[name=?]', 'role[name]'
42 assert_select 'input[name=?]', 'role[permissions][]'
42 assert_select 'input[name=?]', 'role[permissions][]'
43 end
43 end
44
44
45 def test_new_should_prefill_permissions_with_non_member_permissions
45 def test_new_should_prefill_permissions_with_non_member_permissions
46 role = Role.non_member
46 role = Role.non_member
47 role.permissions = [:view_issues, :view_documents]
47 role.permissions = [:view_issues, :view_documents]
48 role.save!
48 role.save!
49
49
50 get :new
50 get :new
51 assert_response :success
51 assert_response :success
52 assert_equal %w(view_documents view_issues),
52 assert_equal %w(view_documents view_issues),
53 css_select('input[name="role[permissions][]"][checked=checked]').map {|e| e.attr('value')}.sort
53 css_select('input[name="role[permissions][]"][checked=checked]').map {|e| e.attr('value')}.sort
54 end
54 end
55
55
56 def test_new_with_copy
56 def test_new_with_copy
57 copy_from = Role.find(2)
57 copy_from = Role.find(2)
58
58
59 get :new, :params => {:copy => copy_from.id.to_s}
59 get :new, :params => {:copy => copy_from.id.to_s}
60 assert_response :success
60 assert_response :success
61 assert_select 'input[name=?]', 'role[name]'
61 assert_select 'input[name=?]', 'role[name]'
62
62
63 assert_select 'form' do
63 assert_select 'form' do
64 # blank name
64 # blank name
65 assert_select 'input[name=?][value=""]', 'role[name]'
65 assert_select 'input[name=?][value=""]', 'role[name]'
66 # edit_project permission checked
66 # edit_project permission checked
67 assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]'
67 assert_select 'input[type=checkbox][name=?][value=edit_project][checked=checked]', 'role[permissions][]'
68 # add_project permission not checked
68 # add_project permission not checked
69 assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]'
69 assert_select 'input[type=checkbox][name=?][value=add_project]', 'role[permissions][]'
70 assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0
70 assert_select 'input[type=checkbox][name=?][value=add_project][checked=checked]', 'role[permissions][]', 0
71 # workflow copy selected
71 # workflow copy selected
72 assert_select 'select[name=?]', 'copy_workflow_from' do
72 assert_select 'select[name=?]', 'copy_workflow_from' do
73 assert_select 'option[value="2"][selected=selected]'
73 assert_select 'option[value="2"][selected=selected]'
74 end
74 end
75 end
75 end
76 end
76 end
77
77
78 def test_create_with_validaton_failure
78 def test_create_with_validaton_failure
79 post :create, :params => {
79 post :create, :params => {
80 :role => {
80 :role => {
81 :name => '',
81 :name => '',
82 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
82 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
83 :assignable => '0'
83 :assignable => '0'
84 }
84 }
85 }
85 }
86 assert_response :success
86 assert_response :success
87 assert_select_error /Name cannot be blank/
87 assert_select_error /Name cannot be blank/
88 end
88 end
89
89
90 def test_create_without_workflow_copy
90 def test_create_without_workflow_copy
91 post :create, :params => {
91 post :create, :params => {
92 :role => {
92 :role => {
93 :name => 'RoleWithoutWorkflowCopy',
93 :name => 'RoleWithoutWorkflowCopy',
94 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
94 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
95 :assignable => '0'
95 :assignable => '0'
96 }
96 }
97 }
97 }
98 assert_redirected_to '/roles'
98 assert_redirected_to '/roles'
99 role = Role.find_by_name('RoleWithoutWorkflowCopy')
99 role = Role.find_by_name('RoleWithoutWorkflowCopy')
100 assert_not_nil role
100 assert_not_nil role
101 assert_equal [:add_issues, :edit_issues, :log_time], role.permissions
101 assert_equal [:add_issues, :edit_issues, :log_time], role.permissions
102 assert !role.assignable?
102 assert !role.assignable?
103 end
103 end
104
104
105 def test_create_with_workflow_copy
105 def test_create_with_workflow_copy
106 post :create, :params => {
106 post :create, :params => {
107 :role => {
107 :role => {
108 :name => 'RoleWithWorkflowCopy',
108 :name => 'RoleWithWorkflowCopy',
109 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
109 :permissions => ['add_issues', 'edit_issues', 'log_time', ''],
110 :assignable => '0'
110 :assignable => '0'
111 },
111 },
112 :copy_workflow_from => '1'
112 :copy_workflow_from => '1'
113 }
113 }
114 assert_redirected_to '/roles'
114 assert_redirected_to '/roles'
115 role = Role.find_by_name('RoleWithWorkflowCopy')
115 role = Role.find_by_name('RoleWithWorkflowCopy')
116 assert_not_nil role
116 assert_not_nil role
117 assert_equal Role.find(1).workflow_rules.size, role.workflow_rules.size
117 assert_equal Role.find(1).workflow_rules.size, role.workflow_rules.size
118 end
118 end
119
119
120 def test_create_with_managed_roles
121 role = new_record(Role) do
122 post :create, :params => {
123 :role => {
124 :name => 'Role',
125 :all_roles_managed => '0',
126 :managed_role_ids => ['2', '3', '']
127 }
128 }
129 assert_response 302
130 end
131 assert_equal false, role.all_roles_managed
132 assert_equal [2, 3], role.managed_role_ids
133 end
134
120 def test_edit
135 def test_edit
121 get :edit, :params => {:id => 1}
136 get :edit, :params => {:id => 1}
122 assert_response :success
137 assert_response :success
123
138
124 assert_select 'input[name=?][value=?]', 'role[name]', 'Manager'
139 assert_select 'input[name=?][value=?]', 'role[name]', 'Manager'
125 assert_select 'select[name=?]', 'role[issues_visibility]'
140 assert_select 'select[name=?]', 'role[issues_visibility]'
126 end
141 end
127
142
128 def test_edit_anonymous
143 def test_edit_anonymous
129 get :edit, :params => {:id => Role.anonymous.id}
144 get :edit, :params => {:id => Role.anonymous.id}
130 assert_response :success
145 assert_response :success
131
146
132 assert_select 'input[name=?]', 'role[name]', 0
147 assert_select 'input[name=?]', 'role[name]', 0
133 assert_select 'select[name=?]', 'role[issues_visibility]', 0
148 assert_select 'select[name=?]', 'role[issues_visibility]', 0
134 end
149 end
135
150
136 def test_edit_invalid_should_respond_with_404
151 def test_edit_invalid_should_respond_with_404
137 get :edit, :params => {:id => 999}
152 get :edit, :params => {:id => 999}
138 assert_response 404
153 assert_response 404
139 end
154 end
140
155
141 def test_update
156 def test_update
142 put :update, :params => {
157 put :update, :params => {
143 :id => 1,
158 :id => 1,
144 :role => {
159 :role => {
145 :name => 'Manager',
160 :name => 'Manager',
146 :permissions => ['edit_project', ''],
161 :permissions => ['edit_project', ''],
147 :assignable => '0'
162 :assignable => '0'
148 }
163 }
149 }
164 }
150 assert_redirected_to '/roles'
165 assert_redirected_to '/roles'
151 role = Role.find(1)
166 role = Role.find(1)
152 assert_equal [:edit_project], role.permissions
167 assert_equal [:edit_project], role.permissions
153 end
168 end
154
169
155 def test_update_trackers_permissions
170 def test_update_trackers_permissions
156 put :update, :params => {
171 put :update, :params => {
157 :id => 1,
172 :id => 1,
158 :role => {
173 :role => {
159 :permissions_all_trackers => {'add_issues' => '0'},
174 :permissions_all_trackers => {'add_issues' => '0'},
160 :permissions_tracker_ids => {'add_issues' => ['1', '3', '']}
175 :permissions_tracker_ids => {'add_issues' => ['1', '3', '']}
161 }
176 }
162 }
177 }
163 assert_redirected_to '/roles'
178 assert_redirected_to '/roles'
164 role = Role.find(1)
179 role = Role.find(1)
165
180
166 assert_equal({'add_issues' => '0'}, role.permissions_all_trackers)
181 assert_equal({'add_issues' => '0'}, role.permissions_all_trackers)
167 assert_equal({'add_issues' => ['1', '3']}, role.permissions_tracker_ids)
182 assert_equal({'add_issues' => ['1', '3']}, role.permissions_tracker_ids)
168
183
169 assert_equal false, role.permissions_all_trackers?(:add_issues)
184 assert_equal false, role.permissions_all_trackers?(:add_issues)
170 assert_equal [1, 3], role.permissions_tracker_ids(:add_issues).sort
185 assert_equal [1, 3], role.permissions_tracker_ids(:add_issues).sort
171 end
186 end
172
187
173 def test_update_with_failure
188 def test_update_with_failure
174 put :update, :params => {:id => 1, :role => {:name => ''}}
189 put :update, :params => {:id => 1, :role => {:name => ''}}
175 assert_response :success
190 assert_response :success
176 assert_select_error /Name cannot be blank/
191 assert_select_error /Name cannot be blank/
177 end
192 end
178
193
179 def test_destroy
194 def test_destroy
180 r = Role.create!(:name => 'ToBeDestroyed', :permissions => [:view_wiki_pages])
195 r = Role.create!(:name => 'ToBeDestroyed', :permissions => [:view_wiki_pages])
181
196
182 delete :destroy, :params => {:id => r}
197 delete :destroy, :params => {:id => r}
183 assert_redirected_to '/roles'
198 assert_redirected_to '/roles'
184 assert_nil Role.find_by_id(r.id)
199 assert_nil Role.find_by_id(r.id)
185 end
200 end
186
201
187 def test_destroy_role_in_use
202 def test_destroy_role_in_use
188 delete :destroy, :params => {:id => 1}
203 delete :destroy, :params => {:id => 1}
189 assert_redirected_to '/roles'
204 assert_redirected_to '/roles'
190 assert_equal 'This role is in use and cannot be deleted.', flash[:error]
205 assert_equal 'This role is in use and cannot be deleted.', flash[:error]
191 assert_not_nil Role.find_by_id(1)
206 assert_not_nil Role.find_by_id(1)
192 end
207 end
193
208
194 def test_get_permissions
209 def test_get_permissions
195 get :permissions
210 get :permissions
196 assert_response :success
211 assert_response :success
197
212
198 assert_select 'input[name=?][type=checkbox][value=add_issues][checked=checked]', 'permissions[3][]'
213 assert_select 'input[name=?][type=checkbox][value=add_issues][checked=checked]', 'permissions[3][]'
199 assert_select 'input[name=?][type=checkbox][value=delete_issues]:not([checked])', 'permissions[3][]'
214 assert_select 'input[name=?][type=checkbox][value=delete_issues]:not([checked])', 'permissions[3][]'
200 end
215 end
201
216
202 def test_post_permissions
217 def test_post_permissions
203 post :permissions, :params => {
218 post :permissions, :params => {
204 :permissions => {
219 :permissions => {
205 '0' => '',
220 '0' => '',
206 '1' => ['edit_issues'],
221 '1' => ['edit_issues'],
207 '3' => ['add_issues', 'delete_issues']
222 '3' => ['add_issues', 'delete_issues']
208 }
223 }
209 }
224 }
210 assert_redirected_to '/roles'
225 assert_redirected_to '/roles'
211
226
212 assert_equal [:edit_issues], Role.find(1).permissions
227 assert_equal [:edit_issues], Role.find(1).permissions
213 assert_equal [:add_issues, :delete_issues], Role.find(3).permissions
228 assert_equal [:add_issues, :delete_issues], Role.find(3).permissions
214 assert Role.find(2).permissions.empty?
229 assert Role.find(2).permissions.empty?
215 end
230 end
216
231
217 def test_clear_all_permissions
232 def test_clear_all_permissions
218 post :permissions, :params => {:permissions => { '0' => '' }}
233 post :permissions, :params => {:permissions => { '0' => '' }}
219 assert_redirected_to '/roles'
234 assert_redirected_to '/roles'
220 assert Role.find(1).permissions.empty?
235 assert Role.find(1).permissions.empty?
221 end
236 end
222
237
223 def test_move_highest
238 def test_move_highest
224 put :update, :params => {:id => 3, :role => {:position => 1}}
239 put :update, :params => {:id => 3, :role => {:position => 1}}
225 assert_redirected_to '/roles'
240 assert_redirected_to '/roles'
226 assert_equal 1, Role.find(3).position
241 assert_equal 1, Role.find(3).position
227 end
242 end
228
243
229 def test_move_higher
244 def test_move_higher
230 position = Role.find(3).position
245 position = Role.find(3).position
231 put :update, :params => {:id => 3, :role => {:position => position - 1}}
246 put :update, :params => {:id => 3, :role => {:position => position - 1}}
232 assert_redirected_to '/roles'
247 assert_redirected_to '/roles'
233 assert_equal position - 1, Role.find(3).position
248 assert_equal position - 1, Role.find(3).position
234 end
249 end
235
250
236 def test_move_lower
251 def test_move_lower
237 position = Role.find(2).position
252 position = Role.find(2).position
238 put :update, :params => {:id => 2, :role => {:position => position + 1}}
253 put :update, :params => {:id => 2, :role => {:position => position + 1}}
239 assert_redirected_to '/roles'
254 assert_redirected_to '/roles'
240 assert_equal position + 1, Role.find(2).position
255 assert_equal position + 1, Role.find(2).position
241 end
256 end
242
257
243 def test_move_lowest
258 def test_move_lowest
244 put :update, :params => {:id => 2, :role => {:position => Role.givable.count}}
259 put :update, :params => {:id => 2, :role => {:position => Role.givable.count}}
245 assert_redirected_to '/roles'
260 assert_redirected_to '/roles'
246 assert_equal Role.givable.count, Role.find(2).position
261 assert_equal Role.givable.count, Role.find(2).position
247 end
262 end
248 end
263 end
General Comments 0
You need to be logged in to leave comments. Login now