Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions openwisp_users/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@

if app_settings.SOCIALACCOUNT_ENABLED:
urlpatterns += [path("social/", include("allauth.socialaccount.urls"))]

for provider in providers.registry.get_class_list():
try:
prov_mod = import_module(provider.get_package() + ".urls")
except ImportError:
continue
prov_urlpatterns = getattr(prov_mod, "urlpatterns", None)
if prov_urlpatterns:
urlpatterns += prov_urlpatterns
for provider in providers.registry.get_class_list():
try:
prov_mod = import_module(provider.get_package() + ".urls")
except ImportError:
continue
prov_urlpatterns = getattr(prov_mod, "urlpatterns", None)
if prov_urlpatterns:
urlpatterns += prov_urlpatterns
28 changes: 23 additions & 5 deletions openwisp_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,32 @@ def get_user(self, obj):
admin.site.unregister(EmailAddress)

if allauth_settings.SOCIALACCOUNT_ENABLED:
socialaccount_models = [
_unregister_socialaccount_models = [
("socialaccount", "SocialToken"),
("socialaccount", "SocialAccount"),
]
# Allow managing secrets if OAuth/SAML is enabled
if not app_settings.SOCIALACCOUNT_ADMIN_NEEDED:
socialaccount_models.append(("socialaccount", "SocialApp"))
for model in socialaccount_models:
# allauth OAuth/SAML not enabled
if not app_settings.SOCIALACCOUNT_ADMIN_NEEDED: # pragma: no cover
_unregister_socialaccount_models.append(("socialaccount", "SocialApp"))
# allauth OAuth/SAML enabled
else:
from allauth.socialaccount.models import SocialAccount

class SocialAccountInline(admin.StackedInline):
model = SocialAccount
extra = 0
readonly_fields = ("provider", "uid", "extra_data")

def has_add_permission(self, request, obj=None):
return False

def has_delete_permission(self, request, obj=None):
return False

UserAdmin.inlines.append(SocialAccountInline)

# Un-register cluttering socialaccount models
for model in _unregister_socialaccount_models:
model_class = apps.get_model(*model)
if admin.site.is_registered(model_class):
admin.site.unregister(model_class)
Expand Down
17 changes: 16 additions & 1 deletion openwisp_users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _get_user_edit_form_inline_params(self, user, organization):

@property
def add_user_inline_params(self):
return {
params = {
"emailaddress_set-TOTAL_FORMS": 0,
"emailaddress_set-INITIAL_FORMS": 0,
"emailaddress_set-MIN_NUM_FORMS": 0,
Expand All @@ -83,6 +83,19 @@ def add_user_inline_params(self):
f"{self.app_label}_organizationuser-MIN_NUM_FORMS": 0,
f"{self.app_label}_organizationuser-MAX_NUM_FORMS": 0,
}
self._add_socialaccount_inline_params(params)
return params

def _add_socialaccount_inline_params(self, params):
if app_settings.SOCIALACCOUNT_ADMIN_NEEDED:
params.update(
{
"socialaccount_set-TOTAL_FORMS": 0,
"socialaccount_set-INITIAL_FORMS": 0,
"socialaccount_set-MIN_NUM_FORMS": 0,
"socialaccount_set-MAX_NUM_FORMS": 0,
}
)

def test_admin_add_user_auto_email(self):
admin = self._create_admin()
Expand Down Expand Up @@ -1702,6 +1715,7 @@ class TestBasicUsersIntegration(

app_label = "openwisp_users"
is_integration_test = True
_add_socialaccount_inline_params = TestUsersAdmin._add_socialaccount_inline_params

def _get_user_edit_form_inline_params(self, user, organization):
params = {
Expand Down Expand Up @@ -1740,6 +1754,7 @@ def _get_user_edit_form_inline_params(self, user, organization):
f"{self.app_label}_organizationuser-0-user": str(user.pk),
}
)
self._add_socialaccount_inline_params(params)
return params

def test_change_user(self):
Expand Down
1 change: 1 addition & 0 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.google", # for test coverage
"openwisp_users",
# openwisp2 admin theme
# (must be loaded here)
Expand Down
Loading