예제 #1
0
    def invite(cls, team, email, role, inviter):
        invitee = User.lookup(email)
        if (not invitee):
            member = cls(team=team,
                         invite_email=email,
                         role=role,
                         inviter=inviter)
        else:
            member = cls(team=team, user=invitee, role=role, inviter=inviter)

        db.session.add(member)
        db.session.commit()
        InviteEmail(member).send()
예제 #2
0
    def validate(self):
        check_validate = super(SignupForm, self).validate()

        # if our field validators do not pass
        if not check_validate:
            return False

        # Does the user exist already? Must return false,
        # otherwise we'll allow anyone to sign in
        user = User.lookup(self.email.data)
        if user:
            self.email.errors.append('That email already has an account')
            return False

        return True
예제 #3
0
def google_logged_in(blueprint, token):
    if not token:
        flash("Failed to log in.", category="warning")
        return False

    resp = blueprint.session.get("/oauth2/v2/userinfo")
    if not resp.ok:
        msg = "Failed to fetch user info."
        flash(msg, category="warning")
        return False

    google_info = resp.json()
    google_user_id = google_info["id"]

    # Find this OAuth token in the database, or create it
    query = OAuth.query.filter_by(provider=blueprint.name,
                                  provider_user_id=google_user_id)
    try:
        oauth = query.one()
    except NoResultFound:
        google_user_login = str(google_info["email"])
        oauth = OAuth(
            provider=blueprint.name,
            provider_user_id=google_user_id,
            provider_user_login=google_user_login,
            token=token,
        )

    existing_user = User.lookup(google_info["email"])
    # Since google verifies their primary emails, we can be more assured that we can directly login a user.

    if oauth.user:
        login_user(oauth.user)
        flash("Welcome back.", 'success')
    elif current_user.is_authenticated and current_user.email == google_info[
            "email"]:
        oauth.user = current_user
        db.session.add(oauth)
        db.session.commit()
        flash("Successfully linked Google account.", 'success')
    elif existing_user and existing_user.email == google_info['email']:
        oauth.user = existing_user
        db.session.add(oauth)
        db.session.commit()
        login_user(existing_user)
        flash("Successfully signed in as {}".format(existing_user.email),
              'success')
    else:
        # Create a new local user account for this user
        user = User(email=google_info["email"],
                    name=google_info["name"],
                    email_confirmed=google_info["verified_email"])
        # Associate the new local user account with the OAuth token
        oauth.user = user
        # Save and commit our database models
        db.session.add_all([user, oauth])
        db.session.commit()
        # Log in the new local user account
        login_user(user)
        flash("Welcome to appname!", 'success')

    # Disable Flask-Dance's default behavior for saving the OAuth token
    return False