]> source.dussan.org Git - redmine.git/commitdiff
Export permissions report to CSV (#13767).
authorGo MAEDA <maeda@farend.jp>
Fri, 12 Mar 2021 05:29:50 +0000 (05:29 +0000)
committerGo MAEDA <maeda@farend.jp>
Fri, 12 Mar 2021 05:29:50 +0000 (05:29 +0000)
Patch by David Escala and Yuichi HARADA.

git-svn-id: http://svn.redmine.org/redmine/trunk@20778 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/roles_controller.rb
app/helpers/roles_helper.rb
app/views/roles/permissions.html.erb
test/functional/roles_controller_test.rb

index 27861d35df768ea48aa53e9a715cb726512d9b76..870ea09ee58b063176f195b354b4c27fe27ff1d5 100644 (file)
@@ -26,6 +26,8 @@ class RolesController < ApplicationController
   before_action :find_role, :only => [:show, :edit, :update, :destroy]
   accept_api_auth :index, :show
 
+  include RolesHelper
+
   require_sudo_mode :create, :update, :destroy
 
   def index
@@ -108,7 +110,13 @@ class RolesController < ApplicationController
       scope = scope.where(:id => params[:ids])
     end
     @roles = scope.to_a
-    @permissions = Redmine::AccessControl.permissions.select {|p| !p.public?}
+    @permissions = Redmine::AccessControl.permissions.reject(&:public?)
+    respond_to do |format|
+      format.html
+      format.csv do
+        send_data(permissions_to_csv(@roles, @permissions), :type => 'text/csv; header=present', :filename => 'permissions.csv')
+      end
+    end
   end
 
   def update_permissions
index 79b627dae18188271fd94013c92b97f3f1321615..6c84138c48bb88b964f4eff918fbbe0e067a7d33 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 module RolesHelper
+  include ApplicationHelper
+
+  def permissions_to_csv(roles, permissions)
+    Redmine::Export::CSV.generate do |csv|
+      # csv header fields
+      headers = [l(:field_cvs_module), l(:label_permissions)] + roles.collect(&:name)
+      csv << headers
+      # csv lines
+      perms_by_module = permissions.group_by {|p| p.project_module.to_s}
+      perms_by_module.keys.sort.each do |mod|
+        perms_by_module[mod].each do |p|
+          names = [
+            l_or_humanize(p.project_module.to_s, :prefix => 'project_module_'),
+            l_or_humanize(p.name, :prefix => 'permission_').to_s,
+          ]
+          fields = names + roles.collect do |role|
+            if role.setable_permissions.include?(p)
+              format_object(role.permissions.include?(p.name), false)
+            else
+              ''
+            end
+          end
+          csv << fields
+        end
+      end
+    end
+  end
 end
index 9cd71b499be592b84326b04b103f45bddcca0b3f..ffdf28b5417a421ae769631ae6172008ec631513 100644 (file)
@@ -85,3 +85,6 @@
 <p><%= check_all_links 'permissions_form' %></p>
 <p><%= submit_tag l(:button_save) %></p>
 <% end %>
+<% other_formats_links do |f| %>
+  <%= f.link_to 'CSV' %>
+<% end %>
index 1f2b2c36b27131c29fe93966a37f3c9da6c44e34..28f7dd1319b27d7bcf6155ce21cec82afd637114 100644 (file)
@@ -22,6 +22,8 @@ require File.expand_path('../../test_helper', __FILE__)
 class RolesControllerTest < Redmine::ControllerTest
   fixtures :roles, :users, :members, :member_roles, :workflows, :trackers
 
+  include Redmine::I18n
+
   def setup
     User.current = nil
     @request.session[:user_id] = 1 # admin
@@ -270,6 +272,36 @@ class RolesControllerTest < Redmine::ControllerTest
     assert_select 'input[name=?][type=checkbox][value=delete_issues]:not([checked])', 'permissions[3][]'
   end
 
+  def test_permissions_csv_export
+    get(
+      :permissions,
+      :params => {
+        :format => 'csv'
+      }
+    )
+    assert_response :success
+
+    assert_equal 'text/csv', @response.media_type
+    lines = @response.body.chomp.split("\n")
+    # Number of lines
+    permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
+    permissions = permissions.group_by{|p| p.project_module.to_s}.sort.collect(&:last).flatten
+    assert_equal permissions.size + 1, lines.size
+    # Header
+    assert_equal 'Module,Permissions,Manager,Developer,Reporter,Non member,Anonymous', lines.first
+    # Details
+    to_test = {
+      :add_project => '"",Create project,Yes,No,No,No,""',
+      :add_issue_notes => 'Issue tracking,Add notes,Yes,Yes,Yes,Yes,Yes',
+      :manage_wiki => 'Wiki,Manage wiki,Yes,No,No,"",""'
+    }
+    to_test.each do |name, expected|
+      index = permissions.find_index {|p| p.name == name}
+      assert_not_nil index
+      assert_equal expected, lines[index + 1]
+    end
+  end
+
   def test_update_permissions
     post(
       :update_permissions,