Changeset 8748 for mauRepo/dj_security


Ignore:
Timestamp:
27/03/13 15:29:56 (7 years ago)
Author:
mnagni
Message:

Incomplete - # 22698: [CEDA Site Python Port] MyCEDA Login needs to be more user-friendly
 http://team.ceda.ac.uk/trac/ceda/ticket/22698
Incomplete - # 22689: [CEDA Site Python Port] Logout link
 http://team.ceda.ac.uk/trac/ceda/ticket/22689

Location:
mauRepo/dj_security/trunk/dj_security
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/dj_security/trunk/dj_security/middleware.py

    r8741 r8748  
    3434from django.conf import settings 
    3535from django.db.utils import DatabaseError 
    36 from dj_security.exception import DSJOpenIDNotFoundError, DJSException 
     36from dj_security.exception import DSJOpenIDNotFoundError 
    3737 
    3838from userdb_model.models import User 
     
    4242import base64 
    4343import logging 
    44 from django.contrib.auth import SESSION_KEY 
    4544 
    4645# Get an instance of a logger 
  • mauRepo/dj_security/trunk/dj_security/views/dj_security_login.py

    r8741 r8748  
    55''' 
    66from django.contrib.auth import authenticate, REDIRECT_FIELD_NAME, \ 
    7     SESSION_KEY, BACKEND_SESSION_KEY 
    8 from django.shortcuts import render_to_response 
     7    SESSION_KEY 
    98from django.core.context_processors import csrf 
    109from django.conf import settings 
    1110from django_authopenid.views import not_authenticated, ask_openid,\ 
    12     signin_failure, _build_context, complete 
     11    signin_failure, _build_context 
    1312from django_authopenid.forms import OpenidSigninForm 
    1413from django.db.models import Q 
     
    3534LOGGER = logging.getLogger(__name__) 
    3635 
    37 OPENID = 'openid' 
     36OPENID = 'openid.identity' 
    3837 
    3938def error_handle(request, context): 
     
    4140    context['form'] = form 
    4241    context.update(csrf(request)) 
    43     return render_to_response('login.html', context) 
     42    return render('login.html', context) 
    4443 
    4544class CedaUserAuthenticationBackend(ModelBackend): 
    4645    """ 
    47     Extends Django's ``ModelBackend`` to allow login via username, or verification token. 
     46    Extends Django's ``ModelBackend`` to allow login via username,  
     47    or verification token. 
    4848 
    4949    Args are either ``username`` and ``password`` 
     
    7272                    if user.md5passwd == md5.new(password).hexdigest(): 
    7373                        return user 
    74        
    75 ''' 
    76 def login(request):        
    77     context = {} 
    78     if request.method == 'POST': # If the form has been submitted... 
    79         form = CEDAAuthenticationForm(request.POST) # A form bound to the POST data 
    80         context['form'] = form 
    81         if form.is_valid(): # All validation rules pass 
    82             username = request.POST.get('username') 
    83             password = request.POST.get('password') 
    84             LOGGER.debug("logging: %s" % (username))             
    85             user = authenticate(username=username, password=password)              
    86             if user is not None: 
    87                 LOGGER.debug("authenticated: %s" % (username)) 
    88                 if user.is_active: 
    89                     LOGGER.debug("user %s is Active" % (username))            
    90                     context['user'] = SecurityEncoder().encode(user) 
    91                     request.auth_user = context.get('user') 
    92                     return _encode_authenticated_response(request, context)          
    93                 else: 
    94                     # Return a 'disabled account' error message 
    95                     context['error'] = u'account disabled' 
    96                     LOGGER.debug("Account Disabled for user %s" % (username))                     
    97                     return error_handle(request, context) 
    98             else: 
    99                 # Return an 'invalid login' error message. 
    100                 context['error'] = u'invalid login'                 
    101                 LOGGER.debug("Invalid login for user %s" % (username))                 
    102                 return error_handle(request, context) 
    103         else: 
    104             context['error'] = u'form is invalid' 
    105             return error_handle(request, context) 
    106     else: 
    107         if getattr(settings, "FAKE_AUTHENTICATION", False): 
    108             context['user'] = '{"username": "mnagni", ' 
    109             '"first_name": "Maurizio", "last_name": "Nagni", ' 
    110             '"is_active": true, "email": "maurizio.nagni@stfc.ac.uk", ' 
    111             '"is_superuser": false, "is_staff": true, ' 
    112             '"last_login": "2012-10-18 11:05:28.700139+00:00", ' 
    113             '"date_joined": "2012-03-22 14:20:56+00:00", "id": 29, ' 
    114             '"permissions": "auth.add_user,proginfo.add_dataproduct, ' 
    115             'proginfo.change_programme, cedainfoapp.delete_vmrequest"}' 
    116             return _encode_authenticated_response(request, context)   
    117          
    118         # An unbound form 
    119         form = CEDAAuthenticationForm()  
    120         context['form'] = form 
    121         context.update(csrf(request)) 
    122         return render_to_response('login.html', context) 
    123 ''' 
    12474    
    12575def get_user_byopenid(user_id): 
     
    150100        base64.b64decode(request.session.get(redirect_parameter, '')) 
    151101    LOGGER.debug("Redirecting to %s" % (context['redirect_url']))    
    152     return render_to_response('logged_in.html', context) 
     102    return render('logged_in.html', context) 
    153103 
    154104class CEDAAuthenticationForm(AuthenticationForm): 
     
    176126def signin(request, template_name='authopenid/signin.html',  
    177127        redirect_field_name=REDIRECT_FIELD_NAME, openid_form=OpenidSigninForm, 
    178         auth_form=CedaUserAuthenticationBackend, on_failure=None, extra_context=None): 
     128        auth_form=CedaUserAuthenticationBackend,  
     129        on_failure=None, extra_context=None): 
    179130    """Signin page. It manage the legacy authentification (user/password)   
    180131    and authentification with openid. 
     
    222173                redirect_to = base64.b64decode(redirect_to)                                    
    223174                response = HttpResponseRedirect(redirect_to) 
    224                 _encode_authenticated_response(request, response, redirect_to, form2.get_user()) 
     175                _encode_authenticated_response(request,  
     176                                               response,  
     177                                               redirect_to,  
     178                                               form2.get_user()) 
    225179                return response 
    226180    return render(template_name, { 
     
    242196    response = HttpResponseRedirect(redirect_url) 
    243197     
    244     #if SESSION_KEY in request.session: 
    245     if OPENID in request.session: 
    246         return _encode_authenticated_response(request, response, redirect_url, get_user_byopenid(request.session[OPENID]))             
     198    if OPENID in request.REQUEST: 
     199        return _encode_authenticated_response(request,  
     200                                response,  
     201                                redirect_url,  
     202                                get_user_byopenid(request.REQUEST[OPENID]))             
    247203    elif SESSION_KEY in request.session: 
    248204        response = HttpResponseRedirect(redirect_url) 
Note: See TracChangeset for help on using the changeset viewer.