Updated login to work correctly with current spline. #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
18 def check_userid_hook(action, **params):
19 """Hook to see if a user is logged in and, if so, stick a user object in
20 c.
21 """
22
23 c.user = None
24
25 if not 'user_id' in session:
26 return
27
28 user = meta.Session.query(model.User).get(session['user_id'])
29 if not user:
30 # Bogus id in the session somehow. Clear it
31 del session['user_id']
32 session.save()
33 return
34
35 c.user = user
36
37
38 class UsersPlugin(PluginBase):
39 def controllers(self):
40 return dict(
41 accounts = spline.plugins.users.controllers.accounts.AccountsController,
42 )
43
44 def model(self):
45 return [
46 spline.plugins.users.model.User,
47 spline.plugins.users.model.OpenID,
48 ]
49
50 def template_dirs(self):
51 return [
52 (resource_filename(__name__, 'templates'), Priority.NORMAL)
53 ]
54
55 def hooks(self):
56 return [
57 ('routes_mapping', Priority.NORMAL, add_routes_hook),
58 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
59 ]
60
61 def widgets(self):
62 return [
63 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
64 ]