Guest User

Untitled

a guest
Mar 12th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ##APPLICATION.RB
  2.  
  3. # Filters added to this controller apply to all controllers in the application.
  4. # Likewise, all the methods added will be available for all controllers.
  5.  
  6. class ApplicationController < ActionController::Base
  7. helper :all # include all helpers, all the time
  8.  
  9. # See ActionController::RequestForgeryProtection for details
  10. # Uncomment the :secret if you're not using the cookie session store
  11. protect_from_forgery # :secret => '91be570dc568d6f0367706e1771e67c1'
  12. #
  13. before_filter :fetch_logged_in_user
  14.  
  15. protected
  16.  
  17. def fetch_logged_in_user
  18. return if session[:user_id].blank?
  19. @current_user = Artist.find_by_id(session[:user_id])
  20. end
  21.  
  22. def logged_in?
  23. ! @current_user.blank?
  24. end
  25.  
  26. helper_method :logged_in?
  27.  
  28. def login_required
  29.  
  30. return true if logged_in?
  31.  
  32. session[:return_to] = request.request_uri
  33.  
  34. redirect_to :controller => "/artist", :action => "login" and
  35.  
  36. return false
  37.  
  38. end
  39.  
  40.  
  41.  
  42.  
  43. end
  44.  
  45.  
  46. ##LOGIN VIEW
  47.  
  48. <h1 class="index">Artist login</h1><br>
  49. <% form_tag do %>
  50.  
  51. <p>Please log in.</p>
  52.  
  53. <p>
  54.  
  55. <label>Username:</label>
  56.  
  57. <%= text_field_tag 'login' %>
  58.  
  59. </p>
  60.  
  61. <p>
  62.  
  63. <label>Password:</label>
  64.  
  65. <%= password_field_tag 'password' %>
  66.  
  67. </p>
  68.  
  69. <p><%= submit_tag 'login' %></p>
  70.  
  71. <% end %>
  72.  
  73.  
  74. ##CONTROLLER
  75.  
  76. def login
  77.  
  78.  
  79. if request.post?
  80.  
  81. @current_user = Artist.find_by_login_and_password(params[:login], params[:password])
  82.  
  83. unless @current_user.nil?
  84.  
  85. session[:user_id] = @current_user.id
  86. unless session[:return_to].blank?
  87.  
  88. redirect_to session[:return_to]
  89.  
  90. session[:return_to] = nil
  91.  
  92. else
  93.  
  94. redirect_to :controller => 'artistadmin'
  95. end
  96.  
  97.  
  98. end
Add Comment
Please, Sign In to add comment