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 22KB

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