annotate MoinMoin/userprefs/oid.py @ 5514:09de6f176a91

userprefs: add ticket to openid add/remove handle code
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 07 Feb 2010 20:03:03 +0100
parents 7bfd4232cfb7
children 5359bfdf91ba
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
2423
76bf8b6b3061 use formatted=False for userprefs titles
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2380
diff changeset
31 _ = self._
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2423
diff changeset
32 self.title = _("OpenID settings")
2380
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
33 openid_auth = False
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
34 if not _openid_disabled:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
35 for authm in self.request.cfg.auth:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
36 if isinstance(authm, OpenIDAuth):
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
37 openid_auth = True
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
38 break
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
39 if not openid_auth:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
40 self.allowed = lambda: False
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 def _handle_remove(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 _ = self.request.getText
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
44 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
45 return
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 openids = self.request.user.openids[:]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 if name in self.request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 openids.remove(oid)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 if not openids and len(self.request.cfg.auth) == 1:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
52 return 'error', _("Cannot remove all OpenIDs.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53 self.request.user.openids = openids
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 self.request.user.save()
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
55 return 'info', _("The selected OpenIDs have been removed.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 def _handle_add(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 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
62 if not openid_id:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
63 return 'error', _("No OpenID given.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
65 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
66 openid_id in request.user.openids):
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
67 return 'error', _("OpenID is already present.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 MoinOpenIDStore(self.request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 try:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 oidreq = oidconsumer.begin(openid_id)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 except HTTPFetchingError:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
74 return 'error', _('Failed to resolve OpenID.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 except DiscoveryFailure:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
76 return 'error', _('OpenID discovery failure, not a valid OpenID.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 if oidreq is None:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
79 return 'error', _("No OpenID given.") # ??
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81 qstr = wikiutil.makeQueryString({'action': 'userprefs',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82 'handler': 'oid',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83 'oid.return': '1'})
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
84 return_to = request.getQualifiedURL(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'})
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
107 return_to = request.getQualifiedURL(request.page.url(request, qstr))
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 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
109 if info.status == consumer.FAILURE:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
110 return 'error', _('OpenID error: %s.') % info.message
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111 elif info.status == consumer.CANCEL:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
112 return 'info', _('Verification canceled.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 elif info.status == consumer.SUCCESS:
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
114 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
115 request.user.openids = []
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
116
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
117 if info.identity_url in request.user.openids:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
118 return 'error', _("OpenID is already present.")
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
119
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
120 if user.getUserIdByOpenId(request, info.identity_url):
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
121 return 'error', _("This OpenID is already used for another account.")
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
122
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
123 # all fine
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
124 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
125 request.user.save()
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
126 return 'info', _("OpenID added successfully.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
127 else:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
128 return 'error', _('OpenID failure.')
2301
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
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131 def handle_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 form = self.request.form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135 if form.has_key('oid.return'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
136 return self._handle_oidreturn()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
138 if form.has_key('cancel'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
139 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140
3326
a82b02396941 openid userprefs: ignore non-POST submissions
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3234
diff changeset
141 if self.request.request_method != 'POST':
a82b02396941 openid userprefs: ignore non-POST submissions
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3234
diff changeset
142 return
a82b02396941 openid userprefs: ignore non-POST submissions
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3234
diff changeset
143
5514
09de6f176a91 userprefs: add ticket to openid add/remove handle code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
144 if not wikiutil.checkTicket(self.request, form.get('ticket', [''])[0]):
09de6f176a91 userprefs: add ticket to openid add/remove handle code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
145 return
09de6f176a91 userprefs: add ticket to openid add/remove handle code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
146
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
147 if form.has_key('remove'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
148 return self._handle_remove()
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 if form.has_key('add'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
151 return self._handle_add()
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_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
154 sn = self.request.getScriptname()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
155 pi = self.request.getPathinfo()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
156 action = u"%s%s" % (sn, pi)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
157 _form = html.FORM(action=action)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
158 _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
159 _form.append(html.INPUT(type="hidden", name="handler", value="oid"))
5514
09de6f176a91 userprefs: add ticket to openid add/remove handle code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
160 ticket = wikiutil.createTicket(self.request)
09de6f176a91 userprefs: add ticket to openid add/remove handle code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
161 _form.append(html.INPUT(type="hidden", name="ticket", value=ticket))
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 return _form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164 def _make_row(self, label, cell, **kw):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
165 """ Create a row in the form table.
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 self._table.append(html.TR().extend([
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 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
169 html.TD().extend(cell),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 ]))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
172 def _oidlist(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
173 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
174 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
175 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
176 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
177 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
178 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
179 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
180 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
181 label = _("Remove selected")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183 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
184
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 def _addoidform(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
186 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
188 # go back to this page
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 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
190 label = _("Add OpenID")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191 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
192 name="openid_identifier",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
193 id="openididentifier"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
194 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 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
196 self._make_row(_('Add OpenID'), [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198 def create_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199 """ Create the complete HTML form code. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
201
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
202 ret = html.P()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203 # Use the user interface language and direction
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 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
205 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
206 self._table = html.TABLE(border="0")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207 ret.append(self._table)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 ret.append(html.Raw("</div>"))
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 request = self.request
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 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
213 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
214 # 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
215 submitjs = """<script type="text/javascript">
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
216 <!--//
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
217 document.getElementById("openid_message").submit();
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
218 //-->
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
219 </script>
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
220 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
221 oidhtml = request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
222 del request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
223 return ''.join([txt, oidhtml, submitjs])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
224
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
225 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
226 self._oidlist()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
227 self._addoidform()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
228
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
229 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
230 label = _("Cancel")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
231 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
232 self._make_row('', [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
233 return unicode(ret)