Class: ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- ApplicationController
- Includes:
- AvatarsHelper, IconsHelper, Redmine::Hook::Helper, Redmine::I18n, Redmine::MenuManager::MenuController, Redmine::Pagination, Redmine::Search::Controller, Redmine::SudoMode::Controller, RoutesHelper
- Defined in:
- app/controllers/application_controller.rb
Direct Known Subclasses
AccountController, ActivitiesController, AdminController, AttachmentsController, AuthSourcesController, AutoCompletesController, BoardsController, CalendarsController, CommentsController, ContextMenusController, CustomFieldEnumerationsController, CustomFieldsController, DocumentsController, EmailAddressesController, EnumerationsController, FilesController, GanttsController, GroupsController, HelpController, ImportsController, IssueCategoriesController, IssueRelationsController, IssueStatusesController, IssuesController, JournalsController, MembersController, MessagesController, MyController, NewsController, PreviewsController, PrincipalMembershipsController, ProjectEnumerationsController, ProjectsController, QueriesController, ReactionsController, ReportsController, RepositoriesController, RolesController, SearchController, SettingsController, TimelogController, TrackersController, TwofaBackupCodesController, TwofaController, UsersController, VersionsController, WatchersController, WelcomeController, WikiController, WikisController, WorkflowsController
Constant Summary
Constants included from IconsHelper
IconsHelper::DEFAULT_ICON_SIZE, IconsHelper::DEFAULT_SPRITE
Class Method Summary collapse
- .accept_api_auth(*actions) ⇒ Object
- .accept_atom_auth(*actions) ⇒ Object
- .model_object(model) ⇒ Object
Instance Method Summary collapse
-
#_include_layout?(*args) ⇒ Boolean
Overrides #_include_layout? so that #render with no arguments doesn’t use the layout for api requests.
- #accept_api_auth?(action = action_name) ⇒ Boolean
- #accept_atom_auth?(action = action_name) ⇒ Boolean
-
#api_key_from_request ⇒ Object
Returns the API key present in the request.
-
#api_offset_and_limit(options = params) ⇒ Object
Returns offset and limit used to retrieve objects for an API response based on offset, limit and page parameters.
- #api_request? ⇒ Boolean
-
#api_switch_user_from_request ⇒ Object
Returns the API ‘switch user’ value if present.
-
#authorize(ctrl = , action = , global = false) ⇒ Object
Authorize the user for the requested action.
-
#authorize_global(ctrl = , action = , global = true) ⇒ Object
Authorize the user for the requested action outside a project.
- #autologin_cookie_name ⇒ Object
- #back_url ⇒ Object
-
#check_if_login_required ⇒ Object
check if login is globally required to access the application.
- #check_password_change ⇒ Object
-
#check_project_privacy ⇒ Object
make sure that the user is a member of the project (or admin) if project is private used as a before_action for actions that do not require any particular permission on the project.
- #check_twofa_activation ⇒ Object
- #deny_access ⇒ Object
-
#filename_for_content_disposition(name) ⇒ Object
Returns a string that can be used as filename value in Content-Disposition header.
- #find_attachments ⇒ Object
-
#find_current_user ⇒ Object
Returns the current user or nil if no user is logged in and starts a session if needed.
-
#find_issue ⇒ Object
Find the issue whose id is the :id parameter Raises a Unauthorized exception if the issue is not visible.
-
#find_issues ⇒ Object
Find issues with a single :id param or :ids array param Raises a Unauthorized exception if one of the issues is not visible.
- #find_model_object ⇒ Object
-
#find_optional_project ⇒ Object
Find a project based on params and authorize the user for the requested action.
-
#find_optional_project_by_id ⇒ Object
Find project of id params if present.
-
#find_project(project_id = ) ⇒ Object
Find project of id params.
-
#find_project_by_project_id ⇒ Object
Find project of id params.
-
#find_project_from_association ⇒ Object
Finds and sets @project based on @object.project.
- #handle_unverified_request ⇒ Object
- #init_twofa_pairing_and_send_code_for(twofa) ⇒ Object
-
#logged_user=(user) ⇒ Object
Sets the logged in user.
-
#logout_user ⇒ Object
Logs out current user.
-
#missing_template(exception) ⇒ Object
Handler for ActionView::MissingTemplate exception.
- #parse_params_for_bulk_update(params) ⇒ Object
-
#parse_qvalues(value) ⇒ Object
qvalues http header parser code taken from webrick.
-
#per_page_option ⇒ Object
Returns the number of objects that should be displayed on the paginated list.
- #query_error(exception) ⇒ Object
-
#query_statement_invalid(exception) ⇒ Object
Rescues an invalid query statement.
- #record_project_usage ⇒ Object
- #redirect_back_or_default(default, options = {}) ⇒ Object
-
#redirect_to_referer_or(*args, &block) ⇒ Object
Redirects to the request referer if present, redirects to args or call block otherwise.
- #render_403(options = {}) ⇒ Object
- #render_404(options = {}) ⇒ Object
- #render_api_errors(*messages) ⇒ Object
-
#render_api_head(status) ⇒ Object
Renders a head API response.
-
#render_api_ok ⇒ Object
Renders a 204 response for successful updates or deletions via the API.
-
#render_attachment_warning_if_needed(obj) ⇒ Object
Renders a warning flash if obj has unsaved attachments.
-
#render_error(arg) ⇒ Object
Renders an error response.
- #render_feed(items, options = {}) ⇒ Object
-
#render_validation_errors(objects) ⇒ Object
Renders API response on validation failure for an object or an array of objects.
- #replace_none_values_with_blank(params) ⇒ Object
- #require_admin ⇒ Object
-
#require_admin_or_api_request ⇒ Object
Filter for actions that provide an API response but have no HTML representation for non admin users.
- #require_login ⇒ Object
- #session_expiration ⇒ Object
- #session_expired? ⇒ Boolean
- #set_localization(user = User.current) ⇒ Object
- #start_user_session(user) ⇒ Object
- #try_to_autologin ⇒ Object
-
#use_layout ⇒ boolean, string
Picks which layout to use based on the request.
- #user_setup ⇒ Object
- #verify_authenticity_token ⇒ Object
Methods included from Redmine::SudoMode::Controller
#process_sudo_form, #render_sudo_form, #require_sudo_mode, #sudo_mode, #sudo_timestamp_valid?, #update_sudo_timestamp!
Methods included from Redmine::MenuManager::MenuController
#current_menu, #current_menu_item, included, #menu_items, #redirect_to_menu_item, #redirect_to_project_menu_item
Methods included from Redmine::Search::Controller
#default_search_scope, #default_search_scopes, included
Methods included from IconsHelper
#activity_event_type_icon, #file_icon, #notice_icon, #principal_icon, #scm_change_icon, #sprite_icon
Methods included from AvatarsHelper
#assignee_avatar, #author_avatar, #avatar, #avatar_edit_link
Methods included from GravatarHelper::PublicMethods
#gravatar, #gravatar_api_url, #gravatar_for, #gravatar_url
Methods included from RoutesHelper
#_bulk_update_issues_path, #_new_project_issue_path, #_new_time_entry_path, #_project_calendar_path, #_project_gantt_path, #_project_issues_path, #_project_issues_url, #_project_news_path, #_report_time_entries_path, #_time_entries_path, #board_path
Methods included from Redmine::Hook::Helper
Methods included from Redmine::Pagination
Methods included from Redmine::I18n
#abbr_day_name, #current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #normalize_float, #set_language_if_valid, #valid_languages
Class Method Details
.accept_api_auth(*actions) ⇒ Object
646 647 648 649 650 651 652 |
# File 'app/controllers/application_controller.rb', line 646 def self.accept_api_auth(*actions) if actions.any? self.accept_api_auth_actions = actions else self.accept_api_auth_actions || [] end end |
.accept_atom_auth(*actions) ⇒ Object
634 635 636 637 638 639 640 |
# File 'app/controllers/application_controller.rb', line 634 def self.accept_atom_auth(*actions) if actions.any? self.accept_atom_auth_actions = actions else self.accept_atom_auth_actions || [] end end |
.model_object(model) ⇒ Object
392 393 394 |
# File 'app/controllers/application_controller.rb', line 392 def self.model_object(model) self.model_object = model end |
Instance Method Details
#_include_layout?(*args) ⇒ Boolean
Overrides #_include_layout? so that #render with no arguments doesn’t use the layout for api requests
784 785 786 |
# File 'app/controllers/application_controller.rb', line 784 def _include_layout?(*args) api_request? ? false : super end |
#accept_api_auth?(action = action_name) ⇒ Boolean
654 655 656 |
# File 'app/controllers/application_controller.rb', line 654 def accept_api_auth?(action=action_name) self.class.accept_api_auth.include?(action.to_sym) end |
#accept_atom_auth?(action = action_name) ⇒ Boolean
642 643 644 |
# File 'app/controllers/application_controller.rb', line 642 def accept_atom_auth?(action=action_name) self.class.accept_atom_auth.include?(action.to_sym) end |
#api_key_from_request ⇒ Object
Returns the API key present in the request
728 729 730 731 732 733 734 |
# File 'app/controllers/application_controller.rb', line 728 def api_key_from_request if params[:key].present? params[:key].to_s elsif request.headers["X-Redmine-API-Key"].present? request.headers["X-Redmine-API-Key"].to_s end end |
#api_offset_and_limit(options = params) ⇒ Object
Returns offset and limit used to retrieve objects for an API response based on offset, limit and page parameters
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'app/controllers/application_controller.rb', line 675 def api_offset_and_limit(=params) if [:offset].present? offset = [:offset].to_i if offset < 0 offset = 0 end end limit = [:limit].to_i if limit < 1 limit = 25 elsif limit > 100 limit = 100 end if offset.nil? && [:page].present? offset = ([:page].to_i - 1) * limit offset = 0 if offset < 0 end offset ||= 0 [offset, limit] end |
#api_request? ⇒ Boolean
723 724 725 |
# File 'app/controllers/application_controller.rb', line 723 def api_request? %w(xml json).include? params[:format] end |
#api_switch_user_from_request ⇒ Object
Returns the API ‘switch user’ value if present
737 738 739 |
# File 'app/controllers/application_controller.rb', line 737 def api_switch_user_from_request request.headers["X-Redmine-Switch-User"].to_s.presence end |
#authorize(ctrl = , action = , global = false) ⇒ Object
Authorize the user for the requested action
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'app/controllers/application_controller.rb', line 322 def (ctrl = params[:controller], action = params[:action], global = false) allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) if allowed true else if @project && @project.archived? @archived_project = @project render_403 :message => :notice_not_authorized_archived_project elsif @project && !@project.allows_to?(:controller => ctrl, :action => action) # Project module is disabled render_403 else deny_access end end end |
#authorize_global(ctrl = , action = , global = true) ⇒ Object
Authorize the user for the requested action outside a project
340 341 342 |
# File 'app/controllers/application_controller.rb', line 340 def (ctrl = params[:controller], action = params[:action], global = true) (ctrl, action, global) end |
#autologin_cookie_name ⇒ Object
175 176 177 |
# File 'app/controllers/application_controller.rb', line 175 def Redmine::Configuration['autologin_cookie_name'].presence || 'autologin' end |
#back_url ⇒ Object
484 485 486 487 488 489 490 |
# File 'app/controllers/application_controller.rb', line 484 def back_url url = params[:back_url] if url.nil? && referer = request.env['HTTP_REFERER'] url = CGI.unescape(referer.to_s) end url end |
#check_if_login_required ⇒ Object
check if login is globally required to access the application
214 215 216 217 218 219 |
# File 'app/controllers/application_controller.rb', line 214 def check_if_login_required # no check needed if user is already logged in return true if User.current.logged? require_login if Setting.login_required? end |
#check_password_change ⇒ Object
221 222 223 224 225 226 227 228 229 230 |
# File 'app/controllers/application_controller.rb', line 221 def check_password_change if session[:pwd] if User.current.must_change_password? flash[:error] = l(:error_password_expired) redirect_to my_password_path else session.delete(:pwd) end end end |
#check_project_privacy ⇒ Object
make sure that the user is a member of the project (or admin) if project is private used as a before_action for actions that do not require any particular permission on the project
463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'app/controllers/application_controller.rb', line 463 def check_project_privacy if @project && !@project.archived? if @project.visible? true else deny_access end else @project = nil render_404 false end end |
#check_twofa_activation ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/controllers/application_controller.rb', line 240 def check_twofa_activation if session[:must_activate_twofa] if User.current.must_activate_twofa? flash[:warning] = l('twofa_warning_require') if Redmine::Twofa.available_schemes.length == 1 twofa_scheme = Redmine::Twofa.for_twofa_scheme(Redmine::Twofa.available_schemes.first) twofa = twofa_scheme.new(User.current) init_twofa_pairing_and_send_code_for(twofa) else redirect_to controller: 'twofa', action: 'select_scheme' end else session.delete(:must_activate_twofa) end end end |
#deny_access ⇒ Object
317 318 319 |
# File 'app/controllers/application_controller.rb', line 317 def deny_access User.current.logged? ? render_403 : require_login end |
#filename_for_content_disposition(name) ⇒ Object
Returns a string that can be used as filename value in Content-Disposition header
719 720 721 |
# File 'app/controllers/application_controller.rb', line 719 def filename_for_content_disposition(name) name end |
#find_attachments ⇒ Object
427 428 429 430 431 432 433 434 435 |
# File 'app/controllers/application_controller.rb', line 427 def if ( = params[:attachments]).present? att = .values.collect do || Attachment.find_by_token([:token]) if [:token].present? end att.compact! end @attachments = att || [] end |
#find_current_user ⇒ Object
Returns the current user or nil if no user is logged in and starts a session if needed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/controllers/application_controller.rb', line 112 def find_current_user user = nil unless api_request? if session[:user_id] # existing session user = begin User.active.find(session[:user_id]) rescue nil end elsif autologin_user = try_to_autologin user = autologin_user elsif params[:format] == 'atom' && params[:key] && request.get? && accept_atom_auth? # ATOM key authentication does not start a session user = User.find_by_atom_key(params[:key]) end end if user.nil? && Setting.rest_api_enabled? && accept_api_auth? if (key = api_key_from_request) # Use API key user = User.find_by_api_key(key) elsif access_token = Doorkeeper.authenticate(request) # Oauth if access_token.accessible? user = User.active.find_by_id(access_token.resource_owner_id) user.oauth_scope = access_token.scopes.all.map(&:to_sym) else doorkeeper_render_error end elsif /\ABasic /i.match?(request..to_s) # HTTP Basic, either username/password or API key/random authenticate_with_http_basic do |username, password| user = User.try_to_login(username, password) # Don't allow using username/password when two-factor auth is active if user&.twofa_active? render_error :message => 'HTTP Basic authentication is not allowed. Use API key instead', :status => 401 return end user ||= User.find_by_api_key(username) end if user && user.must_change_password? render_error :message => 'You must change your password', :status => 403 return end end # Switch user if requested by an admin user if user && user.admin? && (username = api_switch_user_from_request) su = User.find_by_login(username) if su && su.active? logger.info(" User switched by: #{user.login} (id=#{user.id})") if logger user = su else render_error :message => 'Invalid X-Redmine-Switch-User header', :status => 412 end end end # store current ip address in user object ephemerally user.remote_ip = request.remote_ip if user user end |
#find_issue ⇒ Object
Find the issue whose id is the :id parameter Raises a Unauthorized exception if the issue is not visible
398 399 400 401 402 403 404 405 406 407 |
# File 'app/controllers/application_controller.rb', line 398 def find_issue # Issue.visible.find(...) can not be used to redirect user to the login form # if the issue actually exists but requires authentication @issue = Issue.find(params[:id]) raise Unauthorized unless @issue.visible? @project = @issue.project rescue ActiveRecord::RecordNotFound render_404 end |
#find_issues ⇒ Object
Find issues with a single :id param or :ids array param Raises a Unauthorized exception if one of the issues is not visible
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'app/controllers/application_controller.rb', line 411 def find_issues @issues = Issue. where(:id => (params[:id] || params[:ids])). preload(:project, :status, :tracker, :priority, :author, :assigned_to, :relations_to, {:custom_values => :custom_field}). to_a raise ActiveRecord::RecordNotFound if @issues.empty? raise Unauthorized unless @issues.all?(&:visible?) @projects = @issues.filter_map(&:project).uniq @project = @projects.first if @projects.size == 1 rescue ActiveRecord::RecordNotFound render_404 end |
#find_model_object ⇒ Object
382 383 384 385 386 387 388 389 390 |
# File 'app/controllers/application_controller.rb', line 382 def find_model_object model = self.class.model_object if model @object = model.find(params[:id]) self.instance_variable_set('@' + controller_name.singularize, @object) if @object end rescue ActiveRecord::RecordNotFound render_404 end |
#find_optional_project ⇒ Object
Find a project based on params and authorize the user for the requested action
365 366 367 368 369 370 371 372 373 |
# File 'app/controllers/application_controller.rb', line 365 def find_optional_project if params[:project_id].present? @project = Project.find(params[:project_id]) end rescue ActiveRecord::RecordNotFound User.current.logged? ? render_404 : require_login false end |
#find_optional_project_by_id ⇒ Object
Find project of id params if present
357 358 359 360 361 |
# File 'app/controllers/application_controller.rb', line 357 def find_optional_project_by_id if params[:id].present? find_project(params[:id]) end end |
#find_project(project_id = ) ⇒ Object
Find project of id params
345 346 347 348 349 |
# File 'app/controllers/application_controller.rb', line 345 def find_project(project_id=params[:id]) @project = Project.find(project_id) rescue ActiveRecord::RecordNotFound render_404 end |
#find_project_by_project_id ⇒ Object
Find project of id params
352 353 354 |
# File 'app/controllers/application_controller.rb', line 352 def find_project_by_project_id find_project(params[:project_id]) end |
#find_project_from_association ⇒ Object
Finds and sets @project based on @object.project
376 377 378 379 380 |
# File 'app/controllers/application_controller.rb', line 376 def find_project_from_association render_404 unless @object.present? @project = @object.project end |
#handle_unverified_request ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/application_controller.rb', line 49 def handle_unverified_request unless api_request? begin super rescue ActionController::InvalidAuthenticityToken => e logger.error("ActionController::InvalidAuthenticityToken: #{e.}") if logger ensure .delete() self.logged_user = nil set_localization render_error :status => 422, :message => l(:error_invalid_authenticity_token) end end end |
#init_twofa_pairing_and_send_code_for(twofa) ⇒ Object
232 233 234 235 236 237 238 |
# File 'app/controllers/application_controller.rb', line 232 def init_twofa_pairing_and_send_code_for(twofa) twofa.init_pairing! if twofa.send_code(controller: 'twofa', action: 'activate') flash[:notice] = l('twofa_code_sent') end redirect_to controller: 'twofa', action: 'activate_confirm', scheme: twofa.scheme_name end |
#logged_user=(user) ⇒ Object
Sets the logged in user
192 193 194 195 196 197 198 199 200 |
# File 'app/controllers/application_controller.rb', line 192 def logged_user=(user) reset_session if user && user.is_a?(User) User.current = user start_user_session(user) else User.current = User.anonymous end end |
#logout_user ⇒ Object
Logs out current user
203 204 205 206 207 208 209 210 211 |
# File 'app/controllers/application_controller.rb', line 203 def logout_user if User.current.logged? if autologin = .delete() User.current.delete_autologin_token(autologin) end User.current.delete_session_token(session[:tk]) self.logged_user = nil end end |
#missing_template(exception) ⇒ Object
Handler for ActionView::MissingTemplate exception
598 599 600 601 602 |
# File 'app/controllers/application_controller.rb', line 598 def missing_template(exception) logger.warn "Missing template, responding with 404: #{exception}" @project = nil render_404 end |
#parse_params_for_bulk_update(params) ⇒ Object
437 438 439 440 441 442 443 444 |
# File 'app/controllers/application_controller.rb', line 437 def parse_params_for_bulk_update(params) attributes = (params || {}).reject {|k, v| v.blank?} if custom = attributes[:custom_field_values] custom.reject! {|k, v| v.blank?} end replace_none_values_with_blank(attributes) end |
#parse_qvalues(value) ⇒ Object
qvalues http header parser code taken from webrick
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'app/controllers/application_controller.rb', line 699 def parse_qvalues(value) tmp = [] if value parts = value.split(/,\s*/) parts.each do |part| if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) val = m[1] q = (m[2] or 1).to_f tmp.push([val, q]) end end tmp = tmp.sort_by{|val, q| -q} tmp.collect!{|val, q| val} end return tmp rescue nil end |
#per_page_option ⇒ Object
Returns the number of objects that should be displayed on the paginated list
660 661 662 663 664 665 666 667 668 669 670 671 |
# File 'app/controllers/application_controller.rb', line 660 def per_page_option per_page = nil if params[:per_page] && Setting..include?(params[:per_page].to_s.to_i) per_page = params[:per_page].to_s.to_i session[:per_page] = per_page elsif session[:per_page] per_page = session[:per_page] else per_page = Setting..first || 25 end per_page end |
#query_error(exception) ⇒ Object
753 754 755 756 757 758 |
# File 'app/controllers/application_controller.rb', line 753 def query_error(exception) Rails.logger.debug "#{exception.class.name}: #{exception.}" Rails.logger.debug " #{exception.backtrace.join("\n ")}" render_404 end |
#query_statement_invalid(exception) ⇒ Object
Rescues an invalid query statement. Just in case…
747 748 749 750 751 |
# File 'app/controllers/application_controller.rb', line 747 def query_statement_invalid(exception) logger.error "Query::StatementInvalid: #{exception.}" if logger session.delete(:issue_query) render_error l(:error_query_statement_invalid) end |
#record_project_usage ⇒ Object
477 478 479 480 481 482 |
# File 'app/controllers/application_controller.rb', line 477 def record_project_usage if @project && @project.id && User.current.logged? && User.current.allowed_to?(:view_project, @project) Redmine::ProjectJumpBox.new(User.current).project_used(@project) end true end |
#redirect_back_or_default(default, options = {}) ⇒ Object
493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'app/controllers/application_controller.rb', line 493 def redirect_back_or_default(default, = {}) referer = .delete(:referer) if back_url = validate_back_url(params[:back_url].to_s) redirect_to(back_url) return elsif referer redirect_to_referer_or default return end redirect_to default, false end |
#redirect_to_referer_or(*args, &block) ⇒ Object
Redirects to the request referer if present, redirects to args or call block otherwise.
556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'app/controllers/application_controller.rb', line 556 def redirect_to_referer_or(*args, &block) if referer = request.headers["Referer"] redirect_to referer else if args.any? redirect_to *args elsif block yield else raise "#redirect_to_referer_or takes arguments or a block" end end end |
#render_403(options = {}) ⇒ Object
570 571 572 573 574 |
# File 'app/controllers/application_controller.rb', line 570 def render_403(={}) @project = nil render_error({:message => :notice_not_authorized, :status => 403}.merge()) return false end |
#render_404(options = {}) ⇒ Object
576 577 578 579 |
# File 'app/controllers/application_controller.rb', line 576 def render_404(={}) render_error({:message => :notice_file_not_found, :status => 404}.merge()) return false end |
#render_api_errors(*messages) ⇒ Object
777 778 779 780 |
# File 'app/controllers/application_controller.rb', line 777 def render_api_errors(*) @error_messages = .flatten render :template => 'common/error_messages', :format => [:api], :status => :unprocessable_content, :layout => nil end |
#render_api_head(status) ⇒ Object
Renders a head API response
766 767 768 |
# File 'app/controllers/application_controller.rb', line 766 def render_api_head(status) head status end |
#render_api_ok ⇒ Object
Renders a 204 response for successful updates or deletions via the API
761 762 763 |
# File 'app/controllers/application_controller.rb', line 761 def render_api_ok render_api_head :no_content end |
#render_attachment_warning_if_needed(obj) ⇒ Object
Renders a warning flash if obj has unsaved attachments
742 743 744 |
# File 'app/controllers/application_controller.rb', line 742 def (obj) flash[:warning] = l(:warning_attachments_not_saved, obj..size) if obj..present? end |
#render_error(arg) ⇒ Object
Renders an error response
582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'app/controllers/application_controller.rb', line 582 def render_error(arg) arg = {:message => arg} unless arg.is_a?(Hash) @message = arg[:message] @message = l(@message) if @message.is_a?(Symbol) @status = arg[:status] || 500 respond_to do |format| format.html do render :template => 'common/error', :layout => use_layout, :status => @status end format.any {head @status} end end |
#render_feed(items, options = {}) ⇒ Object
625 626 627 628 629 630 631 632 |
# File 'app/controllers/application_controller.rb', line 625 def render_feed(items, ={}) @items = (items || []).to_a @items.sort! {|x, y| y.event_datetime <=> x.event_datetime} @items = @items.slice(0, Setting.feeds_limit.to_i) @title = [:title] || Setting.app_title render :template => "common/feed", :formats => [:atom], :layout => false, :content_type => 'application/atom+xml' end |
#render_validation_errors(objects) ⇒ Object
Renders API response on validation failure for an object or an array of objects
772 773 774 775 |
# File 'app/controllers/application_controller.rb', line 772 def render_validation_errors(objects) = Array.wrap(objects).map {|object| object.errors.}.flatten render_api_errors() end |
#replace_none_values_with_blank(params) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'app/controllers/application_controller.rb', line 446 def replace_none_values_with_blank(params) attributes = (params || {}) attributes.each_key {|k| attributes[k] = '' if attributes[k] == 'none'} if (custom = attributes[:custom_field_values]) custom.each_key do |k| if custom[k].is_a?(Array) custom[k] << '' if custom[k].delete('__none__') else custom[k] = '' if custom[k] == '__none__' end end end attributes end |
#require_admin ⇒ Object
307 308 309 310 311 312 313 314 315 |
# File 'app/controllers/application_controller.rb', line 307 def require_admin return unless require_login unless User.current.admin? render_403 return false end true end |
#require_admin_or_api_request ⇒ Object
Filter for actions that provide an API response but have no HTML representation for non admin users
606 607 608 609 610 611 612 613 614 615 616 |
# File 'app/controllers/application_controller.rb', line 606 def require_admin_or_api_request return true if api_request? if User.current.admin? true elsif User.current.logged? render_error(:status => 406) else deny_access end end |
#require_login ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'app/controllers/application_controller.rb', line 273 def require_login unless User.current.logged? # Extract only the basic url parameters on non-GET requests if request.get? url = request.original_url else url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) end respond_to do |format| format.html do if request.xhr? head :unauthorized else redirect_to signin_path(:back_url => url) end end format.any(:atom, :pdf, :csv) do redirect_to signin_path(:back_url => url) end format.api do if Setting.rest_api_enabled? && accept_api_auth? head(:unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"') else head(:forbidden) end end format.js {head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"'} format.any {head :unauthorized} end return false end true end |
#session_expiration ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'app/controllers/application_controller.rb', line 76 def session_expiration if session[:user_id] && Rails.application.config.redmine_verify_sessions != false if session_expired? && !try_to_autologin set_localization(User.active.find_by_id(session[:user_id])) self.logged_user = nil flash[:error] = l(:error_session_expired) require_login end end end |
#session_expired? ⇒ Boolean
87 88 89 |
# File 'app/controllers/application_controller.rb', line 87 def session_expired? ! User.verify_session_token(session[:user_id], session[:tk]) end |
#set_localization(user = User.current) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'app/controllers/application_controller.rb', line 257 def set_localization(user=User.current) lang = nil if user && user.logged? lang = find_language(user.language) end if lang.nil? && !Setting.force_default_language_for_anonymous? && request.env['HTTP_ACCEPT_LANGUAGE'] accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first if accept_lang.present? accept_lang = accept_lang.downcase lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) end end lang ||= Setting.default_language set_language_if_valid(lang) end |
#start_user_session(user) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'app/controllers/application_controller.rb', line 91 def start_user_session(user) session[:user_id] = user.id session[:tk] = user.generate_session_token if user.must_change_password? session[:pwd] = '1' end if user.must_activate_twofa? session[:must_activate_twofa] = '1' end end |
#try_to_autologin ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/controllers/application_controller.rb', line 179 def try_to_autologin if [] && Setting.autologin? # auto-login feature starts a new session user = User.try_to_autologin([]) if user reset_session start_user_session(user) end user end end |
#use_layout ⇒ boolean, string
Picks which layout to use based on the request
621 622 623 |
# File 'app/controllers/application_controller.rb', line 621 def use_layout request.xhr? ? false : 'base' end |
#user_setup ⇒ Object
102 103 104 105 106 107 108 |
# File 'app/controllers/application_controller.rb', line 102 def user_setup # Check the settings cache for each request Setting.check_cache # Find the current user User.current = find_current_user logger.info(" Current user: " + (User.current.logged? ? "#{User.current.login} (id=#{User.current.id})" : "anonymous")) if logger end |
#verify_authenticity_token ⇒ Object
43 44 45 46 47 |
# File 'app/controllers/application_controller.rb', line 43 def verify_authenticity_token unless api_request? super end end |