Skip to content

Instantly share code, notes, and snippets.

@42milez
Last active August 29, 2015 14:27
Show Gist options
  • Save 42milez/8177579b67b3bd1a74b8 to your computer and use it in GitHub Desktop.
Save 42milez/8177579b67b3bd1a74b8 to your computer and use it in GitHub Desktop.
<?php
/* Aug. 21, 2015 - Akihiro TAKASE
*
* 本ファイルにはユーザー登録に関するアクションを扱うコントローラの定義が含まれます。
*
*/
use Fuel\Core\Security as Security;
class Controller_Users extends Controller {
// ユーザー登録用アクション
public function action_create() {
if (Input::method() == 'POST') {
// CSRF対策用トークンをチェック
if (Security::check_token()) {
// リクエストパラメータ(POST)に対してバリデーションを実施
$val = Model_User::validate('create');
if ($val->run()) {
// 入力値のサニタイズ(HTMLタグ、PHPタグ、およびHTMLエンティティを削除)
$filters = array('strip_tags', 'htmlentities');
$name = Security::clean(Input::post('name'), $filters);
$email = Security::clean(Input::post('email'), $filters);
// ハッシュ化パスワードの生成
$salt = Util_Hash::getSalt();
$hashedPassword = Util_Hash::getHashedPassword(Input::post('password'), $salt);
// リクエストパラメータからモデルのインスタンスを生成
$user = Model_User::forge(array(
'name' => $name,
'email' => $email,
'hashed_password' => $hashedPassword,
'salt' => $salt,
're' => false,
));
// 新規ユーザーを登録
if ($user and $user->save()) {
// 登録が正常に完了したことを告げるフラッシュメッセージをキューに追加し、
// 所定の場所へリダイレクトする
// ...
}
else {
// 登録に失敗したことを告げるフラッシュメッセージをキューに追加する
// ...
}
}
else {
// バリデーションに失敗したことを告げるフラッシュメッセージををキューに追加する
// ...
}
}
}
// CSRF対策用ワンタイムトークンを発行
$data['token_key'] = Config::get('security.csrf_token_key');
$data['token'] = Security::fetch_token();
// その他パラメータを View に渡す
$this->template->title = "新規ユーザー登録";
$this->template->content = View::forge('user/create', $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment