mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: CORS settings per-site in a multisite env
This commit is contained in:
@@ -1,24 +1,30 @@
|
||||
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?
|
||||
|
||||
if GlobalSetting.enable_cors
|
||||
class Discourse::Cors
|
||||
def initialize(app, options = nil)
|
||||
@app = app
|
||||
@origins = GlobalSetting.cors_origin.split(',').map(&:strip)
|
||||
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?
|
||||
@global_origins = GlobalSetting.cors_origin.split(',').map(&:strip)
|
||||
end
|
||||
end
|
||||
|
||||
def call(env)
|
||||
status, headers, body = @app.call(env)
|
||||
origin = nil
|
||||
cors_origins = @global_origins || []
|
||||
cors_origins += SiteSetting.cors_origins.split('|') if SiteSetting.cors_origins
|
||||
|
||||
if origin = env['HTTP_ORIGIN']
|
||||
origin = nil unless @origins.include? origin
|
||||
if cors_origins
|
||||
if origin = env['HTTP_ORIGIN']
|
||||
origin = nil unless cors_origins.include?(origin)
|
||||
end
|
||||
|
||||
headers['Access-Control-Allow-Origin'] = origin || cors_origins[0]
|
||||
headers['Access-Control-Allow-Credentials'] = "true"
|
||||
end
|
||||
|
||||
headers['Access-Control-Allow-Origin'] = origin || @origins[0]
|
||||
headers['Access-Control-Allow-Credentials'] = "true"
|
||||
[status,headers,body]
|
||||
end
|
||||
end
|
||||
|
||||
Rails.configuration.middleware.insert 0, Discourse::Cors
|
||||
Rails.configuration.middleware.use Discourse::Cors
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user