Implemented logout and generally fixed the in/out sequence. #69
[zzz-spline-users.git] / spline / plugins / users / __init__.py
1 from pkg_resources import resource_filename
2
3 from pylons import c, session
4
5 from spline.lib.plugin import PluginBase
6 from spline.lib.plugin import PluginBase, PluginLink, Priority
7 import spline.model as model
8 import spline.model.meta as meta
9
10 import spline.plugins.users.controllers.accounts
11 import spline.plugins.users.model
12
13 def add_routes_hook(map, *args, **kwargs):
14 """Hook to inject some of our behavior into the routes configuration."""
15 map.connect('/accounts/login_begin', controller='accounts', action='login_begin')
16 map.connect('/accounts/login_finish', controller='accounts', action='login_finish')
17 map.connect('/accounts/logout', controller='accounts', action='logout')
18
19 def check_userid_hook(action, **params):
20 """Hook to see if a user is logged in and, if so, stick a user object in
21 c.
22 """
23
24 c.user = None
25
26 if not 'user_id' in session:
27 return
28
29 user = meta.Session.query(model.User).get(session['user_id'])
30 if not user:
31 # Bogus id in the session somehow. Clear it
32 del session['user_id']
33 session.save()
34 return
35
36 c.user = user
37
38
39 class UsersPlugin(PluginBase):
40 def controllers(self):
41 return dict(
42 accounts = spline.plugins.users.controllers.accounts.AccountsController,
43 )
44
45 def model(self):
46 return [
47 spline.plugins.users.model.User,
48 spline.plugins.users.model.OpenID,
49 ]
50
51 def template_dirs(self):
52 return [
53 (resource_filename(__name__, 'templates'), Priority.NORMAL)
54 ]
55
56 def hooks(self):
57 return [
58 ('routes_mapping', Priority.NORMAL, add_routes_hook),
59 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
60 ]
61
62 def widgets(self):
63 return [
64 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
65 ]