Home > OS >  how to override user.check_password(password)
how to override user.check_password(password)

Time:05-09

I need to override user.check_password(password) method because I am using a legacy database which is using passwords hashed with .net framework I created a Function that can hash a password and compare it with the hash password which is already saved in the database.

my question is how can I override check_password function and when I use it with my function, it will return True

CodePudding user response:

I don't if this will work in this case but if you want to overwrite a class' method you can do it as such :

class test:
    def a(self):
        print("ok")

t = test()

You get

t.a() #-> ok

But you can overwrite it with

t.a = lambda *args:print("Redirected")
t.a() #-> Redirected

Not sure it will work in your case thow

you should try something like

user.check_password = lambda *args:True

But maybe this function set some other parameters inside the instance. This insure only that for this instance the function check_password is a function that returns True.

CodePudding user response:

i found a solution it was very easy

i used Django system function to keep admin panel working at the same time i used my own function (hash_validation)

from django.contrib.auth.hashers import check_password

# you have to put this function in your Model Class
 def check_password(self,password):
        if hash_validation(password=password, passHash=self.password):
            return True
        else:
            def setter(password):
                self.set_password(password)
                # Password hash upgrades shouldn't be considered password changes.
                self._password = None
                self.save(update_fields=["password"])
            return check_password(password, self.password, setter)
  • Related