Module: RFacebook::Rails::ControllerExtensions
- Defined in:
- lib/rfacebook_on_rails/controller_extensions.rb
Defined Under Namespace
Classes: APICallbackNeededStandardError, APICanvasPathNeededStandardError, APIFinisherNeededStandardError, APIKeyNeededStandardError, APISecretNeededStandardError
Constant Summary collapse
- CLASSES_EXTENDED =
:section: Extension Helpers
[]
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #added_facebook_application? ⇒ Boolean
-
#facebook_api_key ⇒ Object
:section: Template Methods (must be implemented by concrete subclass).
- #facebook_api_secret ⇒ Object
- #facebook_callback_path ⇒ Object
-
#facebook_canvas_backtrace(exception) ⇒ Object
def rescue_action(exception) # TODO: for security, we only do this in development in the canvas if (in_facebook_canvas? and RAILS_ENV == “development”) render_text “##facebook_debug_panel#facebook_canvas_backtrace(exception)” else # otherwise, do the default super end end.
- #facebook_canvas_path ⇒ Object
- #facebook_debug_panel(options = {}) ⇒ Object
- #facebook_platform_signature_verified? ⇒ Boolean
-
#facebook_redirect_to(url) ⇒ Object
DEPRECATED.
-
#facebook_status_manager ⇒ Object
:nodoc:.
-
#fbparams ⇒ Object
Function: fbparams Accessor for all params beginning with “fb_sig_”.
-
#fbsession ⇒ Object
Function: fbsession Accessor for a FacebookWebSession that has been activated, either in the Canvas (via fb_sig parameters) or in an external app (via an auth_token).
- #finish_facebook_login ⇒ Object
-
#handle_facebook_login ⇒ Object
:section: Before_filters.
- #in_external_app? ⇒ Boolean
- #in_facebook_canvas? ⇒ Boolean
- #in_facebook_frame? ⇒ Boolean
- #in_mock_ajax? ⇒ Boolean
-
#redirect_to__RFACEBOOK(options = {}, *parameters) ⇒ Object
:nodoc:.
-
#render_with_facebook_debug_panel(options = {}) ⇒ Object
:section: Facebook Debug Panel.
- #require_facebook_install ⇒ Object
- #require_facebook_login ⇒ Object
-
#rfacebook_persist_session_to_rails ⇒ Object
:nodoc:.
-
#rfacebook_session_holder ⇒ Object
:section: RFacebook Private Methods.
-
#url_for__RFACEBOOK(options = {}, *parameters) ⇒ Object
:section: URL Management.
Class Method Details
.included(base) ⇒ Object
:nodoc:
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 472 def self.included(base) # :nodoc: # check for a double include doubleInclude = false CLASSES_EXTENDED.each do |klass| if base.allocate.is_a?(klass) doubleInclude = true end end if doubleInclude RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: detected double-include of RFacebook controller extensions. Please see instructions for RFacebook on Rails plugin usage (http://rfacebook.rubyforge.org). You may be including the deprecated RFacebook::RailsControllerExtensions in addition to the plugin." else # keep track that we have already extended this class CLASSES_EXTENDED << base # we need to use an eval since we will be overriding ActionController::Base methods # and we need to be able to call the originals base.class_eval ' alias_method(:url_for__ALIASED, :url_for) alias_method(:url_for, :url_for__RFACEBOOK) alias_method(:redirect_to__ALIASED, :redirect_to) alias_method(:redirect_to, :redirect_to__RFACEBOOK) ' # ensure that every action handles facebook login base.before_filter(:handle_facebook_login) # ensure that we persist the Facebook session into the Rails session (if possible) base.after_filter(:rfacebook_persist_session_to_rails) # fix third party cookies in IE base.before_filter{ |c| c.headers['P3P'] = %|CP="NOI DSP COR NID ADMa OPTa OUR NOR"| } end end |
Instance Method Details
#added_facebook_application? ⇒ Boolean
166 167 168 169 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 166 def added_facebook_application? addedApp = params["fb_sig_added"] || fbparams["added"] return (addedApp == "1" || addedApp == true) end |
#facebook_api_key ⇒ Object
:section: Template Methods (must be implemented by concrete subclass)
47 48 49 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 47 def facebook_api_key raise APIKeyNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'key' defined" end |
#facebook_api_secret ⇒ Object
51 52 53 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 51 def facebook_api_secret raise APISecretNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'secret' defined" end |
#facebook_callback_path ⇒ Object
59 60 61 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 59 def facebook_callback_path raise APICallbackNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'callback_path' defined" end |
#facebook_canvas_backtrace(exception) ⇒ Object
def rescue_action(exception)
# TODO: for security, we only do this in development in the canvas
if (in_facebook_canvas? and RAILS_ENV == "development")
render_text "#{facebook_debug_panel}#{facebook_canvas_backtrace(exception)}"
else
# otherwise, do the default
super
end
end
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 309 def facebook_canvas_backtrace(exception) # TODO: potentially integrate features from Evan Weaver's facebook_exceptions rfacebookBacktraceLines = [] exception.backtrace.each do |line| # escape HTML cleanLine = line.gsub(RAILS_ROOT, "").gsub("<", "<").gsub(">", ">") # split up these lines by carriage return pieces = cleanLine.split("\n") if (pieces and pieces.size> 0) pieces.each do |piece| if matches = /.*[\/\\]+((.*)\:([0-9]+)\:\s*in\s*\`(.*)\')/.match(piece) # for each parsed line, add to the array for later rendering in the template rfacebookBacktraceLines << { :filename => matches[2], :line => matches[3], :method => matches[4], :rawsummary => piece, } end end end end # render to the ERB template template = File.read(File.dirname(__FILE__) + "/templates/exception_backtrace.rhtml") return ERB.new(template).result(Proc.new{}) end |
#facebook_canvas_path ⇒ Object
55 56 57 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 55 def facebook_canvas_path raise APICanvasPathNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'canvas_path' defined" end |
#facebook_debug_panel(options = {}) ⇒ Object
294 295 296 297 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 294 def facebook_debug_panel(={}) template = File.read(File.dirname(__FILE__) + "/templates/debug_panel.rhtml") return ERB.new(template).result(Proc.new{}) end |
#facebook_platform_signature_verified? ⇒ Boolean
171 172 173 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 171 def facebook_platform_signature_verified? return (fbparams and fparams.size > 0) end |
#facebook_redirect_to(url) ⇒ Object
DEPRECATED
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 135 def facebook_redirect_to(url) # :nodoc: RAILS_DEFAULT_LOGGER.info "** RFACEBOOK DEPRECATION NOTICE: facebook_redirect_to is deprecated in RFacebook. Instead, you can use redirect_to like any Rails app." if in_facebook_canvas? render :text => "<fb:redirect url=\"#{url}\" />" elsif url =~ /^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i render :text => "<script type=\"text/javascript\">\ntop.location.href = \"#{url}\";\n</script>"; else redirect_to url end end |
#facebook_status_manager ⇒ Object
:nodoc:
341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 341 def facebook_status_manager # :nodoc: checks = [ SessionStatusCheck.new(self), (FacebookParamsStatusCheck.new(self) unless (!in_facebook_canvas? and !in_facebook_frame?)), InCanvasStatusCheck.new(self), InFrameStatusCheck.new(self), (CanvasPathStatusCheck.new(self) unless (!in_facebook_canvas? or !in_facebook_frame?)), (CallbackPathStatusCheck.new(self) unless (!in_facebook_canvas? or !in_facebook_frame?)), (FinishFacebookLoginStatusCheck.new(self) unless (in_facebook_canvas? or in_facebook_frame?)), APIKeyStatusCheck.new(self), APISecretStatusCheck.new(self) ].compact return StatusManager.new(checks) end |
#fbparams ⇒ Object
Function: fbparams
Accessor for all params beginning with "fb_sig_"
Returns:
A Hash of those parameters, with the fb_sig_ stripped from the keys
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 76 def fbparams # try to get fbparams from the params hash if (!@fbparams || @fbparams.length <= 0) dup_params = (self.params || {}).dup @fbparams = rfacebook_session_holder.get_fb_sig_params(dup_params) end # else, try to get fbparams from the cookies hash if (!@fbparams || @fbparams.length <= 0) = (self. || {}).dup @fbparams = rfacebook_session_holder.get_fb_sig_params() end # finally, if we are an iframe app we may have saved the fbparams # to the session for safekeeping if (!@fbparams || @fbparams.length <= 0) @fbparams = session[:rfacebook_session_iframe_fbparams] || {} end return @fbparams end |
#fbsession ⇒ Object
Function: fbsession
Accessor for a FacebookWebSession that has been activated, either in the Canvas
(via fb_sig parameters) or in an external app (via an auth_token).
Returns:
A FacebookWebSession. You may want to check is_valid? before using it.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 106 def fbsession # if we are in the canvas, iframe, or mock ajax, we should be able to activate the session here if (!rfacebook_session_holder.is_valid? and (in_facebook_canvas? or in_facebook_frame? or in_mock_ajax?)) # then try to activate it somehow (or retrieve from previous state) # these might be nil facebookUid = fbparams["user"] facebookSessionKey = fbparams["session_key"] expirationTime = fbparams["expires"] if (facebookUid and facebookSessionKey and expirationTime) # we have the user id and key from the fb_sig_ params, activate the session rfacebook_session_holder.activate_with_previous_session(facebookSessionKey, facebookUid, expirationTime) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Activated session from inside the canvas" else RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK WARNING: Tried to activate session from inside the canvas, but failed" end end # if all went well, we should definitely have a valid Facebook session object return rfacebook_session_holder end |
#finish_facebook_login ⇒ Object
63 64 65 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 63 def finish_facebook_login raise APIFinisherNeededStandardError, "RFACEBOOK ERROR: in an external Facebook application, you should define finish_facebook_login in your controller (often this is used to redirect to a 'login success' page, but it can also simply do nothing)" end |
#handle_facebook_login ⇒ Object
:section: Before_filters
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 182 def handle_facebook_login if !in_facebook_canvas? if params["auth_token"] # activate with the auth token RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: attempting to create a new Facebook session from auth_token" staleToken = false begin # try to use the auth_token rfacebook_session_holder.activate_with_token(params["auth_token"]) rescue StandardError => e RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Tried to use a stale auth_token" staleToken = true end # template method call upon success if (rfacebook_session_holder.is_valid? and !staleToken) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Login was successful, calling finish_facebook_login" if in_external_app? finish_facebook_login end end elsif (session[:rfacebook_session] and session[:rfacebook_session].is_valid?) # grab saved Facebook session from Rails session RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: grabbing Facebook session from Rails session" @rfacebook_session_holder = session[:rfacebook_session] @rfacebook_session_holder.logger = RAILS_DEFAULT_LOGGER end # warning logs if !rfacebook_session_holder.is_valid? RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: Facebook session could not be activated (from handle_facebook_login)" end end return true end |
#in_external_app? ⇒ Boolean
160 161 162 163 164 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 160 def in_external_app? # FIXME: once you click away in an iframe app, you are considered to be an external app # TODO: read up on the hacks for avoiding nested iframes return (params["fb_sig"] == nil and !in_facebook_frame?) end |
#in_facebook_canvas? ⇒ Boolean
146 147 148 149 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 146 def in_facebook_canvas? in_canvas = params["fb_sig_in_canvas"] || fbparams["in_canvas"] return (in_canvas == "1" || in_canvas == true) end |
#in_facebook_frame? ⇒ Boolean
151 152 153 154 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 151 def in_facebook_frame? in_iframe = params["fb_sig_in_iframe"] || fbparams["in_iframe"] return (in_iframe == "1" || in_iframe == true) end |
#in_mock_ajax? ⇒ Boolean
156 157 158 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 156 def in_mock_ajax? return (params["fb_mockajax_url"] != nil) end |
#redirect_to__RFACEBOOK(options = {}, *parameters) ⇒ Object
:nodoc:
451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 451 def redirect_to__RFACEBOOK( = {}, *parameters) # :nodoc: if in_facebook_canvas? canvasRedirUrl = url_for(, *parameters) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Canvas redirect to #{canvasRedirUrl}" render :text => "<fb:redirect url=\"#{canvasRedirUrl}\" />" else RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Regular redirect_to" redirect_to__ALIASED(, *parameters) end end |
#render_with_facebook_debug_panel(options = {}) ⇒ Object
:section: Facebook Debug Panel
285 286 287 288 289 290 291 292 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 285 def render_with_facebook_debug_panel(={}) begin renderedOutput = render_to_string() rescue Exception => e renderedOutput = facebook_canvas_backtrace(e) end render_text "#{facebook_debug_panel}#{renderedOutput}" end |
#require_facebook_install ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 267 def require_facebook_install if (in_facebook_canvas? or in_facebook_frame?) if (!fbsession.is_valid? or !added_facebook_application?) redirect_to fbsession.get_install_url return false end else RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: require_facebook_install is not intended for external applications, using require_facebook_login instead" return require_facebook_login end return true end |
#require_facebook_login ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 229 def require_facebook_login # now finish it off depending on whether we are in canvas, iframe, or external app if !performed? # try to get the session sess = fbsession # handle invalid sessions by forcing the user to log in if !sess.is_valid? RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Session is not valid" if in_external_app? RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Redirecting to login for external app" redirect_to sess.get_login_url return false elsif (!fbparams or fbparams.size == 0) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK WARNING: Failed to activate due to a bad API key or API secret" render :text => facebook_debug_panel return false else RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Redirecting to login for canvas app" redirect_to sess.get_login_url(:canvas=>true) return false end end end return true end |
#rfacebook_persist_session_to_rails ⇒ Object
:nodoc:
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 372 def rfacebook_persist_session_to_rails # :nodoc: if (!in_facebook_canvas? and rfacebook_session_holder.is_valid?) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: persisting Facebook session information into Rails session" session[:rfacebook_session] = @rfacebook_session_holder.dup # TODO: do we need dup here anymore? if in_facebook_frame? # we need iframe apps to remember they are iframe apps session[:rfacebook_session_iframe_fbparams] = fbparams end end end |
#rfacebook_session_holder ⇒ Object
:section: RFacebook Private Methods
361 362 363 364 365 366 367 368 369 370 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 361 def rfacebook_session_holder # :nodoc: if (@rfacebook_session_holder == nil) @rfacebook_session_holder = FacebookWebSession.new(facebook_api_key, facebook_api_secret) @rfacebook_session_holder.logger = RAILS_DEFAULT_LOGGER end return @rfacebook_session_holder end |
#url_for__RFACEBOOK(options = {}, *parameters) ⇒ Object
:section: URL Management
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/rfacebook_on_rails/controller_extensions.rb', line 389 def url_for__RFACEBOOK(={}, *parameters) # :nodoc: # fix problems that some Rails installations had with sending nil options ||= {} # # error check # if !options # RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: options cannot be nil in call to url_for" # end # use special URL rewriting when inside the canvas # setting the mock_ajax option to true will override this # and force usage of regular Rails rewriting mockajaxSpecified = false if .is_a? Hash mockajaxSpecified = [:mock_ajax] end if (in_facebook_canvas? and !mockajaxSpecified) #TODO: do something separate for in_facebook_frame? if .is_a? Hash [:only_path] = true if [:only_path].nil? end # try to get a regular URL path = url_for__ALIASED(, *parameters) # replace anything that references the callback with the # Facebook canvas equivalent (apps.facebook.com/*) if path.starts_with?(self.facebook_callback_path) path.sub!(self.facebook_callback_path, self.facebook_canvas_path) path = "http://apps.facebook.com#{path}" elsif "#{path}/".starts_with?(self.facebook_callback_path) path.sub!(self.facebook_callback_path.chop, self.facebook_canvas_path.chop) path = "http://apps.facebook.com#{path}" elsif (path.starts_with?("http://www.facebook.com") or path.starts_with?("https://www.facebook.com")) # be sure that URLs that go to some other Facebook service redirect back to the canvas if path.include?("?") path = "#{path}&canvas=true" else path = "#{path}?canvas=true" end elsif (!path.starts_with?("http://") and !path.starts_with?("https://")) # default to a full URL (will link externally) RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: failed to get canvas-friendly URL ("+path+") for ["+.inspect+"], creating an external URL instead" path = "#{request.protocol}#{request.host}:#{request.port}#{path}" end # mock-ajax rewriting elsif mockajaxSpecified .delete(:mock_ajax) # clear it so it doesnt show up in the url [:only_path] = true path = "#{request.protocol}#{request.host}:#{request.port}#{url_for__ALIASED(, *parameters)}" # regular Rails rewriting else path = url_for__ALIASED(, *parameters) end return path end |