Skip to content

Ensure unique email addresses script improvement #19304

@ifleg

Description

@ifleg

Do you see an area that can be clarified or expanded, a technical inaccuracy, or a broken link?

Hi,
The suggested script to Ensure unique email addresses in the documentation seems to have a problem. Indeed, if you add this policy to the default user setting prompt stage, then it prevent users to update their informations (because ak_user_by(email__iexact=email) will match their own account and the policy will return False).

Link

https://docs.goauthentik.io/customize/policies/expression/unique_email/

Solution

The script should then check that the use with this email is different from the current user itself. The following code should do it.

# Ensure this matches the *Field Key* value of the prompt
field_name = "email"
email = request.context["prompt_data"][field_name]
# Search for users with this email
other_user = ak_user_by(email__iexact=email)
if other_user:
  # Ensure that it is another user
  if ( request.user.username != other_user.username):
    ak_message(other_user.username + " already use this email")
    return False
return True

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:docsFeatures or issues related to Docusaurus

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions