Project

General

Profile

Feature #44646 » 0001-Allow-administrators-to-upload-a-custom-favicon.patch

Go MAEDA, 2026-10-20 10:57

View differences:

app/controllers/favicons_controller.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class FaviconsController < ApplicationController
# The favicon is also shown on the login page
skip_before_action :check_if_login_required, :check_password_change, :check_twofa_activation
def show
favicon = Setting.favicon
if favicon['digest'] != params[:digest]
render_404
return
end
# The URL changes whenever the favicon is replaced
expires_in 1.year, :public => true, :immutable => true
send_data Base64.decode64(favicon['data']), :type => favicon['content_type'], :disposition => 'inline'
end
end
app/controllers/settings_controller.rb
before_action :require_admin
require_sudo_mode :index, :edit, :plugin
require_sudo_mode :index, :edit, :delete_favicon, :plugin
def index
edit
......
Redmine::Themes.rescan
end
def delete_favicon
Setting.favicon = {}
flash[:notice] = l(:notice_successful_delete)
redirect_to settings_path(:tab => 'display')
end
def plugin
@plugin = Redmine::Plugin.find(params[:id])
unless @plugin.configurable?
app/helpers/application_helper.rb
end
def favicon
favicon_link_tag(favicon_path, rel: "shortcut icon")
options = {rel: "shortcut icon"}
options[:type] = Setting.favicon['content_type'] if Setting.favicon.present?
favicon_link_tag(favicon_path, options)
end
# Returns the path to the favicon
def favicon_path
return custom_favicon_path(Setting.favicon['digest']) if Setting.favicon.present?
icon = (current_theme && current_theme.favicon?) ? current_theme.favicon_path : 'favicon.ico'
image_path(icon)
end
app/models/setting.rb
Big5-HKSCS
TIS-620)
FAVICON_MAX_SIZE = 32.kilobytes
FAVICON_CONTENT_TYPES = %w(image/png image/vnd.microsoft.icon)
cattr_accessor :available_settings
self.available_settings ||= {}
......
end
end
end
if settings.key?(:favicon)
file = settings[:favicon]
unless file.respond_to?(:original_filename) &&
file.size <= FAVICON_MAX_SIZE &&
FAVICON_CONTENT_TYPES.include?(Marcel::MimeType.for(file))
messages << [:favicon, l('activerecord.errors.messages.invalid')]
end
end
messages
end
......
params
end
def self.favicon_from_params(file)
data = file.read
{
'content_type' => Marcel::MimeType.for(StringIO.new(data)),
'data' => Base64.strict_encode64(data),
'digest' => Digest::SHA256.hexdigest(data)[0, 8]
}
end
def self.default_issue_due_date_offset_from_params(params)
params.to_s.strip
end
app/views/settings/_display.html.erb
<%= form_tag({:action => 'edit', :tab => 'display'}) do %>
<%= form_tag({:action => 'edit', :tab => 'display'}, :multipart => true) do %>
<div class="box tabular settings">
<p><%= setting_select :ui_theme, Redmine::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
<p>
<%= setting_label :favicon %>
<% if Setting.favicon.present? %>
<%= image_tag custom_favicon_path(Setting.favicon['digest']), :size => 32, :alt => '' %>
<%= delete_link settings_favicon_path %>
<br>
<% end %>
<%= file_field_tag 'settings[favicon]', :accept => '.ico,.png' %>
<em class="info"><%= l(:text_setting_favicon_format, :max_size => number_to_human_size(Setting::FAVICON_MAX_SIZE)) %></em>
</p>
<p><%= setting_select :default_language, lang_options_for_select(false) %></p>
<p><%= setting_check_box :force_default_language_for_anonymous %></p>
config/locales/en.yml
setting_app_title: Application title
setting_welcome_text: Welcome text
setting_default_language: Default language
setting_favicon: Custom favicon
setting_login_required: Authentication required
setting_self_registration: Self-registration
setting_show_custom_fields_on_registration: Show custom fields on registration
......
text_setting_config_change: You can configure the behaviour in config/configuration.yml. Please restart the application after editing it.
text_setting_gravatar_default_initials_html: Users' initials are sent to <a href="%{url}">%{url}</a> to generate their avatars.
text_setting_avatar_enabled_imagemagick_unavailable: ImageMagick is not available, so users cannot set their avatar.
text_setting_favicon_format: "PNG or ICO, up to %{max_size}"
text_setting_gravatar_enabled_fallback: If enabled, Gravatar is used for users who have not set their avatar in Redmine.
text_oauth_admin_permission: Full administrative access. When authorized by an Administrator, this application will be able to read and write all data and impersonate other users.
text_oauth_admin_permission_info: This application requests full administrative access. If you are an Administrator (or become one in the future), it will be able to read and write all data and impersonate other users on your behalf. If you want to avoid this, authorize it as a user without Administrator privileges instead.
config/routes.rb
end
end
get 'favicons/:digest', :to => 'favicons#show', :digest => /\h{8}/, :as => 'custom_favicon', :format => false
match 'settings', :controller => 'settings', :action => 'index', :via => :get
match 'settings/edit', :controller => 'settings', :action => 'edit', :via => [:get, :post]
delete 'settings/favicon', :controller => 'settings', :action => 'delete_favicon', :as => 'settings_favicon'
match 'settings/plugin/:id', :controller => 'settings', :action => 'plugin', :via => [:get, :post], :as => 'plugin_settings'
match 'sys/projects', :to => 'sys#projects', :via => :get
config/settings.yml
default: 100
ui_theme:
default: ''
favicon:
serialized: true
default: {}
emails_footer:
default: |-
You have received this notification because you have either subscribed to it, or are involved in it.
test/functional/favicons_controller_test.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../test_helper'
class FaviconsControllerTest < Redmine::ControllerTest
def test_show_should_be_cached_and_available_without_login
data = File.binread(file_fixture('2010/11/101123161450_testfile_1.png'))
digest = Digest::SHA256.hexdigest(data)[0, 8]
favicon = {'content_type' => 'image/png', 'data' => Base64.strict_encode64(data), 'digest' => digest}
with_settings :favicon => favicon, :login_required => '1' do
get :show, :params => {:digest => digest}
assert_response :success
assert_equal 'image/png', response.media_type
assert_equal data, response.body
assert_equal "max-age=#{1.year.to_i}, public, immutable", response.headers['Cache-Control']
end
end
end
test/functional/settings_controller_test.rb
Redmine::Plugin.unregister(:foo)
end
def test_post_edit_should_store_favicon
with_settings :favicon => {} do
post :edit, :params => {
:settings => {:favicon => fixture_file_upload('2010/11/101123161450_testfile_1.png', 'image/png')}
}
assert_redirected_to '/settings'
data = File.binread(file_fixture('2010/11/101123161450_testfile_1.png'))
assert_equal 'image/png', Setting.favicon['content_type']
assert_equal data, Base64.decode64(Setting.favicon['data'])
assert_equal Digest::SHA256.hexdigest(data)[0, 8], Setting.favicon['digest']
end
end
def test_post_edit_should_not_store_favicon_that_is_not_an_icon_image
post :edit, :params => {:settings => {:favicon => fixture_file_upload('testfile.svg', 'image/svg+xml')}}
assert_response :success
assert_select_error /Custom favicon is invalid/
assert_equal({}, Setting.favicon)
end
def test_delete_favicon
with_settings :favicon => {'content_type' => 'image/png', 'data' => 'AA==', 'digest' => '0' * 8} do
delete :delete_favicon
assert_redirected_to '/settings?tab=display'
assert_equal({}, Setting.favicon)
end
end
def test_post_mail_handler_delimiters_should_not_save_invalid_regex_delimiters
post :edit, :params => {
:settings => {
test/integration/lib/redmine/themes_test.rb
@theme.favicons.delete 'a.ico'
end
def test_use_custom_favicon_over_theme_favicon
@theme.favicons.unshift('a.ico')
favicon = {'content_type' => 'image/png', 'data' => 'AA==', 'digest' => '0' * 8}
with_settings :favicon => favicon do
get '/'
assert_response :success
assert_select 'link[rel="shortcut icon"][type="image/png"][href=?]', "/favicons/#{'0' * 8}"
end
ensure
@theme.favicons.delete 'a.ico'
end
def test_use_only_one_theme_favicon_if_theme_provides_many
@theme.favicons.unshift('b.ico', 'a.png')
get '/'
(1-1/2)