Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rules_configuration_controller.rb 12KB

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