Statistics
| Branch: | Tag: | Revision:

root / djangobackends / shibauthBackend.py @ d60db93b

History | View | Annotate | Download (1.4 kB)

1
# -*- coding: utf-8 -*- vim:encoding=utf-8:
2
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3

    
4
from django.contrib.auth.models import User, UserManager, Permission, Group
5
from django.conf import settings
6

    
7
class shibauthBackend:
8
    def authenticate(self, **kwargs):
9
        username = kwargs.get('username')
10
        firstname = kwargs.get('firstname')
11
        lastname = kwargs.get('lastname')
12
        mail = kwargs.get('mail')
13
        authsource = kwargs.get('authsource')
14
        if authsource != 'shibboleth':
15
            return None
16
        try:
17
            user = self._auth_user(username, firstname, lastname, mail)
18
        except:
19
            return None
20
        if not user:
21
            return None
22
        return user
23

    
24
    def _auth_user(self, username, firstname, lastname, mail):
25

    
26
        try:
27
            user = User.objects.get(username__exact=username)
28
        # The user did not exist. Create one with no privileges
29
        except: 
30
            user = User.objects.create_user(username, mail, None)
31
            user.first_name = firstname
32
            user.last_name = lastname
33
            user.is_staff = False
34
            user.is_superuser = False
35
            user.is_active = False
36
            user.save()
37

    
38
        return user
39

    
40
    def get_user(self, user_id):
41
        try:
42
            return User.objects.get(pk=user_id)
43
        except User.DoesNotExist:
44
            return None