very first very rough django setup with a login/passwd view and a
[myslice.git] / auth / backend.py
1 # import the User object
2 from django.contrib.auth.models import User
3
4 # import the IMAP library
5 #from imaplib import IMAP4
6
7 # import time - this is used to create Django's internal username
8 import time
9
10 # Name my backend 'MyCustomBackend'
11 class MyCustomBackend:
12
13     hard_wired_users = { 'jean': '1234',
14                          'root': '2345',
15                          'jacques': '3456',
16                          }
17
18
19     # Create an authentication method
20     # This is called by the standard Django login procedure
21     def authenticate(self, username=None, password=None):
22         users=MyCustomBackend.hard_wired_users
23         if username not in users: return None
24         if password != users[username]: return None
25         try:
26             # Check if the user exists in Django's local database
27             user = User.objects.get(email=username)
28         except User.DoesNotExist:
29             # Create a user in Django's local database
30             user = User.objects.create_user(time.time(), username, 'passworddoesntmatter')
31
32         return user
33
34     # Required for your backend to work properly - unchanged in most scenarios
35     def get_user(self, user_id):
36         try:
37             return User.objects.get(pk=user_id)
38         except User.DoesNotExist:
39             return None