418 |
418 |
homepage = Homepage.as_view()
|
419 |
419 |
|
420 |
420 |
|
421 |
|
class ProfileView(cbv.TemplateNamesMixin, TemplateView):
|
422 |
|
template_names = ['idp/account_management.html', 'authentic2/accounts.html']
|
423 |
|
title = _('Your account')
|
424 |
|
|
425 |
|
def dispatch(self, request, *args, **kwargs):
|
426 |
|
if app_settings.A2_ACCOUNTS_URL:
|
427 |
|
return utils.redirect(request, app_settings.A2_ACCOUNTS_URL)
|
428 |
|
return super(ProfileView, self).dispatch(request, *args, **kwargs)
|
|
421 |
class AuthenticatorsView(cbv.TemplateNamesMixin, TemplateView):
|
|
422 |
template_name = 'authentic2/authenticators.html'
|
|
423 |
title = _('Authenticators')
|
|
424 |
authenticators_level = 0
|
429 |
425 |
|
430 |
426 |
def get_context_data(self, **kwargs):
|
431 |
|
context = super(ProfileView, self).get_context_data(**kwargs)
|
|
427 |
context = super(AuthenticatorsView, self).get_context_data(**kwargs)
|
432 |
428 |
request = self.request
|
433 |
|
auth_level = int(request.GET.get('auth_level', 0))
|
434 |
|
frontends = utils.get_backends('AUTH_FRONTENDS', required_auth_level=auth_level)
|
|
429 |
|
|
430 |
auth_level = self.authenticators_level or int(request.GET.get('auth_level', 0))
|
|
431 |
frontends = utils.get_backends('AUTH_FRONTENDS', auth_level)
|
435 |
432 |
|
436 |
433 |
if request.method == "POST":
|
437 |
434 |
for frontend in frontends:
|
... | ... | |
441 |
438 |
if request.session.test_cookie_worked():
|
442 |
439 |
request.session.delete_test_cookie()
|
443 |
440 |
return frontend.post(request, form, None, '/profile')
|
444 |
|
# User attributes management
|
|
441 |
|
|
442 |
parameters = {'request': self.request,
|
|
443 |
'context': context}
|
|
444 |
profiles = [utils.get_authenticator_method(frontend, 'profile', parameters) for frontend in frontends]
|
|
445 |
# Old frontends data structure for templates
|
|
446 |
blocks = [block['content'] for block in profiles if block]
|
|
447 |
# New frontends data structure for templates
|
|
448 |
blocks_by_id = collections.OrderedDict((block['id'], block) for block in profiles if block)
|
|
449 |
|
|
450 |
context.update({
|
|
451 |
'frontends_block': blocks,
|
|
452 |
'frontends_block_by_id': blocks_by_id,
|
|
453 |
})
|
|
454 |
hooks.call_hooks('modify_context_data', self, context)
|
|
455 |
return context
|
|
456 |
|
|
457 |
authenticators = AuthenticatorsView.as_view()
|
|
458 |
|
|
459 |
|
|
460 |
class ProfileView(AuthenticatorsView):
|
|
461 |
template_names = ['idp/account_management.html', 'authentic2/accounts.html']
|
|
462 |
title = _('Your account')
|
|
463 |
authenticators_level = 1
|
|
464 |
|
|
465 |
def dispatch(self, request, *args, **kwargs):
|
|
466 |
if app_settings.A2_ACCOUNTS_URL:
|
|
467 |
return utils.redirect(request, app_settings.A2_ACCOUNTS_URL)
|
|
468 |
return super(ProfileView, self).dispatch(request, *args, **kwargs)
|
|
469 |
|
|
470 |
def get_context_data(self, **kwargs):
|
|
471 |
context = super(ProfileView, self).get_context_data(**kwargs)
|
|
472 |
user = self.request.user
|
|
473 |
|
445 |
474 |
profile = []
|
446 |
475 |
field_names = app_settings.A2_PROFILE_FIELDS
|
447 |
476 |
if not field_names:
|
448 |
477 |
field_names = list(app_settings.A2_REGISTRATION_FIELDS)
|
449 |
|
for field_name in getattr(request.user, 'USER_PROFILE', []):
|
|
478 |
for field_name in getattr(user, 'USER_PROFILE', []):
|
450 |
479 |
if field_name not in field_names:
|
451 |
480 |
field_names.append(field_name)
|
452 |
481 |
qs = models.Attribute.objects.filter(Q(user_editable=True) | Q(user_visible=True))
|
... | ... | |
471 |
500 |
if not attribute.user_visible:
|
472 |
501 |
continue
|
473 |
502 |
html_value = attribute.get_kind().get('html_value', lambda a, b: b)
|
474 |
|
qs = models.AttributeValue.objects.with_owner(request.user)
|
|
503 |
qs = models.AttributeValue.objects.with_owner(user)
|
475 |
504 |
qs = qs.filter(attribute=attribute)
|
476 |
505 |
qs = qs.select_related()
|
477 |
506 |
value = [at_value.to_python() for at_value in qs]
|
... | ... | |
482 |
511 |
else:
|
483 |
512 |
# fallback to model attributes
|
484 |
513 |
try:
|
485 |
|
field = request.user._meta.get_field(field_name)
|
|
514 |
field = user._meta.get_field(field_name)
|
486 |
515 |
except FieldDoesNotExist:
|
487 |
516 |
continue
|
488 |
517 |
if not title:
|
489 |
518 |
title = field.verbose_name
|
490 |
|
value = getattr(self.request.user, field_name, None)
|
|
519 |
value = getattr(user, field_name, None)
|
491 |
520 |
attribute = models.Attribute(name=field_name, label=title)
|
492 |
521 |
|
493 |
522 |
raw_value = None
|
... | ... | |
501 |
530 |
if value or app_settings.A2_PROFILE_DISPLAY_EMPTY_FIELDS:
|
502 |
531 |
profile.append((title, value))
|
503 |
532 |
attributes.append({'attribute': attribute, 'values': raw_value})
|
504 |
|
|
505 |
|
# Credentials management
|
506 |
|
parameters = {'request': request,
|
507 |
|
'context': context}
|
508 |
|
profiles = [utils.get_authenticator_method(frontend, 'profile', parameters) for frontend in frontends]
|
509 |
|
# Old frontends data structure for templates
|
510 |
|
blocks = [block['content'] for block in profiles if block]
|
511 |
|
# New frontends data structure for templates
|
512 |
|
blocks_by_id = collections.OrderedDict((block['id'], block) for block in profiles if block)
|
513 |
|
|
514 |
|
idp_backends = utils.get_backends()
|
515 |
|
# Get actions for federation management
|
516 |
|
federation_management = []
|
517 |
|
if app_settings.A2_PROFILE_CAN_MANAGE_FEDERATION:
|
518 |
|
for idp_backend in idp_backends:
|
519 |
|
if hasattr(idp_backend, 'federation_management'):
|
520 |
|
federation_management.extend(idp_backend.federation_management(request))
|
521 |
533 |
context.update({
|
522 |
|
'frontends_block': blocks,
|
523 |
|
'frontends_block_by_id': blocks_by_id,
|
524 |
534 |
'profile': profile,
|
525 |
535 |
'attributes': attributes,
|
526 |
536 |
'allow_account_deletion': app_settings.A2_REGISTRATION_CAN_DELETE_ACCOUNT,
|
527 |
537 |
'allow_profile_edit': EditProfile.can_edit_profile(),
|
528 |
538 |
'allow_email_change': app_settings.A2_PROFILE_CAN_CHANGE_EMAIL,
|
|
539 |
'multifactor_available': bool(utils.get_backends('AUTH_FRONTENDS', 0)),
|
529 |
540 |
# TODO: deprecated should be removed when publik-base-theme is updated
|
530 |
|
'allow_password_change': utils.user_can_change_password(request=request),
|
531 |
|
'federation_management': federation_management,
|
532 |
|
'auth_level': auth_level,
|
|
541 |
'allow_password_change': utils.user_can_change_password(request=self.request),
|
533 |
542 |
})
|
|
543 |
self.set_federation_management_context(context)
|
534 |
544 |
hooks.call_hooks('modify_context_data', self, context)
|
535 |
545 |
return context
|
536 |
546 |
|
|
547 |
def set_federation_management_context(self, context):
|
|
548 |
idp_backends = utils.get_backends()
|
|
549 |
# Get actions for federation management
|
|
550 |
federation_management = []
|
|
551 |
if app_settings.A2_PROFILE_CAN_MANAGE_FEDERATION:
|
|
552 |
for idp_backend in idp_backends:
|
|
553 |
if hasattr(idp_backend, 'federation_management'):
|
|
554 |
federation_management.extend(idp_backend.federation_management(self.request))
|
|
555 |
context['federation_management'] = federation_management
|
|
556 |
|
537 |
557 |
profile = login_required(ProfileView.as_view())
|
538 |
558 |
|
539 |
559 |
|
540 |
|
-
|