filenames without _ (spare my pinkie)
[myslice.git] / auth / manifoldbackend.py
1 # import the User object
2 from django.contrib.auth.models import User
3 from engine.manifoldapi import ManifoldAPI
4
5
6 # import time - this is used to create Django's internal username
7 import time
8
9 # Name my backend 'ManifoldBackend'
10 class ManifoldBackend:
11
12     # Create an authentication method
13     # This is called by the standard Django login procedure
14     def authenticate(self, username=None, password=None):
15         if not username or not password:
16             return None
17
18         try:
19             auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
20             api = ManifoldAPI(auth)
21             # Authenticate user and get session key
22             session = api.GetSession()
23             if not session : 
24                 return None
25
26             #self.session = session
27             # Change GetSession() at some point to return expires as well
28             expires = time.time() + (24 * 60 * 60)
29
30             # Change to session authentication
31             api.auth = {'AuthMethod': 'session', 'session': session}
32             #self.api = api
33
34             # Get account details
35             person = api.GetPersons(auth)
36             #self.person = person[0]
37         except:
38             return None
39
40         try:
41             # Check if the user exists in Django's local database
42             user = User.objects.get(email=username)
43         except User.DoesNotExist:
44             # Create a user in Django's local database
45             user = User.objects.create_user(time.time(), username, 'passworddoesntmatter')
46
47         return user
48
49     # Required for your backend to work properly - unchanged in most scenarios
50     def get_user(self, user_id):
51         try:
52             return User.objects.get(pk=user_id)
53         except User.DoesNotExist:
54             return None
55
56