Added a hook for sticking the logged-in user into c.
[zzz-spline-users.git] / spline / plugins / users / __init__.py
1 from pylons import c, session
2
3 from spline.lib.plugin import PluginBase
4 import spline.model as model
5 import spline.model.meta as meta
6
7 import controllers.accounts
8 import model as user_model
9
10 def check_userid_hook(action, **params):
11 """Hook to see if a user is logged in and, if so, stick a user object in
12 c.
13 """
14
15 if not 'user_id' in session:
16 return
17
18 user = meta.Session.query(model.User).get(session['user_id'])
19 if not user:
20 # Bogus id in the session somehow. Clear it
21 del session['user_id']
22 session.save()
23 return
24
25 c.user = user
26
27
28 class UsersPlugin(PluginBase):
29 def controllers(self):
30 return dict(
31 accounts = controllers.accounts.AccountsController,
32 )
33
34 def model(self):
35 return [user_model.User, user_model.OpenID]
36
37 def hooks(self):
38 return [
39 ('before_controller', 1, check_userid_hook),
40 ]