Class: UserSessionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/user_sessions_controller.rb

Overview

Create, destroy sessions by letting users login and logout. When the user does not login, he/she is considered to be the anonymous user.

Instance Method Summary collapse

Methods included from Zena::App

included

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/user_sessions_controller.rb', line 20

def create
  User.send(:with_scope, :find => {:conditions => ['site_id = ?', visitor.site.id]}) do
    if user = User.(params[:login])
      # FAIL: 1s, FAIL: 2s, FAIL: 4s, FAIL: 8s, FAIL: 16s, FAIL: 32s, FAIL: 64s
      wait_in_seconds = 2 ** user..to_i
      elapsed         = Time.now.to_i - user..to_i
      if elapsed < wait_in_seconds
        w = Time.at(wait_in_seconds - elapsed)
        msg = _("You need to wait %ih %im %is before any new attempt (%i failed attempts).")
        flash[:error] = msg % [w.hour, w.min, w.sec, user..to_i]
        return redirect_to 
      end
    else
      flash[:error] = _("Invalid login or password.")
      return redirect_to 
    end
    @user_session = UserSession.new(:login=>params[:login], :password=>params[:password])
    if @user_session.save
      # Reset login attempts count
      Zena::Db.set_attribute(user, 'login_attempt_count', 0)
      Zena::Db.set_attribute(user, 'login_attempted_at',  nil)
      redirect_to  
    else
      flash[:error] = _("Invalid login or password.")
      Zena::Db.set_attribute(user, 'login_attempt_count', user..to_i + 1)
      Zena::Db.set_attribute(user, 'login_attempted_at',  Time.now.utc)
      redirect_to 
    end
  end
end

#destroyObject

Logout



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/user_sessions_controller.rb', line 52

def destroy
  port = request.port == 80 ? '' : ":#{request.port}"
  if @user_session = UserSession.find
    @user_session.destroy
    reset_session
    if current_site.ssl_on_auth
      # SSH only when authenticated
      host = current_site.host
      http = 'http'
    else
      # Keep current host and port settings
      host = host_with_port
      http = host =~ /:443/ ? 'https' : 'http'
    end
    #flash.now[:notice] = _("Successfully logged out.")
    redirect_to "#{http}://#{host}#{params[:redirect] || home_path(:prefix => prefix)}"
  else
    redirect_to "http://#{host}#{home_path(:prefix => prefix)}"
  end
end

#newObject

/login



10
11
12
13
14
15
16
17
18
# File 'app/controllers/user_sessions_controller.rb', line 10

def new
  # If user is already logged in, redirect to home page
  if !visitor.is_anon?
    redirect_to home_path(:prefix => prefix)
  else
    @node = visitor.site.root_node
    render_and_cache :mode => '+login'
  end
end