189 |
189 |
edit_required_profile = login_required(EditRequired.as_view())
|
190 |
190 |
|
191 |
191 |
|
192 |
|
class EmailChangeView(HomeURLMixin, cbv.TemplateNamesMixin, FormView):
|
|
192 |
class RecentAuthenticationMixin:
|
|
193 |
last_authentication_max_age = 600 # 10 minutes
|
|
194 |
|
|
195 |
def reauthenticate(self, action, message):
|
|
196 |
methods = [event['how'] for event in utils_misc.get_authentication_events(self.request)]
|
|
197 |
return utils_misc.login_require(
|
|
198 |
self.request,
|
|
199 |
token={
|
|
200 |
'action': action,
|
|
201 |
'message': message,
|
|
202 |
'methods': methods,
|
|
203 |
},
|
|
204 |
)
|
|
205 |
|
|
206 |
def has_recent_authentication(self):
|
|
207 |
age = time.time() - utils_misc.last_authentication_event(request=self.request)['when']
|
|
208 |
return age < self.last_authentication_max_age
|
|
209 |
|
|
210 |
|
|
211 |
class EmailChangeView(HomeURLMixin, RecentAuthenticationMixin, cbv.TemplateNamesMixin, FormView):
|
193 |
212 |
template_names = ['profiles/email_change.html', 'authentic2/change_email.html']
|
194 |
213 |
title = _('Email Change')
|
195 |
214 |
success_url = '..'
|
196 |
215 |
|
|
216 |
def can_validate_with_password(self):
|
|
217 |
last_event = utils_misc.last_authentication_event(self.request)
|
|
218 |
return last_event and last_event['how'] == 'password-on-https'
|
|
219 |
|
197 |
220 |
def get_form_class(self):
|
198 |
|
if self.request.user.has_usable_password():
|
|
221 |
if self.can_validate_with_password():
|
199 |
222 |
return profile_forms.EmailChangeForm
|
200 |
223 |
return profile_forms.EmailChangeFormNoPassword
|
201 |
224 |
|
... | ... | |
204 |
227 |
kwargs['user'] = self.request.user
|
205 |
228 |
return kwargs
|
206 |
229 |
|
|
230 |
def has_recent_authentication(self):
|
|
231 |
age = time.time() - utils_misc.last_authentication_event(request=self.request)['when']
|
|
232 |
return age < self.last_authentication_max_age
|
|
233 |
|
|
234 |
def dispatch(self, request, *args, **kwargs):
|
|
235 |
if not self.can_validate_with_password() and not self.has_recent_authentication():
|
|
236 |
return self.reauthenticate(
|
|
237 |
action='email-change',
|
|
238 |
message=_('You must re-authenticate to change your email address.'),
|
|
239 |
)
|
|
240 |
return super().dispatch(request, *args, **kwargs)
|
|
241 |
|
207 |
242 |
def post(self, request, *args, **kwargs):
|
208 |
243 |
if 'cancel' in request.POST:
|
209 |
244 |
return utils_misc.redirect(request, 'account_management')
|
... | ... | |
1336 |
1371 |
registration_completion = RegistrationCompletionView.as_view()
|
1337 |
1372 |
|
1338 |
1373 |
|
1339 |
|
class AccountDeleteView(HomeURLMixin, TemplateView):
|
|
1374 |
class AccountDeleteView(HomeURLMixin, RecentAuthenticationMixin, TemplateView):
|
1340 |
1375 |
template_name = 'authentic2/accounts_delete_request.html'
|
1341 |
1376 |
title = _('Request account deletion')
|
1342 |
|
last_authentication_max_age = 600 # 10 minutes
|
1343 |
1377 |
|
1344 |
1378 |
def dispatch(self, request, *args, **kwargs):
|
1345 |
1379 |
if not app_settings.A2_REGISTRATION_CAN_DELETE_ACCOUNT:
|
1346 |
1380 |
return utils_misc.redirect(request, '..')
|
1347 |
1381 |
if not self.request.user.email_verified and not self.has_recent_authentication():
|
1348 |
|
methods = [event['how'] for event in utils_misc.get_authentication_events(request)]
|
1349 |
|
return utils_misc.login_require(
|
1350 |
|
request,
|
1351 |
|
token={
|
1352 |
|
'action': 'account-delete',
|
1353 |
|
'message': _('You must re-authenticate to delete your account.'),
|
1354 |
|
'methods': methods,
|
1355 |
|
},
|
|
1382 |
return self.reauthenticate(
|
|
1383 |
action='account-delete', message=_('You must re-authenticate to delete your account.')
|
1356 |
1384 |
)
|
1357 |
1385 |
return super().dispatch(request, *args, **kwargs)
|
1358 |
1386 |
|
1359 |
|
def has_recent_authentication(self):
|
1360 |
|
age = time.time() - utils_misc.last_authentication_event(request=self.request)['when']
|
1361 |
|
return age < self.last_authentication_max_age
|
1362 |
|
|
1363 |
1387 |
def post(self, request, *args, **kwargs):
|
1364 |
1388 |
if 'cancel' in request.POST:
|
1365 |
1389 |
return utils_misc.redirect(request, 'account_management')
|