include AuthenticatedSystem
include NeedAuthorization::Helper
+ include CookieFlash
before_filter :check_database_version, :set_i18n, :check_authentication
if logged_in?
self.current_user.on_logout
end
- flash[:notice]=message('session.flash_notice.logged_out')
- reset_session
+ cookies.delete 'JWT-SESSION'
+ cookies.delete 'XSRF-TOKEN'
redirect_to(home_path)
end
</div>
<!--<![endif]-->
-<% if flash[:notice] || flash[:warning] || flash[:error] %>
+<% if cookies['flash'] %>
<script>
- <% if flash[:notice] %>info('<%= escape_javascript(flash[:notice])-%>');<% end %>
- <% if flash[:warning] %>warning('<%= escape_javascript(flash[:warning])-%>');<% end %>
- <% if flash[:error] %>error('<%= escape_javascript(flash[:error])-%>');<% end %>
+ var data = JSON.parse(unescape('<%= escape_javascript(cookies['flash']) -%>'));
+ if (data['notice']) {
+ info(data['notice'].toString().replace(/\+/g, ' '));
+ }
+ if (data['warning']) {
+ warning(data['warning'].toString().replace(/\+/g, ' '));
+ }
+ if (data['error']) {
+ error(data['error'].toString().replace(/\+/g, ' '));
+ }
</script>
<% end %>
+<% cookies.delete 'flash' %>
<script src="<%= ApplicationController.root_context -%>/js/bundles/main.js?v=<%= sonar_version -%>"></script>
<%= yield :extra_script -%>
<input type="hidden" name="return_to_anchor" value="<%= h @return_to_anchor %>">
<div class="alert alert-danger alert-authentication-failed hidden"><%= message('session.flash_notice.authentication_failed') %></div>
- <% if flash[:loginerror] %>
- <div class="alert alert-danger alert-flash"> <%= flash[:loginerror] %></div>
- <% end %>
- <% if flash[:notice] %>
- <div class="alert alert-info alert-flash"><%= flash[:notice] %></div>
- <% end %>
+ <div class="hidden" id="messages-panel">
+ <div class="alert alert-danger alert-flash hidden" id="error">
+ <span id="errormsg"></span>
+ </div>
+ <div class="alert alert-info alert-flash hidden" id="info">
+ <span id="infomsg"></span>
+ </div>
+ </div>
<div class="big-spacer-bottom">
<label for="login" class="login-label"><%= message('login') %></label>
401: null
}
});
+
+ <% if cookies['flash'] %>
+ var data = JSON.parse(unescape('<%= escape_javascript(cookies['flash']) -%>'));
+ if (data['loginerror']) {
+ error(data['loginerror'].toString().replace(/\+/g, ' '));
+ }
+ if (data['info']) {
+ info(data['info'].toString().replace(/\+/g, ' '));
+ }
+ <% cookies.delete 'flash' %>
+ <% end %>
+
})(window.jQuery);
</script>
+
# Resets the session by clearing out all the objects stored within and initializing a new session object.
def reset_session #:doc:
- cookies.delete 'JWT-SESSION'
- cookies.delete 'XSRF-TOKEN'
-
request.reset_session
@_session = request.session
end
def store(session, key = "flash")
return if self.empty?
- session[key] = self
+ raise "Session are disabled"
end
private
# to put a new one.
def flash #:doc:
if !defined?(@_flash)
- @_flash = session["flash"] || FlashHash.new
+ @_flash = FlashHash.new
@_flash.sweep
end
if logged_in?
flash[:loginerror]='You are not authorized to access this page. Please log in with more privileges and try again.'
end
+ write_flash_to_cookie
redirect_to url_for :controller => '/sessions', :action => 'new'
end
# format.any doesn't work in rails version < http://dev.rubyonrails.org/changeset/8987
--- /dev/null
+require 'json'
+
+module CookieFlash
+
+ def self.included(base)
+ #base must define around_action or around_filter, as in Rails
+
+ around_method = if base.respond_to?(:around_action)
+ :around_action
+ else
+ :around_filter
+ end
+
+ base.send around_method, :write_flash_to_cookie
+ end
+
+ def write_flash_to_cookie
+ yield if block_given?
+
+ if !flash.empty?
+ cookies['flash'] = { :value => cookie_flash(flash, cookies)}
+ # because flashes are only removed from cookies when they are used.
+ flash.clear
+ end
+ end
+
+ # @parameters
+ # cookies -
+ # There might be crusty flash from a previous request, or set elsewhere, already in the cookie.
+ # Pull it out and parse it so we can preserve it.
+ # flash -
+ # This is the fresh, super-stacked (by stackable_flash gem) FlashHash from the current request.
+ # Needs to be added to the cookie flash.
+ def cookie_flash(flash, cookies)
+ cflash = (JSON.parse(cookies['flash']) if cookies['flash']) || {} rescue {}
+
+ flash.each do |key, value| # key like :notice, or :error, or :sticky
+ # When stacking we won't be escaping anything here, because will be array, not string
+ value = ERB::Util.html_escape(value) if value.kind_of?(String) && !value.html_safe? # Since v0.3.0 only escaping strings
+ skey = key.to_s
+ # This allows any data type to be stored in the cookie; important for using an array as the value with
+ # stackable_flash
+ # The cookie flash will generally be set to a value stacked according to the :stack_with_proc of stackable_flash
+ # But when there is already a value for the cookie when we get here, we need to join them somehow.
+ stacked_value = value.respond_to?(:stack) ? value.stack : value
+ if cflash[skey].kind_of?(Array) # Just because it could be an array
+ if stacked_value.kind_of?(Array)
+ cflash[skey] += stacked_value
+ else
+ cflash[skey] << stacked_value
+ end
+ else
+ cflash[skey] = stacked_value
+ end
+ end
+ # I have forgotten why the gsub + matters, so NOTE: to future self: document weird shit.
+ cflash.to_json.gsub("+", "%2B")
+ end
+end