Closed asucrews closed 2 months ago
I have got this working using purely other people's code, I just combined the account adapter's from both of these packages.
settings.py
ACCOUNT_ADAPTER = 'myapp.adapter.ComboAdapter'
repo/myapp/adapter.py
from allauth_2fa.adapter import OTPAdapter
from allauth.account.signals import user_signed_up
from django.conf import settings
# Subclassing OTPAdapter gives us allauth_2fa functions
class ComboAdapter(OTPAdapter):
# Could not attach these functions directly due to the logic here
# https://github.com/bee-keeper/django-invitations/blob/master/invitations/models.py#L76
# These are copied from InvitationsAdapter from django-invitations
def is_open_for_signup(self, request):
if hasattr(request, 'session') and request.session.get(
'account_verified_email'):
return True
elif settings.INVITATION_ONLY is True:
# Site is ONLY open for invites
return False
else:
# Site is open to signup
return True
def get_user_signed_up_signal(self):
return user_signed_up
Since I'm hard-copying functions from InvitationsAdapter into ComboAdapter, please make sure you do a fresh copy yourself if you're viewing this in the future. Go to https://github.com/bee-keeper/django-invitations/blob/master/invitations/models.py and copy anything inside of InvitationsAdapter into ComboAdapter.
In the future, if the logic I mentioned in my code comment above is removed and InvitationsAdapter is available as a class at all times, then it would be much cleaner (and future-proof) to do this:
# THIS SNIPPET IS A SUGGESTION FOR THE FUTURE, use the code above for now
from allauth_2fa.adapter import OTPAdapter
from invitations.models import InvitationsAdapter
class ComboAdapter(OTPAdapter, InvitationsAdapter):
pass
I think we had this working at some point at percipient, I don't have the code handy though. I believe #30 actually was used to fix this case.
I think your suggested code should pretty much work, you can then set ACCOUNT_ADAPTER = "path.to.ComboAdapter"
.
Yup that's what this was doing:
ACCOUNT_ADAPTER = 'myapp.adapter.ComboAdapter'
ACCOUNT_ADAPTER
is set specifically to invitations.models.InvitationsAdapter
.It would be great if this class https://github.com/bee-keeper/django-invitations/blob/master/invitations/models.py#L81 was always instantiated even when not in use. 🙏🏻
It cannot detect this (nor can it allow you to subclass) because InvitationsAdapter only exists if your
ACCOUNT_ADAPTER
is set specifically toinvitations.models.InvitationsAdapter
.
Ah...yeah that's familiar now. I believe we ended up on the same solution as you -- essentially vendoring that code into our application. I think your proposed solution of always having that code exist makes sense. 👍
just woundering if i model was made always available?
@asucrews No, the latest version of django-invitations still has not changed this approach.
Note if you're getting the error: AttributeError: 'function' object has no attribute 'view_class'
This was an issue in django-allauth-2fa and was recently fixed on the main branch but has not been released yet. https://github.com/valohai/django-allauth-2fa/issues/143
Just checking in to see if this was made possible?
allauth supports two factor authentication out of the box now. Let's close this issue, unless there are people still interested in integrating django-allauth-2fa.
An organization I work with helped fund the development of the allauth native 2fa feature, in hopes that it would be better maintained than the allauth-2fa library. We're migrating and recommend others do too.
Closing, native is better in this case.
Hello, I was wondering if anyone got django-allauth-2fa to work with django-invitations?
I am trying myself, but I can't figure out what changes need to be made to the account adapter. Below is my custom account adapter. With this, I don't get any error messages, but it doesn't seem to load settings correctly. mainly the invitation_only option does not seem to work correctly.
Oh, I have also tested allauth-2fa and it works without invitations, and invitations works without allauth-2fa.
Thanks!
-- INVITATIONS_INVITATION_EXPIRY = 7 INVITATIONS_INVITATION_ONLY = True INVITATIONS_ACCEPT_INVITE_AFTER_SIGNUP = True INVITATIONS_ADAPTER = 'core.adapter.BaseInvitationsAdapter'
from django.conf import settings from django.contrib import messages from django.contrib.sites.models import Site from django.core.mail import EmailMessage, EmailMultiAlternatives from django.template.exceptions import TemplateDoesNotExist from django.utils.encoding import force_text from django.template.loader import render_to_string from allauth.account.signals import user_signed_up from invitations.app_settings import app_settings from allauth_2fa.adapter import DefaultAccountAdapter
class OTPAdapter(DefaultAccountAdapter):
class BaseInvitationsAdapter(object): def stash_verified_email(self, request, email): request.session['account_verified_email'] = email