def get_identity(user):
    """Create an identity for a given user instance.

    Primarily useful for testing.
    """
    identity = Identity(user.id)

    if hasattr(user, 'id'):
        identity.provides.add(UserNeed(user.id))

    for role in getattr(user, 'roles', []):
        identity.provides.add(RoleNeed(role.name))

    identity.user = user
    return identity
Create an identity for a given user instance.

    Primarily useful for testing.