@@ -1,221 +1,222 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class ApplicationController < ActionController::Base |
|
18 | class ApplicationController < ActionController::Base | |
19 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
19 | before_filter :user_setup, :check_if_login_required, :set_localization | |
20 | filter_parameter_logging :password |
|
20 | filter_parameter_logging :password | |
21 |
|
21 | |||
22 | include Redmine::MenuManager::MenuController |
|
22 | include Redmine::MenuManager::MenuController | |
23 | helper Redmine::MenuManager::MenuHelper |
|
23 | helper Redmine::MenuManager::MenuHelper | |
24 |
|
24 | |||
25 | REDMINE_SUPPORTED_SCM.each do |scm| |
|
25 | REDMINE_SUPPORTED_SCM.each do |scm| | |
26 | require_dependency "repository/#{scm.underscore}" |
|
26 | require_dependency "repository/#{scm.underscore}" | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | def current_role |
|
29 | def current_role | |
30 | @current_role ||= User.current.role_for_project(@project) |
|
30 | @current_role ||= User.current.role_for_project(@project) | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def user_setup |
|
33 | def user_setup | |
34 | # Check the settings cache for each request |
|
34 | # Check the settings cache for each request | |
35 | Setting.check_cache |
|
35 | Setting.check_cache | |
36 | # Find the current user |
|
36 | # Find the current user | |
37 | User.current = find_current_user |
|
37 | User.current = find_current_user | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | # Returns the current user or nil if no user is logged in |
|
40 | # Returns the current user or nil if no user is logged in | |
41 | def find_current_user |
|
41 | def find_current_user | |
42 | if session[:user_id] |
|
42 | if session[:user_id] | |
43 | # existing session |
|
43 | # existing session | |
44 | (User.find_active(session[:user_id]) rescue nil) |
|
44 | (User.find_active(session[:user_id]) rescue nil) | |
45 | elsif cookies[:autologin] && Setting.autologin? |
|
45 | elsif cookies[:autologin] && Setting.autologin? | |
46 | # auto-login feature |
|
46 | # auto-login feature | |
47 | User.find_by_autologin_key(cookies[:autologin]) |
|
47 | User.find_by_autologin_key(cookies[:autologin]) | |
48 | elsif params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
48 | elsif params[:key] && accept_key_auth_actions.include?(params[:action]) | |
49 | # RSS key authentication |
|
49 | # RSS key authentication | |
50 | User.find_by_rss_key(params[:key]) |
|
50 | User.find_by_rss_key(params[:key]) | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | # check if login is globally required to access the application |
|
54 | # check if login is globally required to access the application | |
55 | def check_if_login_required |
|
55 | def check_if_login_required | |
56 | # no check needed if user is already logged in |
|
56 | # no check needed if user is already logged in | |
57 | return true if User.current.logged? |
|
57 | return true if User.current.logged? | |
58 | require_login if Setting.login_required? |
|
58 | require_login if Setting.login_required? | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def set_localization |
|
61 | def set_localization | |
62 | User.current.language = nil unless User.current.logged? |
|
62 | User.current.language = nil unless User.current.logged? | |
63 | lang = begin |
|
63 | lang = begin | |
64 | if !User.current.language.blank? and GLoc.valid_languages.include? User.current.language.to_sym |
|
64 | if !User.current.language.blank? and GLoc.valid_languages.include? User.current.language.to_sym | |
65 | User.current.language |
|
65 | User.current.language | |
66 | elsif request.env['HTTP_ACCEPT_LANGUAGE'] |
|
66 | elsif request.env['HTTP_ACCEPT_LANGUAGE'] | |
67 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first |
|
67 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.split('-').first | |
68 | if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym |
|
68 | if accept_lang and !accept_lang.empty? and GLoc.valid_languages.include? accept_lang.to_sym | |
69 | User.current.language = accept_lang |
|
69 | User.current.language = accept_lang | |
70 | end |
|
70 | end | |
71 | end |
|
71 | end | |
72 | rescue |
|
72 | rescue | |
73 | nil |
|
73 | nil | |
74 | end || Setting.default_language |
|
74 | end || Setting.default_language | |
75 | set_language_if_valid(lang) |
|
75 | set_language_if_valid(lang) | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def require_login |
|
78 | def require_login | |
79 | if !User.current.logged? |
|
79 | if !User.current.logged? | |
80 | store_location |
|
80 | store_location | |
81 | redirect_to :controller => "account", :action => "login" |
|
81 | redirect_to :controller => "account", :action => "login" | |
82 | return false |
|
82 | return false | |
83 | end |
|
83 | end | |
84 | true |
|
84 | true | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | def require_admin |
|
87 | def require_admin | |
88 | return unless require_login |
|
88 | return unless require_login | |
89 | if !User.current.admin? |
|
89 | if !User.current.admin? | |
90 | render_403 |
|
90 | render_403 | |
91 | return false |
|
91 | return false | |
92 | end |
|
92 | end | |
93 | true |
|
93 | true | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | # Authorize the user for the requested action |
|
96 | # Authorize the user for the requested action | |
97 | def authorize(ctrl = params[:controller], action = params[:action]) |
|
97 | def authorize(ctrl = params[:controller], action = params[:action]) | |
98 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project) |
|
98 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project) | |
99 | allowed ? true : (User.current.logged? ? render_403 : require_login) |
|
99 | allowed ? true : (User.current.logged? ? render_403 : require_login) | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | # make sure that the user is a member of the project (or admin) if project is private |
|
102 | # make sure that the user is a member of the project (or admin) if project is private | |
103 | # used as a before_filter for actions that do not require any particular permission on the project |
|
103 | # used as a before_filter for actions that do not require any particular permission on the project | |
104 | def check_project_privacy |
|
104 | def check_project_privacy | |
105 | if @project && @project.active? |
|
105 | if @project && @project.active? | |
106 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
106 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? | |
107 | true |
|
107 | true | |
108 | else |
|
108 | else | |
109 | User.current.logged? ? render_403 : require_login |
|
109 | User.current.logged? ? render_403 : require_login | |
110 | end |
|
110 | end | |
111 | else |
|
111 | else | |
112 | @project = nil |
|
112 | @project = nil | |
113 | render_404 |
|
113 | render_404 | |
114 | false |
|
114 | false | |
115 | end |
|
115 | end | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | # store current uri in session. |
|
118 | # store current uri in session. | |
119 | # return to this location by calling redirect_back_or_default |
|
119 | # return to this location by calling redirect_back_or_default | |
120 | def store_location |
|
120 | def store_location | |
121 | session[:return_to_params] = params |
|
121 | session[:return_to_params] = params | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | # move to the last store_location call or to the passed default one |
|
124 | # move to the last store_location call or to the passed default one | |
125 | def redirect_back_or_default(default) |
|
125 | def redirect_back_or_default(default) | |
126 | if session[:return_to_params].nil? |
|
126 | if session[:return_to_params].nil? | |
127 | redirect_to default |
|
127 | redirect_to default | |
128 | else |
|
128 | else | |
129 | redirect_to session[:return_to_params] |
|
129 | redirect_to session[:return_to_params] | |
130 | session[:return_to_params] = nil |
|
130 | session[:return_to_params] = nil | |
131 | end |
|
131 | end | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def render_403 |
|
134 | def render_403 | |
135 | @project = nil |
|
135 | @project = nil | |
136 | render :template => "common/403", :layout => !request.xhr?, :status => 403 |
|
136 | render :template => "common/403", :layout => !request.xhr?, :status => 403 | |
137 | return false |
|
137 | return false | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | def render_404 |
|
140 | def render_404 | |
141 | render :template => "common/404", :layout => !request.xhr?, :status => 404 |
|
141 | render :template => "common/404", :layout => !request.xhr?, :status => 404 | |
142 | return false |
|
142 | return false | |
143 | end |
|
143 | end | |
144 |
|
144 | |||
145 | def render_error(msg) |
|
145 | def render_error(msg) | |
146 | flash.now[:error] = msg |
|
146 | flash.now[:error] = msg | |
147 | render :nothing => true, :layout => !request.xhr?, :status => 500 |
|
147 | render :nothing => true, :layout => !request.xhr?, :status => 500 | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | def render_feed(items, options={}) |
|
150 | def render_feed(items, options={}) | |
151 | @items = items || [] |
|
151 | @items = items || [] | |
152 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
152 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
|
153 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |||
153 | @title = options[:title] || Setting.app_title |
|
154 | @title = options[:title] || Setting.app_title | |
154 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
155 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' | |
155 | end |
|
156 | end | |
156 |
|
157 | |||
157 | def self.accept_key_auth(*actions) |
|
158 | def self.accept_key_auth(*actions) | |
158 | actions = actions.flatten.map(&:to_s) |
|
159 | actions = actions.flatten.map(&:to_s) | |
159 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
160 | write_inheritable_attribute('accept_key_auth_actions', actions) | |
160 | end |
|
161 | end | |
161 |
|
162 | |||
162 | def accept_key_auth_actions |
|
163 | def accept_key_auth_actions | |
163 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
164 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] | |
164 | end |
|
165 | end | |
165 |
|
166 | |||
166 | # TODO: move to model |
|
167 | # TODO: move to model | |
167 | def attach_files(obj, attachments) |
|
168 | def attach_files(obj, attachments) | |
168 | attached = [] |
|
169 | attached = [] | |
169 | if attachments && attachments.is_a?(Hash) |
|
170 | if attachments && attachments.is_a?(Hash) | |
170 | attachments.each_value do |attachment| |
|
171 | attachments.each_value do |attachment| | |
171 | file = attachment['file'] |
|
172 | file = attachment['file'] | |
172 | next unless file && file.size > 0 |
|
173 | next unless file && file.size > 0 | |
173 | a = Attachment.create(:container => obj, |
|
174 | a = Attachment.create(:container => obj, | |
174 | :file => file, |
|
175 | :file => file, | |
175 | :description => attachment['description'].to_s.strip, |
|
176 | :description => attachment['description'].to_s.strip, | |
176 | :author => User.current) |
|
177 | :author => User.current) | |
177 | attached << a unless a.new_record? |
|
178 | attached << a unless a.new_record? | |
178 | end |
|
179 | end | |
179 | end |
|
180 | end | |
180 | attached |
|
181 | attached | |
181 | end |
|
182 | end | |
182 |
|
183 | |||
183 | # Returns the number of objects that should be displayed |
|
184 | # Returns the number of objects that should be displayed | |
184 | # on the paginated list |
|
185 | # on the paginated list | |
185 | def per_page_option |
|
186 | def per_page_option | |
186 | per_page = nil |
|
187 | per_page = nil | |
187 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
188 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
188 | per_page = params[:per_page].to_s.to_i |
|
189 | per_page = params[:per_page].to_s.to_i | |
189 | session[:per_page] = per_page |
|
190 | session[:per_page] = per_page | |
190 | elsif session[:per_page] |
|
191 | elsif session[:per_page] | |
191 | per_page = session[:per_page] |
|
192 | per_page = session[:per_page] | |
192 | else |
|
193 | else | |
193 | per_page = Setting.per_page_options_array.first || 25 |
|
194 | per_page = Setting.per_page_options_array.first || 25 | |
194 | end |
|
195 | end | |
195 | per_page |
|
196 | per_page | |
196 | end |
|
197 | end | |
197 |
|
198 | |||
198 | # qvalues http header parser |
|
199 | # qvalues http header parser | |
199 | # code taken from webrick |
|
200 | # code taken from webrick | |
200 | def parse_qvalues(value) |
|
201 | def parse_qvalues(value) | |
201 | tmp = [] |
|
202 | tmp = [] | |
202 | if value |
|
203 | if value | |
203 | parts = value.split(/,\s*/) |
|
204 | parts = value.split(/,\s*/) | |
204 | parts.each {|part| |
|
205 | parts.each {|part| | |
205 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
206 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
206 | val = m[1] |
|
207 | val = m[1] | |
207 | q = (m[2] or 1).to_f |
|
208 | q = (m[2] or 1).to_f | |
208 | tmp.push([val, q]) |
|
209 | tmp.push([val, q]) | |
209 | end |
|
210 | end | |
210 | } |
|
211 | } | |
211 | tmp = tmp.sort_by{|val, q| -q} |
|
212 | tmp = tmp.sort_by{|val, q| -q} | |
212 | tmp.collect!{|val, q| val} |
|
213 | tmp.collect!{|val, q| val} | |
213 | end |
|
214 | end | |
214 | return tmp |
|
215 | return tmp | |
215 | end |
|
216 | end | |
216 |
|
217 | |||
217 | # Returns a string that can be used as filename value in Content-Disposition header |
|
218 | # Returns a string that can be used as filename value in Content-Disposition header | |
218 | def filename_for_content_disposition(name) |
|
219 | def filename_for_content_disposition(name) | |
219 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
220 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
220 | end |
|
221 | end | |
221 | end |
|
222 | end |
General Comments 0
You need to be logged in to leave comments.
Login now