Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: require 'uri' Chris@1517: require 'cgi' Chris@1517: Chris@1517: class Unauthorized < Exception; end Chris@1517: Chris@1517: class ApplicationController < ActionController::Base Chris@1517: include Redmine::I18n Chris@1517: include Redmine::Pagination Chris@1517: include RoutesHelper Chris@1517: helper :routes Chris@1517: Chris@1517: class_attribute :accept_api_auth_actions Chris@1517: class_attribute :accept_rss_auth_actions Chris@1517: class_attribute :model_object Chris@1517: Chris@1517: layout 'base' Chris@1517: Chris@1517: protect_from_forgery Chris@1517: Chris@1517: def verify_authenticity_token Chris@1517: unless api_request? Chris@1517: super Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def handle_unverified_request Chris@1517: unless api_request? Chris@1517: super Chris@1517: cookies.delete(autologin_cookie_name) Chris@1517: self.logged_user = nil Chris@1517: render_error :status => 422, :message => "Invalid form authenticity token." Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: before_filter :session_expiration, :user_setup, :check_if_login_required, :check_password_change, :set_localization Chris@1517: Chris@1517: rescue_from ::Unauthorized, :with => :deny_access Chris@1517: rescue_from ::ActionView::MissingTemplate, :with => :missing_template Chris@1517: Chris@1517: include Redmine::Search::Controller Chris@1517: include Redmine::MenuManager::MenuController Chris@1517: helper Redmine::MenuManager::MenuHelper Chris@1517: Chris@1517: def session_expiration Chris@1517: if session[:user_id] Chris@1517: if session_expired? && !try_to_autologin Chris@1517: reset_session Chris@1517: flash[:error] = l(:error_session_expired) Chris@1517: redirect_to signin_url Chris@1517: else Chris@1517: session[:atime] = Time.now.utc.to_i Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def session_expired? Chris@1517: if Setting.session_lifetime? Chris@1517: unless session[:ctime] && (Time.now.utc.to_i - session[:ctime].to_i <= Setting.session_lifetime.to_i * 60) Chris@1517: return true Chris@1517: end Chris@1517: end Chris@1517: if Setting.session_timeout? Chris@1517: unless session[:atime] && (Time.now.utc.to_i - session[:atime].to_i <= Setting.session_timeout.to_i * 60) Chris@1517: return true Chris@1517: end Chris@1517: end Chris@1517: false Chris@1517: end Chris@1517: Chris@1517: def start_user_session(user) Chris@1517: session[:user_id] = user.id Chris@1517: session[:ctime] = Time.now.utc.to_i Chris@1517: session[:atime] = Time.now.utc.to_i Chris@1517: if user.must_change_password? Chris@1517: session[:pwd] = '1' Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def user_setup Chris@1517: # Check the settings cache for each request Chris@1517: Setting.check_cache Chris@1517: # Find the current user Chris@1517: User.current = find_current_user Chris@1517: logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger Chris@1517: end Chris@1517: Chris@1517: # Returns the current user or nil if no user is logged in Chris@1517: # and starts a session if needed Chris@1517: def find_current_user Chris@1517: user = nil Chris@1517: unless api_request? Chris@1517: if session[:user_id] Chris@1517: # existing session Chris@1517: user = (User.active.find(session[:user_id]) rescue nil) Chris@1517: elsif autologin_user = try_to_autologin Chris@1517: user = autologin_user Chris@1517: elsif params[:format] == 'atom' && params[:key] && request.get? && accept_rss_auth? Chris@1517: # RSS key authentication does not start a session Chris@1517: user = User.find_by_rss_key(params[:key]) Chris@1517: end Chris@1517: end Chris@1517: if user.nil? && Setting.rest_api_enabled? && accept_api_auth? Chris@1517: if (key = api_key_from_request) Chris@1517: # Use API key Chris@1517: user = User.find_by_api_key(key) Chris@1517: elsif request.authorization.to_s =~ /\ABasic /i Chris@1517: # HTTP Basic, either username/password or API key/random Chris@1517: authenticate_with_http_basic do |username, password| Chris@1517: user = User.try_to_login(username, password) || User.find_by_api_key(username) Chris@1517: end Chris@1517: if user && user.must_change_password? Chris@1517: render_error :message => 'You must change your password', :status => 403 Chris@1517: return Chris@1517: end Chris@1517: end Chris@1517: # Switch user if requested by an admin user Chris@1517: if user && user.admin? && (username = api_switch_user_from_request) Chris@1517: su = User.find_by_login(username) Chris@1517: if su && su.active? Chris@1517: logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger Chris@1517: user = su Chris@1517: else Chris@1517: render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: user Chris@1517: end Chris@1517: Chris@1517: def autologin_cookie_name Chris@1517: Redmine::Configuration['autologin_cookie_name'].presence || 'autologin' Chris@1517: end Chris@1517: Chris@1517: def try_to_autologin Chris@1517: if cookies[autologin_cookie_name] && Setting.autologin? Chris@1517: # auto-login feature starts a new session Chris@1517: user = User.try_to_autologin(cookies[autologin_cookie_name]) Chris@1517: if user Chris@1517: reset_session Chris@1517: start_user_session(user) Chris@1517: end Chris@1517: user Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Sets the logged in user Chris@1517: def logged_user=(user) Chris@1517: reset_session Chris@1517: if user && user.is_a?(User) Chris@1517: User.current = user Chris@1517: start_user_session(user) Chris@1517: else Chris@1517: User.current = User.anonymous Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Logs out current user Chris@1517: def logout_user Chris@1517: if User.current.logged? Chris@1517: cookies.delete(autologin_cookie_name) Chris@1517: Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin']) Chris@1517: self.logged_user = nil Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # check if login is globally required to access the application Chris@1517: def check_if_login_required Chris@1517: # no check needed if user is already logged in Chris@1517: return true if User.current.logged? Chris@1517: require_login if Setting.login_required? Chris@1517: end Chris@1517: Chris@1517: def check_password_change Chris@1517: if session[:pwd] Chris@1517: if User.current.must_change_password? Chris@1517: redirect_to my_password_path Chris@1517: else Chris@1517: session.delete(:pwd) Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def set_localization Chris@1517: lang = nil Chris@1517: if User.current.logged? Chris@1517: lang = find_language(User.current.language) Chris@1517: end Chris@1517: if lang.nil? && !Setting.force_default_language_for_anonymous? && request.env['HTTP_ACCEPT_LANGUAGE'] Chris@1517: accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first Chris@1517: if !accept_lang.blank? Chris@1517: accept_lang = accept_lang.downcase Chris@1517: lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) Chris@1517: end Chris@1517: end Chris@1517: lang ||= Setting.default_language Chris@1517: set_language_if_valid(lang) Chris@1517: end Chris@1517: Chris@1517: def require_login Chris@1517: if !User.current.logged? Chris@1517: # Extract only the basic url parameters on non-GET requests Chris@1517: if request.get? Chris@1517: url = url_for(params) Chris@1517: else Chris@1517: url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) Chris@1517: end Chris@1517: respond_to do |format| Chris@1517: format.html { Chris@1517: if request.xhr? Chris@1517: head :unauthorized Chris@1517: else Chris@1517: redirect_to :controller => "account", :action => "login", :back_url => url Chris@1517: end Chris@1517: } Chris@1517: format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } Chris@1517: format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } Chris@1517: format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } Chris@1517: format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } Chris@1517: end Chris@1517: return false Chris@1517: end Chris@1517: true Chris@1517: end Chris@1517: Chris@1517: def require_admin Chris@1517: return unless require_login Chris@1517: if !User.current.admin? Chris@1517: render_403 Chris@1517: return false Chris@1517: end Chris@1517: true Chris@1517: end Chris@1517: Chris@1517: def deny_access Chris@1517: User.current.logged? ? render_403 : require_login Chris@1517: end Chris@1517: Chris@1517: # Authorize the user for the requested action Chris@1517: def authorize(ctrl = params[:controller], action = params[:action], global = false) Chris@1517: allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) Chris@1517: if allowed Chris@1517: true Chris@1517: else Chris@1517: if @project && @project.archived? Chris@1517: render_403 :message => :notice_not_authorized_archived_project Chris@1517: else Chris@1517: deny_access Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Authorize the user for the requested action outside a project Chris@1517: def authorize_global(ctrl = params[:controller], action = params[:action], global = true) Chris@1517: authorize(ctrl, action, global) Chris@1517: end Chris@1517: Chris@1517: # Find project of id params[:id] Chris@1517: def find_project Chris@1517: @project = Project.find(params[:id]) Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: # Find project of id params[:project_id] Chris@1517: def find_project_by_project_id Chris@1517: @project = Project.find(params[:project_id]) Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: # Find a project based on params[:project_id] Chris@1517: # TODO: some subclasses override this, see about merging their logic Chris@1517: def find_optional_project Chris@1517: @project = Project.find(params[:project_id]) unless params[:project_id].blank? Chris@1517: allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) Chris@1517: allowed ? true : deny_access Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: # Finds and sets @project based on @object.project Chris@1517: def find_project_from_association Chris@1517: render_404 unless @object.present? Chris@1517: Chris@1517: @project = @object.project Chris@1517: end Chris@1517: Chris@1517: def find_model_object Chris@1517: model = self.class.model_object Chris@1517: if model Chris@1517: @object = model.find(params[:id]) Chris@1517: self.instance_variable_set('@' + controller_name.singularize, @object) if @object Chris@1517: end Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: def self.model_object(model) Chris@1517: self.model_object = model Chris@1517: end Chris@1517: Chris@1517: # Find the issue whose id is the :id parameter Chris@1517: # Raises a Unauthorized exception if the issue is not visible Chris@1517: def find_issue Chris@1517: # Issue.visible.find(...) can not be used to redirect user to the login form Chris@1517: # if the issue actually exists but requires authentication Chris@1517: @issue = Issue.find(params[:id]) Chris@1517: raise Unauthorized unless @issue.visible? Chris@1517: @project = @issue.project Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: # Find issues with a single :id param or :ids array param Chris@1517: # Raises a Unauthorized exception if one of the issues is not visible Chris@1517: def find_issues Chris@1517: @issues = Issue.where(:id => (params[:id] || params[:ids])).preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to).to_a Chris@1517: raise ActiveRecord::RecordNotFound if @issues.empty? Chris@1517: raise Unauthorized unless @issues.all?(&:visible?) Chris@1517: @projects = @issues.collect(&:project).compact.uniq Chris@1517: @project = @projects.first if @projects.size == 1 Chris@1517: rescue ActiveRecord::RecordNotFound Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: def find_attachments Chris@1517: if (attachments = params[:attachments]).present? Chris@1517: att = attachments.values.collect do |attachment| Chris@1517: Attachment.find_by_token( attachment[:token] ) if attachment[:token].present? Chris@1517: end Chris@1517: att.compact! Chris@1517: end Chris@1517: @attachments = att || [] Chris@1517: end Chris@1517: Chris@1517: # make sure that the user is a member of the project (or admin) if project is private Chris@1517: # used as a before_filter for actions that do not require any particular permission on the project Chris@1517: def check_project_privacy Chris@1517: if @project && !@project.archived? Chris@1517: if @project.visible? Chris@1517: true Chris@1517: else Chris@1517: deny_access Chris@1517: end Chris@1517: else Chris@1517: @project = nil Chris@1517: render_404 Chris@1517: false Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def back_url Chris@1517: url = params[:back_url] Chris@1517: if url.nil? && referer = request.env['HTTP_REFERER'] Chris@1517: url = CGI.unescape(referer.to_s) Chris@1517: end Chris@1517: url Chris@1517: end Chris@1517: Chris@1517: def redirect_back_or_default(default, options={}) Chris@1517: back_url = params[:back_url].to_s Chris@1517: if back_url.present? && valid_back_url?(back_url) Chris@1517: redirect_to(back_url) Chris@1517: return Chris@1517: elsif options[:referer] Chris@1517: redirect_to_referer_or default Chris@1517: return Chris@1517: end Chris@1517: redirect_to default Chris@1517: false Chris@1517: end Chris@1517: Chris@1517: # Returns true if back_url is a valid url for redirection, otherwise false Chris@1517: def valid_back_url?(back_url) Chris@1517: if CGI.unescape(back_url).include?('..') Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: begin Chris@1517: uri = URI.parse(back_url) Chris@1517: rescue URI::InvalidURIError Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: if uri.host.present? && uri.host != request.host Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: if uri.path.match(%r{/(login|account/register)}) Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: if relative_url_root.present? && !uri.path.starts_with?(relative_url_root) Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: return true Chris@1517: end Chris@1517: private :valid_back_url? Chris@1517: Chris@1517: # Redirects to the request referer if present, redirects to args or call block otherwise. Chris@1517: def redirect_to_referer_or(*args, &block) Chris@1517: redirect_to :back Chris@1517: rescue ::ActionController::RedirectBackError Chris@1517: if args.any? Chris@1517: redirect_to *args Chris@1517: elsif block_given? Chris@1517: block.call Chris@1517: else Chris@1517: raise "#redirect_to_referer_or takes arguments or a block" Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def render_403(options={}) Chris@1517: @project = nil Chris@1517: render_error({:message => :notice_not_authorized, :status => 403}.merge(options)) Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: def render_404(options={}) Chris@1517: render_error({:message => :notice_file_not_found, :status => 404}.merge(options)) Chris@1517: return false Chris@1517: end Chris@1517: Chris@1517: # Renders an error response Chris@1517: def render_error(arg) Chris@1517: arg = {:message => arg} unless arg.is_a?(Hash) Chris@1517: Chris@1517: @message = arg[:message] Chris@1517: @message = l(@message) if @message.is_a?(Symbol) Chris@1517: @status = arg[:status] || 500 Chris@1517: Chris@1517: respond_to do |format| Chris@1517: format.html { Chris@1517: render :template => 'common/error', :layout => use_layout, :status => @status Chris@1517: } Chris@1517: format.any { head @status } Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Handler for ActionView::MissingTemplate exception Chris@1517: def missing_template Chris@1517: logger.warn "Missing template, responding with 404" Chris@1517: @project = nil Chris@1517: render_404 Chris@1517: end Chris@1517: Chris@1517: # Filter for actions that provide an API response Chris@1517: # but have no HTML representation for non admin users Chris@1517: def require_admin_or_api_request Chris@1517: return true if api_request? Chris@1517: if User.current.admin? Chris@1517: true Chris@1517: elsif User.current.logged? Chris@1517: render_error(:status => 406) Chris@1517: else Chris@1517: deny_access Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Picks which layout to use based on the request Chris@1517: # Chris@1517: # @return [boolean, string] name of the layout to use or false for no layout Chris@1517: def use_layout Chris@1517: request.xhr? ? false : 'base' Chris@1517: end Chris@1517: Chris@1517: def render_feed(items, options={}) Chris@1517: @items = items || [] Chris@1517: @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } Chris@1517: @items = @items.slice(0, Setting.feeds_limit.to_i) Chris@1517: @title = options[:title] || Setting.app_title Chris@1517: render :template => "common/feed", :formats => [:atom], :layout => false, Chris@1517: :content_type => 'application/atom+xml' Chris@1517: end Chris@1517: Chris@1517: def self.accept_rss_auth(*actions) Chris@1517: if actions.any? Chris@1517: self.accept_rss_auth_actions = actions Chris@1517: else Chris@1517: self.accept_rss_auth_actions || [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def accept_rss_auth?(action=action_name) Chris@1517: self.class.accept_rss_auth.include?(action.to_sym) Chris@1517: end Chris@1517: Chris@1517: def self.accept_api_auth(*actions) Chris@1517: if actions.any? Chris@1517: self.accept_api_auth_actions = actions Chris@1517: else Chris@1517: self.accept_api_auth_actions || [] Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def accept_api_auth?(action=action_name) Chris@1517: self.class.accept_api_auth.include?(action.to_sym) Chris@1517: end Chris@1517: Chris@1517: # Returns the number of objects that should be displayed Chris@1517: # on the paginated list Chris@1517: def per_page_option Chris@1517: per_page = nil Chris@1517: if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) Chris@1517: per_page = params[:per_page].to_s.to_i Chris@1517: session[:per_page] = per_page Chris@1517: elsif session[:per_page] Chris@1517: per_page = session[:per_page] Chris@1517: else Chris@1517: per_page = Setting.per_page_options_array.first || 25 Chris@1517: end Chris@1517: per_page Chris@1517: end Chris@1517: Chris@1517: # Returns offset and limit used to retrieve objects Chris@1517: # for an API response based on offset, limit and page parameters Chris@1517: def api_offset_and_limit(options=params) Chris@1517: if options[:offset].present? Chris@1517: offset = options[:offset].to_i Chris@1517: if offset < 0 Chris@1517: offset = 0 Chris@1517: end Chris@1517: end Chris@1517: limit = options[:limit].to_i Chris@1517: if limit < 1 Chris@1517: limit = 25 Chris@1517: elsif limit > 100 Chris@1517: limit = 100 Chris@1517: end Chris@1517: if offset.nil? && options[:page].present? Chris@1517: offset = (options[:page].to_i - 1) * limit Chris@1517: offset = 0 if offset < 0 Chris@1517: end Chris@1517: offset ||= 0 Chris@1517: Chris@1517: [offset, limit] Chris@1517: end Chris@1517: Chris@1517: # qvalues http header parser Chris@1517: # code taken from webrick Chris@1517: def parse_qvalues(value) Chris@1517: tmp = [] Chris@1517: if value Chris@1517: parts = value.split(/,\s*/) Chris@1517: parts.each {|part| Chris@1517: if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) Chris@1517: val = m[1] Chris@1517: q = (m[2] or 1).to_f Chris@1517: tmp.push([val, q]) Chris@1517: end Chris@1517: } Chris@1517: tmp = tmp.sort_by{|val, q| -q} Chris@1517: tmp.collect!{|val, q| val} Chris@1517: end Chris@1517: return tmp Chris@1517: rescue Chris@1517: nil Chris@1517: end Chris@1517: Chris@1517: # Returns a string that can be used as filename value in Content-Disposition header Chris@1517: def filename_for_content_disposition(name) Chris@1517: request.env['HTTP_USER_AGENT'] =~ %r{(MSIE|Trident)} ? ERB::Util.url_encode(name) : name Chris@1517: end Chris@1517: Chris@1517: def api_request? Chris@1517: %w(xml json).include? params[:format] Chris@1517: end Chris@1517: Chris@1517: # Returns the API key present in the request Chris@1517: def api_key_from_request Chris@1517: if params[:key].present? Chris@1517: params[:key].to_s Chris@1517: elsif request.headers["X-Redmine-API-Key"].present? Chris@1517: request.headers["X-Redmine-API-Key"].to_s Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Returns the API 'switch user' value if present Chris@1517: def api_switch_user_from_request Chris@1517: request.headers["X-Redmine-Switch-User"].to_s.presence Chris@1517: end Chris@1517: Chris@1517: # Renders a warning flash if obj has unsaved attachments Chris@1517: def render_attachment_warning_if_needed(obj) Chris@1517: flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? Chris@1517: end Chris@1517: Chris@1517: # Rescues an invalid query statement. Just in case... Chris@1517: def query_statement_invalid(exception) Chris@1517: logger.error "Query::StatementInvalid: #{exception.message}" if logger Chris@1517: session.delete(:query) Chris@1517: sort_clear if respond_to?(:sort_clear) Chris@1517: render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." Chris@1517: end Chris@1517: Chris@1517: # Renders a 200 response for successfull updates or deletions via the API Chris@1517: def render_api_ok Chris@1517: render_api_head :ok Chris@1517: end Chris@1517: Chris@1517: # Renders a head API response Chris@1517: def render_api_head(status) Chris@1517: # #head would return a response body with one space Chris@1517: render :text => '', :status => status, :layout => nil Chris@1517: end Chris@1517: Chris@1517: # Renders API response on validation failure Chris@1517: def render_validation_errors(objects) Chris@1517: if objects.is_a?(Array) Chris@1517: @error_messages = objects.map {|object| object.errors.full_messages}.flatten Chris@1517: else Chris@1517: @error_messages = objects.errors.full_messages Chris@1517: end Chris@1517: render :template => 'common/error_messages.api', :status => :unprocessable_entity, :layout => nil Chris@1517: end Chris@1517: Chris@1517: # Overrides #_include_layout? so that #render with no arguments Chris@1517: # doesn't use the layout for api requests Chris@1517: def _include_layout?(*args) Chris@1517: api_request? ? false : super Chris@1517: end Chris@1517: end