Skip to content

Commit 5f32137

Browse files
committed
Added email on user when creating a new user
1 parent b62970e commit 5f32137

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ an issue.
5959
- [Santiago Gandolfo](https://github.com/santigandolfo)
6060
- [Greg Wong](https://github.com/gregorywong)
6161
- [Michael V. Battista](https://github.com/mvbattista)
62+
- [Maël Pedretti](https://github.com/73VW)

django_saml2_auth/user.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
requires_cryptography)
2323
from jwt.exceptions import PyJWTError
2424

25+
FIRST_NAME_FIELD_NAME = "first_name"
26+
LAST_NAME_FIELD_NAME = "last_name"
2527

26-
def create_new_user(email: str,
27-
first_name: Optional[str] = None,
28-
last_name: Optional[str] = None,
29-
**kwargs) -> User:
28+
29+
def create_new_user(user_id: str, user: dict, **kwargs) -> User:
3030
"""Create a new user with the given information
3131
3232
Args:
33-
email (str): Email
34-
first_name (str): First name
35-
last_name (str): Last name
33+
user_id (str): User ID
34+
user (dict): User informations
3635
3736
Keyword Args:
3837
**kwargs: Additional keyword arguments
@@ -52,12 +51,16 @@ def create_new_user(email: str,
5251
is_superuser = dictor(saml2_auth_settings, "NEW_USER_PROFILE.SUPERUSER_STATUS", default=False)
5352
user_groups = dictor(saml2_auth_settings, "NEW_USER_PROFILE.USER_GROUPS", default=[])
5453

55-
if first_name and last_name:
56-
kwargs['first_name'] = first_name
57-
kwargs['last_name'] = last_name
54+
for field in [
55+
FIRST_NAME_FIELD_NAME,
56+
LAST_NAME_FIELD_NAME,
57+
user_model.EMAIL_FIELD,
58+
]:
59+
if field in user:
60+
kwargs[field] = user[field]
5861

5962
try:
60-
user = user_model.objects.create_user(email, **kwargs)
63+
user = user_model.objects.create_user(user_id, **kwargs)
6164
user.is_active = is_active
6265
user.is_staff = is_staff
6366
user.is_superuser = is_superuser
@@ -119,7 +122,7 @@ def get_or_create_user(user: Dict[str, Any]) -> Tuple[bool, User]:
119122
"reason": "Cannot create user. Missing user_id.",
120123
"status_code": 400
121124
})
122-
target_user = create_new_user(user_id, user["first_name"], user["last_name"])
125+
target_user = create_new_user(user_id, user)
123126

124127
create_user_trigger = dictor(saml2_auth_settings, "TRIGGER.CREATE_USER")
125128
if create_user_trigger:

0 commit comments

Comments
 (0)