Skip to content

Commit

Permalink
Added email on user when creating a new user
Browse files Browse the repository at this point in the history
  • Loading branch information
73VW committed Jan 30, 2024
1 parent b62970e commit 5f32137
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ an issue.
- [Santiago Gandolfo](https://github.com/santigandolfo)
- [Greg Wong](https://github.com/gregorywong)
- [Michael V. Battista](https://github.com/mvbattista)
- [Maël Pedretti](https://github.com/73VW)
27 changes: 15 additions & 12 deletions django_saml2_auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
requires_cryptography)
from jwt.exceptions import PyJWTError

FIRST_NAME_FIELD_NAME = "first_name"
LAST_NAME_FIELD_NAME = "last_name"

def create_new_user(email: str,
first_name: Optional[str] = None,
last_name: Optional[str] = None,
**kwargs) -> User:

def create_new_user(user_id: str, user: dict, **kwargs) -> User:
"""Create a new user with the given information
Args:
email (str): Email
first_name (str): First name
last_name (str): Last name
user_id (str): User ID
user (dict): User informations
Keyword Args:
**kwargs: Additional keyword arguments
Expand All @@ -52,12 +51,16 @@ def create_new_user(email: str,
is_superuser = dictor(saml2_auth_settings, "NEW_USER_PROFILE.SUPERUSER_STATUS", default=False)
user_groups = dictor(saml2_auth_settings, "NEW_USER_PROFILE.USER_GROUPS", default=[])

if first_name and last_name:
kwargs['first_name'] = first_name
kwargs['last_name'] = last_name
for field in [
FIRST_NAME_FIELD_NAME,
LAST_NAME_FIELD_NAME,
user_model.EMAIL_FIELD,
]:
if field in user:
kwargs[field] = user[field]

try:
user = user_model.objects.create_user(email, **kwargs)
user = user_model.objects.create_user(user_id, **kwargs)
user.is_active = is_active
user.is_staff = is_staff
user.is_superuser = is_superuser
Expand Down Expand Up @@ -119,7 +122,7 @@ def get_or_create_user(user: Dict[str, Any]) -> Tuple[bool, User]:
"reason": "Cannot create user. Missing user_id.",
"status_code": 400
})
target_user = create_new_user(user_id, user["first_name"], user["last_name"])
target_user = create_new_user(user_id, user)

create_user_trigger = dictor(saml2_auth_settings, "TRIGGER.CREATE_USER")
if create_user_trigger:
Expand Down

0 comments on commit 5f32137

Please sign in to comment.