From 62364c7288b5a7795d34829abbf9c68c1816a4c1 Mon Sep 17 00:00:00 2001 From: ryu818 Date: Mon, 30 Mar 2020 10:41:51 +0900 Subject: [PATCH] =?UTF-8?q?change:=20=EF=BC=92=E9=87=8D=E7=99=BB=E9=8C=B2?= =?UTF-8?q?=E3=81=95=E3=81=9B=E3=81=AA=E3=81=84=E5=88=B6=E5=BE=A1=E3=82=92?= =?UTF-8?q?session=5Fid=E3=81=A7=E5=88=A4=E6=96=AD=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=81=AE=E3=81=A7=E3=81=AF=E3=81=AA=E3=81=8F=E3=80=81=E3=82=BB?= =?UTF-8?q?=E3=83=83=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AB=E7=99=BB=E9=8C=B2?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=81=9F=E3=82=AD=E3=83=BC=E3=81=A7=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=20https://github.com/researchmap/RmNetCommons3/issues?= =?UTF-8?q?/1750#issuecomment-596823362?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/LikesController.php | 22 ++++++++++++++++++++-- Model/Like.php | 10 +++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 4fc087f..57bfe2d 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -47,8 +47,8 @@ public function like() { if (! $this->request->is('post')) { return $this->throwBadRequest(); } - - if ($this->Like->existsLike($this->data['Like']['content_key'])) { + $md5SessionKey = $this->__getKey(); + if ($this->Like->existsLike($this->data['Like']['content_key'], $md5SessionKey)) { return; } @@ -58,9 +58,27 @@ public function like() { 'conditions' => array('content_key' => $data['Like']['content_key']) )); $data = Hash::merge($like, $data); + if (!Current::read('User.id')) { + $data['LikesUser']['session_key'] = $md5SessionKey; + } if ($this->Like->saveLike($data)) { return; } $this->NetCommons->handleValidationError($this->Like->validationErrors); } + +/** + * session_keyを返す + * + * @return string + */ + private function __getKey() { + $md5SessionKey = $this->Session->read('Likes.md5_session_key'); + if ($md5SessionKey) { + return $md5SessionKey; + } + $md5SessionKey = md5(CakeSession::id()); + $this->Session->write('Likes.md5_session_key', $md5SessionKey); + return $md5SessionKey; + } } diff --git a/Model/Like.php b/Model/Like.php index 68fc326..fe99f0c 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -176,9 +176,11 @@ public function afterSave($created, $options = array()) { * Exists like data * * @param string $contentKey Content key of each plugin. + * @param string $md5SessionKey md5(SessionKey) + * @see https://github.com/researchmap/RmNetCommons3/issues/1750#issuecomment-596823362 * @return bool */ - public function existsLike($contentKey) { + public function existsLike($contentKey, $md5SessionKey = null) { $this->LikesUser = ClassRegistry::init('Likes.LikesUser'); $joinConditions = array( @@ -186,8 +188,11 @@ public function existsLike($contentKey) { ); if (Current::read('User.id')) { $joinConditions[$this->LikesUser->alias . '.user_id'] = Current::read('User.id'); + } elseif (!is_null($md5SessionKey)) { + $joinConditions[$this->LikesUser->alias . '.session_key'] = (string)$md5SessionKey; } else { - $joinConditions[$this->LikesUser->alias . '.session_key'] = (string)CakeSession::id(); + // 常にインクリメント + return false; } $count = $this->find('count', array( @@ -224,7 +229,6 @@ public function saveLike($data) { $this->begin(); //バリデーション - $data['LikesUser']['session_key'] = CakeSession::id(); $this->set($data); if (! $this->validates()) { $this->rollback();