|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | namespace Parse;
|
| 4 | +use Zend\I18n\Exception\ParseException; |
4 | 5 |
|
5 | 6 | /**
|
6 | 7 | * ParseUser - Representation of a user object stored on Parse.
|
@@ -116,7 +117,7 @@ public function signUp()
|
116 | 117 | }
|
117 | 118 |
|
118 | 119 | /**
|
119 |
| - * Logs in a and returns a valid ParseUser, or throws if invalid. |
| 120 | + * Logs in and returns a valid ParseUser, or throws if invalid. |
120 | 121 | *
|
121 | 122 | * @param string $username
|
122 | 123 | * @param string $password
|
@@ -145,6 +146,87 @@ public static function logIn($username, $password)
|
145 | 146 | return $user;
|
146 | 147 | }
|
147 | 148 |
|
| 149 | + /** |
| 150 | + * Logs in with Facebook details, or throws if invalid. |
| 151 | + * |
| 152 | + * @param string $id the Facebook user identifier |
| 153 | + * @param string $access_token the access token for this session |
| 154 | + * @param \DateTime $expiration_date defaults to 60 days |
| 155 | + * |
| 156 | + * @throws ParseException |
| 157 | + * |
| 158 | + * @return ParseUser |
| 159 | + */ |
| 160 | + public static function logInWithFacebook($id, $access_token, $expiration_date = null) |
| 161 | + { |
| 162 | + if (!$id) { |
| 163 | + throw new ParseException("Cannot log in Facebook user without an id."); |
| 164 | + } |
| 165 | + if (!$access_token) { |
| 166 | + throw new ParseException( |
| 167 | + "Cannot log in Facebook user without an access token." |
| 168 | + ); |
| 169 | + } |
| 170 | + if (!$expiration_date) { |
| 171 | + $expiration_date = new DateTime(); |
| 172 | + $expiration_date->setTimestamp(time() + 86400 * 60); |
| 173 | + } |
| 174 | + $data = ["facebook" => [ |
| 175 | + "id" => $id, "access_token" => $access_token, |
| 176 | + "expiration_date" => ParseClient::getProperDateFormat($expiration_date) |
| 177 | + ]]; |
| 178 | + $result = ParseClient::_request("POST", "/1/users", "", $data); |
| 179 | + $user = new ParseUser(); |
| 180 | + $user->_mergeAfterFetch($result); |
| 181 | + $user->handleSaveResult(true); |
| 182 | + ParseClient::getStorage()->set("user", $user); |
| 183 | + return $user; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Link the user with Facebook details. |
| 188 | + * |
| 189 | + * @param string $id the Facebook user identifier |
| 190 | + * @param string $access_token the access token for this session |
| 191 | + * @param \DateTime $expiration_date defaults to 60 days |
| 192 | + * @param boolean $useMasterKey whether to override security |
| 193 | + * |
| 194 | + * @throws ParseException |
| 195 | + * |
| 196 | + * @return ParseUser |
| 197 | + */ |
| 198 | + public function linkWithFacebook($id, $access_token, $expiration_date = null, $useMasterKey = false){ |
| 199 | + if (!$this->getObjectId()) { |
| 200 | + throw new ParseException("Cannot link an unsaved user, use ParseUser::logInWithFacebook"); |
| 201 | + } |
| 202 | + if (!$id) { |
| 203 | + throw new ParseException("Cannot link Facebook user without an id."); |
| 204 | + } |
| 205 | + if (!$access_token) { |
| 206 | + throw new ParseException( |
| 207 | + "Cannot link Facebook user without an access token." |
| 208 | + ); |
| 209 | + } |
| 210 | + if (!$expiration_date) { |
| 211 | + $expiration_date = new DateTime(); |
| 212 | + $expiration_date->setTimestamp(time() + 86400 * 60); |
| 213 | + } |
| 214 | + $data = ["authData" => |
| 215 | + ["facebook" => [ |
| 216 | + "id" => $id, "access_token" => $access_token, |
| 217 | + "expiration_date" => ParseClient::getProperDateFormat($expiration_date) |
| 218 | + ]] |
| 219 | + ]; |
| 220 | + $result = ParseClient::_request( |
| 221 | + "PUT", "/1/users/" + $this->getObjectId(), |
| 222 | + $this->getSessionToken(), $data, $useMasterKey |
| 223 | + ); |
| 224 | + $user = new ParseUser(); |
| 225 | + $user->_mergeAfterFetch($result); |
| 226 | + $user->handleSaveResult(true); |
| 227 | + return $user; |
| 228 | + } |
| 229 | + |
148 | 230 | /**
|
149 | 231 | * Logs in a user with a session token. Calls the /users/me route and if
|
150 | 232 | * valid, creates and returns the current user.
|
|
0 commit comments