Class: AlLogin

Inherits:
Object
  • Object
show all
Defined in:
lib/al_login_main.rb

Overview

ログインマネージャ

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name = nil) ⇒ AlLogin

constructor

Parameters:

  • template_name (String) (defaults to: nil)

    テンプレートファイル名(option)



35
36
37
38
39
40
41
42
43
44
# File 'lib/al_login_main.rb', line 35

def initialize( template_name = nil )
  @template_name = template_name || "./al_login.rhtml"

  @form = AlForm.new( [
      AlText.new( 'user_id', :label=>'ユーザID',
                  :required=>true, :validator=>/^[\w_-]+$/ ),
      AlText.new( 'password', :label=>'パスワード',
                  :required=>true, :validator=>/^[\w_-]+$/ ),
    ] )
end

Instance Attribute Details

#formAlForm (readonly)

Returns ログイン用フォームオブジェクト.

Returns:

  • (AlForm)

    ログイン用フォームオブジェクト



22
23
24
# File 'lib/al_login_main.rb', line 22

def form
  @form
end

#template_nameString (readonly)

Returns ログイン画面テンプレートファイル名.

Returns:

  • (String)

    ログイン画面テンプレートファイル名



25
26
27
# File 'lib/al_login_main.rb', line 25

def template_name
  @template_name
end

#valuesHash (readonly)

Returns フォームから受け取った値.

Returns:

  • (Hash)

    フォームから受け取った値



28
29
30
# File 'lib/al_login_main.rb', line 28

def values
  @values
end

Class Method Details

.logoutObject

Note:

ログアウト

セッション情報を削除して、ログアウトとする。



125
126
127
# File 'lib/al_login_main.rb', line 125

def self.logout()
  AlSession::destroy()
end

Instance Method Details

#confirmTrue, False

Note:

認証

サブクラスでオーバライドする。

Returns:

  • (True)

    認証成功

  • (False)

    認証失敗



138
139
140
# File 'lib/al_login_main.rb', line 138

def confirm()
  return false
end

#display_confirm_screenObject

認証用画面の表示



50
51
52
53
# File 'lib/al_login_main.rb', line 50

def display_confirm_screen()
  AlTemplate.run( @template_name, binding )
  AlSession[:al_login_token] = 'DUMMY_ID_D0Sv3ebV32'
end

#loginTrue, False

Note:

ログインメイン処理

ログインの一連の処理を実施し、ログインが成功したら trueを返す。 ログイン前にリクエストされたURIは保存されるが、POSTデータは保存されないので、 必要なら自分で処理するか、当システム内へ内包する方法を立案すべき。

Returns:

  • (True)

    ログイン成功

  • (False)

    ログイン失敗



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/al_login_main.rb', line 66

def ()
  # 初めてのアクセスならフォームを表示して終了。
  if ! @form.fetch_request( 'POST' )
    display_confirm_screen()
    return false
  end

  # セッションが使えるか確認。NGならあきらめる。
  if ! AlSession[:al_user_id] &&
      AlSession[:al_login_token] != 'DUMMY_ID_D0Sv3ebV32'
    puts "You must enable cookie. <br>"
    puts "And click bellow, jump login page.<br>"
    puts "<a href=\"#{AL_LOGIN_URI}\">To Login page.</a><br>"
    exit
  end

  # テンポラリログアウト
  AlSession::delete( :al_user_id )
  AlSession::delete( :al_login_token )

  # バリデーション。NGなら、フォーム表示して終了。
  if ! @form.validate()
    display_confirm_screen()
    return false
  end
  @values = @form.values.dup

  # 認証。ユーザコードでオーバライドされた confirm()が呼び出される。
  # NGなら、フォームを表示して終了。
  if ! confirm()
    @form.add_message( "ユーザIDまたはパスワードが違います。" )
    display_confirm_screen()
    return false
  end

  # 認証成功。
  # セッションへユーザIDを保存して、ログイン済みであることを明示する。
  AlSession::change_session_id()
  AlSession[:al_user_id] = @values[:user_id]

  # ログイン前にリクエストされていたURIがあれば、そこへリダイレクトし、
  # 呼び出し元へは戻らない。
  if AlSession[:al_request_uri]
    Alone::redirect_to( AlSession[:al_request_uri] )
    AlSession.delete( :al_request_uri )
    Alone::send_http_headers()
    exit
  end

  return true
end