FIX: Respect permalinks starting with "/category" (#7171)

This commit is contained in:
Penar Musaraj 2019-03-18 10:24:46 -04:00 committed by GitHub
parent 4e8c174ee5
commit 2506acae80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -499,6 +499,13 @@ class ApplicationController < ActionController::Base
SecureSession.new(session["secure_session_id"] ||= SecureRandom.hex)
end
def handle_permalink(path)
permalink = Permalink.find_by_url(path)
if permalink && permalink.target_url
redirect_to permalink.target_url, status: :moved_permanently
end
end
private
def check_readonly_mode

View File

@ -9,6 +9,7 @@ class CategoriesController < ApplicationController
skip_before_action :check_xhr, only: [:index, :categories_and_latest, :categories_and_top, :redirect]
def redirect
return if handle_permalink("/category/#{params[:path]}")
redirect_to path("/c/#{params[:path]}")
end

View File

@ -49,6 +49,20 @@ describe CategoriesController do
expect(response.body).to have_tag "title", text: "Discourse - Official community"
end
it "redirects /category paths to /c paths" do
get "/category/uncategorized"
expect(response.status).to eq(302)
expect(response.body).to include("c/uncategorized")
end
it "respects permalinks before redirecting /category paths to /c paths" do
perm = Permalink.create!(url: "category/something", category_id: category.id)
get "/category/something"
expect(response.status).to eq(301)
expect(response.body).to include(category.slug)
end
end
context 'extensibility event' do