1.1 --- a/XSLForms/Resources/Login.py Sun Jun 10 19:47:36 2007 +0000
1.2 +++ b/XSLForms/Resources/Login.py Sun Jun 10 22:48:40 2007 +0000
1.3 @@ -36,7 +36,10 @@
1.4 following attributes:
1.5
1.6 * resource_dir
1.7 - * template_resources - including a "login" entry for the login screen
1.8 + * template_resources - including a "login" entry for the login screen and
1.9 + a "success" entry for a screen indicating a
1.10 + successful login (used when redirects are not in
1.11 + use)
1.12 * document_resources - including a "translations" entry
1.13
1.14 The latter attribute is optional.
1.15 @@ -47,20 +50,31 @@
1.16 Such a structure would be as follows for the default configuration:
1.17
1.18 <login username="..." password="..."/>
1.19 +
1.20 + The success template must provide a document structure where the location of
1.21 + the application can be found through this class's 'path_to_success_element'
1.22 + attribute (which can be overridden or modified). Such a structure would be
1.23 + as follows for the default configuration:
1.24 +
1.25 + <success location="..."/>
1.26 """
1.27
1.28 path_to_login_element = "/login"
1.29 + path_to_success_element = "/success"
1.30
1.31 - def __init__(self, authenticator):
1.32 + def __init__(self, authenticator, use_redirect=1):
1.33
1.34 """
1.35 - Initialise the resource with an 'authenticator'.
1.36 + Initialise the resource with an 'authenticator'. If the optional
1.37 + 'use_redirect' parameter is specified and set to a false value (unlike
1.38 + the default),
1.39
1.40 To get the root of the application, this resource needs an attribute on
1.41 the transaction called "root".
1.42 """
1.43
1.44 self.authenticator = authenticator
1.45 + self.use_redirect = use_redirect
1.46
1.47 def respond_to_form(self, trans, form):
1.48
1.49 @@ -76,22 +90,35 @@
1.50 # Ensure the presence of a document.
1.51
1.52 if documents.has_key("login"):
1.53 - login = documents["login"]
1.54 + doc = documents["login"]
1.55 else:
1.56 - login = form.new_instance("login")
1.57 + doc = form.new_instance("login")
1.58 +
1.59 + template_name = "login"
1.60 +
1.61 + # NOTE: Consider initialisation of both the login and success documents.
1.62
1.63 # Test for login.
1.64
1.65 if parameters.has_key("login"):
1.66 - logelem = login.xpath(self.path_to_login_element)[0]
1.67 + logelem = doc.xpath(self.path_to_login_element)[0]
1.68 username = logelem.getAttribute("username")
1.69 password = logelem.getAttribute("password")
1.70
1.71 if self.authenticator.authenticate(trans, username, password):
1.72 app, path, qs = WebStack.Resources.Login.get_target(trans)
1.73 - trans.redirect(app + trans.encode_path(path) + qs)
1.74 +
1.75 + # Either redirect or switch to the success template.
1.76 +
1.77 + if self.use_redirect:
1.78 + trans.redirect(app + trans.encode_path(path) + qs)
1.79 + else:
1.80 + template_name = "success"
1.81 + doc = form.new_instance("success")
1.82 + successelem = doc.xpath(self.path_to_success_element)[0]
1.83 + successelem.setAttribute("location", app + trans.encode_path(path) + qs)
1.84 else:
1.85 - error = login.createElement("error")
1.86 + error = doc.createElement("error")
1.87 logelem.appendChild(error)
1.88 error.setAttribute("message", "Username or password not valid")
1.89
1.90 @@ -116,9 +143,9 @@
1.91
1.92 # Complete the response.
1.93
1.94 - trans_xsl = self.prepare_output("login")
1.95 + trans_xsl = self.prepare_output(template_name)
1.96 stylesheet_parameters["root"] = attributes["root"]
1.97 - self.send_output(trans, [trans_xsl], login, stylesheet_parameters, references=references)
1.98 + self.send_output(trans, [trans_xsl], doc, stylesheet_parameters, references=references)
1.99
1.100 class LoginRedirectResource(WebStack.Resources.LoginRedirect.LoginRedirectResource):
1.101