Search code examples
pythonauthenticationturbogears2repoze.who

Turbogears change user via function calls


In TG1, you could change the logged in user by doing something like:

identity.set_current_identity(identity)

Is it possible to do something similar in TG2? It seems like repoze.who should provide something similar, but I can't seem to find the magic words.

Alternatively, is their any documentation on how to use repoze.who in any way other than the usual approach of asking for a login and password, and then submitting that data to /login_handler. Where is the code that processes login_handler?

Thanks!


Solution

  • You can use the identifier to remember a new user. By default in TurboGears2 users are identified by their username, so you can switch them using the username.

    def force_user(user_name):
        request = tg.request
        response = tg.response
    
        request.cookies.clear()
        authentication_plugins = request.environ['repoze.who.plugins']
        identifier = authentication_plugins['main_identifier']
    
        try:
            response.headers = identifier.remember(request.environ, {'repoze.who.userid':user_name})
        except:
            pass