From 621f6190ac75beaba7913820a4f62610f1d8cc32 Mon Sep 17 00:00:00 2001 From: RyujiAMANO Date: Thu, 17 Dec 2015 14:58:46 +0900 Subject: [PATCH 1/2] =?UTF-8?q?action=E5=86=85=E3=81=A7=E8=AA=8D=E8=A8=BC?= =?UTF-8?q?=E3=82=AD=E3=83=BC=E3=81=AB=E3=82=88=E3=82=8BGuard=E3=81=8C?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=BF=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Component/AuthorizationKeyComponent.php | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Controller/Component/AuthorizationKeyComponent.php b/Controller/Component/AuthorizationKeyComponent.php index 1b0ccc4..72ecfb1 100644 --- a/Controller/Component/AuthorizationKeyComponent.php +++ b/Controller/Component/AuthorizationKeyComponent.php @@ -189,10 +189,30 @@ public function startup(Controller $controller) { return; } + if ($this->model === null || $this->contentId === null) { + // model, contentIdが未設定なら抜ける + return; + } + $this->_guard(); + } + + public function guard($operationType, $modelName, $data, $additionalId = null) { + if(Hash::get($data, 'AuthorizationKey', false)) { + $id = $data[$modelName]['id']; + + $this->operationType = $operationType; + $this->model = $modelName; + $this->contentId = $id; + //$this->additionalId = $additionalId; + $this->_guard(); + } + } + + protected function _guard() { // 切り替え、埋め込みの場合は認証キー動作が発生するので // 指定されているModel、IDに該当する認証キー情報を取得しておく // - if ($controller->request->isGet()) { + if ($this->controller->request->isGet()) { // もしかしたら空データかもしれないが、ここではチェックしない // 後程入力された認証キーとの一致を調べるときに空データの場合は絶対一致しなくなって // 決して解除されないガードとなる @@ -200,13 +220,13 @@ public function startup(Controller $controller) { // 後から利用となるのでセッションに記録 //$this->_hashKey = Security::hash($controller->name . mt_rand() . microtime(), 'md5'); $this->_hashKey = Security::hash($this->model . $this->contentId . $this->additionalId, 'md5'); - $controller->Session->write( - 'AuthorizationKey.currentAuthorizationKey.' . $this->_hashKey, - $this->AuthorizationKey->getAuthorizationKeyByContentId($this->model, $this->contentId, $this->additionalId)); + $this->controller->Session->write( + 'AuthorizationKey.currentAuthorizationKey.' . $this->_hashKey, + $this->AuthorizationKey->getAuthorizationKeyByContentId($this->model, $this->contentId, $this->additionalId)); // Controllerにキーを探し出すためのハッシュキーを覚えておいてもらう // 実際の認証キーinputを生成するときに、この値がhiddenで埋め込まれる - $controller->request->data['AuthorizationKey']['authorization_hash'] = $this->_hashKey; + $this->controller->request->data['AuthorizationKey']['authorization_hash'] = $this->_hashKey; } // 埋め込み型の時 @@ -216,16 +236,15 @@ public function startup(Controller $controller) { } // 切り替え型のとき if ($this->operationType == AuthorizationKeyComponent::OPERATION_REDIRECT) { - $this->_redirectStartup($controller); + $this->_redirectStartup($this->controller); return; } // POPUP型の時 if ($this->operationType == AuthorizationKeyComponent::OPERATION_POPUP) { - $this->_popupStartup($controller); + $this->_popupStartup($this->controller); return; } } - /** * _redirectStartup * 認証に成功したあとの戻りURLをセッションに保存して From e6d249b909a854bd2aeee4aed8f192fcc1416f54 Mon Sep 17 00:00:00 2001 From: RyujiAMANO Date: Thu, 17 Dec 2015 16:16:05 +0900 Subject: [PATCH 2/2] reformat code --- .../Component/AuthorizationKeyComponent.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Controller/Component/AuthorizationKeyComponent.php b/Controller/Component/AuthorizationKeyComponent.php index 72ecfb1..37f948b 100644 --- a/Controller/Component/AuthorizationKeyComponent.php +++ b/Controller/Component/AuthorizationKeyComponent.php @@ -196,18 +196,37 @@ public function startup(Controller $controller) { $this->_guard(); } +/** + * guard リダイレクト型の場合にアクション内で実行をガードする。認証キー入力画面にリダイレクトし、認証が成功するとguard()以降のコードが実行されるようになる。 + * + * ## sample + * ``` + * $this->AuthorizationKey->guard('redirect', 'BlogEntry', $blogEntry, 'pdf'); + * ``` + * + * @param string $operationType 認証タイプ + * @param string $modelName モデル名 + * @param array $data モデルデータ + * @param null $additionalId 付加ID + * @return void + */ public function guard($operationType, $modelName, $data, $additionalId = null) { - if(Hash::get($data, 'AuthorizationKey', false)) { + if (Hash::get($data, 'AuthorizationKey', false)) { $id = $data[$modelName]['id']; $this->operationType = $operationType; $this->model = $modelName; $this->contentId = $id; - //$this->additionalId = $additionalId; + $this->additionalId = $additionalId; $this->_guard(); } } +/** + * 認証キーチェック + * + * @return void + */ protected function _guard() { // 切り替え、埋め込みの場合は認証キー動作が発生するので // 指定されているModel、IDに該当する認証キー情報を取得しておく