vigiboard / vigiboard / controllers / vigiboard_controller.py @ 10848680
History | View | Annotate | Download (2.31 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
# vim:set expandtab tabstop=4 shiftwidth=4:
|
3 |
"""
|
4 |
Controller for authentification
|
5 |
"""
|
6 |
|
7 |
from tg import expose, flash, require, url, request, redirect |
8 |
|
9 |
from pylons.i18n import ugettext as _, lazy_ugettext as l_ |
10 |
from repoze.what import predicates |
11 |
|
12 |
from vigiboard.lib.base import BaseController |
13 |
from vigiboard.model import DBSession, Access |
14 |
from vigiboard.controllers.error import ErrorController |
15 |
|
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=url('/')): |
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=url('/')): |
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(url('/login', came_from=came_from, __logins=login_counter))
|
50 |
userid = request.identity['repoze.who.userid']
|
51 |
Access.add_login(userid, request.remote_addr, 'Vigiboard')
|
52 |
flash(_('Welcome back, %s!') % userid)
|
53 |
redirect(came_from) |
54 |
|
55 |
@expose()
|
56 |
def post_logout(self, came_from=url('/')): |
57 |
"""
|
58 |
Redirect the user to the initially requested page on logout and say
|
59 |
goodbye as well.
|
60 |
"""
|
61 |
# XXX Ne fonctionne pas, l'identité est déjà oubliée arrivé ici.
|
62 |
# userid = request.identity['repoze.who.userid']
|
63 |
# Access.add_logout(userid, request.remote_addr, 'Vigiboard')
|
64 |
flash(_('We hope to see you soon!'))
|
65 |
redirect(came_from) |
66 |
|