Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / vigiboard_controller.py @ 3d2eb396

History | View | Annotate | Download (2.29 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4: 
3
"""
4
Controller for authentification
5
"""
6
import logging
7
from tg import expose, flash, request, redirect
8
from pylons.i18n import ugettext as _
9

    
10
from vigilo.turbogears.controllers import BaseController
11
from vigilo.turbogears.controllers.error import ErrorController
12

    
13
LOGGER = logging.getLogger(__name__)
14

    
15
# pylint: disable-msg=R0201
16
class VigiboardRootController(BaseController):
17
    """
18
    The root controller for the vigiboard application.
19
    
20
    All the other controllers and WSGI applications should be mounted on this
21
    controller. For example::
22
    
23
        panel = ControlPanelController()
24
        another_app = AnotherWSGIApplication()
25
    
26
    Keep in mind that WSGI applications shouldn't be mounted directly: They
27
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
28
    """
29

    
30
    error = ErrorController()
31

    
32
    @expose('login.html')
33
    def login(self, came_from='/'):
34
        """Start the user login."""
35
        login_counter = request.environ['repoze.who.logins']
36
        if login_counter > 0:
37
            flash(_('Wrong credentials'), 'warning')
38
        return dict(page='login', login_counter=str(login_counter),
39
                    came_from=came_from)
40

    
41
    @expose()
42
    def post_login(self, came_from='/'):
43
        """
44
        Redirect the user to the initially requested page on successful
45
        authentication or redirect her back to the login page if login failed.
46
        """
47
        if not request.identity:
48
            login_counter = request.environ['repoze.who.logins'] + 1
49
            redirect('/login', came_from=came_from, __logins=login_counter)
50
        userid = request.identity['repoze.who.userid']
51
        LOGGER.info(_('"%(username)s" logged in (from %(IP)s)') % {
52
                'username': userid,
53
                'IP': request.remote_addr,
54
            })
55
        flash(_('Welcome back, %s!') % userid)
56
        redirect(came_from)
57

    
58
    @expose()
59
    def post_logout(self, came_from='/'):
60
        """
61
        Redirect the user to the initially requested page on logout and say
62
        goodbye as well.
63
        """
64
        LOGGER.info(_('Some user logged out (from %(IP)s)') % {
65
                'IP': request.remote_addr,
66
            })
67
        flash(_('We hope to see you soon!'))
68
        redirect(came_from)
69