1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2009 SonarSource SA
# mailto:contact AT sonarsource DOT com
#
# Sonar is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Sonar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
require 'cgi'
class RulesConfigurationController < ApplicationController
SECTION=Navigation::SECTION_CONFIGURATION
STATUS_ACTIVE = "ACTIVE"
STATUS_INACTIVE = "INACTIVE"
ANY_SELECTION = [["Any", '']]
RULE_PRIORITIES = Sonar::RulePriority.as_options.reverse
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => ['activate_rule', 'update_param', 'bulk_edit', 'create', 'update', 'delete', 'change_parent'], :redirect_to => { :action => 'index' }
before_filter :admin_required, :except => [ 'index', 'export' ]
def index
unless params[:id].blank?
if params[:id].to_i<=0
redirect_to :controller => 'profiles'
return
end
begin
@profile = RulesProfile.find(params[:id].to_i)
rescue
redirect_to :controller => 'profiles'
return
end
else
@profile = RulesProfile.default_profile
end
init_params()
@select_plugins = ANY_SELECTION + java_facade.getRuleRepositoriesByLanguage(@profile.language).collect { |repo| [repo.getName(true), repo.getKey()]}.sort
@select_priority = ANY_SELECTION + RULE_PRIORITIES
@select_status = [['Any',''], ["Active", STATUS_ACTIVE], ["Inactive", STATUS_INACTIVE]]
@child_profiles = RulesProfile.find(:all, :conditions => {:parent_id => @profile.id})
@select_parent = [['', nil]] + RulesProfile.find(:all, :conditions => {:language => @profile.language, :parent_id => nil}).collect { |profile| [profile.name, profile.id] }.sort
@rules = Rule.search(java_facade, {
:profile => @profile, :status => @status, :priorities => @priorities,
:plugins => @plugins, :searchtext => @searchtext, :include_parameters => true, :language => @profile.language})
unless @searchtext.blank?
if @status==STATUS_ACTIVE
@hidden_inactives=Rule.search(java_facade, {
:profile => @profile, :status => STATUS_INACTIVE, :priorities => @priorities,
:plugins => @plugins, :language => @profile.language, :searchtext => @searchtext, :include_parameters => false}).size
elsif @status==STATUS_INACTIVE
@hidden_actives=Rule.search(java_facade, {
:profile => @profile, :status => STATUS_ACTIVE, :priorities => @priorities,
:plugins => @plugins, :language => @profile.language, :searchtext => @searchtext, :include_parameters => false}).size
end
end
end
#
#
# POST /rules_configuration/change_parent?id=<profile id>&parent_id=<parent profile id>
#
#
def change_parent
id = params[:id].to_i
parent_id = params[:parent_id]
if parent_id.blank?
java_facade.changeParentProfile(id, nil)
else
java_facade.changeParentProfile(id, parent_id.to_i)
end
redirect_to :action => 'index', :id => params[:id]
end
#
#
# POST /rules_configuration/activate_rule?id=<profile id>&rule_id=<rule id>&level=<priority>
#
# If the parameter "level" is blank or null, then the rule is removed from the profile.
#
#
def activate_rule
profile = RulesProfile.find(params[:id].to_i)
if profile && !profile.provided?
rule=Rule.find(:first, :conditions => {:id => params[:rule_id].to_i, :enabled => true})
priority=params[:level]
active_rule=profile.active_by_rule_id(rule.id)
if priority.blank?
# deactivate the rule
active_rule.destroy if active_rule
active_rule=nil
java_facade.ruleDeactivated(profile.id, rule.id)
else
# activate the rule
if active_rule.nil?
active_rule = ActiveRule.new(:profile_id => profile.id, :rule => rule)
rule.parameters.select{|p| p.default_value.present?}.each do |p|
active_rule.active_rule_parameters.build(:rules_parameter => p, :value => p.default_value)
end
end
active_rule.failure_level=Sonar::RulePriority.id(priority)
active_rule.save!
java_facade.ruleActivatedOrChanged(profile.id, active_rule.id)
end
is_admin=true # security has already been checked by controller filters
render :update do |page|
page.replace_html("rule_#{rule.id}", :partial => 'rule', :object => rule, :locals => {:profile => profile, :active_rule => active_rule, :is_admin => is_admin})
page.assign('localModifications', true)
end
end
end
#
#
# GET /rules_configuration/new/<profile id>?rule_id=<rule id>
#
#
def new
# form to duplicate a rule
@profile = RulesProfile.find(params[:id].to_i)
@rule = Rule.find(params[:rule_id])
end
#
#
# POST /rules_configuration/create/<profile id>?rule_id=<rule id>&rule[name]=<new name>&...
#
#
def create
template=Rule.find(params[:rule_id])
rule=Rule.create(params[:rule].merge(
{
:priority => Sonar::RulePriority.id(params[:rule][:priority]),
:parent_id => template.id,
:plugin_name => template.plugin_name,
:cardinality => 'SINGLE',
:plugin_rule_key => "#{template.plugin_rule_key}_#{Time.now.to_i}",
:plugin_config_key => template.plugin_config_key,
:enabled => true}))
template.parameters.each do |template_parameter|
rule.rules_parameters.build(:name => template_parameter.name, :param_type => template_parameter.param_type, :description => template_parameter.description,
:default_value => params[:rule_param][template_parameter.name])
end
if rule.save
redirect_to :action => 'index', :id => params[:id], :searchtext => rule.name, :rule_status => 'INACTIVE', "plugins[]" => rule.plugin_name
else
flash[:error]="Rule is not valid: <br/>#{rule.errors.full_messages.join('<br/>')}"
redirect_to :action => 'new', :id => params[:id], :rule_id => params[:rule_id]
end
end
# deprecated since 2.3
def export
redirect_to request.query_parameters.merge({:controller => 'profiles', :action => 'export'})
end
#
#
# GET /rules_configuration/new/<profile id>?rule_id=<rule id>
#
#
def edit
# form to edit a rule
@profile = RulesProfile.find(params[:id])
@rule = Rule.find(params[:rule_id])
if !@rule.editable?
redirect_to :action => 'index', :id => params[:id]
end
end
#
#
# POST /rules_configuration/update/<profile id>?rule_id=<rule id>&rule[name]=<new name>&...
#
#
def update
rule=Rule.find(params[:rule_id])
if rule.editable?
rule.name=params[:rule][:name]
rule.description=params[:rule][:description]
rule.priority=Sonar::RulePriority.id(params[:rule][:priority])
rule.parameters.each do |parameter|
parameter.default_value=params[:rule_param][parameter.name]
parameter.save
end
if rule.save
redirect_to :action => 'index', :id => params[:id], :searchtext => rule.name, :rule_status => '', "plugins[]" => rule.plugin_name
else
flash[:error]="Rule is not valid: <br/>#{rule.errors.full_messages.join('<br/>')}"
redirect_to :action => 'new', :id => params[:id], :rule_id => params[:rule_id]
end
else
flash[:error]='Unknown rule'
redirect_to :action => 'index', :id => params[:id]
end
end
#
#
# POST /rules_configuration/delete/<profile id>?rule_id=<rule id>
#
#
def delete
rule=Rule.find(params[:rule_id])
if rule.editable?
rule.enabled=false
rule.save
# it's mandatory to execute 'destroy_all' but not 'delete_all' because active_rule_parameters must
# also be destroyed in cascade.
ActiveRule.destroy_all("rule_id=#{rule.id}")
flash[:notice]='Rule deleted'
else
flash[:error]='Unknown rule'
end
redirect_to :action => 'index', :id => params[:id]
end
#
#
# POST /rules_configuration/bulk_edit?id=<profile id>&bulk_rule_ids=<list of rule ids>&bulk_action=<action>
#
# Values of the parameter 'bulk_action' :
# - 'activate' : activate all the selected rules with their default priority
# - 'deactivate' : deactivate all the selected rules
#
#
def bulk_edit
profile = Profile.find(params[:id].to_i)
rule_ids = params[:bulk_rule_ids].split(',').map{|id| id.to_i}
status=params[:rule_status]
case params[:bulk_action]
when 'activate'
count=activate_rules(profile, rule_ids)
flash[:notice]="#{count} rules have been activated."
status=STATUS_ACTIVE if status==STATUS_INACTIVE
when 'deactivate'
count=deactivate_rules(profile, rule_ids)
flash[:notice]="#{count} rules have been deactivated."
status=STATUS_INACTIVE if status==STATUS_ACTIVE
end
url_parameters=request.query_parameters.merge({:action => 'index', :bulk_action => nil, :bulk_rule_ids => nil, :id => profile.id, :rule_status => status})
redirect_to url_parameters
end
def update_param
is_admin=true # security has already been checked by controller filters
profile = RulesProfile.find(params[:profile_id].to_i)
rule_param = RulesParameter.find(params[:param_id].to_i)
active_rule = ActiveRule.find(params[:active_rule_id].to_i)
active_param = ActiveRuleParameter.find(params[:id].to_i) if params[:id].to_i > 0
value = params[:value]
if !profile.provided?
if value != ""
active_param = ActiveRuleParameter.new(:rules_parameter => rule_param, :active_rule => active_rule ) if active_param.nil?
active_param.value = value
active_param.save
active_param.valid?
active_param.reload
elsif !active_param.nil?
active_param.destroy
active_param = nil
end
java_facade.ruleActivatedOrChanged(profile.id, active_rule.id)
end
render :partial => 'rule_param', :object => nil,
:locals => {:parameter => rule_param, :active_parameter => active_param, :profile => profile, :active_rule => active_rule, :is_admin => is_admin }
end
private
# return the number of newly activated rules
def activate_rules(profile, rule_ids)
count=0
rule_ids_to_activate=(rule_ids - profile.active_rules.map{|ar| ar.rule_id})
unless rule_ids_to_activate.empty?
rules_to_activate=Rule.find(:all, :conditions => {:enabled=>true, :id => rule_ids_to_activate})
count = rules_to_activate.size
rules_to_activate.each do |rule|
active_rule = profile.active_rules.create(:rule => rule, :failure_level => rule.priority)
java_facade.ruleActivatedOrChanged(profile.id, active_rule.id)
end
end
count
end
def deactivate_rules(profile, rule_ids)
count=0
profile.active_rules.each do |ar|
if rule_ids.include?(ar.rule_id) && !ar.inherited
ar.destroy
java_facade.ruleDeactivated(profile.id, ar.rule_id)
count+=1
end
end
count
end
def init_params
@id = params[:id]
@priorities = filter_any(params[:priorities]) || ['']
@plugins=filter_any(params[:plugins]) || ['']
@status=params[:rule_status] || STATUS_ACTIVE
@searchtext=params[:searchtext]
end
def filter_any(array)
if array && array.size>1 && array.include?('')
array=[''] #keep only 'any'
end
array
end
end
|