/generators/youser/templates/youser_system.rb

https://github.com/smart/younety_client_plugin · Ruby · 190 lines · 146 code · 6 blank · 38 comment · 10 complexity · d5658aad9ca8af455496f6825a1678c4 MD5 · raw file

  1. module YouserSystem
  2. require 'authenticators/open_id_helper'
  3. include OpenIdHelper
  4. require 'authenticators/facebook_helper'
  5. include FacebookHelper
  6. require 'authenticators/local_user_helper'
  7. include LocalUserHelper
  8. protected
  9. # Returns true or false if the <%= file_name %> is logged in.
  10. # Preloads @current_<%= file_name %> with the <%= file_name %> model if they're logged in.
  11. def logged_in?
  12. current_<%= file_name %> != :false
  13. end
  14. # Accesses the current <%= file_name %> from the session.
  15. def current_<%= file_name %>
  16. @current_<%= file_name %> ||= (session[:<%= file_name %>] && <%= class_name %>.find_by_id(session[:<%= file_name %>])) || :false
  17. end
  18. # Store the given <%= file_name %> in the session.
  19. def current_<%= file_name %>=(new_<%= file_name %>)
  20. session[:<%= file_name %>] = (new_<%= file_name %>.nil? || new_<%= file_name %>.is_a?(Symbol)) ? nil : new_<%= file_name %>.id
  21. @current_<%= file_name %> = new_<%= file_name %>
  22. end
  23. # Check if the <%= file_name %> is authorized.
  24. #
  25. # Override this method in your controllers if you want to restrict access
  26. # to only a few actions or if you want to check if the <%= file_name %>
  27. # has the correct rights.
  28. #
  29. # Example:
  30. #
  31. # # only allow nonbobs
  32. # def authorize?
  33. # current_<%= file_name %>.login != "bob"
  34. # end
  35. def authorized?
  36. true
  37. end
  38. # Filter method to enforce a login requirement.
  39. #
  40. # To require logins for all actions, use this in your controllers:
  41. #
  42. # before_filter :login_required
  43. #
  44. # To require logins for specific actions, use this in your controllers:
  45. #
  46. # before_filter :login_required, :only => [ :edit, :update ]
  47. #
  48. # To skip this in a subclassed controller:
  49. #
  50. # skip_before_filter :login_required
  51. #
  52. def login_required
  53. <%= file_name %>name, passwd = get_auth_data
  54. p fbsession
  55. self.current_<%= file_name %> ||= LocalUser.authenticate(<%= file_name %>name, passwd) || :false if <%= file_name %>name && passwd
  56. if logged_in? && authorized?
  57. current_<%= file_name %>.complete? ? true : unfinished_registration
  58. else
  59. access_denied
  60. end
  61. end
  62. # Redirect as appropriate when an access request fails.
  63. #
  64. # The default action is to redirect to the login screen.
  65. #
  66. # Override this method in your controllers if you want to have special
  67. # behavior in case the <%= file_name %> is not authorized
  68. # to access the requested action. For example, a popup window might
  69. # simply close itself.
  70. def access_denied
  71. respond_to do |accepts|
  72. accepts.html do
  73. store_location
  74. redirect_to :controller => 'sessions', :action => 'new'
  75. end
  76. accepts.xml do
  77. headers["Status"] = "Unauthorized"
  78. headers["WWW-Authenticate"] = %(Basic realm="Web Password")
  79. render :text => "Could't authenticate you", :status => '401 Unauthorized'
  80. end
  81. end
  82. false
  83. end
  84. # Redirect as appropriate when a <%= file_name %> registration is not complete
  85. #
  86. # The default action is to redirect to the register screen
  87. #
  88. # Override this method in your controllers if you want to have special
  89. # behavior in case the <%= file_name %> is not complete
  90. # to access the requested action. For example, a popup window might
  91. # simply close itself.
  92. def unfinished_registration
  93. params = {:<%= file_name %> => current_<%= file_name %>.attributes}
  94. respond_to do |accepts|
  95. accepts.html do
  96. #store_location
  97. redirect_to :controller => '<%= model_controller_file_name %>', :action => 'finish_registration'
  98. end
  99. accepts.xml do
  100. headers["Status"] = "Unauthorized"
  101. headers["WWW-Authenticate"] = %(Basic realm="Web Password")
  102. render :text => "You do not have a complete registration please visit the site to complete.", :status => '401 Unauthorized'
  103. end
  104. end
  105. false
  106. end
  107. # determines while authenticator to load
  108. #
  109. # returns false is none is detected
  110. def determine_authenticator
  111. return "OpenId" if using_open_id?
  112. return "Facebook" if using_facebook?
  113. return "LocalUser" if using_local_user?
  114. return false
  115. end
  116. #if one of the authenticatos is successful this is called
  117. def successful_login
  118. if params[:remember_me] == "1"
  119. self.current_<%= file_name %>.remember_me
  120. cookies[:auth_token] = { :value => self.current_<%= file_name %>.remember_token , :expires => self.current_<%= file_name %>.remember_token_expires_at }
  121. end
  122. if !current_<%= file_name %>.complete?
  123. unfinished_registration
  124. else
  125. redirect_back_or_default('/')
  126. flash[:notice] = "Logged in successfully"
  127. end
  128. end
  129. #if the authenticator failes this is called
  130. def failed_login(message)
  131. flash.now[:error] = message
  132. render :action => 'new'
  133. end
  134. # Store the URI of the current request in the session.
  135. #
  136. # We can return to this location by calling #redirect_back_or_default.
  137. def store_location
  138. session[:return_to] = request.request_uri
  139. end
  140. # Redirect to the URI stored by the most recent store_location call or
  141. # to the passed default.
  142. def redirect_back_or_default(default)
  143. session[:return_to] ? redirect_to_url(session[:return_to]) : redirect_to(default)
  144. session[:return_to] = nil
  145. end
  146. # Inclusion hook to make #current_<%= file_name %> and #logged_in?
  147. # available as ActionView helper methods.
  148. def self.included(base)
  149. base.send :helper_method, :current_<%= file_name %>, :logged_in?
  150. end
  151. # When called with before_filter :login_from_cookie will check for an :auth_token
  152. # cookie and log the <%= file_name %> back in if apropriate
  153. def login_from_cookie
  154. return unless cookies[:auth_token] && !logged_in?
  155. <%= file_name %> = <%= class_name %>.find_by_remember_token(cookies[:auth_token])
  156. if <%= file_name %> && <%= file_name %>.remember_token?
  157. <%= file_name %>.remember_me
  158. self.current_<%= file_name %> = <%= file_name %>
  159. cookies[:auth_token] = { :value => self.current_<%= file_name %>.remember_token , :expires => self.current_<%= file_name %>.remember_token_expires_at }
  160. flash[:notice] = "Logged in successfully"
  161. end
  162. end
  163. private
  164. @@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
  165. # gets BASIC auth info
  166. def get_auth_data
  167. auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
  168. auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
  169. return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
  170. end
  171. end