annotate MoinMoin/userprefs/oid.py @ 2380:0d04c8c82eb6

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