Skip to content

Instantly share code, notes, and snippets.

@Heavyblade
Created May 30, 2012 20:17
Show Gist options
  • Save Heavyblade/2838661 to your computer and use it in GitHub Desktop.
Save Heavyblade/2838661 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter proc { |controller| (controller.action_has_layout = false) if controller.request.xhr? }
before_filter :ensure_current_user
before_filter :km_init
analytical :modules=>[:kiss_metrics]
# store some additional info in the papertrail changelog
def info_for_paper_trail
{ :ip => request.remote_ip, :user_agent => request.user_agent }
end
def generate_identifier
now = Time.now.to_i
Digest::MD5.hexdigest(
(request.referrer || '') +
rand(now).to_s +
now.to_s +
(request.user_agent || '')
)
end
def km_init
if not identity = cookies[:km_identity]
identity = generate_identifier
cookies[:km_identity] = {
:value => identity, :expires => 5.years.from_now
}
end
# This example assumes you have a current_user, with a
# property "email". Use whatever makes sense for your
# app.
if current_user.present?
if not cookies[:km_aliased]
KM.alias(identity, current_user.email)
cookies[:km_aliased] = {
:value => true,
:expires => 5.years.from_now
}
end
identity = current_user.email
end
KM.identify(identity)
end
def render_404
respond_to do |format|
format.html { render "/pages/404", :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
protected
def ensure_admin
redirect_to '/wait_for_approval' and return unless current_user.approved?
redirect_to '/' unless current_user.admin?
end
def ensure_current_user
unless current_user
session[:after_login] = (request.path + '?' + request.query_string) if request.get?
redirect_to '/users/auth/att'
return
end
redirect_to '/wait_for_approval' and return unless current_user.approved?
end
def disallow_developer
redirect_to '/wait_for_approval' and return unless current_user.approved?
redirect_to '/' and return if current_user.developer?
end
def can?(task,what)
what.can?(task,current_user)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment