Example #1
0
def create_user():
    try:
        data = request.get_json()
        if (User.find_by_email(data["email"]) is not None
                or User.find_by_username(data["username"]) is not None):
            return response_with(resp.INVALID_INPUT_422)
        data["password"] = User.generate_hash(data["password"])
        user_schema = UserSchema()
        user = user_schema.load(data)
        token = generate_verification_token(data["email"])
        verification_email = url_for("user_routes.verify_email",
                                     token=token,
                                     _external=True)
        html = render_template_string(
            "<p>Welcome! Thanks for signing up. Please follow this link to activate your account:</p> <p><a href='{{ verification_email }}'>{{ verification_email }}</a></p> <br> <p>Thanks!</p>",
            verification_email=verification_email,
        )
        subject = "Please verify your email."
        send_email(to=user.email, subject=subject, template=html)
        result = user_schema.dump(user.create())
        return response_with(resp.SUCCESS_201, value={"user": result})
    except IntegrityError:
        return response_with(resp.INVALID_INPUT_422,
                             message="User already registered.")
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
Example #2
0
def test_user_schema_has_user_role_by_default(app):
    with app.app_context():
        schema = UserSchema()

        user = schema.load(
            {"first": "Emmy", "last": "Noether", "email": "*****@*****.**"}
        )

    assert user["roles"] == [{"name": "user"}]
Example #3
0
def register_user():
    schema = UserSchema()
    input_data = request.get_json()
    if 'uid' not in input_data['data']['attributes'].keys():
        data, err = schema.load(input_data)
        if err:
            return jsonify(err)
        try:
            user = auth.create_user(
                email=data['email'],
                email_verified=False,
                password=data['password'],
                display_name=data['username'],
            )
        except auth.AuthError as e:
            if e.code == 'USER_CREATE_ERROR':
                errmsg = 'User with email already exists'
            return ErrorResponse(
                FirebaseError(errmsg).message, 422, {
                    'Content-Type': 'application/json'
                }).respond()

        newUser = User(id_=user.uid,
                       username=data['username'],
                       email=user.email,
                       password=data['password'])

        if user.email in admins:
            newUser.siteAdmin = True

        newUser.save_to_db()

        if newUser.email in admins:
            perm = Permissions(isUser=True,
                               isAdmin=True,
                               user_permissions=newUser)
            perm.save_to_db()
        else:
            perm = Permissions(isUser=True, user_permissions=newUser)
            perm.save_to_db()

        return jsonify(schema.dump(newUser).data)
    else:
        schema = OAuthUserSchema()
        data, err = schema.load(input_data)
        if err:
            return jsonify(err)

        uid = input_data['data']['attributes']['uid']
        user_ = User.getUser(user_id=uid)
        if not user_:
            newUser = User(id_=uid,
                           username=data['username'],
                           email=data['email'],
                           password=data['password'],
                           photoURL=data['photoURL'])
            if data['email'] in admins:
                newUser.siteAdmin = True
            newUser.save_to_db()

            if newUser.email in admins:
                perm = Permissions(isUser=True,
                                   isAdmin=True,
                                   user_permissions=newUser)
                perm.save_to_db()
            else:
                perm = Permissions(isUser=True, user_permissions=newUser)
                perm.save_to_db()
        else:
            newUser = user_
        return jsonify(schema.dump(newUser).data)