Skip to content

Commit a653695

Browse files
authored
Password Reset: Disable for SSO users (#13079)
1 parent 7338c97 commit a653695

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

dojo/api_v2/serializers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,13 @@ class Meta:
614614
model = UserContactInfo
615615
fields = "__all__"
616616

617+
def validate(self, data):
618+
user = data.get("user", None) or self.instance.user
619+
if data.get("force_password_reset", False) and not user.has_usable_password():
620+
msg = "Password resets are not allowed for users authorized through SSO."
621+
raise ValidationError(msg)
622+
return super().validate(data)
623+
617624

618625
class UserStubSerializer(serializers.ModelSerializer):
619626
class Meta:

dojo/forms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,7 +2388,14 @@ class Meta:
23882388
exclude = ["user", "slack_user_id"]
23892389

23902390
def __init__(self, *args, **kwargs):
2391+
user = kwargs.pop("user", None)
23912392
super().__init__(*args, **kwargs)
2393+
# Do not expose force password reset if the current user does not have a password to reset
2394+
if user is not None:
2395+
if not user.has_usable_password():
2396+
self.fields["force_password_reset"].disabled = True
2397+
self.fields["force_password_reset"].help_text = "This user is authorized through SSO, and does not have a password to reset"
2398+
# Determine some other settings based on the current user
23922399
current_user = get_current_user()
23932400
if not current_user.is_superuser:
23942401
if not user_has_configuration_permission(current_user, "auth.change_user") and \

dojo/user/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def view_profile(request):
230230
group_members = get_authorized_group_members_for_user(user)
231231

232232
user_contact = user.usercontactinfo if hasattr(user, "usercontactinfo") else None
233-
contact_form = UserContactInfoForm() if user_contact is None else UserContactInfoForm(instance=user_contact)
233+
contact_form = UserContactInfoForm(user=user) if user_contact is None else UserContactInfoForm(instance=user_contact, user=user)
234234

235235
global_role = user.global_role if hasattr(user, "global_role") else None
236236
if global_role is None:
@@ -242,7 +242,7 @@ def view_profile(request):
242242

243243
if request.method == "POST":
244244
form = DojoUserForm(request.POST, instance=user)
245-
contact_form = UserContactInfoForm(request.POST, instance=user_contact)
245+
contact_form = UserContactInfoForm(request.POST, instance=user_contact, user=user)
246246
global_role_form = GlobalRoleForm(request.POST, instance=global_role)
247247
if form.is_valid() and contact_form.is_valid() and global_role_form.is_valid():
248248
form.save()
@@ -393,17 +393,17 @@ def edit_user(request, uid):
393393
form = EditDojoUserForm(instance=user)
394394

395395
user_contact = user.usercontactinfo if hasattr(user, "usercontactinfo") else None
396-
contact_form = UserContactInfoForm() if user_contact is None else UserContactInfoForm(instance=user_contact)
396+
contact_form = UserContactInfoForm(user=user) if user_contact is None else UserContactInfoForm(instance=user_contact, user=user)
397397

398398
global_role = user.global_role if hasattr(user, "global_role") else None
399399
global_role_form = GlobalRoleForm() if global_role is None else GlobalRoleForm(instance=global_role)
400400

401401
if request.method == "POST":
402402
form = EditDojoUserForm(request.POST, instance=user)
403403
if user_contact is None:
404-
contact_form = UserContactInfoForm(request.POST)
404+
contact_form = UserContactInfoForm(request.POST, user=user)
405405
else:
406-
contact_form = UserContactInfoForm(request.POST, instance=user_contact)
406+
contact_form = UserContactInfoForm(request.POST, instance=user_contact, user=user)
407407

408408
if global_role is None:
409409
global_role_form = GlobalRoleForm(request.POST)

0 commit comments

Comments
 (0)