3 from django.contrib.auth.models import User
5 from manifoldapi.manifoldapi import ManifoldAPI, ManifoldException, ManifoldResult
6 from manifold.core.query import Query
8 # Name my backend 'ManifoldBackend'
11 # Create an authentication method
12 # This is called by the standard Django login procedure
13 def authenticate(self, token=None):
18 username = token['username']
19 password = token['password']
20 request = token['request']
22 auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
23 api = ManifoldAPI(auth)
24 sessions_result = api.forward(Query.create('local:session').to_dict())
26 sessions = sessions_result.ok_value()
29 print "GetSession failed", sessions_result.error()
31 print "first", sessions
34 # Change to session authentication
35 api.auth = {'AuthMethod': 'session', 'session': session['session']}
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()
43 print "GetPersons failed",persons_result.error()
46 print "PERSON=", person
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
53 print "E: manifoldbackend", e
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.email = person['email']
66 if 'firstname' in person:
67 user.first_name = person['firstname']
68 if 'lastname' in person:
69 user.last_name = person['lastname']
73 # Required for your backend to work properly - unchanged in most scenarios
74 def get_user(self, user_id):
76 return User.objects.get(pk=user_id)
77 except User.DoesNotExist: