Skip to content

Commit

Permalink
Merge pull request #2 from mcfedr/develop
Browse files Browse the repository at this point in the history
Fixes for Redmine 3.0
  • Loading branch information
mavimo authored Aug 19, 2018
2 parents 83106f5 + df995b5 commit 2b03a46
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
17 changes: 13 additions & 4 deletions app/controllers/cors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ class CorsController < ApplicationController
skip_before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization

def preflight
headers['Access-Control-Allow-Origin'] = Setting.plugin_redmine_cors["cors_domain"].to_s
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Content-Type, X-Redmine-API-Key'
headers['Access-Control-Max-Age'] = '1728000'
allowed_origins = Setting.plugin_redmine_cors["cors_domain"].to_s.downcase.split(/[ ,]/).reject { |c| c.empty? }
if not request.headers["Origin"].nil?
if allowed_origins.include?("*") || allowed_origins.include?(request.headers["Origin"].to_s.downcase)
headers['Access-Control-Allow-Origin'] = request.headers["Origin"].to_s
headers['Access-Control-Allow-Methods'] = Setting.plugin_redmine_cors["cors_methods"]
headers['Access-Control-Allow-Headers'] = Setting.plugin_redmine_cors["cors_headers"]
headers['Access-Control-Max-Age'] = Setting.plugin_redmine_cors["cors_maxage"]
if (Setting.plugin_redmine_cors["cors_credentials"] == '1')
headers['Access-Control-Allow-Credentials'] = 'true'
end
end
end
headers['Vary'] = 'Origin'
render :text => '', :content_type => 'text/plain'
end
end
31 changes: 31 additions & 0 deletions app/views/settings/_cors_settings.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,34 @@
<br/>
<%= l(:description_cors_domain) %>
</p>
<p>
<%=content_tag(:label, l(:label_cors_headers) + ":") %>
<%=text_field_tag "settings[cors_headers]", @settings["cors_headers"], :size => 100 %><br/>
(<%=l(:label_default)%>: 0)
<br/>
<%= l(:description_cors_headers) %>
</p>

<p>
<%=content_tag(:label, l(:label_cors_methods) + ":") %>
<%=text_field_tag "settings[cors_methods]", @settings["cors_methods"], :size => 100 %><br/>
(<%=l(:label_default)%>: 0)
<br/>
<%= l(:description_cors_methods) %>
</p>

<p>
<%=content_tag(:label, l(:label_cors_maxage) + ":") %>
<%=text_field_tag "settings[cors_maxage]", @settings["cors_maxage"], :size => 100 %><br/>
(<%=l(:label_default)%>: 0)
<br/>
<%= l(:description_cors_maxage) %>
</p>

<p>
<%=content_tag(:label, l(:label_cors_credentials) + ":") %>
<%=check_box_tag "settings[cors_credentials]", '1', @settings["cors_credentials"] %><br/>
(<%=l(:label_default)%>: 0)
<br/>
<%= l(:description_cors_credentials) %>
</p>
12 changes: 10 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
en:
label_cors_domain: Domain
description_cors_domain: Define domain allowed in CORS
label_cors_domain: Domain(s)
description_cors_domain: Define origins(s) (comma or space separated) allowed in CORS, you should include http(s)://
label_cors_headers: Allowed Headers
description_cors_headers: Define headers (comma separated) allowed in CORS requests
label_cors_methods: Allowed Methods
description_cors_methods: Define methods (comma separated) allowed in CORS requests
label_cors_maxage: Max age
description_cors_maxage: Define max age (in seconds) that browsers will cache CORS headers
label_cors_credentials: Allow Credentials
description_cors_credentials: Whether to allow credentials to be sent by browser (use of Authorization header)
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RedmineApp::Application.routes.draw do
match '*path', :to => 'cors#preflight', :constraints => {:method => 'OPTIONS'}
match '*path', :to => 'cors#preflight', via: [:options]
end
6 changes: 5 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

settings :partial => 'settings/cors_settings',
:default => {
"cors_domain" => "*",
"cors_domain" => "",
"cors_headers" => "X-Requested-With, X-Prototype-Version, Content-Type, X-Redmine-API-Key, accept, authorization",
"cors_methods" => "POST, GET, OPTIONS, PUT",
"cors_maxage" => "1728000",
"cors_credentials" => '1'
}
end
13 changes: 10 additions & 3 deletions lib/redmine_cors/patches/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ def self.included(base) # :nodoc:

module InstanceMethods
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = Setting.plugin_redmine_cors["cors_domain"].to_s
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
headers['Access-Control-Max-Age'] = "1728000"
allowed_origins = Setting.plugin_redmine_cors["cors_domain"].to_s.downcase.split(/[ ,]/).reject { |c| c.empty? }
if not request.headers["Origin"].nil?
if allowed_origins.include?("*") || allowed_origins.include?(request.headers["Origin"].to_s.downcase)
headers['Access-Control-Allow-Origin'] = request.headers["Origin"].to_s
if (Setting.plugin_redmine_cors["cors_credentials"] == '1')
headers['Access-Control-Allow-Credentials'] = 'true'
end
end
headers['Vary'] = 'Origin'
end
end
end
end
Expand Down

1 comment on commit 2b03a46

@gabriel-cardoso
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi ! Does it support Redmine 4.x ?

Please sign in to comment.