page_adsence

2016年1月29日金曜日

Laravel4.2で入力->確認->完了の画面遷移を実装してみる。(CSRF対策もおまけで)

Laravelでフォームを作るとどんな感じになるのか試してみた。
CSRF対策と2重送信の防止をするという前提で作ってみました。

app/routes.phpを編集し、入力、確認、完了画面で実行するコントローラーとアクションを定義する。
入力画面に関してはCSRF対策用のフィルタは通さずに、確認、完了画面に関してはCSRF対策用のフィルタを通す様に記述しました。
CSRF対策用のフィルタ処理はapp/filters.phpに書いてあります。(Laravelが元々用意している機能)
$ vi app/routes.php
Route::get('/input', 'HomeController@input');
Route::group(array('before' => 'csrf'), function()
{
    Route::post('/confirm',  'HomeController@confirm');
    Route::post('/complete', 'HomeController@complete');
});

続いてアクション。
下記の3つのメソッドを追加。
$ vi app/controllers/HomeController.php
public function input()
{
    return View::make('input');
}

public function confirm()
{
    return View::make('confirm');
}

public function complete()
{
    Session::regenerateToken();

    // 登録処理

    return View::make('complete');
}
完了画面で遷移してきた段階で、Laravelが作っているCSRF対策用のトークンを再生成しています。
これは、このトークンを2重送信防止用にも使用しているため、こういった形をとっています。
CSRF対策だけならこういった対応は必要ないと思います。
結局、「Session::regenerateToken()」は_tokenという名前でトークンをセッションに保存しているので、1セッションで複数フォームを開いた場合、
先に送信した方は登録されるが、後で送信された方はトークンが一致せずにエラーになる。
こういう挙動を許すかどうかは、そのシステムの要件次第なので、使用時に判断してもらえればと思います。

最後にテンプレート。
入力画面
$ vi app/views/input.blade.php
{{ Form::open(array('url' => 'confirm')) }}
<table>
<tr>
<td>タイトル</td><td>{{ Form::text('title') }}</td>
</tr>
<tr>
<td>メッセージ</td><td>{{ Form::textarea('message') }}</td>
</tr>
<tr>
<td colspan="2">{{ Form::submit('内容を確認') }}</td>
</tr>
</table>
{{ Form::close() }}

確認画面
$ vi app/views/confirm.blade.php
{{ Form::open(array('url' => 'complete')) }}
<table>
<tr>
<td>タイトル</td><td>{{ Input::get('title') }}{{ Form::hidden('title', Input::get('title')) }}</td>
</tr>
<tr>
<td>メッセージ</td><td>{{ Input::get('message') }}{{ Form::hidden('message', Input::get('message')) }}</td>
</tr>
<tr>
<td colspan="2">{{ Form::submit('内容を登録') }}</td>
</tr>
</table>
{{ Form::close() }}
完了画面
$ vi app/views/complete.blade.php
complete!

2016年1月5日火曜日

rbenvを利用してruby2系をインストール(特定のユーザーのみ)

rbenvをインストール
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Initialized empty Git repository in /home/vagrant/.rbenv/.git/
remote: Counting objects: 2484, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 2484 (delta 9), reused 0 (delta 0), pack-reused 2464
Receiving objects: 100% (2484/2484), 445.80 KiB | 142 KiB/s, done.
Resolving deltas: 100% (1562/1562), done.
パスを通し、ターミナル起動時に「rbenv init」する様にしておく。
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
リロード
$ source ~/.bash_profile
ruby-buildをインストール
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Initialized empty Git repository in /home/vagrant/.rbenv/plugins/ruby-build/.git/
remote: Counting objects: 5431, done.
remote: Total 5431 (delta 0), reused 0 (delta 0), pack-reused 5431
Receiving objects: 100% (5431/5431), 1013.75 KiB | 389 KiB/s, done.
Resolving deltas: 100% (3029/3029), done.
rbenvのバージョン確認
$ rbenv
rbenv 1.0.0-14-gc388331
Usage: rbenv  []

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help ' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme
rubyをインストールする際に必要になるパッケージをインストール
$ sudo yum install gcc make openssl-devel readline-devel
インストール可能なrubyのバージョンの一覧を確認する
$ rbenv install -l
Available versions:
  :
  略
  :
  2.2.0
  2.2.1
  2.2.2
  2.2.3
  2.2.4
  2.3.0-dev
  2.3.0-preview1
  2.3.0-preview2
  2.3.0
  2.4.0-dev
  :
  略
  :
今回はとりあえず2.2.4をインストールしてみる。
$ rbenv install 2.2.4
Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
error: failed to download ruby-2.2.4.tar.bz2

BUILD FAILED (CentOS release 6.7 (Final) using ruby-build 20151230)
ダウンロードに失敗したのでログを確認。 ググると、時間が一致していない事が問題らしいので、サーバ側の時間を現時刻に合わせた。(ntp入れた)
tail /tmp/ruby-build.YYYYMMDDHHIISS.XXXX.log
curl: (60) Peer certificate cannot be authenticated with known CA certificates
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
現時刻があったことを確認して、下記コマンドを再度実行。
$ rbenv install 2.2.4
Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
Installing ruby-2.2.4...
Installed ruby-2.2.4 to /home/vagrant/.rbenv/versions/2.2.4
デフォルトで使用するrubyのバージョンを下記の通りにする。
$ rbenv global 2.2.4
バージョンを確認(rubyコマンドが見つからない場合は、$ source ~/.bash_profileする)
$ ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
以上でインストールは完了。