You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application_controller.rb 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'uri'
  18. require 'cgi'
  19. class Unauthorized < Exception; end
  20. class ApplicationController < ActionController::Base
  21. include Redmine::I18n
  22. include Redmine::Pagination
  23. include Redmine::Hook::Helper
  24. include RoutesHelper
  25. helper :routes
  26. class_attribute :accept_api_auth_actions
  27. class_attribute :accept_rss_auth_actions
  28. class_attribute :model_object
  29. layout 'base'
  30. protect_from_forgery
  31. def verify_authenticity_token
  32. unless api_request?
  33. super
  34. end
  35. end
  36. def handle_unverified_request
  37. unless api_request?
  38. super
  39. cookies.delete(autologin_cookie_name)
  40. self.logged_user = nil
  41. set_localization
  42. render_error :status => 422, :message => "Invalid form authenticity token."
  43. end
  44. end
  45. before_action :session_expiration, :user_setup, :check_if_login_required, :set_localization, :check_password_change
  46. rescue_from ::Unauthorized, :with => :deny_access
  47. rescue_from ::ActionView::MissingTemplate, :with => :missing_template
  48. include Redmine::Search::Controller
  49. include Redmine::MenuManager::MenuController
  50. helper Redmine::MenuManager::MenuHelper
  51. include Redmine::SudoMode::Controller
  52. def session_expiration
  53. if session[:user_id] && Rails.application.config.redmine_verify_sessions != false
  54. if session_expired? && !try_to_autologin
  55. set_localization(User.active.find_by_id(session[:user_id]))
  56. self.logged_user = nil
  57. flash[:error] = l(:error_session_expired)
  58. require_login
  59. end
  60. end
  61. end
  62. def session_expired?
  63. ! User.verify_session_token(session[:user_id], session[:tk])
  64. end
  65. def start_user_session(user)
  66. session[:user_id] = user.id
  67. session[:tk] = user.generate_session_token
  68. if user.must_change_password?
  69. session[:pwd] = '1'
  70. end
  71. end
  72. def user_setup
  73. # Check the settings cache for each request
  74. Setting.check_cache
  75. # Find the current user
  76. User.current = find_current_user
  77. logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger
  78. end
  79. # Returns the current user or nil if no user is logged in
  80. # and starts a session if needed
  81. def find_current_user
  82. user = nil
  83. unless api_request?
  84. if session[:user_id]
  85. # existing session
  86. user = (User.active.find(session[:user_id]) rescue nil)
  87. elsif autologin_user = try_to_autologin
  88. user = autologin_user
  89. elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth?
  90. # RSS key authentication does not start a session
  91. user = User.find_by_rss_key(params[:key])
  92. end
  93. end
  94. if user.nil? && Setting.rest_api_enabled? && accept_api_auth?
  95. if (key = api_key_from_request)
  96. # Use API key
  97. user = User.find_by_api_key(key)
  98. elsif request.authorization.to_s =~ /\ABasic /i
  99. # HTTP Basic, either username/password or API key/random
  100. authenticate_with_http_basic do |username, password|
  101. user = User.try_to_login(username, password) || User.find_by_api_key(username)
  102. end
  103. if user && user.must_change_password?
  104. render_error :message => 'You must change your password', :status => 403
  105. return
  106. end
  107. end
  108. # Switch user if requested by an admin user
  109. if user && user.admin? && (username = api_switch_user_from_request)
  110. su = User.find_by_login(username)
  111. if su && su.active?
  112. logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger
  113. user = su
  114. else
  115. render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412
  116. end
  117. end
  118. end
  119. # store current ip address in user object ephemerally
  120. user.remote_ip = request.remote_ip if user
  121. user
  122. end
  123. def autologin_cookie_name
  124. Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
  125. end
  126. def try_to_autologin
  127. if cookies[autologin_cookie_name] && Setting.autologin?
  128. # auto-login feature starts a new session
  129. user = User.try_to_autologin(cookies[autologin_cookie_name])
  130. if user
  131. reset_session
  132. start_user_session(user)
  133. end
  134. user
  135. end
  136. end
  137. # Sets the logged in user
  138. def logged_user=(user)
  139. reset_session
  140. if user && user.is_a?(User)
  141. User.current = user
  142. start_user_session(user)
  143. else
  144. User.current = User.anonymous
  145. end
  146. end
  147. # Logs out current user
  148. def logout_user
  149. if User.current.logged?
  150. cookies.delete(autologin_cookie_name)
  151. Token.where(["user_id = ? AND action = ?", User.current.id, 'autologin']).delete_all
  152. Token.where(["user_id = ? AND action = ? AND value = ?", User.current.id, 'session', session[:tk]]).delete_all
  153. self.logged_user = nil
  154. end
  155. end
  156. # check if login is globally required to access the application
  157. def check_if_login_required
  158. # no check needed if user is already logged in
  159. return true if User.current.logged?
  160. require_login if Setting.login_required?
  161. end
  162. def check_password_change
  163. if session[:pwd]
  164. if User.current.must_change_password?
  165. flash[:error] = l(:error_password_expired)
  166. redirect_to my_password_path
  167. else
  168. session.delete(:pwd)
  169. end
  170. end
  171. end
  172. def set_localization(user=User.current)
  173. lang = nil
  174. if user && user.logged?
  175. lang = find_language(user.language)
  176. end
  177. if lang.nil? && !Setting.force_default_language_for_anonymous? && request.env['HTTP_ACCEPT_LANGUAGE']
  178. accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first
  179. if !accept_lang.blank?
  180. accept_lang = accept_lang.downcase
  181. lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
  182. end
  183. end
  184. lang ||= Setting.default_language
  185. set_language_if_valid(lang)
  186. end
  187. def require_login
  188. if !User.current.logged?
  189. # Extract only the basic url parameters on non-GET requests
  190. if request.get?
  191. url = request.original_url
  192. else
  193. url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id])
  194. end
  195. respond_to do |format|
  196. format.html {
  197. if request.xhr?
  198. head :unauthorized
  199. else
  200. redirect_to signin_path(:back_url => url)
  201. end
  202. }
  203. format.any(:atom, :pdf, :csv) {
  204. redirect_to signin_path(:back_url => url)
  205. }
  206. format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  207. format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  208. format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' }
  209. format.any { head :unauthorized }
  210. end
  211. return false
  212. end
  213. true
  214. end
  215. def require_admin
  216. return unless require_login
  217. if !User.current.admin?
  218. render_403
  219. return false
  220. end
  221. true
  222. end
  223. def deny_access
  224. User.current.logged? ? render_403 : require_login
  225. end
  226. # Authorize the user for the requested action
  227. def authorize(ctrl = params[:controller], action = params[:action], global = false)
  228. allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
  229. if allowed
  230. true
  231. else
  232. if @project && @project.archived?
  233. render_403 :message => :notice_not_authorized_archived_project
  234. else
  235. deny_access
  236. end
  237. end
  238. end
  239. # Authorize the user for the requested action outside a project
  240. def authorize_global(ctrl = params[:controller], action = params[:action], global = true)
  241. authorize(ctrl, action, global)
  242. end
  243. # Find project of id params[:id]
  244. def find_project
  245. @project = Project.find(params[:id])
  246. rescue ActiveRecord::RecordNotFound
  247. render_404
  248. end
  249. # Find project of id params[:project_id]
  250. def find_project_by_project_id
  251. @project = Project.find(params[:project_id])
  252. rescue ActiveRecord::RecordNotFound
  253. render_404
  254. end
  255. # Find a project based on params[:project_id]
  256. # TODO: some subclasses override this, see about merging their logic
  257. def find_optional_project
  258. @project = Project.find(params[:project_id]) unless params[:project_id].blank?
  259. allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
  260. allowed ? true : deny_access
  261. rescue ActiveRecord::RecordNotFound
  262. render_404
  263. end
  264. # Finds and sets @project based on @object.project
  265. def find_project_from_association
  266. render_404 unless @object.present?
  267. @project = @object.project
  268. end
  269. def find_model_object
  270. model = self.class.model_object
  271. if model
  272. @object = model.find(params[:id])
  273. self.instance_variable_set('@' + controller_name.singularize, @object) if @object
  274. end
  275. rescue ActiveRecord::RecordNotFound
  276. render_404
  277. end
  278. def self.model_object(model)
  279. self.model_object = model
  280. end
  281. # Find the issue whose id is the :id parameter
  282. # Raises a Unauthorized exception if the issue is not visible
  283. def find_issue
  284. # Issue.visible.find(...) can not be used to redirect user to the login form
  285. # if the issue actually exists but requires authentication
  286. @issue = Issue.find(params[:id])
  287. raise Unauthorized unless @issue.visible?
  288. @project = @issue.project
  289. rescue ActiveRecord::RecordNotFound
  290. render_404
  291. end
  292. # Find issues with a single :id param or :ids array param
  293. # Raises a Unauthorized exception if one of the issues is not visible
  294. def find_issues
  295. @issues = Issue.
  296. where(:id => (params[:id] || params[:ids])).
  297. preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to, {:custom_values => :custom_field}).
  298. to_a
  299. raise ActiveRecord::RecordNotFound if @issues.empty?
  300. raise Unauthorized unless @issues.all?(&:visible?)
  301. @projects = @issues.collect(&:project).compact.uniq
  302. @project = @projects.first if @projects.size == 1
  303. rescue ActiveRecord::RecordNotFound
  304. render_404
  305. end
  306. def find_attachments
  307. if (attachments = params[:attachments]).present?
  308. att = attachments.values.collect do |attachment|
  309. Attachment.find_by_token( attachment[:token] ) if attachment[:token].present?
  310. end
  311. att.compact!
  312. end
  313. @attachments = att || []
  314. end
  315. def parse_params_for_bulk_update(params)
  316. attributes = (params || {}).reject {|k,v| v.blank?}
  317. attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
  318. if custom = attributes[:custom_field_values]
  319. custom.reject! {|k,v| v.blank?}
  320. custom.keys.each do |k|
  321. if custom[k].is_a?(Array)
  322. custom[k] << '' if custom[k].delete('__none__')
  323. else
  324. custom[k] = '' if custom[k] == '__none__'
  325. end
  326. end
  327. end
  328. attributes
  329. end
  330. # make sure that the user is a member of the project (or admin) if project is private
  331. # used as a before_action for actions that do not require any particular permission on the project
  332. def check_project_privacy
  333. if @project && !@project.archived?
  334. if @project.visible?
  335. true
  336. else
  337. deny_access
  338. end
  339. else
  340. @project = nil
  341. render_404
  342. false
  343. end
  344. end
  345. def back_url
  346. url = params[:back_url]
  347. if url.nil? && referer = request.env['HTTP_REFERER']
  348. url = CGI.unescape(referer.to_s)
  349. end
  350. url
  351. end
  352. def redirect_back_or_default(default, options={})
  353. back_url = params[:back_url].to_s
  354. if back_url.present? && valid_url = validate_back_url(back_url)
  355. redirect_to(valid_url)
  356. return
  357. elsif options[:referer]
  358. redirect_to_referer_or default
  359. return
  360. end
  361. redirect_to default
  362. false
  363. end
  364. # Returns a validated URL string if back_url is a valid url for redirection,
  365. # otherwise false
  366. def validate_back_url(back_url)
  367. if CGI.unescape(back_url).include?('..')
  368. return false
  369. end
  370. begin
  371. uri = URI.parse(back_url)
  372. rescue URI::InvalidURIError
  373. return false
  374. end
  375. [:scheme, :host, :port].each do |component|
  376. if uri.send(component).present? && uri.send(component) != request.send(component)
  377. return false
  378. end
  379. uri.send(:"#{component}=", nil)
  380. end
  381. # Always ignore basic user:password in the URL
  382. uri.userinfo = nil
  383. path = uri.to_s
  384. # Ensure that the remaining URL starts with a slash, followed by a
  385. # non-slash character or the end
  386. if path !~ %r{\A/([^/]|\z)}
  387. return false
  388. end
  389. if path.match(%r{/(login|account/register|account/lost_password)})
  390. return false
  391. end
  392. if relative_url_root.present? && !path.starts_with?(relative_url_root)
  393. return false
  394. end
  395. return path
  396. end
  397. private :validate_back_url
  398. def valid_back_url?(back_url)
  399. !!validate_back_url(back_url)
  400. end
  401. private :valid_back_url?
  402. # Redirects to the request referer if present, redirects to args or call block otherwise.
  403. def redirect_to_referer_or(*args, &block)
  404. redirect_to :back
  405. rescue ::ActionController::RedirectBackError
  406. if args.any?
  407. redirect_to *args
  408. elsif block_given?
  409. block.call
  410. else
  411. raise "#redirect_to_referer_or takes arguments or a block"
  412. end
  413. end
  414. def render_403(options={})
  415. @project = nil
  416. render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
  417. return false
  418. end
  419. def render_404(options={})
  420. render_error({:message => :notice_file_not_found, :status => 404}.merge(options))
  421. return false
  422. end
  423. # Renders an error response
  424. def render_error(arg)
  425. arg = {:message => arg} unless arg.is_a?(Hash)
  426. @message = arg[:message]
  427. @message = l(@message) if @message.is_a?(Symbol)
  428. @status = arg[:status] || 500
  429. respond_to do |format|
  430. format.html {
  431. render :template => 'common/error', :layout => use_layout, :status => @status
  432. }
  433. format.any { head @status }
  434. end
  435. end
  436. # Handler for ActionView::MissingTemplate exception
  437. def missing_template
  438. logger.warn "Missing template, responding with 404"
  439. @project = nil
  440. render_404
  441. end
  442. # Filter for actions that provide an API response
  443. # but have no HTML representation for non admin users
  444. def require_admin_or_api_request
  445. return true if api_request?
  446. if User.current.admin?
  447. true
  448. elsif User.current.logged?
  449. render_error(:status => 406)
  450. else
  451. deny_access
  452. end
  453. end
  454. # Picks which layout to use based on the request
  455. #
  456. # @return [boolean, string] name of the layout to use or false for no layout
  457. def use_layout
  458. request.xhr? ? false : 'base'
  459. end
  460. def render_feed(items, options={})
  461. @items = (items || []).to_a
  462. @items.sort! {|x,y| y.event_datetime <=> x.event_datetime }
  463. @items = @items.slice(0, Setting.feeds_limit.to_i)
  464. @title = options[:title] || Setting.app_title
  465. render :template => "common/feed", :formats => [:atom], :layout => false,
  466. :content_type => 'application/atom+xml'
  467. end
  468. def self.accept_rss_auth(*actions)
  469. if actions.any?
  470. self.accept_rss_auth_actions = actions
  471. else
  472. self.accept_rss_auth_actions || []
  473. end
  474. end
  475. def accept_rss_auth?(action=action_name)
  476. self.class.accept_rss_auth.include?(action.to_sym)
  477. end
  478. def self.accept_api_auth(*actions)
  479. if actions.any?
  480. self.accept_api_auth_actions = actions
  481. else
  482. self.accept_api_auth_actions || []
  483. end
  484. end
  485. def accept_api_auth?(action=action_name)
  486. self.class.accept_api_auth.include?(action.to_sym)
  487. end
  488. # Returns the number of objects that should be displayed
  489. # on the paginated list
  490. def per_page_option
  491. per_page = nil
  492. if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i)
  493. per_page = params[:per_page].to_s.to_i
  494. session[:per_page] = per_page
  495. elsif session[:per_page]
  496. per_page = session[:per_page]
  497. else
  498. per_page = Setting.per_page_options_array.first || 25
  499. end
  500. per_page
  501. end
  502. # Returns offset and limit used to retrieve objects
  503. # for an API response based on offset, limit and page parameters
  504. def api_offset_and_limit(options=params)
  505. if options[:offset].present?
  506. offset = options[:offset].to_i
  507. if offset < 0
  508. offset = 0
  509. end
  510. end
  511. limit = options[:limit].to_i
  512. if limit < 1
  513. limit = 25
  514. elsif limit > 100
  515. limit = 100
  516. end
  517. if offset.nil? && options[:page].present?
  518. offset = (options[:page].to_i - 1) * limit
  519. offset = 0 if offset < 0
  520. end
  521. offset ||= 0
  522. [offset, limit]
  523. end
  524. # qvalues http header parser
  525. # code taken from webrick
  526. def parse_qvalues(value)
  527. tmp = []
  528. if value
  529. parts = value.split(/,\s*/)
  530. parts.each {|part|
  531. if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
  532. val = m[1]
  533. q = (m[2] or 1).to_f
  534. tmp.push([val, q])
  535. end
  536. }
  537. tmp = tmp.sort_by{|val, q| -q}
  538. tmp.collect!{|val, q| val}
  539. end
  540. return tmp
  541. rescue
  542. nil
  543. end
  544. # Returns a string that can be used as filename value in Content-Disposition header
  545. def filename_for_content_disposition(name)
  546. request.env['HTTP_USER_AGENT'] =~ %r{(MSIE|Trident|Edge)} ? ERB::Util.url_encode(name) : name
  547. end
  548. def api_request?
  549. %w(xml json).include? params[:format]
  550. end
  551. # Returns the API key present in the request
  552. def api_key_from_request
  553. if params[:key].present?
  554. params[:key].to_s
  555. elsif request.headers["X-Redmine-API-Key"].present?
  556. request.headers["X-Redmine-API-Key"].to_s
  557. end
  558. end
  559. # Returns the API 'switch user' value if present
  560. def api_switch_user_from_request
  561. request.headers["X-Redmine-Switch-User"].to_s.presence
  562. end
  563. # Renders a warning flash if obj has unsaved attachments
  564. def render_attachment_warning_if_needed(obj)
  565. flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
  566. end
  567. # Rescues an invalid query statement. Just in case...
  568. def query_statement_invalid(exception)
  569. logger.error "Query::StatementInvalid: #{exception.message}" if logger
  570. session.delete(:query)
  571. sort_clear if respond_to?(:sort_clear)
  572. render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  573. end
  574. # Renders a 200 response for successfull updates or deletions via the API
  575. def render_api_ok
  576. render_api_head :ok
  577. end
  578. # Renders a head API response
  579. def render_api_head(status)
  580. head :status => status
  581. end
  582. # Renders API response on validation failure
  583. # for an object or an array of objects
  584. def render_validation_errors(objects)
  585. messages = Array.wrap(objects).map {|object| object.errors.full_messages}.flatten
  586. render_api_errors(messages)
  587. end
  588. def render_api_errors(*messages)
  589. @error_messages = messages.flatten
  590. render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil
  591. end
  592. # Overrides #_include_layout? so that #render with no arguments
  593. # doesn't use the layout for api requests
  594. def _include_layout?(*args)
  595. api_request? ? false : super
  596. end
  597. end