Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / vigiboard_controller.py @ b8500d1a

History | View | Annotate | Download (2.96 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 catwalk.tg2 import Catwalk
11
from repoze.what import predicates
12

    
13
from vigiboard.lib.base import BaseController
14
from vigiboard.model import DBSession 
15
from vigiboard.controllers.error import ErrorController
16
from vigiboard import model
17
from vigiboard.controllers.secure import SecureController
18
class VigiboardRootController(BaseController):
19
    """
20
    The root controller for the vigiboard application.
21
    
22
    All the other controllers and WSGI applications should be mounted on this
23
    controller. For example::
24
    
25
        panel = ControlPanelController()
26
        another_app = AnotherWSGIApplication()
27
    
28
    Keep in mind that WSGI applications shouldn't be mounted directly: They
29
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
30
    
31
    """
32
    secc = SecureController()
33
    
34
    admin = Catwalk(model, DBSession)
35
    
36
    error = ErrorController()
37

    
38
    @expose('vigiboard.templates.authentication')
39
    def auth(self):
40
        """Display some information about auth* on this application."""
41
        return dict(page='auth')
42

    
43
    @expose('vigiboard.templates.index')
44
    @require(predicates.has_permission('manage', msg=l_('Only for managers')))
45
    def manage_permission_only(self, **kw):
46
        """Illustrate how a page for managers only works."""
47
        return dict(page='managers stuff')
48

    
49
    @expose('vigiboard.templates.index')
50
    @require(predicates.is_user('editor', msg=l_('Only for the editor')))
51
    def editor_user_only(self, **kw):
52
        """Illustrate how a page exclusive for the editor works."""
53
        return dict(page='editor stuff')
54

    
55
    @expose('vigiboard.templates.login')
56
    def login(self, came_from=url('/')):
57
        """Start the user login."""
58
        login_counter = request.environ['repoze.who.logins']
59
        if login_counter > 0:
60
            flash(_('Wrong credentials'), 'warning')
61
        return dict(page='login', login_counter=str(login_counter),
62
                    came_from=came_from)
63
    
64
    @expose()
65
    def post_login(self, came_from=url('/')):
66
        """
67
        Redirect the user to the initially requested page on successful
68
        authentication or redirect her back to the login page if login failed.
69
        
70
        """
71
        if not request.identity:
72
            login_counter = request.environ['repoze.who.logins'] + 1
73
            redirect(url('/login', came_from=came_from, __logins=login_counter))
74
        userid = request.identity['repoze.who.userid']
75
        flash(_('Welcome back, %s!') % userid)
76
        redirect(came_from)
77

    
78
    @expose()
79
    def post_logout(self, came_from=url('/')):
80
        """
81
        Redirect the user to the initially requested page on logout and say
82
        goodbye as well.
83
        
84
        """
85
        flash(_('We hope to see you soon!'))
86
        redirect(came_from)
87