Added very basic permission system. #71
[zzz-spline-users.git] / splinext / users / __init__.py
1 from pkg_resources import resource_filename
2
3 from pylons import c, config, session
4
5 from spline.lib.plugin import PluginBase, PluginLink, Priority
6 import spline.model.meta as meta
7
8 import splinext.users.controllers.accounts
9 import splinext.users.controllers.admin
10 import splinext.users.controllers.users
11 from splinext.users import model as users_model
12
13 def add_routes_hook(map, *args, **kwargs):
14 """Hook to inject some of our behavior into the routes configuration."""
15 def id_is_numeric(environ, result):
16 try:
17 int(result['id'])
18 return True
19 except (KeyError, ValueError):
20 return False
21
22 # Login, logout
23 map.connect('/accounts/login', controller='accounts', action='login')
24 map.connect('/accounts/login_begin', controller='accounts', action='login_begin')
25 map.connect('/accounts/login_finish', controller='accounts', action='login_finish')
26 map.connect('/accounts/logout', controller='accounts', action='logout')
27
28 # Self-admin
29 map.connect('/users/{id};{name}/edit', controller='users', action='profile_edit',
30 conditions=dict(function=id_is_numeric))
31
32 # Public user pages
33 map.connect('/users', controller='users', action='list')
34 map.connect('/users/{id};{name}', controller='users', action='profile',
35 conditions=dict(function=id_is_numeric))
36 map.connect('/users/{id}', controller='users', action='profile',
37 conditions=dict(function=id_is_numeric))
38
39 # Big-boy admin
40 map.connect('/admin/users/permissions', controller='admin_users', action='permissions')
41
42 def monkeypatch_user_hook(*args, **kwargs):
43 """Hook to tell the `User` model who the root user is."""
44 try:
45 users_model.User._root_user_id \
46 = int(config['spline-users.root_user_id'])
47 except KeyError:
48 # No config set; oh well!
49 pass
50
51 def check_userid_hook(action, **params):
52 """Hook to see if a user is logged in and, if so, stick a user object in
53 c.
54 """
55
56 if not 'user_id' in session:
57 c.user = users_model.AnonymousUser()
58 return
59
60 user = meta.Session.query(users_model.User).get(session['user_id'])
61 if not user:
62 # Bogus id in the session somehow. Clear it
63 del session['user_id']
64 session.save()
65
66 c.user = users_model.AnonymousUser()
67 return
68
69 c.user = user
70
71
72 class UsersPlugin(PluginBase):
73 def controllers(self):
74 return dict(
75 accounts = splinext.users.controllers.accounts.AccountsController,
76 admin_users = splinext.users.controllers.admin.AdminController,
77 users = splinext.users.controllers.users.UsersController,
78 )
79
80 def template_dirs(self):
81 return [
82 (resource_filename(__name__, 'templates'), Priority.NORMAL)
83 ]
84
85 def hooks(self):
86 return [
87 ('routes_mapping', Priority.NORMAL, add_routes_hook),
88 ('after_setup', Priority.NORMAL, monkeypatch_user_hook),
89 ('before_controller', Priority.VERY_FIRST, check_userid_hook),
90 ]
91
92 def widgets(self):
93 return [
94 ('page_header', Priority.NORMAL, 'widgets/user_state.mako'),
95 ]