Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #244 #245

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
73VW marked this conversation as resolved.
Show resolved Hide resolved

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
Loading