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)



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

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

  @form = AlForm.new( [
      AlText.new( 'user_id', :label=>p_("al", "User ID"),
                  :required=>true, :validator=>/^[\w_-]+$/ ),
      AlText.new( 'password', :label=>p_("al", "Password"),
                  :required=>true, :validator=>/^[\w_-]+$/ ),
    ] )
end

Instance Attribute Details

#formAlForm (readonly)

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

Returns:

  • (AlForm)

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



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

def form
  @form
end

#template_nameString (readonly)

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

Returns:

  • (String)

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



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

def template_name
  @template_name
end

#valuesHash (readonly)

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

Returns:

  • (Hash)

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



30
31
32
# File 'lib/al_login_main.rb', line 30

def values
  @values
end

Class Method Details

.logoutObject

Note:

ログアウト

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



128
129
130
# File 'lib/al_login_main.rb', line 128

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

Instance Method Details

#confirmTrue, False

Note:

認証

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

Returns:

  • (True)

    認証成功

  • (False)

    認証失敗



141
142
143
# File 'lib/al_login_main.rb', line 141

def confirm()
  return false
end

#display_confirm_screenObject

認証用画面の表示



52
53
54
55
56
# File 'lib/al_login_main.rb', line 52

def display_confirm_screen()
  Alone.add_http_header("X-Frame-Options: DENY")
  AlTemplate.run( @template_name, binding )
  AlSession[:al_login_token] = 'DUMMY_ID_D0Sv3ebV32'
end

#gettext_message_uriString

翻訳ファイルのパスを返す

Examples:

htmlテンプレート中で以下のように使う
<script src="<%=s gettext_message_uri() %>"></script>

Returns:

  • (String)

    翻訳ファイルのパス



154
155
156
157
# File 'lib/al_login_main.rb', line 154

def gettext_message_uri()
  @al_locale = $AlController.init_locale()  if !@al_locale
  return "locale/#{@al_locale}/messages.js"
end

#loginTrue, False

Note:

ログインメイン処理

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

Returns:

  • (True)

    ログイン成功

  • (False)

    ログイン失敗



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
117
118
119
# File 'lib/al_login_main.rb', line 69

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( p_("al", "Invalid User ID or password."))
    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