Module: Symphonia::ControllerExtensions
- Extended by:
- ActiveSupport::Concern
- Included in:
- ApplicationController
- Defined in:
- lib/symphonia/controller_extensions.rb
Instance Method Summary collapse
- #admin_require ⇒ Object (also: #require_admin)
- #authorize ⇒ Object
- #back_url ⇒ Object
- #current_user ⇒ Object
- #current_user_session ⇒ Object
- #handle_unverified_request ⇒ Object
-
#login_require(format = nil) ⇒ Object
(also: #require_login, #require_user)
protected.
- #menu_item(item = nil) ⇒ 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 ⇒ Object
- #render_404 ⇒ Object
-
#render_api_head(status) ⇒ Object
Renders a head API response.
-
#render_api_ok ⇒ Object
Renders a 200 response for successful updates or deletions via the API.
- #set_default_locale ⇒ Object
-
#set_locale ⇒ Object
private.
- #store_location ⇒ Object
Instance Method Details
#admin_require ⇒ Object Also known as: require_admin
115 116 117 118 119 120 121 122 |
# File 'lib/symphonia/controller_extensions.rb', line 115 def admin_require return unless login_require unless current_user.admin? render_403 return false end end |
#authorize ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/symphonia/controller_extensions.rb', line 168 def if Symphonia::User.current.(controller_name, action_name) return true else if Symphonia::User.current.logged_in? raise Unauthorized else respond_to do |format| format.html do return redirect_to(symphonia.login_path(back_url: request.path), error: t(:text_error_login_required)) end format.any { return head 401 } end end end raise Unauthorized end |
#back_url ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/symphonia/controller_extensions.rb', line 33 def back_url url = params[:back_url] if url.nil? && (referer = request.env['HTTP_REFERER']) url = CGI.unescape(referer.to_s) end url end |
#current_user ⇒ Object
161 162 163 164 165 166 |
# File 'lib/symphonia/controller_extensions.rb', line 161 def current_user return (Symphonia::User.current ||= @current_user) if defined?(@current_user) @current_user = current_user_session && current_user_session.user Symphonia::User.current = @current_user || Symphonia::User::Anonymous.new end |
#current_user_session ⇒ Object
155 156 157 158 159 |
# File 'lib/symphonia/controller_extensions.rb', line 155 def current_user_session return @current_user_session if defined?(@current_user_session) @current_user_session = UserSession.find end |
#handle_unverified_request ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/symphonia/controller_extensions.rb', line 186 def handle_unverified_request # raise an exception fail ActionController::InvalidAuthenticityToken # or destroy session, redirect if current_user_session current_user_session.destroy end redirect_to main_app.root_url end |
#login_require(format = nil) ⇒ Object Also known as: require_login, require_user
protected
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/symphonia/controller_extensions.rb', line 97 def login_require(format = nil) if current_user.nil? || !current_user.logged_in? respond_to do |format| format.html do store_location redirect_to symphonia.login_path, flash: { error: t(:text_login_require) } end format.json { render json: { errors: 'You must be logged in to access this endpoint' }, status: 401 } format.any { head 401 } end return false end true end |
#menu_item(item = nil) ⇒ Object
151 152 153 |
# File 'lib/symphonia/controller_extensions.rb', line 151 def (item = nil) @menu_item = (item || controller_name) end |
#redirect_back_or_default(default, options = {}) ⇒ Object
200 201 202 203 |
# File 'lib/symphonia/controller_extensions.rb', line 200 def redirect_back_or_default(default, = {}) ||= {} redirect_to(params[:back_url] || default, ) end |
#redirect_to_referer_or(*args, &block) ⇒ Object
Redirects to the request referer if present, redirects to args or call block otherwise.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/symphonia/controller_extensions.rb', line 64 def redirect_to_referer_or(*args, &block) redirect_to :back rescue ::ActionController::RedirectBackError if args.any? redirect_to *args elsif block_given? block.call else raise "#redirect_to_referer_or takes arguments or a block" end end |
#render_403 ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/symphonia/controller_extensions.rb', line 126 def render_403 respond_to do |format| format.html { render template: 'common/403', message: :notice_not_authorized, status: 403 } format.js { render plain: "alert('#{t :text_access_deny}')", message: :notice_not_authorized, status: 403 } format.json { head 403, message: :notice_not_authorized } end end |
#render_404 ⇒ Object
134 135 136 137 138 139 |
# File 'lib/symphonia/controller_extensions.rb', line 134 def render_404 respond_to do |format| format.html { render template: 'common/404', message: :notice_page_not_found, status: 404 } format.json { head 404, message: :not_found } end end |
#render_api_head(status) ⇒ Object
Renders a head API response
147 148 149 |
# File 'lib/symphonia/controller_extensions.rb', line 147 def render_api_head(status) head status end |
#render_api_ok ⇒ Object
Renders a 200 response for successful updates or deletions via the API
142 143 144 |
# File 'lib/symphonia/controller_extensions.rb', line 142 def render_api_ok head :ok end |
#set_default_locale ⇒ Object
89 90 91 92 93 |
# File 'lib/symphonia/controller_extensions.rb', line 89 def set_default_locale if (enforce_default = Symphonia.config.default_locale) I18n.locale = enforce_default end end |
#set_locale ⇒ Object
private
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/symphonia/controller_extensions.rb', line 78 def set_locale client_lang = Array(params.fetch(:locale, nil).presence || session[:locale].presence || request.env['HTTP_ACCEPT_LANGUAGE'].to_s.split(',').collect { |l| l.scan(/^[a-z]{2}/) }.flatten) client_lang.unshift current_user.language if current_user.language @client_lang = client_lang.detect { |l| I18n.available_locales.include?(l.to_sym) } I18n.locale = (@client_lang || I18n.default_locale) session[:locale] = I18n.locale logger.debug "* Locale set to '#{I18n.locale}'" end |
#store_location ⇒ Object
196 197 198 |
# File 'lib/symphonia/controller_extensions.rb', line 196 def store_location session[:return_to] = request.url end |