From 1ac7e40d2210a59f3650d3c1d5a394afdbe09e45 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 26 Nov 2012 17:27:58 +0100 Subject: [PATCH] SONAR-3825 manage system filters --- .../resources/org/sonar/l10n/core.properties | 9 ++- .../org/sonar/core/persistence/schema-h2.ddl | 1 + .../app/controllers/measures_controller.rb | 4 +- .../system_measure_filters_controller.rb | 61 ++++++++++++++++ .../WEB-INF/app/models/measure_filter.rb | 2 + .../app/views/layouts/_layout.html.erb | 2 + .../app/views/measures/manage.html.erb | 8 +-- .../app/views/measures/search.html.erb | 11 ++- .../system_measure_filters/index.html.erb | 71 +++++++++++++++++++ .../db/migrate/356_create_measure_filters.rb | 1 + .../357_move_existing_measure_filters.rb | 3 +- 11 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/controllers/system_measure_filters_controller.rb create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/system_measure_filters/index.html.erb diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties index a0a87406463..a21573326fc 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties @@ -353,6 +353,7 @@ resource_deletion.page={0} Deletion update_key.page=Update Key project_quality_profiles.page=Quality Profiles bulk_deletion.page=Bulk Deletion +system_measure_filters.page=System Measure Filters # GWT pages @@ -391,7 +392,13 @@ measure_filter.col.short_name=Name measure_filter.col.version=Version measure_filter.missing_name=Name is missing measure_filter.name_too_long=Name is too long - +measure_filter.sharing=Sharing +measure_filter.delete_confirm_title=Delete Filter +measure_filter.are_you_sure_want_delete_filter_x=Are you sure that you want to delete the filter "{0}"? +measure_filter.remove_from_system=Remove from system +measure_filter.add_to_system=Add to system +measure_filter.title_shared_filters=Shared Filters +measure_filter.title_system_filters=System Filters #------------------------------------------------------------------------------ # diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 576451a88a2..ea4589cf455 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -517,6 +517,7 @@ CREATE TABLE "SEMAPHORES" ( CREATE TABLE "MEASURE_FILTERS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "NAME" VARCHAR(100) NOT NULL, + "SYSTEM" BOOLEAN, "SHARED" BOOLEAN NOT NULL DEFAULT FALSE, "USER_ID" INTEGER, "DESCRIPTION" VARCHAR(4000), diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb index 1b47de87cd9..6e4d283ff16 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb @@ -87,8 +87,8 @@ class MeasuresController < ApplicationController @shared_filters = MeasureFilter.find(:all, :include => :user, :conditions => ['shared=? and user_id<>?', true, current_user.id]) - @fav_filter_ids = current_user.measure_filter_favourites.map { |fav| fav.measure_filter_id } Api::Utils.insensitive_sort!(@shared_filters) { |elt| elt.name } + @fav_filter_ids = current_user.measure_filter_favourites.map { |fav| fav.measure_filter_id } end # GET /measures/edit_form/ @@ -183,7 +183,7 @@ class MeasuresController < ApplicationController end def owner?(filter) - current_user && filter.user_id==current_user.id + current_user && (filter.user_id==current_user.id || (filter.user_id==nil && has_role?(:admin))) end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_measure_filters_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_measure_filters_controller.rb new file mode 100644 index 00000000000..43cebd75465 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_measure_filters_controller.rb @@ -0,0 +1,61 @@ +# +# Sonar, open source software quality management tool. +# Copyright (C) 2008-2012 SonarSource +# 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 +# +class SystemMeasureFiltersController < ApplicationController + SECTION = Navigation::SECTION_CONFIGURATION + + # GET /system_measure_filters/index + def index + access_denied unless has_role?(:admin) + + @system_filters = MeasureFilter.find(:all, :include => :user, :conditions => ['system=?', true]) + Api::Utils.insensitive_sort!(@system_filters) { |f| f.name } + + @shared_filters = MeasureFilter.find(:all, :include => :user, :conditions => ['shared=? and system=?', true, false]) + end + + # POST /system_measure_filters/add/ + def add + verify_post_request + access_denied unless has_role?(:admin) + require_parameters :id + + filter = MeasureFilter.find(params[:id]) + filter.system=true + unless filter.save + flash[:error]=filter.errors.full_messages.join("
") + end + redirect_to :action => :index + end + + # POST /system_measure_filters/remove/ + def remove + verify_post_request + access_denied unless has_role?(:admin) + require_parameters :id + + filter = MeasureFilter.find(params[:id]) + filter.system=false + unless filter.save + flash[:error]=filter.errors.full_messages.join("
") + end + redirect_to :action => :index + end + +end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb index 984ecc84b78..cc5e50dc5b2 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb @@ -362,5 +362,7 @@ class MeasureFilter < ActiveRecord::Base count = MeasureFilter.count('id', :conditions => ['name=? and shared=? and user_id!=?', name, true, user_id]) errors.add_to_base('Other users already shared filters with the same name') if count>0 end + + errors.add_to_base('Only shared filters can be flagged as system filter') if system && !shared end end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb index cc5851e7536..0ab7c92f1a9 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb @@ -147,6 +147,8 @@ <%= message('manual_rules.page') -%>
  • <%= message('default_dashboards.page') -%>
  • +
  • + <%= message('system_measure_filters.page') -%>
  • <% controller.java_facade.getPages(Navigation::SECTION_CONFIGURATION, nil, nil, nil, nil).each do |page| page_url = (page.isController() ? page.getId() : "/plugins/configuration/#{page.getId()}") %> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/manage.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/manage.html.erb index 48e5a3fc889..a5b6f5ddd2e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/manage.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/manage.html.erb @@ -29,7 +29,7 @@ <%= message('name') -%> - <%= message('sharing') -%> + <%= message('measure_filter.sharing') -%> <%= message('operations') -%> @@ -66,8 +66,8 @@ :class => 'link-action link-red', :id => "delete_#{filter.name.parameterize}", :confirm_button => message('delete'), - :confirm_title => 'measure_filters.delete_confirm_title', - :confirm_msg => 'measure_filters.are_you_sure_want_delete_filter_x', + :confirm_title => 'measure_filter.delete_confirm_title', + :confirm_msg => 'measure_filter.are_you_sure_want_delete_filter_x', :confirm_msg_params => [filter.name] -%> @@ -106,7 +106,7 @@ <% end %> - <%= h filter.user.name -%> + <%= filter.user ? h(filter.user.name) : '-' -%> <%= message('copy') -%> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb index e27ee1e0307..5e9fe5f1be1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb @@ -106,9 +106,18 @@ +
    +
      + <% MeasureFilter.find(:all, :select => ['id,name'], :conditions => ['system=?', true]).each do |system_filter| %> +
    • + <%= h system_filter.name -%> +
    • + <% end %> +
    + <% if logged_in? %>
    -
      +
        <% current_user.favourited_measure_filters.each do |my_filter| %>
      • <%= h my_filter.name -%> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/system_measure_filters/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/system_measure_filters/index.html.erb new file mode 100644 index 00000000000..bb87a5bc4a4 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/system_measure_filters/index.html.erb @@ -0,0 +1,71 @@ +

        <%= h message('measure_filter.title_system_filters') %>

        + + + + + + + + + + <% if @system_filters.empty? %> + + + + <% else %> + <% @system_filters.each do |filter| %> + + + + + + <% end %> + <% end %> + +
        <%= message('name') -%><%= message('shared_by') -%><%= message('operations') -%>
        <%= message('filters.no_filters') -%>
        + <%= link_to h(filter.name), :action => 'filter', :id => filter.id -%> + <% if filter.description %> +
        <%= h filter.description -%>
        + <% end %> +
        + <%= filter.user ? h(filter.user.name) : '-' -%> + + <%= link_to message('measure_filter.remove_from_system'), {:action => :remove, :id => filter.id}, {:class => 'button', :method => :post} -%> +
        + +
        + +

        <%= h message('measure_filter.title_shared_filters') %>

        + + + + + + + + + + <% if @shared_filters.empty? %> + + + + <% else %> + <% @shared_filters.each do |filter| %> + + + + + + <% end %> + <% end %> + +
        <%= message('name') -%><%= message('shared_by') -%><%= message('operations') -%>
        <%= message('filters.no_filters') -%>
        + <%= link_to h(filter.name), :action => 'filter', :id => filter.id -%> + <% if filter.description %> +
        <%= h filter.description -%>
        + <% end %> +
        + <%= filter.user ? h(filter.user.name) : '-' -%> + + <%= link_to message('measure_filter.add_to_system'), {:action => :add, :id => filter.id}, {:class => 'button', :method => :post} -%> +
        diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/356_create_measure_filters.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/356_create_measure_filters.rb index 30f5210435a..b7066509f5f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/356_create_measure_filters.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/356_create_measure_filters.rb @@ -27,6 +27,7 @@ class CreateMeasureFilters < ActiveRecord::Migration t.column 'name', :string, :null => false, :limit => 100 t.column 'user_id', :integer, :null => true t.column 'shared', :boolean, :default => false, :null => false + t.column 'system', :boolean t.column 'description', :string, :null => true, :limit => 4000 t.column 'data', :text, :null => true t.timestamps diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb index f0b4b836f90..a1a5b1b8f53 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/357_move_existing_measure_filters.rb @@ -57,7 +57,8 @@ class MoveExistingMeasureFilters < ActiveRecord::Migration new_filter = MeasureFilter.new new_filter.name = old_filter.name new_filter.user_id = old_filter.user_id - new_filter.shared = old_filter.shared + new_filter.system = old_filter.user_id.nil? + new_filter.shared = (old_filter.shared || old_filter.user_id.nil?) data = [] data << 'onFavourites=true' if old_filter.favourites if old_filter.resource_id -- 2.39.5