ForgotPass: Check the entered email with the local:user in manifold db
[unfold.git] / auth / manifoldbackend.py
1 import time
2
3 from django.contrib.auth.models import User
4
5 from manifold.manifoldapi import ManifoldAPI, ManifoldException, ManifoldResult
6 from manifold.core.query        import Query
7
8 # Name my backend 'ManifoldBackend'
9 class ManifoldBackend:
10
11     # Create an authentication method
12     # This is called by the standard Django login procedure
13     def authenticate(self, token=None):
14         if not token:
15             return None
16
17         try:
18             username = token['username']
19             password = token['password']
20             request = token['request']
21
22             auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
23             api = ManifoldAPI(auth)
24             sessions_result = api.forward(Query.create('local:session').to_dict())
25             print "result"
26             sessions = sessions_result.ok_value()
27             print "ok"
28             if not sessions:
29                 print "GetSession failed", sessions_result.error()
30                 return
31             print "first", sessions
32             session = sessions[0]
33
34             # Change to session authentication
35             api.auth = {'AuthMethod': 'session', 'session': session['session']}
36             self.api = api
37
38             # Get account details
39             # the new API would expect Get('local:user') instead
40             persons_result = api.forward(Query.get('local:user').to_dict())
41             persons = persons_result.ok_value()
42             if not persons:
43                 print "GetPersons failed",persons_result.error()
44                 return
45             person = persons[0]
46             print "PERSON=", person
47
48             request.session['manifold'] = {'auth': api.auth, 'person': person, 'expires': session['expires']}
49         except ManifoldException, e:
50             print "ManifoldBackend.authenticate caught ManifoldException, returning corresponding ManifoldResult"
51             return e.manifold_result
52         except Exception, e:
53             print "E: manifoldbackend", e
54             import traceback
55             traceback.print_exc()
56             return None
57
58         try:
59             # Check if the user exists in Django's local database
60             user = User.objects.get(username=username)
61         except User.DoesNotExist:
62             # Create a user in Django's local database
63             user = User.objects.create_user(username, username, 'passworddoesntmatter')
64             user.first_name = "DUMMY_FIRST_NAME" #person['first_name']
65             user.last_name = "DUMMY LAST NAME" # person['last_name']
66             user.email = person['email']
67         return user
68
69     # Required for your backend to work properly - unchanged in most scenarios
70     def get_user(self, user_id):
71         try:
72             return User.objects.get(pk=user_id)
73         except User.DoesNotExist:
74             return None
75
76