mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 23:03:38 +08:00
1859025228
Links like `/my/preferences` were invalid in custom section. The reason is that `/my` links are just redirects from backend, and they are not recognized as valid Ember paths. https://github.com/discourse/discourse/blob/main/config/routes.rb#L433 Therefore, regex match allowlist was added - similar to backend check: https://github.com/discourse/discourse/blob/main/app/controllers/users_controller.rb#L471 /safe-mode is same case
52 lines
1.4 KiB
Ruby
52 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class SidebarUrl < ActiveRecord::Base
|
|
FULL_RELOAD_LINKS_REGEX = [%r{\A/my/[a-z_\-/]+\z}, %r{\A/safe-mode\z}]
|
|
|
|
validates :icon, presence: true, length: { maximum: 40 }
|
|
validates :name, presence: true, length: { maximum: 80 }
|
|
validates :value, presence: true, length: { maximum: 200 }
|
|
|
|
validate :path_validator
|
|
|
|
before_validation :remove_internal_hostname, :set_external
|
|
|
|
def path_validator
|
|
if external?
|
|
raise ActionController::RoutingError.new("Not Found") if value !~ Discourse::Utils::URI_REGEXP
|
|
else
|
|
Rails.application.routes.recognize_path(value)
|
|
end
|
|
rescue ActionController::RoutingError
|
|
errors.add(
|
|
:value,
|
|
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
|
|
)
|
|
end
|
|
|
|
def remove_internal_hostname
|
|
self.value = self.value.sub(%r{\Ahttp(s)?://#{Discourse.current_hostname}}, "")
|
|
end
|
|
|
|
def set_external
|
|
self.external = value.start_with?("http://", "https://")
|
|
end
|
|
|
|
def full_reload?
|
|
FULL_RELOAD_LINKS_REGEX.any? { |regex| value =~ regex }
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: sidebar_urls
|
|
#
|
|
# id :bigint not null, primary key
|
|
# name :string(80) not null
|
|
# value :string(200) not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# icon :string(40) not null
|
|
# external :boolean default(FALSE), not null
|
|
#
|