Fixes #3617 - Creating and editing users via office 365 fails with image source is invalid.

This commit is contained in:
Rolf Schmidt 2021-07-01 08:58:09 +00:00 committed by Thorsten Eckel
parent 7feca73300
commit cb3dced002
4 changed files with 62 additions and 6 deletions

View file

@ -8,9 +8,16 @@ class User
after_create :avatar_for_email_check, unless: -> { BulkImportInfo.enabled? }
after_update :avatar_for_email_check, unless: -> { BulkImportInfo.enabled? }
validates :image_source, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]), allow_blank: true
before_validation :ensure_existing_image, :remove_invalid_image_source
end
before_validation :ensure_existing_image
def remove_invalid_image_source
return if image_source.blank?
return if image_source.match?(URI::DEFAULT_PARSER.make_regexp(%w[http https]))
Rails.logger.debug "Removed invalid image source '#{image_source}' for user '#{email}'"
self.image_source = nil
end
def avatar_for_email_check

View file

@ -0,0 +1,12 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
class Issue3617UserImageSourceFix < ActiveRecord::Migration[5.2]
def change
return if !Setting.exists?(name: 'system_init_done')
User.where.not(image_source: nil).find_each do |user|
user.remove_invalid_image_source
user.save!
end
end
end

View file

@ -0,0 +1,31 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
require 'rails_helper'
RSpec.describe Issue3617UserImageSourceFix, type: :db_migration, db_strategy: :reset do
describe 'when invalid user' do
let!(:user) do
user = create(:user)
user.update_column(:image_source, 'invalid stuff!!!')
user
end
it 'removes invalid image sources' do
migrate
expect(user.reload.image_source).to eq(nil)
end
end
describe 'when valid user' do
let!(:user) do
user = create(:user)
user.update_column(:image_source, 'https://zammad.org/avatar.png')
user
end
it 'does not change anything' do
migrate
expect(user.reload.image_source).to eq('https://zammad.org/avatar.png')
end
end
end

View file

@ -837,12 +837,18 @@ RSpec.describe User, type: :model do
let(:value) { 'Th1515n0t4v4l1dh45h' }
let(:escaped) { Regexp.escape(value) }
it 'prevents create' do
expect { create(:user, image_source: value) }.to raise_error(ActiveRecord::RecordInvalid, %r{Image source})
it 'valid create' do
expect(create(:user, image_source: 'https://zammad.org/avatar.png').image_source).not_to eq(nil)
end
it 'prevents update' do
expect { create(:user).update!(image_source: value) }.to raise_error(ActiveRecord::RecordInvalid, %r{Image source})
it 'removes invalid image source of create' do
expect(create(:user, image_source: value).image_source).to eq(nil)
end
it 'removes invalid image source of update' do
user = create(:user)
user.update!(image_source: value)
expect(user.image_source).to eq(nil)
end
end
end