Back to index

moin  1.9.0~rc2
login.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Login form
00004 
00005     @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
00006                 2003-2007 MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 from MoinMoin.widget import html
00011 
00012 
00013 class Login:
00014     """ User login. """
00015 
00016     def __init__(self, request):
00017         """ Initialize user settings form.
00018         """
00019         self.request = request
00020         self._ = request.getText
00021         self.cfg = request.cfg
00022 
00023     def make_row(self, label, cell, **kw):
00024         """ Create a row in the form table.
00025         """
00026         self._table.append(html.TR().extend([
00027             html.TD(**kw).extend([html.B().append(label), '   ']),
00028             html.TD().extend(cell),
00029         ]))
00030 
00031 
00032     def asHTML(self):
00033         """ Create the complete HTML form code. """
00034         _ = self._
00035         request = self.request
00036         action = "%s%s" % (request.script_root, request.path)
00037         hints = []
00038         for authm in request.cfg.auth:
00039             hint = authm.login_hint(request)
00040             if hint:
00041                 hints.append(hint)
00042         self._form = html.FORM(action=action, name="loginform", id="loginform")
00043         self._table = html.TABLE(border="0")
00044 
00045         # Use the user interface language and direction
00046         lang_attr = request.theme.ui_lang_attr()
00047         self._form.append(html.Raw('<div class="userpref"%s>' % lang_attr))
00048 
00049         self._form.append(html.INPUT(type="hidden", name="action", value="login"))
00050         self._form.append(self._table)
00051         for hint in hints:
00052             self._form.append(html.P().append(html.Raw(hint)))
00053         self._form.append(html.Raw("</div>"))
00054 
00055         cfg = request.cfg
00056         if 'username' in cfg.auth_login_inputs:
00057             self.make_row(_('Name'), [
00058                 html.INPUT(
00059                     type="text", size="32", name="name",
00060                 ),
00061             ])
00062 
00063         if 'password' in cfg.auth_login_inputs:
00064             self.make_row(_('Password'), [
00065                 html.INPUT(
00066                     type="password", size="32", name="password",
00067                 ),
00068             ])
00069 
00070         # Restrict type of input available for OpenID input
00071         # based on wiki configuration.
00072         if 'openid_identifier' in cfg.auth_login_inputs:
00073             if len(cfg.openidrp_allowed_op) == 1:
00074                 self.make_row(_('OpenID'), [
00075                      html.INPUT(
00076                          type="hidden", name="openid_identifier",
00077                          value=cfg.openidrp_allowed_op[0]
00078                      ),
00079                 ])
00080             elif len(cfg.openidrp_allowed_op) > 1:
00081                 op_select = html.SELECT(name="openid_identifier",
00082                     id="openididentifier")
00083                 for op_uri in cfg.openidrp_allowed_op:
00084                     op_select.append(html.OPTION(value=op_uri).append(
00085                         html.Raw(op_uri)))
00086 
00087                 self.make_row(_('OpenID'), [op_select, ])
00088             else:
00089                 self.make_row(_('OpenID'), [
00090                     html.INPUT(
00091                         type="text", size="32", name="openid_identifier",
00092                         id="openididentifier"
00093                     ),
00094                 ])
00095 
00096         # Need both hidden field and submit values for auto-submit to work
00097         self.make_row('', [
00098             html.INPUT(type="hidden", name="login", value=_('Login')),
00099             html.INPUT(
00100                 type="submit", name='login', value=_('Login')
00101             ),
00102         ])
00103 
00104         # Automatically submit the form if only a single OpenID Provider is allowed
00105         if 'openid_identifier' in cfg.auth_login_inputs and len(cfg.openidrp_allowed_op) == 1:
00106             self._form.append("""<script type="text/javascript">
00107 <!--//
00108 document.getElementById("loginform").submit();
00109 //-->
00110 </script>
00111 """)
00112 
00113         return unicode(self._form)
00114 
00115 def getLogin(request):
00116     """ Return HTML code for the login. """
00117     return Login(request).asHTML()