summaryrefslogtreecommitdiff
path: root/views.py
blob: 43860d733dbf587775944b1b478b3cf51b914166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from django.contrib.auth.decorators import login_required
from apps.changepw.models import ChangePasswordForm, ChangeOtherForm
from django.shortcuts import render_to_response
from django.template import RequestContext
# import your_pw_change_module

def _change_password(request, user, new_password):
    '''
    Use this to call your change password function.
    '''
    # ret = your_pw_change_module.change_password(user, new_password)
    return 0

def _reset_password(request, user, new_password):
    '''
    Use this to call your reset password function.
    '''
    # ret = your_pw_change_module.reset_password(user, new_password)
    return 0
    
def _change_other(request):
    '''
    Use this to call your change function.
    '''
    # user = request.user
    # ssh_key = request.POST.getattr('ssh_key')
    # ret = your_ldap_module.change_public_ssh_key(user, ssh_key)
    return 0

def _get_username(request):
    '''
    Returns the actual username from the Shibboleth uid.
    request.user.username == username@domain.com
    '''
    return request.user.username.split('@')[0]

def _generate_password(n):
    '''
    Returns a psudo random string of lenght n.
    http://code.activestate.com/recipes/576722-pseudo-random-string/
    '''
    import os, math
    from base64 import b64encode
    return b64encode(os.urandom(int(math.ceil(0.75*n))),'-_')[:n]

@login_required(login_url='/sso/accounts/login/')
def index(request):
    '''
    Greets the user and presents the choices available.
    '''
    try:
        full_name = '%s %s' % (request.user.firstname, request.user.lastname)
    except AttributeError:
        full_name = _get_username(request)
    username = _get_username(request)
    return render_to_response('changepw/index.html',
                            {'full_name': full_name, 'username': username},
                            context_instance=RequestContext(request))

@login_required(login_url='/sso/accounts/login/')
def change_password(request):
    '''
    If the user is authenticated and the form is valid the password
    changing script will be run with the username and new password.
    The function that changes the password has to be provided as func.
    '''
    username = _get_username(request)
    if request.method == 'POST':
        form = ChangePasswordForm(request.POST)
        if form.is_valid():
            new_password = form.cleaned_data['new_password']
            return_value = _change_password(request, request.user, new_password)
            return render_to_response('changepw/change_password.html',
                            {'return_value': return_value},
                            context_instance=RequestContext(request))
    else:
        form = ChangePasswordForm()
    return render_to_response('changepw/change_password.html',
                            {'form': form, 'username': username},
                            context_instance=RequestContext(request))

@login_required(login_url='/sso/accounts/login/')
def reset_password(request):
    '''
    Resets password for the authenticated user to a random string.
    The function that actually sets the new password has to be provided as func.
    '''
    password_length = 8 # chars
    username = _get_username(request)
    if request.method == 'POST':
        new_password = _generate_password(password_length)
        return_value = _reset_password(request, request.user, new_password)
        return render_to_response('changepw/reset_password.html',
                        {'username': username, 'new_password': new_password,
                         'return_value': return_value},
                        context_instance=RequestContext(request))
    else:
        return render_to_response('changepw/reset_password.html',
                        {'username': username, 'return_value': None},
                        context_instance=RequestContext(request))

@login_required(login_url='/sso/accounts/login/')
def change_other(request):
    '''
    Just passes along the request so that something can be done for that user.
    '''
    username = _get_username(request)
    if request.method == 'POST':
        return_value = _change_other(request)
        return render_to_response('changepw/change_other.html',
                        {'username': username, 'return_value': return_value},
                        context_instance=RequestContext(request))    
    else:
        form = ChangeOtherForm()
        return render_to_response('changepw/change_other.html',
                        {'username': username, 'return_value': None,
                         'form': form},
                        context_instance=RequestContext(request))