title: How to disable django-admin's change password, for users who signed up via LDAP server
body: <p>We have an application where users can be uploaded via GUI. Other users of the application can come from a LDAP server. LDAP Server-backed users must not be able to change their django-admin password. The best, if they don't even have one. I know that there is a possibility to disable their password:</p>
  <pre><code>user.set_unusable_password()
  </code></pre>
  <p>Now the thing is that I must not disable the passwords for the GUI-uploaded users, only for those who came from a LDAP-server (because only LDAP-server backed users can access the application anyways).</p>
  <p>What is the best place and way to distinguish between the two? I've overridden <code>create_user()</code> in managers.py:</p>
  <pre><code>class CustomUserManager(BaseUserManager):
  &quot;&quot;&quot;
  Custom user model manager where email is the unique identifiers
  for authentication instead of usernames.
  &quot;&quot;&quot;
  def create_user(self, email, password, **extra_fields):
      &quot;&quot;&quot;
      Create and save a User with the given email and password.
      &quot;&quot;&quot;
     
      if not email:
          raise ValueError(_('The Email must be set'))
      email = self.normalize_email(email)
      user = self.model(email=email, **extra_fields)
      user.set_password(password)
      user.save()
     
      return user
  </code></pre>
  <p>If I put <code>user.set_unusable_password()</code> into the method above, I would get <strong>the exact opposite</strong> (i.e. I would disable the pw for GUI-uploaded users, while left it intact for those who come from LDAP-server)</p>
  <p>What should I modify/override/add etc if my goal is <strong>to intercept the event when a LDAP-backed user first logs in our application?</strong> Which method, function, class, in which file?</p>
  <p>Below is the relevant model:</p>
  <pre><code>class CustomUser(AbstractBaseUser, PermissionsMixin):
      email = models.EmailField(_('email address'), unique=True)       
      is_staff = models.BooleanField(default=False)
      is_active = models.BooleanField(default=True)
      date_joined = models.DateTimeField(default=timezone.now)
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = []
  
      objects = CustomUserManager()
  
      def __str__(self):
          return self.email
  </code></pre>
  <p>and the relevant part of <code>forms.py</code>:</p>
  <pre><code>class CustomUserCreationForm(UserCreationForm):
  
      class Meta:
          model = CustomUser
          fields = ('email',)
  
  
  class CustomUserChangeForm(UserChangeForm):
  
      class Meta:
          model = CustomUser
          fields = ('email',)
  </code></pre>
  <p>Thanks in advance.</p>