Exemple #1
0
 /**
  * @throws UserSignupException if the user could not be signed up, with a reason
  * @throws UserAlreadyExistsException if the identity already exists in the database
  */
 static function addIdentity(\Db\Connection $db, User $user, OAuth2Providers $provider)
 {
     if (!$user) {
         throw new \InvalidArgumentException("No user provided.");
     }
     $identity = UserOAuth2::auth($provider->getProvider());
     if (!$identity) {
         throw new UserSignupException("Could not login with OAuth2.");
     }
     $uid = $identity->uid;
     if (!$uid) {
         throw new UserSignupException("No UID found.");
     }
     // does such an identity already exist?
     $q = $db->prepare("SELECT * FROM user_oauth2_identities WHERE provider=? AND uid=? LIMIT 1");
     $q->execute(array($provider->getKey(), $uid));
     if ($q->fetch()) {
         throw new UserAlreadyExistsException("That OAuth2 identity is already in use.");
     }
     // create a new identity
     $q = $db->prepare("INSERT INTO user_oauth2_identities SET user_id=?, provider=?, uid=?");
     $q->execute(array($user->getId(), $provider->getKey(), $uid));
     return true;
 }