Project

General

Profile

Feature #44424 » 0001-Add-Export-issues-and-Export-time-entries-permission.patch

Go MAEDA, 2026-09-07 03:33

View differences:

app/controllers/application_controller.rb
private :valid_back_url?
helper_method :valid_back_url?
# Returns true if the current user has the given export permission on
# the projects of all the records in scope (issues or time entries)
def export_allowed?(permission, scope)
user = User.current
return true if user.admin?
if @project
# Deny if the user does not have the permission on @project itself
return false unless user.allowed_to?(permission, @project)
# @project has no subprojects, so no further check is needed
return true if @project.leaf?
else
# Cross-project view: deny if none of the user's roles has the permission
return false unless user.allowed_to?(permission, nil, :global => true)
end
# Deny if the scope contains records of a project where
# the user does not have the permission
!scope.where.not(:project_id => Project.allowed_to(user, permission).select(:id)).exists?
end
private :export_allowed?
helper_method :export_allowed?
# Redirects to the request referer if present, redirects to args or call block otherwise.
def redirect_to_referer_or(*args, &block)
if referer = request.headers["Referer"]
app/controllers/gantts_controller.rb
@query.group_by = nil
@gantt.query = @query if @query.valid?
@export_allowed = export_allowed?(:export_issues, @query.valid? ? @query.base_scope : Issue.none)
if (request.format.pdf? || request.format.png?) && !@export_allowed
deny_access
return
end
basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
respond_to do |format|
app/controllers/issues_controller.rb
retrieve_query(IssueQuery, use_session)
if @query.valid?
if (request.format.csv? || request.format.pdf?) && !export_allowed?(:export_issues, @query.base_scope)
deny_access
return
end
respond_to do |format|
format.html do
@issue_count = @query.issue_count
......
end
def show
if request.format.pdf? && !User.current.allowed_to?(:export_issues, @project)
deny_access
return
end
if !api_request? || include_in_api_response?('journals')
@journals = @issue.visible_journals_with_index
@journals.reverse! if User.current.wants_comments_in_reverse_order?
app/controllers/reports_controller.rb
def issue_report_details
with_subprojects = Setting.display_subprojects_issues?
@export_allowed = export_allowed?(:export_issues, Issue.visible(User.current, :project => @project, :with_subprojects => with_subprojects))
if request.format.csv? && !@export_allowed
deny_access
return
end
case params[:detail]
when "tracker"
@field = "tracker_id"
app/controllers/timelog_controller.rb
def index
retrieve_time_entry_query
if request.format.csv? && !export_allowed?(:export_time_entries, @query.base_scope)
deny_access
return
end
scope = time_entry_scope.
preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
preload(:project, :user)
......
def report
retrieve_time_entry_query
if request.format.csv? && !export_allowed?(:export_time_entries, @query.base_scope)
deny_access
return
end
scope = time_entry_scope
@report = Redmine::Helpers::TimeReport.new(@project, params[:criteria], params[:columns], scope)
app/views/gantts/_chart.html.erb
</ul>
</span>
<% if @export_allowed %>
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'PDF', gantt.params %>
<%= f.link_to_with_query_parameters('PNG', gantt.params) if gantt.respond_to?('to_image') %>
<% end %>
<% end %>
app/views/issues/index.html.erb
<span class="pagination"><%= pagination_links_full @issue_pages, @issue_count %></span>
<% end %>
<% export_allowed = export_allowed?(:export_issues, @query.base_scope) %>
<% other_formats_links do |f| %>
<% if export_allowed %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '350px'); return false;" %>
<%= f.link_to_with_query_parameters 'PDF' %>
<% end %>
<%= f.link_to_with_query_parameters 'Atom', :key => User.current.atom_key %>
<% end %>
<% if export_allowed %>
<div id="csv-export-options" style="display:none;">
<h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
<%= form_tag(_project_issues_path(@project, :format => 'csv'), :method => :get, :id => 'csv-export-form') do %>
......
</p>
<% end %>
</div>
<% end %>
<% end %>
<%= call_hook(:view_issues_index_bottom, { :issues => @issues, :project => @project, :query => @query }) %>
app/views/issues/show.html.erb
<%= render partial: 'action_menu_edit' unless User.current.wants_comments_in_reverse_order? %>
<% other_formats_links do |f| %>
<%= f.link_to 'PDF' %>
<%= f.link_to 'PDF' if User.current.allowed_to?(:export_issues, @project) %>
<%= f.link_to 'Atom', :url => {:key => User.current.atom_key} %>
<% end %>
app/views/reports/_details.html.erb
</tr>
</tfoot>
</table>
<% if @export_allowed %>
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<% end %>
......
</p>
<% end %>
</div>
<% end %>
<div class="issue-report-graph hide-when-print" data-controller="reports--details" data-reports--details-title-value="<%= l(:label_issues_by, @report_title) %>">
<template data-reports--details-target="labels">
app/views/timelog/index.html.erb
<%= render :partial => 'list', :locals => { :entries => @entries }%>
<span class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></span>
<% export_allowed = export_allowed?(:export_time_entries, @query.base_scope) %>
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<%= f.link_to_with_query_parameters('CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;") if export_allowed %>
<%= f.link_to_with_query_parameters 'Atom', :key => User.current.atom_key %>
<% end %>
<% if export_allowed %>
<div id="csv-export-options" style="display:none;">
<h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
<%= form_tag(_time_entries_path(@project, nil, :format => 'csv'), :method => :get, :id => 'csv-export-form') do %>
......
</div>
<% end %>
<% end %>
<% end %>
<% content_for :sidebar do %>
<%= render :partial => 'timelog/sidebar' %>
app/views/timelog/report.html.erb
</table>
</div>
<% if export_allowed?(:export_time_entries, @query.base_scope) %>
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<% end %>
<% end %>
<div id="csv-export-options" style="display: none;">
<h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
<%= export_csv_encoding_select_tag %>
......
</div>
<% end %>
<% end %>
<% end %>
<% end %>
<% content_for :sidebar do %>
<%= render :partial => 'sidebar' %>
config/locales/en.yml
permission_export_wiki_pages: Export wiki pages
permission_manage_subtasks: Manage subtasks
permission_manage_related_issues: Manage related issues
permission_export_issues: Export issues
permission_import_issues: Import issues
permission_log_time_for_other_users: Log spent time for other users
permission_view_project: View projects
......
label_member_management_all_roles: All roles
label_member_management_selected_roles_only: Only these roles
label_import_issues: Import issues
permission_export_time_entries: Export time entries
permission_import_time_entries: Import time entries
label_select_file_to_import: Select the file to import
label_fields_separator: Field separator
db/migrate/20260906000000_add_export_issues_and_export_time_entries_permissions.rb
class AddExportIssuesAndExportTimeEntriesPermissions < ActiveRecord::Migration[8.1]
def up
Role.find_each do |role|
role.add_permission!(:export_issues) if role.has_permission?(:view_issues)
role.add_permission!(:export_time_entries) if role.has_permission?(:view_time_entries)
end
end
def down
Role.find_each do |role|
role.remove_permission!(:export_issues, :export_time_entries)
end
end
end
lib/redmine/default_data/loader.rb
:manage_versions,
:manage_categories,
:view_issues,
:export_issues,
:add_issues,
:edit_issues,
:view_private_notes,
......
:view_calendar,
:log_time,
:view_time_entries,
:export_time_entries,
:view_news,
:comment_news,
:view_documents,
......
:position => 3,
:permissions => [
:view_issues,
:export_issues,
:add_issues,
:add_issue_notes,
:save_queries,
......
:view_calendar,
:log_time,
:view_time_entries,
:export_time_entries,
:view_news,
:comment_news,
:view_documents,
......
]
)
Role.non_member.update_attribute :permissions, [:view_issues,
:export_issues,
:add_issues,
:add_issue_notes,
:save_queries,
:view_gantt,
:view_calendar,
:view_time_entries,
:export_time_entries,
:view_news,
:comment_news,
:view_documents,
lib/redmine/preparation.rb
map.permission :view_issue_watchers, {}, :read => true
map.permission :add_issue_watchers, {:watchers => [:new, :create, :append, :autocomplete_for_user, :autocomplete_for_mention]}
map.permission :delete_issue_watchers, {:watchers => :destroy}
map.permission :export_issues, {}, :read => true
map.permission :import_issues, {}
# Issue categories
map.permission :manage_categories, {:projects => :settings, :issue_categories => [:index, :show, :new, :create, :edit, :update, :destroy]}, :require => :member
......
{:projects => :settings, :project_enumerations => [:update, :destroy]},
:require => :member
map.permission :log_time_for_other_users, :require => :member
map.permission :export_time_entries, {}, :read => true
map.permission :import_time_entries, {}
end
test/fixtures/roles.yml
- :manage_versions
- :manage_categories
- :view_issues
- :export_issues
- :add_issues
- :edit_issues
- :copy_issues
......
- :view_calendar
- :log_time
- :view_time_entries
- :export_time_entries
- :edit_time_entries
- :delete_time_entries
- :import_time_entries
......
- :manage_versions
- :manage_categories
- :view_issues
- :export_issues
- :add_issues
- :edit_issues
- :copy_issues
......
- :view_calendar
- :log_time
- :view_time_entries
- :export_time_entries
- :edit_own_time_entries
- :view_news
- :manage_news
......
- :manage_versions
- :manage_categories
- :view_issues
- :export_issues
- :add_issues
- :edit_issues
- :manage_issue_relations
......
- :view_calendar
- :log_time
- :view_time_entries
- :export_time_entries
- :view_news
- :manage_news
- :comment_news
......
permissions: |
---
- :view_issues
- :export_issues
- :add_issues
- :edit_issues
- :manage_issue_relations
......
- :view_calendar
- :log_time
- :view_time_entries
- :export_time_entries
- :view_news
- :comment_news
- :view_documents
......
permissions: |
---
- :view_issues
- :export_issues
- :add_issue_notes
- :view_gantt
- :view_calendar
- :view_time_entries
- :export_time_entries
- :view_news
- :view_documents
- :view_wiki_pages
test/functional/gantts_controller_test.rb
assert @response.body.starts_with?('%PDF')
end
def test_gantt_pdf_and_png_without_export_issues_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :show, :params => {:project_id => 1, :format => 'pdf'}
assert_response :forbidden
get :show, :params => {:project_id => 1, :format => 'png'}
assert_response :forbidden
end
def test_gantt_should_not_show_export_links_without_export_issues_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :show, :params => {:project_id => 1}
assert_response :success
assert_select 'p.other-formats', 0
end
if Object.const_defined?(:MiniMagick) && convert_installed?
def test_gantt_should_export_to_png
get(
test/functional/issues_controller_test.rb
assert_equal Setting.issue_list_default_columns.size + 2, lines[0].split(',').size
end
def test_index_csv_and_pdf_without_export_issues_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :index, :params => {:project_id => 1, :format => 'csv'}
assert_response :forbidden
get :index, :params => {:project_id => 1, :format => 'pdf'}
assert_response :forbidden
end
def test_index_csv_across_projects_without_export_issues_permission_should_be_denied
@request.session[:user_id] = 2
Role.all.each {|role| role.remove_permission! :export_issues}
get :index, :params => {:format => 'csv'}
assert_response :forbidden
end
def test_index_csv_should_require_export_issues_permission_on_every_project_of_the_issues
# User 2 is Developer on project 2 and has visible issues there
@request.session[:user_id] = 2
Role.find_by_name('Developer').remove_permission! :export_issues
get :index, :params => {:format => 'csv'}
assert_response :forbidden
get :index, :params => {:project_id => 1, :format => 'csv'}
assert_response :success
end
def test_index_should_not_show_export_links_without_export_issues_permission_on_every_project_of_the_issues
@request.session[:user_id] = 2
Role.find_by_name('Developer').remove_permission! :export_issues
get :index
assert_response :success
assert_select 'p.other-formats a.csv', 0
assert_select 'p.other-formats a.pdf', 0
get :index, :params => {:project_id => 1}
assert_response :success
assert_select 'p.other-formats a.csv'
assert_select 'p.other-formats a.pdf'
end
def test_index_should_not_show_export_links_without_export_issues_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :index, :params => {:project_id => 1}
assert_response :success
assert_select 'p.other-formats a.csv', 0
assert_select 'p.other-formats a.pdf', 0
assert_select 'p.other-formats a.atom'
assert_select '#csv-export-options', 0
end
def test_index_csv_filename_without_query_name_param
get :index, :params => {:format => 'csv'}
assert_response :success
......
assert @response.body.starts_with?('%PDF')
end
def test_show_pdf_without_export_issues_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :show, :params => {:id => 1, :format => 'pdf'}
assert_response :forbidden
end
def test_show_should_not_show_pdf_link_without_export_issues_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :show, :params => {:id => 1}
assert_response :success
assert_select 'p.other-formats a.pdf', 0
assert_select 'p.other-formats a.atom'
end
def test_export_to_pdf_with_utf8_u_fffd
issue = Issue.generate!(:subject => "�")
["en", "zh", "zh-TW", "ja", "ko", "ar"].each do |lang|
test/functional/reports_controller_test.rb
end
end
def test_issue_report_details_csv_without_export_issues_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :issue_report_details, :params => {:id => 1, :detail => 'tracker', :format => 'csv'}
assert_response :forbidden
end
def test_issue_report_details_csv_should_require_export_issues_permission_on_subprojects
# User 2 is a non member of the public subproject 3 which has visible issues
@request.session[:user_id] = 2
Role.non_member.remove_permission! :export_issues
with_settings :display_subprojects_issues => '1' do
get :issue_report_details, :params => {:id => 1, :detail => 'tracker', :format => 'csv'}
assert_response :forbidden
end
with_settings :display_subprojects_issues => '0' do
get :issue_report_details, :params => {:id => 1, :detail => 'tracker', :format => 'csv'}
assert_response :success
end
end
def test_issue_report_details_should_not_show_csv_link_without_export_issues_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_issues
get :issue_report_details, :params => {:id => 1, :detail => 'tracker'}
assert_response :success
assert_select 'p.other-formats', 0
assert_select '#csv-export-options', 0
end
def test_issue_report_details_with_tracker_detail_should_csv_export
project = Project.find(1)
tracker = project.trackers.find_by(:name => 'Support request')
test/functional/timelog_controller_test.rb
end
end
def test_index_should_not_include_csv_export_without_export_time_entries_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_time_entries
get :index, :params => {:project_id => 1}
assert_response :success
assert_select 'p.other-formats a.csv', 0
assert_select 'p.other-formats a.atom'
assert_select '#csv-export-options', 0
end
def test_index_csv_should_require_export_time_entries_permission_on_every_project_of_the_entries
# User 2 is a non member of the public subproject 3 which has visible time entries
@request.session[:user_id] = 2
Role.non_member.remove_permission! :export_time_entries
get :index, :params => {:format => 'csv'}
assert_response :forbidden
with_settings :display_subprojects_issues => '0' do
get :index, :params => {:project_id => 1, :format => 'csv'}
assert_response :success
end
end
def test_index_csv_without_export_time_entries_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_time_entries
get :index, :params => {:project_id => 1, :format => 'csv'}
assert_response :forbidden
end
def test_report_should_not_include_csv_export_without_export_time_entries_permission
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_time_entries
get :report, :params => {:project_id => 1, :columns => 'month', :criteria => ['project']}
assert_response :success
assert_select 'p.other-formats', 0
assert_select '#csv-export-options', 0
end
def test_report_csv_without_export_time_entries_permission_should_be_denied
@request.session[:user_id] = 2
Role.find_by_name('Manager').remove_permission! :export_time_entries
get :report, :params => {:project_id => 1, :columns => 'month', :criteria => ['project'], :format => 'csv'}
assert_response :forbidden
end
def test_index_csv_all_projects
with_settings :date_format => '%m/%d/%Y' do
get :index, :params => {:format => 'csv'}
    (1-1/1)