Skip to content

Instantly share code, notes, and snippets.

@Panmind
Created May 28, 2010 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Panmind/417127 to your computer and use it in GitHub Desktop.
Save Panmind/417127 to your computer and use it in GitHub Desktop.
Zendesk dropbox and remote authentication plugin for Rails -- obsoleted by http://github.com/Panmind/zendesk
### This gist is obsoleted by the full plugin release:
### http://github.com/Panmind/zendesk - check it out :-)
module ApplicationHelper
include PM::Zendesk::Helpers
# .... more helpers ....
end
Rails::Initializer.run do |config|
# .... more configuration ...
config.after_initialize do
require 'pm/zendesk'
end
# NOTE: we use config.load_once_paths.push((Rails.root + 'lib').to_s)
# thus we don't suffer from the "Already initialized constant XYZ" ;)
# .... more configuration ...
end
ActionController::Routing::Routes.draw do |map|
# .... more routes ....
# Zendesk remote authentication
#
map.zendesk '/support', :controller => 'sessions'
# .... more routes ....
end
class SessionsController < ApplicationController
include PM::Zendesk::Controller if PM::Zendesk.enabled?
# .... more controller methods ....
end
<!-- Support link -->
<%= zendesk_link_to "Our support forum" %>
<!-- Zendesk dropbox tags -->
<%= zendesk_dropbox_tags %>
# Adds a .force_utf8 to the String class if running on 1.9.
# It's a no-op on 1.8: returns self.
#
class String
if '1.9'.respond_to?(:force_encoding)
def force_utf8
force_encoding('UTF-8')
end
else
def force_utf8
self
end
end
end
require 'digest/md5'
module PM
# Zendesk remote authentication helper for Rails. Implements JS generation,
# controller actions and route helpers. Have a look at the code, because it
# is more explanatory than a thousand words :-)
#
# Kudos to the Zendesk staff for such a simple and effective interface.
#
# (C) 2010 Mind2Mind.is, spinned off from http://panmind.org/ - MIT License.
#
# - vjt Fri May 28 14:45:27 CEST 2010
#
module Zendesk
Token = 'Your API Token'.force_utf8.freeze
Hostname = 'your_hostname.zendesk.com'.freeze
AuthURL = "http://#{Hostname}/access/remote/".freeze
ReturnURL = "http://#{Hostname}/login".freeze
SupportURL = "http://#{Hostname}/home".freeze
module Helpers
def zendesk_dropbox_tags
return unless PM::Zendesk.enabled?
%(<script type="text/javascript">
var zenbox_params = {
tab_id: 'feedback',
tab_color: 'black',
title: 'Panmind',
text: "How may we help you? Please fill in details below, and we'll get back to you as soon as possible.",
tag: 'feedback',
url: '#{Hostname}',
email: '#{current_user.email rescue nil}'
};
</script>
<style type='text/css'>@import url('//assets0.zendesk.com/external/zenbox/overlay.css');</style>
<script type='text/javascript' src='//assets0.zendesk.com/external/zenbox/overlay.js'></script>)
end
def zendesk_link_to(text, options = {})
return unless PM::Zendesk.enabled?
link_to text, support_path, options
end
end
module Controller
def self.included(base)
base.before_filter :zendesk_handle_guests, :only => :zendesk_login
end
def zendesk_login
now = params[:timestamp] || Time.now.to_i.to_s
name = current_user.name.force_utf8
email = current_user.email.force_utf8
hash = Digest::MD5.hexdigest(name + email + Token + now)
back = params[:return_to] || ReturnURL
auth_params = [
'?name=' + CGI.escape(name),
'&email=' + CGI.escape(email),
'&timestamp=' + now,
'&hash=' + hash,
'&return_to=' + back
].join.force_utf8
redirect_to(AuthURL + auth_params)
end
def zendesk_logout
flash[:notice] = "Thanks for visiting our support forum."
redirect_to root_url
end
private
def zendesk_handle_guests
return if current_user
if params[:timestamp] && params[:return_to]
# User clicked on Zendesk "login", thus redirect to our
# login page, that'll redirect him/her back to Zendesk.
#
redirect_to login_url(:return_to => support_url)
else
# User clicked on our "support" link, and maybe doesn't
# have an account yet: redirect him/her to the support.
#
redirect_to SupportURL
end
end
end
module Routes
def zendesk(base, options)
return unless PM::Zendesk.enabled?
self.support base, :controller => options[:controller], :action => :zendesk_login
self.connect "#{base}/exit", :controller => options[:controller], :action => :zendesk_logout
end
end
def self.enabled?
Rails.env.production?
end
end
end
ActionController::Routing::RouteSet::Mapper.send :include, PM::Zendesk::Routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment