Skip to main content

SetUnusablePasswordMixin

Form mixin that allows setting an unusable password for a user.

Attributes

AttributeTypeDescription
usable_password_help_textstringWhether the user will be able to authenticate using a password or not. If disabled, they may still be able to authenticate using other backends, such as Single Sign-On or LDAP.

Methods


create_usable_password_field()

@classmethod
def create_usable_password_field(
help_text: string = usable_password_help_text
) - > forms.ChoiceField

Creates a form field that allows users to toggle whether password-based authentication is enabled or disabled.

Parameters

NameTypeDescription
help_textstring = usable_password_help_textThe explanatory text displayed alongside the field to describe the implications of disabling password authentication.

Returns

TypeDescription
forms.ChoiceFieldA radio select choice field configured with 'Enabled' and 'Disabled' options.

validate_passwords()

@classmethod
def validate_passwords(
password1_field_name: string = "password1",
password2_field_name: string = "password2",
usable_password_field_name: string = "usable_password"
) - > null

Validates the password fields based on whether the usable password toggle is enabled. If disabled, it bypasses standard password validation; otherwise, it ensures required password fields are present and valid.

Parameters

NameTypeDescription
password1_field_namestring = "password1"The name of the first password input field in the form.
password2_field_namestring = "password2"The name of the second password input field used for confirmation.
usable_password_field_namestring = "usable_password"The name of the field indicating if the user should have a usable password.

Returns

TypeDescription
nullThis method does not return a value but updates the form's cleaned_data and error collection.

validate_password_for_user()

@classmethod
def validate_password_for_user(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
**kwargs: dict
) - > null

Performs user-specific password validation only if the form is configured to set a usable password.

Parameters

NameTypeDescription
user[User](../models/user.md?sid=django_contrib_auth_models_user)The user instance against which the password complexity or policy is being validated.
**kwargsdictAdditional keyword arguments passed to the superclass validation method.

Returns

TypeDescription
nullThis method does not return a value but may raise ValidationErrors via the superclass.

set_password_and_save()

@classmethod
def set_password_and_save(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
commit: boolean = True,
**kwargs: dict
) - > [User](../models/user.md?sid=django_contrib_auth_models_user)

Updates the user's password or sets it as unusable based on the form selection, then optionally persists the user to the database.

Parameters

NameTypeDescription
user[User](../models/user.md?sid=django_contrib_auth_models_user)The user instance whose password credentials are being updated.
commitboolean = TrueDetermines whether the user instance should be immediately saved to the database.
**kwargsdictAdditional keyword arguments passed to the superclass password setting method.

Returns

TypeDescription
[User](../models/user.md?sid=django_contrib_auth_models_user)The updated user instance with the new password state applied.