From 9a6c8e8a12e17ad239a43651008210a402434111 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 12:22:32 +0800 Subject: [PATCH 1/8] Add User#getRoles close #116 --- src/LeanCloud/User.php | 15 +++++++++++++++ test/UserTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/LeanCloud/User.php b/src/LeanCloud/User.php index e5e1da7..5087948 100644 --- a/src/LeanCloud/User.php +++ b/src/LeanCloud/User.php @@ -232,6 +232,21 @@ private static function clearCurrentUser() { self::setCurrentSessionToken(null); } + /** + * Get roles the user belongs to + * + * @return array Array of Role + */ + public function getRoles() { + if (!$this->getObjectId()) { + return array(); + } + $query = new Query("_Role"); + $query->equalTo("users", $this); + $roles = $query->find(); + return $roles; + } + /** * Log-in user by session token * diff --git a/test/UserTest.php b/test/UserTest.php index e22b5c0..8d5c1ff 100644 --- a/test/UserTest.php +++ b/test/UserTest.php @@ -2,6 +2,8 @@ use LeanCloud\Client; use LeanCloud\User; +use LeanCloud\Role; +use LeanCloud\ACL; use LeanCloud\File; use LeanCloud\Query; use LeanCloud\CloudException; @@ -191,6 +193,31 @@ public function testUnlinkService() { $user2->destroy(); } + public function testGetRoles() { + $user = new User(); + $user->setUsername("alice3"); + $user->setPassword("blabla"); + $user->signUp(); + + $role = new Role(); + $role->setName("test_role"); + $acl = new ACL(); + $acl->setPublicWriteAccess(true); + $acl->setPublicReadAccess(true); + + $role->setACL($acl); + $rel = $role->getUsers(); + $rel->add($user); + $role->save(); + $this->assertNotEmpty($role->getObjectId()); + + $roles = $user->getRoles(); + $this->assertEquals("test_role", $roles[0]->getName()); + + $user->destroy(); + $role->destroy(); + } + /* * Get current user with file attribute shall not * circularly invoke getCurrentUser. From 7577a877225195cd7f4cff5f487c24b19de00c50 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 14:15:10 +0800 Subject: [PATCH 2/8] Add User#isAuthenticated close #118 --- src/LeanCloud/User.php | 21 +++++++++++++++++++++ test/UserTest.php | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/LeanCloud/User.php b/src/LeanCloud/User.php index 5087948..33533aa 100644 --- a/src/LeanCloud/User.php +++ b/src/LeanCloud/User.php @@ -232,6 +232,27 @@ private static function clearCurrentUser() { self::setCurrentSessionToken(null); } + /** + * Test if user logged in and session token is valid. + * + * @return bool + */ + public function isAuthenticated() { + $token = $this->getSessionToken(); + if (!$token) { + return false; + } + try { + static::become($token); + } catch(CloudException $ex) { + if ($ex->getCode() === 211) { + return false; + } + throw ex; + } + return true; + } + /** * Get roles the user belongs to * diff --git a/test/UserTest.php b/test/UserTest.php index 8d5c1ff..3c786a3 100644 --- a/test/UserTest.php +++ b/test/UserTest.php @@ -218,6 +218,17 @@ public function testGetRoles() { $role->destroy(); } + public function testIsAuthenticated() { + $user = User::logIn("alice", "blabla"); + $this->assertTrue($user->isAuthenticated()); + + $user->mergeAfterFetch(array("sessionToken" => "invalid-token")); + $this->assertFalse($user->isAuthenticated()); + + $user = new User(); + $this->assertFalse($user->isAuthenticated()); + } + /* * Get current user with file attribute shall not * circularly invoke getCurrentUser. From ff0c2a2bc6c0c0f6c9c79e7c99af87576f9ae97a Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 16:16:38 +0800 Subject: [PATCH 3/8] Add User#refreshSessionToken close #117 --- src/LeanCloud/Client.php | 1 + src/LeanCloud/User.php | 9 +++++++++ test/UserTest.php | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/src/LeanCloud/Client.php b/src/LeanCloud/Client.php index 1ed250d..b9a7719 100644 --- a/src/LeanCloud/Client.php +++ b/src/LeanCloud/Client.php @@ -400,6 +400,7 @@ public static function request($method, $path, $data, } $reqId = rand(100,999); if (self::$debugMode) { + error_log("[DEBUG] HEADERS {$reqId}:" . json_encode($headersList)); error_log("[DEBUG] REQUEST {$reqId}: {$method} {$url} {$json}"); } $resp = curl_exec($req); diff --git a/src/LeanCloud/User.php b/src/LeanCloud/User.php index 33533aa..5d813d8 100644 --- a/src/LeanCloud/User.php +++ b/src/LeanCloud/User.php @@ -232,6 +232,15 @@ private static function clearCurrentUser() { self::setCurrentSessionToken(null); } + /** + * Refresh session token + */ + public function refreshSessionToken() { + $resp = Client::put("/users/{$this->getObjectId()}/refreshSessionToken", + null); + $this->mergeAfterFetch($resp); + } + /** * Test if user logged in and session token is valid. * diff --git a/test/UserTest.php b/test/UserTest.php index 3c786a3..e5f6f07 100644 --- a/test/UserTest.php +++ b/test/UserTest.php @@ -124,6 +124,14 @@ public function testBecome() { $this->assertEquals($user2, User::getCurrentUser()); } + public function testRefreshSessionToken() { + $user = User::logIn("alice", "blabla"); + $token = $user->getSessionToken(); + $user->refreshSessionToken(); + $this->assertNotEmpty($user->getSessionToken()); + $this->assertNotEquals($token, $user->getSessionToken()); + } + public function testLogOut() { $user = User::logIn("alice", "blabla"); $this->assertEquals($user, User::getCurrentUser()); From 88dce178cf269c336e384bf2be12de5c221c9484 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 16:59:20 +0800 Subject: [PATCH 4/8] Fix global session token not updated after refresh --- src/LeanCloud/User.php | 1 + test/UserTest.php | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/LeanCloud/User.php b/src/LeanCloud/User.php index 5d813d8..334204b 100644 --- a/src/LeanCloud/User.php +++ b/src/LeanCloud/User.php @@ -239,6 +239,7 @@ public function refreshSessionToken() { $resp = Client::put("/users/{$this->getObjectId()}/refreshSessionToken", null); $this->mergeAfterFetch($resp); + static::saveCurrentUser($this); } /** diff --git a/test/UserTest.php b/test/UserTest.php index e5f6f07..1023802 100644 --- a/test/UserTest.php +++ b/test/UserTest.php @@ -125,11 +125,16 @@ public function testBecome() { } public function testRefreshSessionToken() { - $user = User::logIn("alice", "blabla"); + $user = new User(); + $user->setUsername("alice4"); + $user->setPassword("blabla"); + $user->signUp(); + $token = $user->getSessionToken(); $user->refreshSessionToken(); $this->assertNotEmpty($user->getSessionToken()); $this->assertNotEquals($token, $user->getSessionToken()); + $user->destroy(); } public function testLogOut() { From 71379af9357b08976d391b52b33513b2a32b9fdc Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 17:02:46 +0800 Subject: [PATCH 5/8] Add preserved_keys close #119 --- src/LeanCloud/Object.php | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/LeanCloud/Object.php b/src/LeanCloud/Object.php index a302758..4c0f40a 100644 --- a/src/LeanCloud/Object.php +++ b/src/LeanCloud/Object.php @@ -13,6 +13,9 @@ * */ class Object { + + const PRESERVED_KEYS = array("objectId", "ACL", + "updatedAt", "createdAt"); /** * Map of registered className to class. * @@ -141,15 +144,15 @@ public function getClassName() { } public function disableBeforeHook() { - $this->set("__before", - Client::signHook("__before_for_{$this->getClassName()}", - round(microtime(true) * 1000))); + $this->_set("__before", + Client::signHook("__before_for_{$this->getClassName()}", + round(microtime(true) * 1000))); } public function disableAfterHook() { - $this->set("__after", - Client::signHook("__after_for_{$this->getClassName()}", - round(microtime(true) * 1000))); + $this->_set("__after", + Client::signHook("__after_for_{$this->getClassName()}", + round(microtime(true) * 1000))); } /** @@ -230,6 +233,14 @@ public function getUpdatedAt() { return $this->get("updatedAt"); } + private function _set($key, $val) { + if (!($val instanceof IOperation)) { + $val = new SetOperation($key, $val); + } + $this->_applyOperation($val); + return $this; + } + /** * Set field value by key * @@ -239,14 +250,10 @@ public function getUpdatedAt() { * @throws RuntimeException */ public function set($key, $val) { - if (in_array($key, array("objectId", "createdAt", "updatedAt"))) { + if (in_array($key, self::PRESERVED_KEYS)) { throw new \RuntimeException("Preserved field could not be set."); } - if (!($val instanceof IOperation)) { - $val = new SetOperation($key, $val); - } - $this->_applyOperation($val); - return $this; + return $this->_set($key, $val); } /** @@ -256,7 +263,7 @@ public function set($key, $val) { * @return self */ public function setACL(ACL $acl) { - return $this->set("ACL", $acl); + return $this->_set("ACL", $acl); } /** From d55643732d7d5b6bf3dba3de7a0efac604d38120 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 17:36:02 +0800 Subject: [PATCH 6/8] Remove side effects in User#isAuthenticated --- src/LeanCloud/User.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/LeanCloud/User.php b/src/LeanCloud/User.php index 334204b..377bb1d 100644 --- a/src/LeanCloud/User.php +++ b/src/LeanCloud/User.php @@ -253,7 +253,8 @@ public function isAuthenticated() { return false; } try { - static::become($token); + $resp = Client::get("/users/me", + array("session_token" => $token)); } catch(CloudException $ex) { if ($ex->getCode() === 211) { return false; From 997d72fd3d8ab382feb0deaa0de753e1f9b52d94 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 16 Nov 2016 17:36:38 +0800 Subject: [PATCH 7/8] Use setACL prompt --- src/LeanCloud/Object.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/LeanCloud/Object.php b/src/LeanCloud/Object.php index 4c0f40a..aff7acb 100644 --- a/src/LeanCloud/Object.php +++ b/src/LeanCloud/Object.php @@ -250,6 +250,10 @@ private function _set($key, $val) { * @throws RuntimeException */ public function set($key, $val) { + if ($key === "ACL") { + throw new \RuntimeException("`ACL` is preserved,". + " please use setACL instead."); + } if (in_array($key, self::PRESERVED_KEYS)) { throw new \RuntimeException("Preserved field could not be set."); } From 1225abcfe516510c1063c0d3896a7d9377b07d02 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Thu, 17 Nov 2016 15:15:32 +0800 Subject: [PATCH 8/8] Allow set ACL --- src/LeanCloud/Object.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/LeanCloud/Object.php b/src/LeanCloud/Object.php index aff7acb..be8df91 100644 --- a/src/LeanCloud/Object.php +++ b/src/LeanCloud/Object.php @@ -14,8 +14,7 @@ */ class Object { - const PRESERVED_KEYS = array("objectId", "ACL", - "updatedAt", "createdAt"); + const PRESERVED_KEYS = array("objectId", "updatedAt", "createdAt"); /** * Map of registered className to class. * @@ -234,6 +233,10 @@ public function getUpdatedAt() { } private function _set($key, $val) { + if ($key === "ACL" && + !($val instanceof ACL)) { + throw new RuntimeException("Invalid ACL."); + } if (!($val instanceof IOperation)) { $val = new SetOperation($key, $val); } @@ -250,10 +253,6 @@ private function _set($key, $val) { * @throws RuntimeException */ public function set($key, $val) { - if ($key === "ACL") { - throw new \RuntimeException("`ACL` is preserved,". - " please use setACL instead."); - } if (in_array($key, self::PRESERVED_KEYS)) { throw new \RuntimeException("Preserved field could not be set."); }