Class: AlSession

Inherits:
Object
  • Object
show all
Defined in:
lib/al_session.rb,
lib/al_session_rdb.rb

Overview

Note:

セッション変数には、String,Numeric,Hashなど、基本的な(requreの必要のない) オブジェクトのみ保存すること。 実装上は任意のオブジェクトが保存できるが、セッション復帰が難しくなる。 また、アプリケーション側にとっても、アプリケーションのバージョンアップが 難しくなるので、基本オブジェクトに限定したほうが良い。

セッションマネージャ セッション変数を管理する。

See Also:

Constant Summary

TBL_SESSIONS =

セッションを保存するテーブル名

"sessions"
@@session_id =

セッションID

nil
@@session_var =

セッション変数のHash

{}
@@session_file =

保存ファイルオブジェクト

nil
@@session_rdbw =

保存RDBWオブジェクト

nil

Class Method Summary (collapse)

Class Method Details

+ (Object) [](k)

セッション変数の取得

Parameters:

  • (String, Symbol) k

    セッション変数名 key

Returns:

  • (Object)



211
212
213
# File 'lib/al_session.rb', line 211

def self.[]( k )
  return @@session_var[k.to_sym]
end

+ (Object) []=(k, v)

セッション変数の設定

Parameters:

  • (String, Symbol) k

    セッション変数名 key

  • (Object) v



222
223
224
# File 'lib/al_session.rb', line 222

def self.[]=( k, v )
  @@session_var[k.to_sym] = v
end

+ (Object) _end

Note:

セッションの終了ではなく、htmlの接続ごとの終了。

終了処理(内部メソッド)



126
127
128
129
130
131
132
# File 'lib/al_session.rb', line 126

def self._end()
  #
  # DBへセッション変数を登録
  #
  data = { :data=>Marshal.dump( @@session_var ).dump, :updated_at=>Time.now }
  @@session_rdbw.update( TBL_SESSIONS, data, {:session_id=>@@session_id} )
end

+ (Object) _start

Note:

当ファイルをインクルードした段階で自動スタートするので、 明示的に呼び出す必要は無い。

セッション開始(内部メソッド)



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
# File 'lib/al_session.rb', line 79

def self._start()
  @@session_rdbw = AlRdbw.connect_suitable( AL_SESS_CONN )
  @@session_id = Alone::get_cookie( :ALSESSID )

  #
  # すでにセッションIDが割り当て済みの場合
  #
  if /\A[a-zA-Z0-9]{32}\Z/ =~ @@session_id
    begin
      rows = @@session_rdbw.select( "select data from #{TBL_SESSIONS} where session_id='#{@@session_id}';" )
      @@session_var = Marshal.load( eval( rows[0][:data] ) )
      return

    rescue
      # データが何らかの理由により存在しないか、不正なデータである。
      # よって、セッションID未割り当ての場合と同じ処理へ移行する。
    end
  end

  #
  # セッションID未割り当ての場合
  #
  retry_count = 0
  begin
    @@session_id = make_session_id()
    @@session_rdbw.insert( TBL_SESSIONS, { :session_id=>@@session_id, :updated_at=>Time.now } )
    Alone::set_cookie( :ALSESSID, @@session_id, nil, '/' )

  rescue
    retry_count += 1
    if retry_count > 10
      puts "Can't create session data in database. Fix AL_SESS_* parameter in al_config.rb file."
      exit
    end
    retry
  end
end

+ (Object) change_session_id

Note:

任意のタイミングで現在のセッションIDを無効化、新しいIDを付与する。

セッションIDの変更



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/al_session.rb', line 177

def self.change_session_id()
  #
  # 今のセッションファイルを無効化(消去)
  #
  @@session_rdbw.delete( TBL_SESSIONS, {:session_id=>@@session_id} )

  #
  # 新しいセッションidおよびファイルを作る
  #
  retry_count = 0
  begin
    @@session_id = make_session_id()
    @@session_rdbw.insert( TBL_SESSIONS, { :session_id=>@@session_id, :updated_at=>Time.now } )
    Alone::set_cookie( :ALSESSID, @@session_id, nil, '/' )

  rescue
    retry_count += 1
    if retry_count > 10
      raise "Can't create session."
    end
    retry
  end
end

+ (String) debug_dump

デバッグ用:セッション変数をすべて出力する。

Returns:

  • (String)

    セッション変数の内容



53
54
55
56
57
58
59
60
# File 'lib/al_session.rb', line 53

def self.debug_dump()
  r = "AlSession::debug_dump() outputs\n"
  r << "Session ID '#{get_session_id()}'\n"
  @@session_var.each do |k,v|
    r << "#{k} => '#{v.to_s}'\n"
  end
  return r
end

+ (Object) delete(k)

セッション変数の消去

Parameters:

  • (String, Symbol) k

    セッション変数名 key



242
243
244
# File 'lib/al_session.rb', line 242

def self.delete( k )
  @@session_var.delete( k.to_sym )
end

+ (Object) delete_all

セッション変数の全消去



250
251
252
# File 'lib/al_session.rb', line 250

def self.delete_all()
  @@session_var.clear()
end

+ (Object) destroy

Note:

セッションそのものを終了する。 セッション変数もセッションIDも(クッキーも)消去する。

セッションの終了



262
263
264
265
266
267
# File 'lib/al_session.rb', line 262

def self.destroy()
  delete_all()
  Alone::delete_cookie( :ALSESSID, '/' )
  @@session_rdbw.delete( TBL_SESSIONS, {:session_id=>@@session_id} )
  @@session_id = nil
end

+ (String) get_session_id

セッションIDの取得

Returns:

  • (String)

    セッションID



67
68
69
# File 'lib/al_session.rb', line 67

def self.get_session_id()
  return @@session_id
end

+ (Array<Symbol>) keys

セッション変数のキー一覧

Returns:

  • (Array<Symbol>)

    キーの配列



232
233
234
# File 'lib/al_session.rb', line 232

def self.keys()
  return @@session_var.keys()
end