def group_by_identity_provider(users):
    providers = {}
    for user in users:
        for provider in get_available_providers():
            key = get_provider_key(provider)
            if user.get(key, None):
                if provider in providers:
                    providers[provider] += 1
                else:
                    providers[provider] = 1

    return sorted(providers.items(), key=operator.itemgetter(1), reverse=True)
Exemple #2
0
def register_or_update(request, provider, user_id, info, default_url='/'):
    provider_key = get_provider_key(provider)
    user = user_from_provider_id(request.db, provider, user_id)
    if user is None:

        new_info = {'provider': provider, provider_key: user_id}
        for attribute in ('screen_name', 'first_name', 'last_name', 'email'):
            if attribute in info:
                new_info[attribute] = info[attribute]
            else:
                new_info[attribute] = ''

        request.session['user_info'] = new_info
        if 'next_url' not in request.session:
            request.session['next_url'] = default_url
        return HTTPFound(location=request.route_path('register_new_user'))
    else:
        changes = {'last_login': datetime.datetime.now(tz=utc)}

        ga = request.google_analytics
        if ga.is_in_session():
            if not ga.is_stored_in_user(user):
                changes.update(ga.get_user_attr(ga.show_in_session()))
            ga.clean_session()

        update_user(request.db, user, info, changes)

        if 'next_url' in request.session:
            next_url = request.session['next_url']
            del request.session['next_url']
        else:
            next_url = default_url

        request.session['current_provider'] = provider
        remember_headers = remember(request, str(user['_id']))
        return HTTPFound(location=next_url, headers=remember_headers)
Exemple #3
0
def user_from_provider_id(db, provider, user_id):
    provider_key = get_provider_key(provider)
    return db.users.find_one({provider_key: user_id})