@@ -1,252 +1,258 | |||
|
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 | include Redmine::I18n |
|
23 | 23 | |
|
24 | 24 | layout 'base' |
|
25 | 25 | |
|
26 | 26 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
27 | 27 | filter_parameter_logging :password |
|
28 | 28 | |
|
29 | 29 | include Redmine::Search::Controller |
|
30 | 30 | include Redmine::MenuManager::MenuController |
|
31 | 31 | helper Redmine::MenuManager::MenuHelper |
|
32 | 32 | |
|
33 | 33 | REDMINE_SUPPORTED_SCM.each do |scm| |
|
34 | 34 | require_dependency "repository/#{scm.underscore}" |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | def user_setup |
|
38 | 38 | # Check the settings cache for each request |
|
39 | 39 | Setting.check_cache |
|
40 | 40 | # Find the current user |
|
41 | 41 | User.current = find_current_user |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | # Returns the current user or nil if no user is logged in |
|
45 | 45 | # and starts a session if needed |
|
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 starts a new session |
|
52 | 52 | user = User.try_to_autologin(cookies[:autologin]) |
|
53 | 53 | session[:user_id] = user.id if user |
|
54 | 54 | user |
|
55 | 55 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
56 | 56 | # RSS key authentication does not start a session |
|
57 | 57 | User.find_by_rss_key(params[:key]) |
|
58 | 58 | end |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | # Sets the logged in user |
|
62 | 62 | def logged_user=(user) |
|
63 | 63 | if user && user.is_a?(User) |
|
64 | 64 | User.current = user |
|
65 | 65 | session[:user_id] = user.id |
|
66 | 66 | else |
|
67 | 67 | User.current = User.anonymous |
|
68 | 68 | session[:user_id] = nil |
|
69 | 69 | end |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | # check if login is globally required to access the application |
|
73 | 73 | def check_if_login_required |
|
74 | 74 | # no check needed if user is already logged in |
|
75 | 75 | return true if User.current.logged? |
|
76 | 76 | require_login if Setting.login_required? |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def set_localization |
|
80 | 80 | lang = nil |
|
81 | 81 | if User.current.logged? |
|
82 | 82 | lang = find_language(User.current.language) |
|
83 | 83 | end |
|
84 | 84 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
85 | 85 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase |
|
86 | 86 | if !accept_lang.blank? |
|
87 | 87 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
88 | 88 | end |
|
89 | 89 | end |
|
90 | 90 | lang ||= Setting.default_language |
|
91 | 91 | set_language_if_valid(lang) |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def require_login |
|
95 | 95 | if !User.current.logged? |
|
96 | redirect_to :controller => "account", :action => "login", :back_url => url_for(params) | |
|
96 | # Extract only the basic url parameters on non-GET requests | |
|
97 | if request.get? | |
|
98 | url = url_for(params) | |
|
99 | else | |
|
100 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) | |
|
101 | end | |
|
102 | redirect_to :controller => "account", :action => "login", :back_url => url | |
|
97 | 103 | return false |
|
98 | 104 | end |
|
99 | 105 | true |
|
100 | 106 | end |
|
101 | 107 | |
|
102 | 108 | def require_admin |
|
103 | 109 | return unless require_login |
|
104 | 110 | if !User.current.admin? |
|
105 | 111 | render_403 |
|
106 | 112 | return false |
|
107 | 113 | end |
|
108 | 114 | true |
|
109 | 115 | end |
|
110 | 116 | |
|
111 | 117 | def deny_access |
|
112 | 118 | User.current.logged? ? render_403 : require_login |
|
113 | 119 | end |
|
114 | 120 | |
|
115 | 121 | # Authorize the user for the requested action |
|
116 | 122 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
117 | 123 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) |
|
118 | 124 | allowed ? true : deny_access |
|
119 | 125 | end |
|
120 | 126 | |
|
121 | 127 | # Authorize the user for the requested action outside a project |
|
122 | 128 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
123 | 129 | authorize(ctrl, action, global) |
|
124 | 130 | end |
|
125 | 131 | |
|
126 | 132 | # make sure that the user is a member of the project (or admin) if project is private |
|
127 | 133 | # used as a before_filter for actions that do not require any particular permission on the project |
|
128 | 134 | def check_project_privacy |
|
129 | 135 | if @project && @project.active? |
|
130 | 136 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
131 | 137 | true |
|
132 | 138 | else |
|
133 | 139 | User.current.logged? ? render_403 : require_login |
|
134 | 140 | end |
|
135 | 141 | else |
|
136 | 142 | @project = nil |
|
137 | 143 | render_404 |
|
138 | 144 | false |
|
139 | 145 | end |
|
140 | 146 | end |
|
141 | 147 | |
|
142 | 148 | def redirect_back_or_default(default) |
|
143 | 149 | back_url = CGI.unescape(params[:back_url].to_s) |
|
144 | 150 | if !back_url.blank? |
|
145 | 151 | begin |
|
146 | 152 | uri = URI.parse(back_url) |
|
147 | 153 | # do not redirect user to another host or to the login or register page |
|
148 | 154 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
149 | 155 | redirect_to(back_url) and return |
|
150 | 156 | end |
|
151 | 157 | rescue URI::InvalidURIError |
|
152 | 158 | # redirect to default |
|
153 | 159 | end |
|
154 | 160 | end |
|
155 | 161 | redirect_to default |
|
156 | 162 | end |
|
157 | 163 | |
|
158 | 164 | def render_403 |
|
159 | 165 | @project = nil |
|
160 | 166 | render :template => "common/403", :layout => !request.xhr?, :status => 403 |
|
161 | 167 | return false |
|
162 | 168 | end |
|
163 | 169 | |
|
164 | 170 | def render_404 |
|
165 | 171 | render :template => "common/404", :layout => !request.xhr?, :status => 404 |
|
166 | 172 | return false |
|
167 | 173 | end |
|
168 | 174 | |
|
169 | 175 | def render_error(msg) |
|
170 | 176 | flash.now[:error] = msg |
|
171 | 177 | render :text => '', :layout => !request.xhr?, :status => 500 |
|
172 | 178 | end |
|
173 | 179 | |
|
174 | 180 | def render_feed(items, options={}) |
|
175 | 181 | @items = items || [] |
|
176 | 182 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
177 | 183 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
178 | 184 | @title = options[:title] || Setting.app_title |
|
179 | 185 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
180 | 186 | end |
|
181 | 187 | |
|
182 | 188 | def self.accept_key_auth(*actions) |
|
183 | 189 | actions = actions.flatten.map(&:to_s) |
|
184 | 190 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
185 | 191 | end |
|
186 | 192 | |
|
187 | 193 | def accept_key_auth_actions |
|
188 | 194 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
189 | 195 | end |
|
190 | 196 | |
|
191 | 197 | # TODO: move to model |
|
192 | 198 | def attach_files(obj, attachments) |
|
193 | 199 | attached = [] |
|
194 | 200 | unsaved = [] |
|
195 | 201 | if attachments && attachments.is_a?(Hash) |
|
196 | 202 | attachments.each_value do |attachment| |
|
197 | 203 | file = attachment['file'] |
|
198 | 204 | next unless file && file.size > 0 |
|
199 | 205 | a = Attachment.create(:container => obj, |
|
200 | 206 | :file => file, |
|
201 | 207 | :description => attachment['description'].to_s.strip, |
|
202 | 208 | :author => User.current) |
|
203 | 209 | a.new_record? ? (unsaved << a) : (attached << a) |
|
204 | 210 | end |
|
205 | 211 | if unsaved.any? |
|
206 | 212 | flash[:warning] = l(:warning_attachments_not_saved, unsaved.size) |
|
207 | 213 | end |
|
208 | 214 | end |
|
209 | 215 | attached |
|
210 | 216 | end |
|
211 | 217 | |
|
212 | 218 | # Returns the number of objects that should be displayed |
|
213 | 219 | # on the paginated list |
|
214 | 220 | def per_page_option |
|
215 | 221 | per_page = nil |
|
216 | 222 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
217 | 223 | per_page = params[:per_page].to_s.to_i |
|
218 | 224 | session[:per_page] = per_page |
|
219 | 225 | elsif session[:per_page] |
|
220 | 226 | per_page = session[:per_page] |
|
221 | 227 | else |
|
222 | 228 | per_page = Setting.per_page_options_array.first || 25 |
|
223 | 229 | end |
|
224 | 230 | per_page |
|
225 | 231 | end |
|
226 | 232 | |
|
227 | 233 | # qvalues http header parser |
|
228 | 234 | # code taken from webrick |
|
229 | 235 | def parse_qvalues(value) |
|
230 | 236 | tmp = [] |
|
231 | 237 | if value |
|
232 | 238 | parts = value.split(/,\s*/) |
|
233 | 239 | parts.each {|part| |
|
234 | 240 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
235 | 241 | val = m[1] |
|
236 | 242 | q = (m[2] or 1).to_f |
|
237 | 243 | tmp.push([val, q]) |
|
238 | 244 | end |
|
239 | 245 | } |
|
240 | 246 | tmp = tmp.sort_by{|val, q| -q} |
|
241 | 247 | tmp.collect!{|val, q| val} |
|
242 | 248 | end |
|
243 | 249 | return tmp |
|
244 | 250 | rescue |
|
245 | 251 | nil |
|
246 | 252 | end |
|
247 | 253 | |
|
248 | 254 | # Returns a string that can be used as filename value in Content-Disposition header |
|
249 | 255 | def filename_for_content_disposition(name) |
|
250 | 256 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
251 | 257 | end |
|
252 | 258 | end |
@@ -1,43 +1,49 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 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 "#{File.dirname(__FILE__)}/../test_helper" |
|
19 | 19 | |
|
20 | 20 | class AdminTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :all |
|
22 | 22 | |
|
23 | 23 | def test_add_user |
|
24 | 24 | log_user("admin", "admin") |
|
25 | 25 | get "/users/add" |
|
26 | 26 | assert_response :success |
|
27 | 27 | assert_template "users/add" |
|
28 | 28 | post "/users/add", :user => { :login => "psmith", :firstname => "Paul", :lastname => "Smith", :mail => "psmith@somenet.foo", :language => "en" }, :password => "psmith09", :password_confirmation => "psmith09" |
|
29 | 29 | |
|
30 | 30 | user = User.find_by_login("psmith") |
|
31 | 31 | assert_kind_of User, user |
|
32 | 32 | assert_redirected_to "/users/#{ user.id }/edit" |
|
33 | 33 | |
|
34 | 34 | logged_user = User.try_to_login("psmith", "psmith09") |
|
35 | 35 | assert_kind_of User, logged_user |
|
36 | 36 | assert_equal "Paul", logged_user.firstname |
|
37 | 37 | |
|
38 | 38 | post "users/edit", :id => user.id, :user => { :status => User::STATUS_LOCKED } |
|
39 | 39 | assert_redirected_to "/users/#{ user.id }/edit" |
|
40 | 40 | locked_user = User.try_to_login("psmith", "psmith09") |
|
41 | 41 | assert_equal nil, locked_user |
|
42 | 42 | end |
|
43 | ||
|
44 | test "Add a user as an anonymous user should fail" do | |
|
45 | post '/users/add', :user => { :login => 'psmith', :firstname => 'Paul'}, :password => "psmith09", :password_confirmation => "psmith09" | |
|
46 | assert_response :redirect | |
|
47 | assert_redirected_to "/login?back_url=http%3A%2F%2Fwww.example.com%2Fusers%2Fnew" | |
|
48 | end | |
|
43 | 49 | end |
General Comments 0
You need to be logged in to leave comments.
Login now