1
|
'''Host object. Configuration variables and utilities'''
|
2
|
|
3
|
import os
|
4
|
from shutil import rmtree
|
5
|
|
6
|
from quixote import get_request
|
7
|
|
8
|
from qommon.storage import StorableObject
|
9
|
|
10
|
from Defaults import APP_DIR
|
11
|
|
12
|
def get_proxied_site_name():
|
13
|
nb_subdirs = get_request().environ['SCRIPT_NAME'].count('/')
|
14
|
return get_request().get_path().split('/')[nb_subdirs + 2]
|
15
|
|
16
|
class Host(StorableObject):
|
17
|
'''Host object. Configuration variables and utilities'''
|
18
|
_names = 'hosts'
|
19
|
|
20
|
# Main settings
|
21
|
label = None
|
22
|
name = None
|
23
|
orig_site = None
|
24
|
new_url = None
|
25
|
scheme = None
|
26
|
auth_url = None
|
27
|
auth_form_places = 'form_once'
|
28
|
auth_form_page_url = None
|
29
|
auth_form = None
|
30
|
auth_form_url = None
|
31
|
logout_url = None
|
32
|
reversed_hostname = None
|
33
|
reversed_directory = None
|
34
|
organization_name = None
|
35
|
use_ssl = False
|
36
|
private_key = None
|
37
|
public_key = None
|
38
|
site_dir = None
|
39
|
|
40
|
# Auto detected settings
|
41
|
auth_mode = 'form'
|
42
|
auth_form_action = None
|
43
|
auth_check_url = None
|
44
|
login_field_name = None
|
45
|
password_field_name = None
|
46
|
select_fields = {}
|
47
|
post_parameters = {}
|
48
|
http_headers = {}
|
49
|
|
50
|
# Advanced settings
|
51
|
return_url = '/'
|
52
|
root_url = '/'
|
53
|
auth_system = 'password'
|
54
|
auth_match_text = ''
|
55
|
send_hidden_fields = True
|
56
|
initiate_sso_url = None
|
57
|
redirect_root_to_login = False
|
58
|
|
59
|
# Other attributes
|
60
|
provider_id = None
|
61
|
# Default value that indicates the proxy (if configured) is not disabled for this host yet
|
62
|
use_proxy = True
|
63
|
|
64
|
valid_username = None
|
65
|
valid_password = None
|
66
|
apache_output_filters = []
|
67
|
apache_output_python_filters = []
|
68
|
apache_python_paths = []
|
69
|
|
70
|
# Plugins
|
71
|
# If name is set to None, use the default site authentication class
|
72
|
site_authentication_plugin = None
|
73
|
|
74
|
def get_host_from_url(cls):
|
75
|
try:
|
76
|
host = list(Host.select(lambda x: x.name == get_proxied_site_name()))[0]
|
77
|
if hasattr(host, 'site_authentication_instance'):
|
78
|
del host.site_authentication_instance
|
79
|
return list(Host.select(lambda x: x.name == get_proxied_site_name()))[0]
|
80
|
except IndexError:
|
81
|
return None
|
82
|
get_host_from_url = classmethod(get_host_from_url)
|
83
|
|
84
|
def get_host_with_provider_id(cls, provider_id):
|
85
|
try:
|
86
|
return list(Host.select(lambda x: x.provider_id == provider_id))[0]
|
87
|
except IndexError:
|
88
|
return None
|
89
|
get_host_with_provider_id = classmethod(get_host_with_provider_id)
|
90
|
|
91
|
def get_root_url(self):
|
92
|
if self.root_url.startswith('/'):
|
93
|
if self.reversed_directory:
|
94
|
return '%s/%s%s' % (get_request().environ['SCRIPT_NAME'],
|
95
|
self.reversed_directory,
|
96
|
self.root_url)
|
97
|
else:
|
98
|
return '%s%s' % (get_request().environ['SCRIPT_NAME'], self.root_url)
|
99
|
# In this case, must be a full url
|
100
|
return self.root_url
|
101
|
|
102
|
def get_return_url(self):
|
103
|
if self.return_url.startswith('/'):
|
104
|
if self.reversed_directory:
|
105
|
return '%s/%s%s' % (get_request().environ['SCRIPT_NAME'],
|
106
|
self.reversed_directory,
|
107
|
self.return_url)
|
108
|
else:
|
109
|
return '%s%s' % (get_request().environ['SCRIPT_NAME'], self.return_url)
|
110
|
# In this case, must be a full url
|
111
|
return self.return_url
|
112
|
|
113
|
def __cmp__(self, other):
|
114
|
hostname_cmp = cmp(self.reversed_hostname, other.reversed_hostname)
|
115
|
if hostname_cmp != 0:
|
116
|
return hostname_cmp
|
117
|
return cmp(self.reversed_directory, other.reversed_directory)
|
118
|
|
119
|
def remove_self(self):
|
120
|
# Main configuration file
|
121
|
StorableObject.remove_self(self)
|
122
|
# Other generated files
|
123
|
if self.site_dir and os.path.exists(self.site_dir):
|
124
|
rmtree(self.site_dir, ignore_errors=1)
|
125
|
# Also remove hostname directory if empty (meaning there was no other subdirectory
|
126
|
# for this hostname)
|
127
|
try:
|
128
|
os.rmdir('/'.join(self.site_dir.split('/')[:-1]))
|
129
|
except OSError:
|
130
|
pass
|
131
|
# Virtual host directory
|
132
|
if self.reversed_hostname:
|
133
|
path = os.path.join(APP_DIR, self.reversed_hostname)
|
134
|
if os.path.exists(path):
|
135
|
rmtree(path, ignore_errors=1)
|
136
|
|