annotate MoinMoin/userprefs/oid.py @ 2364:96ec23afac7d

fix some bugs in openid prefs
author Johannes Berg <johannes AT sipsolutions DOT net>
date Thu, 12 Jul 2007 11:39:35 +0200
parents 8eb96bfafef9
children 6d9970321d79
rev   line source
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3 MoinMoin - OpenID preferences
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 @copyright: 2007 MoinMoin:JohannesBerg
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
9 from MoinMoin import wikiutil, user
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 from MoinMoin.widget import html
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 from MoinMoin.userprefs import UserPrefBase
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 from MoinMoin.auth.openidrp import OpenIDAuth
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 import sha
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from MoinMoin.util.moinoid import MoinOpenIDStore
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from openid.consumer import consumer
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
16 from openid.yadis.discover import DiscoveryFailure
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
17 from openid.fetchers import HTTPFetchingError
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 class Settings(UserPrefBase):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21 def __init__(self, request):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 """ Initialize OpenID settings form. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 UserPrefBase.__init__(self, request)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 self.request = request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 self._ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 self.cfg = request.cfg
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 self.title = self._("OpenID settings")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 def allowed(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 for authm in self.request.cfg.auth:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 if isinstance(authm, OpenIDAuth):
2304
8eb96bfafef9 require a valid user by default for all preferences plugins
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2303
diff changeset
32 return UserPrefBase.allowed(self)
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 return False
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35 def _handle_remove(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 _ = self.request.getText
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
37 if not hasattr(self.request.user, 'openids'):
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
38 return
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 openids = self.request.user.openids[:]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 if name in self.request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 openids.remove(oid)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 if not openids and len(self.request.cfg.auth) == 1:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 return _("Cannot remove all OpenIDs.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 self.request.user.openids = openids
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 self.request.user.save()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 return _("The selected OpenIDs have been removed.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 def _handle_add(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 openid_id = request.form.get('openid_identifier', [''])[0]
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
55 if not openid_id:
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
56 return _("No OpenID.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
58 if (hasattr(self.request.user, 'openids') and
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
59 openid_id in request.user.openids):
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 return _("OpenID is already present.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63 MoinOpenIDStore(self.request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 try:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 oidreq = oidconsumer.begin(openid_id)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 except HTTPFetchingError:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 return _('Failed to resolve OpenID.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68 except DiscoveryFailure:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 return _('OpenID discovery failure, not a valid OpenID.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 if oidreq is None:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 return _("No OpenID.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 qstr = wikiutil.makeQueryString({'action': 'userprefs',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 'handler': 'oid',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 'oid.return': '1'})
2303
4f73af30fd75 fix return_to URL in OpenID preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2301
diff changeset
77 return_to = '%s/%s' % (request.getBaseURL(),
4f73af30fd75 fix return_to URL in OpenID preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2301
diff changeset
78 request.page.url(request, qstr))
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79 trust_root = request.getBaseURL()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80 if oidreq.shouldSendRedirect():
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81 redirect_url = oidreq.redirectURL(trust_root, return_to)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82 request.http_redirect(redirect_url)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
84 form_html = oidreq.formMarkup(trust_root, return_to,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 form_tag_attrs={'id': 'openid_message'})
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86 request.session['openid.prefs.form_html'] = form_html
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 def _handle_oidreturn(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91 _ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
94 MoinOpenIDStore(request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95 query = {}
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 for key in request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 query[key] = request.form[key][0]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 qstr = wikiutil.makeQueryString({'action': 'userprefs',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99 'handler': 'oid',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 'oid.return': '1'})
2303
4f73af30fd75 fix return_to URL in OpenID preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2301
diff changeset
101 return_to = '%s/%s' % (request.getBaseURL(),
4f73af30fd75 fix return_to URL in OpenID preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2301
diff changeset
102 request.page.url(request, qstr))
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 info = oidconsumer.complete(query, return_to=return_to)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 if info.status == consumer.FAILURE:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 return _('OpenID error: %s.') % info.message
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 elif info.status == consumer.CANCEL:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107 return _('Verification canceled.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 elif info.status == consumer.SUCCESS:
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
109 if not hasattr(self.request.user, 'openids'):
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
110 request.user.openids = []
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
111
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
112 if info.identity_url in request.user.openids:
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 return _("OpenID is already present.")
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
114
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
115 if user.getUserIdByOpenId(request, info.identity_url):
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
116 return _("This OpenID is already used for another account.")
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
117
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
118 # all fine
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
119 request.user.openids.append(info.identity_url)
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
120 request.user.save()
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
121 return _("OpenID added successfully.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
122 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
123 return _('OpenID failure.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
124
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
125
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
126 def handle_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
127 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
128 form = self.request.form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
129
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
130 if form.has_key('oid.return'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131 return self._handle_oidreturn()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 if form.has_key('cancel'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
136 if form.has_key('remove'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137 return self._handle_remove()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
138
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
139 if form.has_key('add'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140 return self._handle_add()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
141
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
142 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
143
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
144 def _make_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
145 sn = self.request.getScriptname()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
146 pi = self.request.getPathinfo()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
147 action = u"%s%s" % (sn, pi)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
148 _form = html.FORM(action=action)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
149 _form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
150 _form.append(html.INPUT(type="hidden", name="handler", value="oid"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
151 return _form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
152
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
153 def _make_row(self, label, cell, **kw):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
154 """ Create a row in the form table.
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
155 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
156 self._table.append(html.TR().extend([
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
157 html.TD(**kw).extend([html.B().append(label), ' ']),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
158 html.TD().extend(cell),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
159 ]))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
160
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
161 def _oidlist(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
165 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
166 form.append(html.INPUT(type="checkbox", name=name, id=name))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
167 form.append(html.LABEL(for_=name).append(html.Text(oid)))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
169 self._make_row(_("Current OpenIDs"), [form], valign='top')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 label = _("Remove selected")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
172 form.append(html.INPUT(type="submit", name="remove", value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
173
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
174 def _addoidform(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
175 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
176 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
177 # go back to this page
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
178 form.append(html.INPUT(type="hidden", name="sub", value="oid"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
179 label = _("Add OpenID")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
180 form.append(html.INPUT(type="text", size="32",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
181 name="openid_identifier",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 id="openididentifier"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
184 form.append(html.INPUT(type="submit", name="add", value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 self._make_row(_('Add OpenID'), [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
186
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 def create_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
188 """ Create the complete HTML form code. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
190
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191 ret = html.P()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
192 # Use the user interface language and direction
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
193 lang_attr = self.request.theme.ui_lang_attr()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
194 ret.append(html.Raw('<div %s>' % lang_attr))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 self._table = html.TABLE(border="0")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
196 ret.append(self._table)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197 ret.append(html.Raw("</div>"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
201 if 'openid.prefs.form_html' in request.session:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
202 txt = _('OpenID verification requires that you click this button:')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203 # create JS to automatically submit the form if possible
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 submitjs = """<script type="text/javascript">
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
205 <!--//
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
206 document.getElementById("openid_message").submit();
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207 //-->
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 </script>
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
209 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
210 oidhtml = request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
211 del request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
212 return ''.join([txt, oidhtml, submitjs])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
213
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
214 if hasattr(request.user, 'openids') and request.user.openids:
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
215 self._oidlist()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
216 self._addoidform()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
217
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
218 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
219 label = _("Cancel")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
220 form.append(html.INPUT(type="submit", name='cancel', value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
221 self._make_row('', [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
222 return unicode(ret)