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