/lib/rboard/auth.rb

https://github.com/chrisdarroch/rboard · Ruby · 120 lines · 91 code · 18 blank · 11 comment · 15 complexity · 19beab7bfa3319ea4c0f471716f8339c MD5 · raw file

  1. module Rboard::Auth
  2. # Store the given user in the session.
  3. def current_user=(new_user)
  4. return if new_user.nil?
  5. new_user.previous_login = current_user.login_time
  6. new_user.login_time = Time.now
  7. new_user.ip = request.remote_addr
  8. ip = Ip.find_or_create_by_ip(request.remote_addr)
  9. ip.users << new_user unless ip.users.include?(new_user)
  10. new_user.save
  11. session[:user] = new_user.id
  12. @current_user = new_user
  13. end
  14. #Per Page value for paginated sections of the forums,
  15. def per_page
  16. logged_in? ? current_user.per_page : PER_PAGE
  17. end
  18. #how the user has selected they want to display the time
  19. def time_display
  20. logged_in? ? current_user.time_display : TIME_DISPLAY
  21. end
  22. #how the user has selected they want to display the date
  23. def date_display
  24. logged_in? ? current_user.date_display : DATE_DISPLAY
  25. end
  26. def date_time_display
  27. date_display + " " + time_display
  28. end
  29. def non_admin_redirect
  30. if !current_user.can?(:access_admin_section)
  31. flash[:notice] = t(:need_to_be_admin)
  32. redirect_back_or_default(root_path)
  33. end
  34. end
  35. def non_moderator_redirect
  36. if !current_user.can?(:access_moderator_section)
  37. flash[:notice] = t(:need_to_be_moderator)
  38. redirect_back_or_default(root_path)
  39. end
  40. end
  41. def ip_banned?
  42. @ips = BannedIp.find(:all, :conditions => ["ban_time > ?",Time.now]).select do |ip|
  43. !Regexp.new(ip.ip).match(request.remote_addr).nil? unless ip.nil?
  44. end
  45. flash[:ip] = @ips.first unless @ips.empty?
  46. end
  47. def ip_banned_redirect
  48. redirect_to :controller => ip_is_banned_users_path unless params[:action] == "ip_is_banned" if ip_banned?
  49. end
  50. def user_banned?
  51. logged_in? ? current_user.banned? : false
  52. end
  53. def theme
  54. ThemesLoader.new
  55. theme = logged_in? && !current_user.theme.nil? ? current_user.theme : Theme.find_by_is_default(true)
  56. theme.nil? ? Theme.first : theme
  57. end
  58. def active_user
  59. current_user.update_attribute("login_time",Time.now) if logged_in?
  60. end
  61. # Modified for rBoard
  62. # Returns true or false, depending on if the user is an anonymous user or not.
  63. def logged_in?
  64. current_user != User.find_by_login("anonymous")
  65. end
  66. # Accesses the current user from the session.
  67. # Will also return the anonymous user if the user is not logged in.
  68. def current_user
  69. @current_user ||= (session[:user] && User.find_by_id(session[:user])) || User.find_by_login("anonymous")
  70. end
  71. # Use as a before filter to ensure that the user is logged in.
  72. def login_required
  73. # Gather data from HTTP-based authentication.
  74. username, password = get_auth_data
  75. self.current_user ||= User.authenticate(username, password) if username && password
  76. self.current_user ||= User.find_by_login("anonymous")
  77. if !logged_in?
  78. flash[:notice] = t(:you_must_be_logged_in)
  79. redirect_to login_path
  80. end
  81. end
  82. def self.included(base)
  83. base.send :helper_method,
  84. :is_moderator?,
  85. :ip_banned?,
  86. :logged_in?,
  87. :user_banned?,
  88. :theme,
  89. :time_display,
  90. :date_display,
  91. :date_time_display,
  92. :per_page
  93. end
  94. private
  95. @@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
  96. # gets BASIC auth info
  97. def get_auth_data
  98. auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
  99. auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
  100. return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
  101. end
  102. end