From e99ef0322b4101633a8f0e27b33723793d18f44f Mon Sep 17 00:00:00 2001
From: Ahesh Lakmal
Date: Thu, 19 Nov 2026 00:29:43 +0100
Subject: [PATCH] Allow users to choose the background color of their initials
avatar (#44717).
The color of the initials avatar was always derived from the user id, so
users with the same initials could not tell their avatars apart and could
not change the color. The avatar page now lists the colors of the palette
and stores the selected one in users.avatar_color. The color can be set
without ImageMagick and administrators can set it for other users. Users
who have not chosen a color keep the color derived from their id.
---
app/assets/stylesheets/application.css | 2 +
app/controllers/avatars_controller.rb | 14 ++++-
app/helpers/avatars_helper.rb | 2 +-
app/models/user.rb | 9 +++
app/views/avatars/edit.html.erb | 15 +++++
config/locales/en.yml | 1 +
config/routes.rb | 2 +
...0261119000000_add_avatar_color_to_users.rb | 5 ++
test/functional/avatars_controller_test.rb | 62 +++++++++++++++++++
test/helpers/avatars_helper_test.rb | 11 ++++
test/integration/routing/my_test.rb | 1 +
test/integration/routing/users_test.rb | 1 +
test/unit/user_test.rb | 22 +++++++
13 files changed, 145 insertions(+), 2 deletions(-)
create mode 100644 db/migrate/20261119000000_add_avatar_color_to_users.rb
diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css
index 1a006291b..ad0193843 100644
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -2435,6 +2435,8 @@ img.filecontent.image {
}
.avatar.locked, .avatar.registered {opacity: .6;}
.avatar-preview {display: block; margin-block-start: 8px;}
+.avatar-colors {display: inline-flex; flex-wrap: wrap; gap: 4px 8px; vertical-align: middle;}
+.avatar-colors label.inline {display: inline-flex; align-items: center; gap: 2px;}
span[role="img"].avatar {
font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
diff --git a/app/controllers/avatars_controller.rb b/app/controllers/avatars_controller.rb
index 4effab2af..843a9abb8 100644
--- a/app/controllers/avatars_controller.rb
+++ b/app/controllers/avatars_controller.rb
@@ -24,7 +24,7 @@ class AvatarsController < ApplicationController
before_action :find_avatar, :only => :show
before_action :find_user, :except => :show
- before_action :check_avatar_available, :except => :edit
+ before_action :check_avatar_available, :except => [:edit, :update_color]
accept_api_auth :show
@@ -69,6 +69,16 @@ class AvatarsController < ApplicationController
redirect_to @avatar_path
end
+ def update_color
+ @user.avatar_color = params[:avatar_color]
+ if @user.save
+ flash[:notice] = l(:notice_successful_update)
+ redirect_to @avatar_path
+ else
+ render :edit, :status => :unprocessable_content
+ end
+ end
+
private
def find_avatar
@@ -87,12 +97,14 @@ class AvatarsController < ApplicationController
@user = User.find_by(:id => params[:user_id])
if @user
@avatar_path = edit_user_avatar_path(@user)
+ @avatar_color_path = user_avatar_color_path(@user)
else
render_404
end
elsif require_login
@user = User.current
@avatar_path = my_avatar_path
+ @avatar_color_path = my_avatar_color_path
end
end
diff --git a/app/helpers/avatars_helper.rb b/app/helpers/avatars_helper.rb
index 415220f2d..14bc1a066 100644
--- a/app/helpers/avatars_helper.rb
+++ b/app/helpers/avatars_helper.rb
@@ -162,7 +162,7 @@ module AvatarsHelper
def initials_avatar_css_class(user, options)
size = (options[:size] || GravatarHelper::DEFAULT_OPTIONS[:size]).to_i
- ["avatar-color-#{user.id % 16}", "s#{size}", options[:class]].compact.join(' ')
+ ["avatar-color-#{user.initials_avatar_color}", "s#{size}", options[:class]].compact.join(' ')
end
def extract_email_from_user(user)
diff --git a/app/models/user.rb b/app/models/user.rb
index 0045fb85a..8b8e90e38 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -122,6 +122,8 @@ class User < Principal
attr_accessor :oauth_scope
AVATAR_CONTENT_TYPES = %w(image/avif image/gif image/jpeg image/png image/webp).freeze
+ # Number of background colors of the initials avatar (avatar-color-0 to avatar-color-15 in CSS)
+ AVATAR_COLORS = 16
LOGIN_LENGTH_LIMIT = 60
MAIL_LENGTH_LIMIT = 254
@@ -134,6 +136,7 @@ class User < Principal
validates_length_of :firstname, :maximum => 30
validates_length_of :lastname, :maximum => 255
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
+ validates_inclusion_of :avatar_color, :in => 0...AVATAR_COLORS, :allow_nil => true
Setting::PASSWORD_CHAR_CLASSES.each do |k, v|
validates_format_of :password, :with => v, :message => :"must_contain_#{k}", :allow_blank => true, :if => Proc.new {Setting.password_required_char_classes.include?(k)}
end
@@ -472,6 +475,12 @@ class User < Principal
OpenSSL::HMAC.hexdigest('SHA256', secret, avatar_id.to_s).first(16)
end
+ # Returns the index of the background color of the initials avatar,
+ # derived from the user id unless the user has chosen one
+ def initials_avatar_color
+ avatar_color || (id % AVATAR_COLORS)
+ end
+
def delete_avatar
replace_avatar(nil)
end
diff --git a/app/views/avatars/edit.html.erb b/app/views/avatars/edit.html.erb
index 37284704d..766d3d63e 100644
--- a/app/views/avatars/edit.html.erb
+++ b/app/views/avatars/edit.html.erb
@@ -22,6 +22,21 @@
<% end %>
+
+ <%= form_tag(@avatar_color_path, :method => :put) do %>
+
+
+
+ <% User::AVATAR_COLORS.times do |color| %>
+
+ <% end %>
+
+ <%= submit_tag l(:button_save) %>
+
+ <% end %>
<% if User.avatar_available? %>
<%= form_tag(@avatar_path, :method => :put, :multipart => true) do %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ab16a1d1d..1f898e156 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -359,6 +359,7 @@ en:
field_done_ratio: "% Done"
field_auth_source: Authentication mode
field_avatar: Avatar
+ field_avatar_color: Avatar color
field_hide_mail: Hide my email address
field_comments: Comment
field_url: URL
diff --git a/config/routes.rb b/config/routes.rb
index e6f9e9232..a72747a6b 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -105,6 +105,7 @@ Rails.application.routes.draw do
get 'my/avatar', :to => 'avatars#edit', :as => 'my_avatar', :format => false
put 'my/avatar', :to => 'avatars#update', :format => false
delete 'my/avatar', :to => 'avatars#destroy', :format => false
+ put 'my/avatar/color', :to => 'avatars#update_color', :as => 'my_avatar_color', :format => false
match 'my/add_block', :controller => 'my', :action => 'add_block', :via => :post
match 'my/remove_block', :controller => 'my', :action => 'remove_block', :via => :post
match 'my/order_blocks', :controller => 'my', :action => 'order_blocks', :via => :post
@@ -136,6 +137,7 @@ Rails.application.routes.draw do
get 'users/:user_id/avatar', :to => 'avatars#edit', :user_id => /\d+/, :as => 'edit_user_avatar', :format => false
put 'users/:user_id/avatar', :to => 'avatars#update', :user_id => /\d+/, :format => false
delete 'users/:user_id/avatar', :to => 'avatars#destroy', :user_id => /\d+/, :format => false
+ put 'users/:user_id/avatar/color', :to => 'avatars#update_color', :user_id => /\d+/, :as => 'user_avatar_color', :format => false
post 'watchers/watch', :to => 'watchers#watch', :as => 'watch'
delete 'watchers/watch', :to => 'watchers#unwatch'
diff --git a/db/migrate/20261119000000_add_avatar_color_to_users.rb b/db/migrate/20261119000000_add_avatar_color_to_users.rb
new file mode 100644
index 000000000..d881e986e
--- /dev/null
+++ b/db/migrate/20261119000000_add_avatar_color_to_users.rb
@@ -0,0 +1,5 @@
+class AddAvatarColorToUsers < ActiveRecord::Migration[8.1]
+ def change
+ add_column :users, :avatar_color, :integer
+ end
+end
diff --git a/test/functional/avatars_controller_test.rb b/test/functional/avatars_controller_test.rb
index 716443114..02c797833 100644
--- a/test/functional/avatars_controller_test.rb
+++ b/test/functional/avatars_controller_test.rb
@@ -99,6 +99,22 @@ class AvatarsControllerTest < Redmine::ControllerTest
assert_response :success
assert_select 'h2 a[href="/users/2/edit"]'
assert_select 'form[action="/users/2/avatar"]'
+ assert_select 'form[action="/users/2/avatar/color"]'
+ end
+
+ def test_edit_should_show_avatar_colors
+ @request.session[:user_id] = 2
+ with_settings :avatar_enabled => '0' do
+ get :edit
+ end
+ assert_response :success
+ assert_select 'form[action="/my/avatar/color"]' do
+ assert_select 'input[type=radio][name=avatar_color]', User::AVATAR_COLORS
+ assert_select 'input[type=radio][name=avatar_color][checked]', 1
+ assert_select 'input#avatar_color_2[checked]'
+ assert_select 'label.inline span[role="img"].avatar.avatar-color-5', :text => 'JS'
+ assert_select 'input[type=submit][value=Save]'
+ end
end
def test_edit_with_user_id_should_require_admin
@@ -121,6 +137,52 @@ class AvatarsControllerTest < Redmine::ControllerTest
assert_equal User.find(2), avatar.author
end
+ def test_update_color
+ @request.session[:user_id] = 2
+ with_settings :avatar_enabled => '0' do
+ put :update_color, :params => {:avatar_color => '5'}
+ end
+ assert_redirected_to '/my/avatar'
+ assert_equal 'Successful update.', flash[:notice]
+ assert_equal 5, User.find(2).avatar_color
+ end
+
+ def test_update_color_without_color_should_reset_color
+ User.find(2).update_column(:avatar_color, 5)
+ @request.session[:user_id] = 2
+ put :update_color
+ assert_redirected_to '/my/avatar'
+ assert_nil User.find(2).avatar_color
+ end
+
+ def test_update_color_with_invalid_color
+ @request.session[:user_id] = 2
+ put :update_color, :params => {:avatar_color => User::AVATAR_COLORS.to_s}
+ assert_response :unprocessable_content
+ assert_select_error /Avatar color is not included in the list/
+ assert_nil User.find(2).avatar_color
+ end
+
+ def test_update_color_with_user_id
+ @request.session[:user_id] = 1
+ put :update_color, :params => {:user_id => 2, :avatar_color => '5'}
+ assert_redirected_to '/users/2/avatar'
+ assert_equal 5, User.find(2).avatar_color
+ end
+
+ def test_update_color_with_user_id_should_require_admin
+ @request.session[:user_id] = 2
+ put :update_color, :params => {:user_id => 3, :avatar_color => '5'}
+ assert_response :forbidden
+ assert_nil User.find(3).avatar_color
+ end
+
+ def test_update_color_should_require_login
+ put :update_color, :params => {:avatar_color => '5'}
+ assert_response :redirect
+ assert_match %r{/login}, response.location
+ end
+
def test_destroy
Redmine::Thumbnail.stubs(:convert_available?).returns(true)
set_tmp_attachments_directory
diff --git a/test/helpers/avatars_helper_test.rb b/test/helpers/avatars_helper_test.rb
index 9b468d539..afd997063 100644
--- a/test/helpers/avatars_helper_test.rb
+++ b/test/helpers/avatars_helper_test.rb
@@ -137,6 +137,17 @@ class AvatarsHelperTest < Redmine::HelperTest
end
end
+ def test_initials_avatar_should_use_chosen_color
+ user = User.find_by_mail('miscuser8@foo.bar')
+ user.avatar_color = 3
+ with_settings :gravatar_enabled => '0' do
+ assert_equal "UM", avatar(user)
+ end
+ with_settings :gravatar_default => '' do
+ assert_select_in avatar(user), 'img[data-initials-avatar-fallback-class-value="avatar-color-3 s24 avatar active"]'
+ end
+ end
+
def test_avatar_should_have_user_status_class
assert_select_in avatar(User.find(1)), '.avatar.active'
assert_select_in avatar(User.find(5)), '.avatar.locked'
diff --git a/test/integration/routing/my_test.rb b/test/integration/routing/my_test.rb
index 4367aa294..8cbbf93d5 100644
--- a/test/integration/routing/my_test.rb
+++ b/test/integration/routing/my_test.rb
@@ -41,6 +41,7 @@ class RoutingMyTest < Redmine::RoutingTest
should_route 'GET /my/avatar' => 'avatars#edit'
should_route 'PUT /my/avatar' => 'avatars#update'
should_route 'DELETE /my/avatar' => 'avatars#destroy'
+ should_route 'PUT /my/avatar/color' => 'avatars#update_color'
should_route 'POST /my/add_block' => 'my#add_block'
should_route 'POST /my/remove_block' => 'my#remove_block'
diff --git a/test/integration/routing/users_test.rb b/test/integration/routing/users_test.rb
index 05b6f99a6..1a9a0e65a 100644
--- a/test/integration/routing/users_test.rb
+++ b/test/integration/routing/users_test.rb
@@ -38,6 +38,7 @@ class RoutingUsersTest < Redmine::RoutingTest
should_route 'GET /users/44/avatar' => 'avatars#edit', :user_id => '44'
should_route 'PUT /users/44/avatar' => 'avatars#update', :user_id => '44'
should_route 'DELETE /users/44/avatar' => 'avatars#destroy', :user_id => '44'
+ should_route 'PUT /users/44/avatar/color' => 'avatars#update_color', :user_id => '44'
should_route 'GET /users/44/avatar/0123456789abcdef/48' => 'avatars#show', :user_id => '44', :avatar_key => '0123456789abcdef', :size => '48'
end
end
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
index b44db0615..bbf2e1c2e 100644
--- a/test/unit/user_test.rb
+++ b/test/unit/user_test.rb
@@ -1610,6 +1610,28 @@ class UserTest < ActiveSupport::TestCase
assert_not User.avatar_available?
end
+ def test_initials_avatar_color_should_be_derived_from_id_by_default
+ assert_nil @jsmith.avatar_color
+ assert_equal 2, @jsmith.initials_avatar_color
+ end
+
+ def test_initials_avatar_color_should_return_chosen_color
+ @jsmith.avatar_color = 5
+ assert_equal 5, @jsmith.initials_avatar_color
+ end
+
+ def test_avatar_color_should_be_one_of_the_initials_avatar_colors
+ [nil, 0, User::AVATAR_COLORS - 1].each do |color|
+ @jsmith.avatar_color = color
+ assert @jsmith.valid?, "#{color.inspect} should be valid"
+ end
+ [-1, User::AVATAR_COLORS].each do |color|
+ @jsmith.avatar_color = color
+ assert_not @jsmith.valid?, "#{color.inspect} should be invalid"
+ assert_include 'Avatar color is not included in the list', @jsmith.errors.full_messages
+ end
+ end
+
def test_update_avatar_should_reject_images_without_imagemagick
Redmine::Thumbnail.stubs(:convert_available?).returns(false)
assert_no_difference 'Attachment.count' do
--
2.56.1.windows.1