diff --git a/src/Parse/ParseUser.php b/src/Parse/ParseUser.php index a59433dc..131d1b77 100644 --- a/src/Parse/ParseUser.php +++ b/src/Parse/ParseUser.php @@ -157,6 +157,33 @@ public static function logIn($username, $password) return $user; } + /** + * Uses the master key to log in and return a valid ParseUser, or throws if invalid. + * + * @param $userId + * + * @throws ParseException + * + * @return ParseUser + */ + public static function logInAs($userId) + { + if (!$userId) { + throw new ParseException( + 'Cannot log in as user with an empty user id', + 200 + ); + } + $data = ['userId' => $userId]; + $result = ParseClient::_request('POST', 'loginAs', '', json_encode($data), true); + $user = new static(); + $user->_mergeAfterFetch($result); + $user->handleSaveResult(true); + ParseClient::getStorage()->set('user', $user); + + return $user; + } + /** * Logs in with Facebook details, or throws if invalid. * diff --git a/tests/Parse/ParseUserTest.php b/tests/Parse/ParseUserTest.php index adf626cd..2e99f912 100644 --- a/tests/Parse/ParseUserTest.php +++ b/tests/Parse/ParseUserTest.php @@ -83,6 +83,33 @@ public function testLoginWrongPassword() ParseUser::logIn('asdf', 'bogus'); } + public function testLoginAsSuccess() + { + $user = new ParseUser(); + $user->setUsername('plainusername'); + $user->setPassword('plainpassword'); + $user->signUp(); + + $id = $user->getObjectId(); + $loggedInUser = ParseUser::logInAs($id); + $this->assertTrue($loggedInUser->isAuthenticated()); + $this->assertEquals('plainusername', $loggedInUser->get('username')); + + ParseUser::logOut(); + } + + public function testLoginAsEmptyUsername() + { + $this->expectException('Parse\ParseException', 'Cannot log in as user with an empty user id.'); + ParseUser::logInAs(''); + } + + public function testLoginAsNonexistentUser() + { + $this->expectException('Parse\ParseException', 'user not found.'); + ParseUser::logInAs('a1b2c3d4e5'); + } + public function testLoginWithFacebook() { $this->expectException(