import repository from arizona
[raven.git] / apps / mprepo / frontend.py
1 import os
2 import signal
3 import sys
4
5 from mod_python import apache, Session
6
7 import arizonaconfig
8 import arizonageneral
9 import arizonareport
10 import arizonatemplate
11
12 import repoconfig
13 import repoplc
14
15 glo_initialized = False
16 glo_need_storkrpm = False
17
18 def set_need_storkrpm(x):
19     global glo_need_storkrpm
20     glo_need_storkrpm = x
21
22 def fix_option(name, default):
23     if not arizonaconfig.get_option(name):
24         arizonaconfig.set_option(name, default)
25
26 def initialize(bindir, mainpythonfile):
27    """
28    <Purpose>
29       Perform front-end initialization.
30
31    <Arguments>
32       None.
33
34    <Side Effects>
35       Arizonaconfig is started up, cwd is changed, cherrypy config is created
36
37    <Returns>
38       None.
39    """
40    global glo_initialized
41
42    if glo_initialized:
43        return
44
45    os.chdir(bindir) # basedir = "/usr/local/mprepo/bin" mainpythonfile="mprfrontend.py"
46    arizonaconfig.init_options(mainpythonfile, configfile_optvar="configfile", alt_paths=["/var/www/html/src"])
47
48    if glo_need_storkrpm:
49        import package.storkrpm
50
51    # XXX a bunch of arizonaconfig options aren't processed correctly, so set
52    # them here manually
53
54    fix_option("keytype", "rsa")
55    fix_option("keygen", "genrsa")
56    fix_option("numbits", "1024")
57    fix_option("xmlsignaturedtd", "arizonacrypt_signature.dtd")
58    fix_option("hashtype", "-sha1")
59
60    repoconfig.init()
61
62    arizonatemplate.glo_template_dir = repoconfig.repo_etc_dir
63
64    glo_initialized = True
65
66 class BasePageHelper:
67     def __init__(self, req):
68
69         initialize(self.bindir, self.mainpythonfile)
70
71         self.req = req
72         self.session = Session.Session(req)
73
74         self.req.content_type = "text/html"
75
76         repoplc.getsession = self.getsession
77         repoplc.setsession = self.setsession
78
79         self.trustedroots = None
80         self.gacksclient = None
81         self.grmgid = None
82
83     def initialize(self):
84         raise "you forgot to override this function"
85
86     def get_timezone(self):
87         return self.session.get("timezone", -7)
88
89     def set_timezone(self, timezone):
90         if str(timezone) != str(self.session.get("timezone")):
91             self.session["timezone"] = float(timezone)
92
93     def done(self):
94         self.session.save()
95
96     def is_logged_in(self):
97         return self.session.get("loggedin", False)
98
99     def set_logged_in(self, v, loginkind="unknown"):
100         self.session['loggedin'] = v
101         self.session['loginkind'] = loginkind
102
103     def check_login(self):
104         if not self.is_logged_in():
105             self.redirect("login")
106             return False
107         return True
108
109     def refuse_trailing_slash(self):
110         if self.req.uri.endswith("/"):
111             new_url = self.req.uri[:-1]
112             if self.req.args:
113                 new_url = new_url + "?" + self.req.args
114             self.redirect(new_url)
115             return False
116         return True
117
118     def is_https(self):
119         return self.req.subprocess_env.has_key('HTTPS') and \
120                self.req.subprocess_env['HTTPS'] == 'on'\r
121
122     def refuse_http(self):
123         if not self.is_https():
124             if self.req.hostname:
125                 hostname = self.req.hostname
126             elif self.req.connection.hostname:
127                 hostname = self.req.connection.local_host
128             else:
129                 hostname = str(self.req.connection.local_ip)
130
131             new_url = "https://" + hostname + self.req.uri
132             if self.req.args:
133                 new_url = new_url + "?" + self.req.args
134
135             self.redirect(new_url)
136             return False
137         return True
138
139
140     def get_name(self):
141         if self.session['loginkind'] == "PLC":
142             return self.session['userdata']['first_name'] + " " + self.session['userdata']['last_name']
143         elif self.session['loginkind'] == "geni":
144             return self.get_hrn()
145         else:
146             return "unknown_loginkind"
147
148     def get_loginkind(self):
149         return self.session.get("loginkind", "unknown")
150
151     def get_slices(self):
152         if self.session['loginkind'] == "PLC":
153             names = repoplc.get_slicenames()
154             dicts = []
155             for name in names:
156                 dict = {"name": name, "displayname": name}
157                 dicts.append(dict)
158             return dicts
159         elif self.session['loginkind'] == "geni":
160             import repogeni
161             return repogeni.get_slices(self.get_hrn())
162         else:
163             return []
164
165     def redirect(self, url):
166         self.req.headers_out['location'] = url
167         self.req.status = apache.HTTP_MOVED_TEMPORARILY
168         return "You are being redirected to " + url\r
169 \r
170     def getsession(self, name):\r
171         return self.session[name]\r
172 \r
173     def setsession(self, name, val):\r
174         #self.req.write("setsession " + name + "=" + str(val) + "\n")\r
175         self.session[name] = val\r
176         #self.req.write("session " + str(self.session) + "\n")\r
177 \r
178     def backpage(self, message):\r
179         self.req.write( arizonatemplate.retrieve_template("errorpage.stemp",\r
180                                             {"errormessage":str(message)}) )\r
181         self.done()\r
182 \r
183     # ---- geni stuff ----\r
184 \r
185     def get_gid(self):\r
186         from sfa.trust.gid import *
187         if self.session.get('gidstr',None) == None:
188             return None
189         return GID(string=self.session['gidstr'])
190
191     def set_gid(self, gid):
192         self.session['gidstr'] = gid.save_to_string(save_parents = True)
193
194     def get_hrn(self):
195         gid = self.get_gid()
196         if not gid:
197             return "no gid"
198         return gid.get_hrn()
199
200     def get_trusted_cert_list(self):
201         if not self.trustedroots:
202             from sfa.trust.trustedroot import *
203             self.trustedroots = TrustedRootList(dir = repoconfig.repo_trusted_roots_dir)
204         return self.trustedroots.get_list()\r
205 \r
206     def plc_to_geni(self):\r
207         """ Assuming we're logged in using PLC, get a GID that corresponds to\r
208             the PLC user.\r
209         """\r
210         import repogeni\r
211         hrn = repoplc.get_user_hrn()
212         gid = repogeni.get_gid(hrn)
213         self.set_gid(gid)\r
214 \r
215 \r
216 \r
217