Skip to content

Commit

Permalink
Add management for setting SOCIALACCOUNT_USER_GROUP (#1001) (#1002)
Browse files Browse the repository at this point in the history
Co-authored-by: wlorenzetti <[email protected]>
(cherry picked from commit a38eabd)

Co-authored-by: Walter Lorenzetti <[email protected]>
  • Loading branch information
github-actions[bot] and wlorenzetti authored Dec 23, 2024
1 parent da83083 commit d7c4904
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions g3w-admin/usersmanage/vendors/allauth/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
from usersmanage.models import User, Group as AuthGroup, Userbackend, USER_BACKEND_DEFAULT
from usersmanage.configs import G3W_EDITOR1, G3W_EDITOR2, G3W_VIEWER1

import logging

logging = logging.getLogger('g3wadmin.debug')

class G3WSocialAccountAdapter(DefaultSocialAccountAdapter):

def _set_user_role_backend(self, user):
"""
Set the role and the backend for the user login by social
Set up alse the group of user if the settings is set
"""

# Role to se from settings
Expand All @@ -34,6 +39,22 @@ def _set_user_role_backend(self, user):
if not hasattr(user, 'userbackend'):
Userbackend(user=user, backend=USER_BACKEND_DEFAULT).save()

# Add User group if is set
if hasattr(settings, 'SOCIALACCOUNT_USER_GROUP'):
try:
ugroup = AuthGroup.objects.get(name=settings.SOCIALACCOUNT_USER_GROUP)

# Test user role group compatibility
if role in (G3W_VIEWER1, ) and ugroup.grouprole.role != 'viewer':
raise Exception(f"User role {G3W_VIEWER1} not compatible with user group {ugroup}")

# Assign user to group
ugroup.user_set.add(user)
except Exception as e:
logging.error("[SOCIAL AUTH] Error setting user group: {}".format(e))



def pre_social_login(self, request, sociallogin):

# Social account already exists, so this is just a login
Expand Down

0 comments on commit d7c4904

Please sign in to comment.