1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
""" |
3
|
|
|
Global configuration file for TG2-specific settings in pyjobsweb. |
4
|
|
|
|
5
|
|
|
This file complements development/deployment.ini. |
6
|
|
|
|
7
|
|
|
""" |
8
|
|
|
from tg.configuration import AppConfig |
9
|
|
|
|
10
|
|
|
import pyjobsweb |
11
|
|
|
from pyjobsweb import model |
12
|
|
|
|
13
|
|
|
base_config = AppConfig() |
14
|
|
|
base_config.renderers = [] |
15
|
|
|
|
16
|
|
|
# True to prevent dispatcher from striping extensions |
17
|
|
|
# For example /socket.io would be served by "socket_io" |
18
|
|
|
# method instead of "socket". |
19
|
|
|
base_config.disable_request_extensions = False |
20
|
|
|
|
21
|
|
|
# Set None to disable escaping punctuation characters to "_" |
22
|
|
|
# when dispatching methods. |
23
|
|
|
# Set to a function to provide custom escaping. |
24
|
|
|
base_config.dispatch_path_translator = True |
25
|
|
|
|
26
|
|
|
base_config.prefer_toscawidgets2 = True |
27
|
|
|
|
28
|
|
|
base_config.package = pyjobsweb |
29
|
|
|
|
30
|
|
|
# Enable json in expose |
31
|
|
|
base_config.renderers.append('json') |
32
|
|
|
# Enable genshi in expose to have a lingua franca |
33
|
|
|
# for extensions and pluggable apps. |
34
|
|
|
# You can remove this if you don't plan to use it. |
35
|
|
|
base_config.renderers.append('genshi') |
36
|
|
|
|
37
|
|
|
# Set the default renderer |
38
|
|
|
base_config.default_renderer = 'mako' |
39
|
|
|
base_config.renderers.append('mako') |
40
|
|
|
# Configure the base SQLALchemy Setup |
41
|
|
|
base_config.use_sqlalchemy = True |
42
|
|
|
base_config.model = pyjobsweb.model |
43
|
|
|
base_config.DBSession = pyjobsweb.model.DBSession |
44
|
|
|
# Configure the authentication backend |
45
|
|
|
base_config.auth_backend = 'sqlalchemy' |
46
|
|
|
# YOU MUST CHANGE THIS VALUE IN PRODUCTION TO SECURE YOUR APP |
47
|
|
|
base_config.sa_auth.cookie_secret = "61e866fc-c1ef-41f1-90ef-46054f5ab28d" |
48
|
|
|
# what is the class you want to use to search for users in the database |
49
|
|
|
base_config.sa_auth.user_class = model.User |
50
|
|
|
|
51
|
|
|
from tg.configuration.auth import TGAuthMetadata |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
# This tells to TurboGears how to retrieve the data for your user |
55
|
|
|
class ApplicationAuthMetadata(TGAuthMetadata): |
56
|
|
|
def __init__(self, sa_auth): |
57
|
|
|
self.sa_auth = sa_auth |
58
|
|
|
|
59
|
|
|
def authenticate(self, environ, identity): |
60
|
|
|
login = identity['login'] |
61
|
|
|
user = self.sa_auth.dbsession.query(self.sa_auth.user_class).filter_by( |
62
|
|
|
user_name=login |
63
|
|
|
).first() |
64
|
|
|
|
65
|
|
|
if not user: |
66
|
|
|
login = None |
67
|
|
|
elif not user.validate_password(identity['password']): |
68
|
|
|
login = None |
69
|
|
|
|
70
|
|
|
if login is None: |
71
|
|
|
try: |
72
|
|
|
from urllib.parse import parse_qs, urlencode |
73
|
|
|
except ImportError: |
74
|
|
|
from urlparse import parse_qs |
75
|
|
|
from urllib import urlencode |
76
|
|
|
from tg.exceptions import HTTPFound |
77
|
|
|
|
78
|
|
|
params = parse_qs(environ['QUERY_STRING']) |
79
|
|
|
params.pop('password', None) # Remove password in case it was there |
80
|
|
|
if user is None: |
81
|
|
|
params['failure'] = 'user-not-found' |
82
|
|
|
else: |
83
|
|
|
params['login'] = identity['login'] |
84
|
|
|
params['failure'] = 'invalid-password' |
85
|
|
|
|
86
|
|
|
# When authentication fails send user to login page. |
87
|
|
|
environ['repoze.who.application'] = HTTPFound( |
88
|
|
|
location='?'.join(('/login', urlencode(params, True))) |
89
|
|
|
) |
90
|
|
|
|
91
|
|
|
return login |
92
|
|
|
|
93
|
|
|
def get_user(self, identity, userid): |
94
|
|
|
return self.sa_auth.dbsession.query(self.sa_auth.user_class).filter_by( |
95
|
|
|
user_name=userid |
96
|
|
|
).first() |
97
|
|
|
|
98
|
|
|
def get_groups(self, identity, userid): |
99
|
|
|
return [g.group_name for g in identity['user'].groups] |
100
|
|
|
|
101
|
|
|
def get_permissions(self, identity, userid): |
102
|
|
|
return [p.permission_name for p in identity['user'].permissions] |
103
|
|
|
|
104
|
|
|
base_config.sa_auth.dbsession = model.DBSession |
105
|
|
|
|
106
|
|
|
base_config.sa_auth.authmetadata = ApplicationAuthMetadata(base_config.sa_auth) |
107
|
|
|
|
108
|
|
|
# In case ApplicationAuthMetadata didn't find the user discard the whole identity. |
109
|
|
|
# This might happen if logged-in users are deleted. |
110
|
|
|
base_config['identity.allow_missing_user'] = False |
111
|
|
|
|
112
|
|
|
# You can use a different repoze.who Authenticator if you want to |
113
|
|
|
# change the way users can login |
114
|
|
|
# base_config.sa_auth.authenticators = [('myauth', SomeAuthenticator()] |
115
|
|
|
|
116
|
|
|
# You can add more repoze.who metadata providers to fetch |
117
|
|
|
# user metadata. |
118
|
|
|
# Remember to set base_config.sa_auth.authmetadata to None |
119
|
|
|
# to disable authmetadata and use only your own metadata providers |
120
|
|
|
# base_config.sa_auth.mdproviders = [('myprovider', SomeMDProvider()] |
121
|
|
|
|
122
|
|
|
# override this if you would like to provide a different who plugin for |
123
|
|
|
# managing login and logout of your application |
124
|
|
|
base_config.sa_auth.form_plugin = None |
125
|
|
|
|
126
|
|
|
# You may optionally define a page where you want users to be redirected to |
127
|
|
|
# on login: |
128
|
|
|
base_config.sa_auth.post_login_url = '/post_login' |
129
|
|
|
|
130
|
|
|
# You may optionally define a page where you want users to be redirected to |
131
|
|
|
# on logout: |
132
|
|
|
base_config.sa_auth.post_logout_url = '/post_logout' |
133
|
|
|
try: |
134
|
|
|
# Enable DebugBar if available, install tgext.debugbar to turn it on |
135
|
|
|
from tgext.debugbar import enable_debugbar |
136
|
|
|
enable_debugbar(base_config) |
137
|
|
|
except ImportError: |
138
|
|
|
pass |
139
|
|
|
|