jazzband / django-two-factor-auth

Complete Two-Factor Authentication for Django providing the easiest integration into most Django projects.
MIT License
1.71k stars 448 forks source link

How to conditionally disable some methods? #533

Closed last-partizan closed 1 year ago

last-partizan commented 2 years ago

Hello.

We want to disable using SMS methods for admin users, but allow it for normal users.

Right now we're using overriden SetupView, with following code:

class SetupView(two_factor.views.SetupView):
    condition_dict = {
        "method": lambda self: not self.__deny_sms_method(),
    }

    def __deny_sms_method(self):
        return self.request.user.is_staff

    def get_method(self):
        from two_factor.plugins.registry import registry
        if self.__deny_sms_method():
            return registry.get_method("generator")
        return super().get_method()

But it would be a lot cleaner if SetupView had get_methods method. And i can just override it and do any filtering i want.

What do you think? maybe there other ideas?

I can make PR if this is welcome change.

moggers87 commented 2 years ago

That sounds reasonable, feel free to make a PR :+1:

last-partizan commented 2 years ago

Please, take a look!