1 from pkg_resources
import resource_filename
3 from pylons
import config
, session
, tmpl_context
as c
5 from spline
.lib
.plugin
import PluginBase
, PluginLink
, Priority
6 import spline
.model
.meta
as meta
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
13 def add_routes_hook(map, *args
, **kwargs
):
14 """Hook to inject some of our behavior into the routes configuration."""
15 require_GET
= dict(conditions
=dict(method
=['GET']))
16 require_POST
= dict(conditions
=dict(method
=['POST']))
19 map.connect('/accounts/login', controller
='accounts', action
='login')
20 map.connect('/accounts/login_begin', controller
='accounts', action
='login_begin')
21 map.connect('/accounts/login_finish', controller
='accounts', action
='login_finish')
22 map.connect('/accounts/logout', controller
='accounts', action
='logout')
25 map.connect(r
'/users/{id:\d+};{name}/edit', controller
='users', action
='profile_edit', **require_GET
)
26 map.connect(r
'/users/{id:\d+};{name}/edit', controller
='users', action
='profile_edit_commit', **require_POST
)
29 map.connect('/users', controller
='users', action
='list')
30 map.connect(r
'/users/{id:\d+};{name}', controller
='users', action
='profile')
31 map.connect(r
'/users/{id:\d+}', controller
='users', action
='profile')
34 map.connect('/admin/users/permissions', controller
='admin_users', action
='permissions')
36 def monkeypatch_user_hook(config
, *args
, **kwargs
):
37 """Hook to tell the `User` model who the root user is."""
39 users_model
.User
._root_user_id \
40 = int(config
['spline-users.root_user_id'])
42 # No config set; oh well!
45 def check_userid_hook(action
, **params
):
46 """Hook to see if a user is logged in and, if so, stick a user object in
50 if not 'user_id' in session
:
51 c
.user
= users_model
.AnonymousUser()
54 user
= meta
.Session
.query(users_model
.User
).get(session
['user_id'])
56 # Bogus id in the session somehow. Clear it
57 del session
['user_id']
60 c
.user
= users_model
.AnonymousUser()
66 class UsersPlugin(PluginBase
):
67 def controllers(self
):
69 accounts
= splinext
.users
.controllers
.accounts
.AccountsController
,
70 admin_users
= splinext
.users
.controllers
.admin
.AdminController
,
71 users
= splinext
.users
.controllers
.users
.UsersController
,
74 def template_dirs(self
):
76 (resource_filename(__name__
, 'templates'), Priority
.NORMAL
)
81 ('routes_mapping', Priority
.NORMAL
, add_routes_hook
),
82 ('after_setup', Priority
.NORMAL
, monkeypatch_user_hook
),
83 ('before_controller', Priority
.VERY_FIRST
, check_userid_hook
),
88 ('page_header', Priority
.NORMAL
, 'widgets/user_state.mako'),