Beispiel #1
0
 /**
  * @throws UserSignupException if the user could not be signed up, with a reason
  * @throws UserAlreadyExistsException if the user already exists in the database
  * @return the created {@link User}
  */
 static function trySignup(\Db\Connection $db, OAuth2Providers $provider)
 {
     $identity = UserOAuth2::auth($provider->getProvider());
     if (!$identity) {
         throw new UserSignupException("Could not login with OAuth2.");
     }
     $email = $identity->email;
     if ($email || \Openclerk\Config::get('users_require_email', false)) {
         if (!$email) {
             throw new UserSignupException("No email address found.");
         }
         if (!is_valid_email($email)) {
             throw new UserSignupException("That is not a valid email.");
         }
         // does a user already exist with this email?
         $q = $db->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
         $q->execute(array($email));
         if ($q->fetch()) {
             throw new UserAlreadyExistsException("That email '" . $email . "' is already in use.");
         }
     }
     $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 user
     $q = $db->prepare("INSERT INTO users SET email=?");
     $q->execute(array($email));
     $user_id = $db->lastInsertId();
     // create a new identity
     $q = $db->prepare("INSERT INTO user_oauth2_identities SET user_id=?, provider=?, uid=?");
     $q->execute(array($user_id, $provider->getKey(), $uid));
     return User::findUser($db, $user_id);
 }