| 671 | | from django.conf import settings |
|---|
| 672 | | from django.contrib.auth.models import User, check_password |
|---|
| 673 | | |
|---|
| 674 | | class SettingsBackend: |
|---|
| 675 | | """ |
|---|
| 676 | | Authenticate against vars in settings.py Use the login name, and a hash |
|---|
| 677 | | of the password. For example: |
|---|
| 678 | | |
|---|
| 679 | | ADMIN_LOGIN = 'admin' |
|---|
| 680 | | ADMIN_PASSWORD = 'sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de' |
|---|
| 681 | | """ |
|---|
| 682 | | def authenticate(self, username=None, password=None): |
|---|
| 683 | | login_valid = (settings.ADMIN_LOGIN == username) |
|---|
| 684 | | pwd_valid = check_password(password, settings.ADMIN_PASSWORD) |
|---|
| 685 | | if login_valid and pwd_valid: |
|---|
| | 671 | from django.conf import settings |
|---|
| | 672 | from django.contrib.auth.models import User, check_password |
|---|
| | 673 | |
|---|
| | 674 | class SettingsBackend: |
|---|
| | 675 | """ |
|---|
| | 676 | Authenticate against vars in settings.py Use the login name, and a hash |
|---|
| | 677 | of the password. For example: |
|---|
| | 678 | |
|---|
| | 679 | ADMIN_LOGIN = 'admin' |
|---|
| | 680 | ADMIN_PASSWORD = 'sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de' |
|---|
| | 681 | """ |
|---|
| | 682 | def authenticate(self, username=None, password=None): |
|---|
| | 683 | login_valid = (settings.ADMIN_LOGIN == username) |
|---|
| | 684 | pwd_valid = check_password(password, settings.ADMIN_PASSWORD) |
|---|
| | 685 | if login_valid and pwd_valid: |
|---|
| | 686 | try: |
|---|
| | 687 | user = User.objects.get(username=username) |
|---|
| | 688 | except User.DoesNotExist: |
|---|
| | 689 | # Create a new user. Note that we can set password to anything |
|---|
| | 690 | # as it won't be checked, the password from settings.py will. |
|---|
| | 691 | user = User(username=username, password='get from settings.py') |
|---|
| | 692 | user.is_staff = True |
|---|
| | 693 | user.is_superuser = True |
|---|
| | 694 | user.save() |
|---|
| | 695 | return user |
|---|
| | 696 | return None |
|---|
| | 697 | |
|---|
| | 698 | def get_user(self, user_id): |
|---|
| 689 | | # Create a new user. Note that we can set password to anything |
|---|
| 690 | | # as it won't be checked, the password from settings.py will. |
|---|
| 691 | | user = User(username=username, password='get from settings.py') |
|---|
| 692 | | user.is_staff = True |
|---|
| 693 | | user.is_superuser = True |
|---|
| 694 | | user.save() |
|---|
| 695 | | return user |
|---|
| 696 | | return None |
|---|
| 697 | | |
|---|
| 698 | | def get_user(self, user_id): |
|---|
| 699 | | try: |
|---|
| 700 | | return User.objects.get(pk=user_id) |
|---|
| 701 | | except User.DoesNotExist: |
|---|
| 702 | | return None |
|---|
| 703 | | |
|---|
| 704 | | .. _django.contrib.auth.backends.SettingsBackend: http://code.djangoproject.com/browser/django/branches/magic-removal/django/contrib/auth/backends.py |
|---|
| | 702 | return None |
|---|