Back to index

moin  1.9.0~rc2
oidserv.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - OpenID preferences
00004 
00005     @copyright: 2007     MoinMoin:JohannesBerg
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from MoinMoin import wikiutil, user
00010 from MoinMoin.widget import html
00011 from MoinMoin.userprefs import UserPrefBase
00012 import base64
00013 
00014 
00015 class Settings(UserPrefBase):
00016     def __init__(self, request):
00017         """ Initialize OpenID settings form. """
00018         UserPrefBase.__init__(self, request)
00019         self.request = request
00020         self._ = request.getText
00021         self.cfg = request.cfg
00022         _ = self._
00023         self.title = _("OpenID server")
00024 
00025     def allowed(self):
00026         if not self.request.cfg.openid_server_enabled:
00027             return False
00028 
00029         groups = self.request.groups
00030         openid_group_name = self.request.cfg.openid_server_restricted_users_group
00031 
00032         if openid_group_name and self.request.user.name not in groups.get(openid_group_name, []):
00033                 return False
00034 
00035         return True
00036 
00037     def _handle_remove(self):
00038         _ = self.request.getText
00039         if not hasattr(self.request.user, 'openid_trusted_roots'):
00040             return
00041         roots = self.request.user.openid_trusted_roots[:]
00042         for root in self.request.user.openid_trusted_roots:
00043             name = "rm-%s" % root
00044             if name in self.request.form:
00045                 roots.remove(root)
00046         self.request.user.openid_trusted_roots = roots
00047         self.request.user.save()
00048         return 'info', _("The selected websites have been removed.")
00049 
00050     def handle_form(self):
00051         _ = self._
00052         form = self.request.form
00053 
00054         if form.has_key('cancel'):
00055             return
00056 
00057         if self.request.request_method != 'POST':
00058             return
00059 
00060         if form.has_key('remove'):
00061             return self._handle_remove()
00062 
00063     def _make_form(self):
00064         sn = self.request.script_root
00065         pi = self.request.getPathinfo()
00066         action = u"%s%s" % (sn, pi)
00067         _form = html.FORM(action=action)
00068         _form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
00069         _form.append(html.INPUT(type="hidden", name="handler", value="oidserv"))
00070         return _form
00071 
00072     def _make_row(self, label, cell, **kw):
00073         """ Create a row in the form table.
00074         """
00075         self._table.append(html.TR().extend([
00076             html.TD(**kw).extend([html.B().append(label), '   ']),
00077             html.TD().extend(cell),
00078         ]))
00079 
00080     def _trust_root_list(self):
00081         _ = self.request.getText
00082         form = self._make_form()
00083         for root in self.request.user.openid_trusted_roots:
00084             display = base64.decodestring(root)
00085             name = 'rm-%s' % root
00086             form.append(html.INPUT(type="checkbox", name=name, id=name))
00087             form.append(html.LABEL(for_=name).append(html.Text(display)))
00088             form.append(html.BR())
00089         self._make_row(_("Trusted websites"), [form], valign='top')
00090         label = _("Remove selected")
00091         form.append(html.BR())
00092         form.append(html.INPUT(type="submit", name="remove", value=label))
00093 
00094     def create_form(self):
00095         """ Create the complete HTML form code. """
00096         _ = self._
00097 
00098         ret = html.P()
00099         # Use the user interface language and direction
00100         lang_attr = self.request.theme.ui_lang_attr()
00101         ret.append(html.Raw('<div %s>' % lang_attr))
00102         self._table = html.TABLE(border="0")
00103         ret.append(self._table)
00104         ret.append(html.Raw("</div>"))
00105 
00106         request = self.request
00107 
00108         if hasattr(request.user, 'openid_trusted_roots') and request.user.openid_trusted_roots:
00109             self._trust_root_list()
00110 
00111         form = self._make_form()
00112         label = _("Cancel")
00113         form.append(html.INPUT(type="submit", name='cancel', value=label))
00114         self._make_row('', [form])
00115         return unicode(ret)