get rid of MyCustomBackend that had hard-wired user accounts
[unfold.git] / auth / views.py
1 # Create your views here.
2 from django.core.context_processors import csrf
3 from django.template import RequestContext
4 from django.shortcuts import render_to_response
5 from django.contrib.auth import authenticate, login, logout
6 from django.http import HttpResponseRedirect
7
8 from myslice.viewutils import topmenu_items, the_user
9 from myslice.config import Config
10
11 def login_user(request):
12     state = "Please log in below..."
13     username = password = ''
14     env={
15         'manifold_url':Config.manifold_url,
16         }
17
18     if request.POST:
19         username = request.POST.get('username')
20         password = request.POST.get('password')
21         
22         # pass request within the token, so manifold session key could be attached to the request session.
23         token = {'username': username, 'password': password, 'request': request}    
24
25         user = authenticate(token=token)
26         if user is not None:
27             if user.is_active:
28                 login(request, user)
29                 #state = "You're successfully logged in!"
30                 return HttpResponseRedirect ('/portal/dashboard')
31                 #return HttpResponseRedirect ('/login-ok')
32             else:
33                 env['state'] = "Your account is not active, please contact the site admin."
34                 return render_to_response('view-login.html',env, context_instance=RequestContext(request))
35         else:
36             env['state'] = "Your username and/or password were incorrect."
37             return render_to_response('view-login.html',env, context_instance=RequestContext(request))
38     else:
39         state='' #Welcome to MySlice'
40         env['state']=state
41         env['username']=the_user(request)
42         env['topmenu_items'] = topmenu_items('', request)
43         return render_to_response('view-login.html',env, context_instance=RequestContext(request))
44
45 # hard question : where should we redirect requests to logout if user is not logged in ?
46 def logout_user (request):
47     # check that we're indeed logged in
48     if not request.user.is_authenticated():
49         return HttpResponseRedirect ('/')
50     logout(request)
51     return HttpResponseRedirect ('/')
52         
53