before_action :find_role, :only => [:show, :edit, :update, :destroy]
accept_api_auth :index, :show
+ include RolesHelper
+
require_sudo_mode :create, :update, :destroy
def index
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
# 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
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
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,