Statistics
| Branch: | Tag: | Revision:

root / pithos / im / target / local.py @ bee5ffa6

History | View | Annotate | Download (3 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
35
from django.conf import settings
36

    
37
from pithos.im.target.util import prepare_response
38
from pithos.im.models import User
39

    
40
def login(request):
41
    username = '%s@local' % request.POST.get('username')
42
    password = request.POST.get('password')
43
    
44
    if not username:
45
        return HttpResponseBadRequest('No user')
46
    
47
    if not username:
48
        return HttpResponseBadRequest('No password')
49
    
50
    try:
51
        user = User.objects.get(uniq=username)
52
    except User.DoesNotExist:
53
        return HttpResponseBadRequest('No such user')
54
    
55
    if not password or user.password != password:
56
        return HttpResponseBadRequest('Wrong password')
57
    
58
    if user.state == 'UNVERIFIED':
59
        return HttpResponseBadRequest('Unverified account')
60
    
61
    next = request.POST.get('next')
62
    if not next:
63
        return HttpResponse('')
64
    if not request.user:
65
        return HttpResponseRedirect(next)
66
    
67
    return prepare_response(request.user, next)
68

    
69
def activate(request):
70
    token = request.GET.get('auth')
71
    url = request.GET.get('next')
72
    try:
73
        user = User.objects.get(auth_token=token)
74
    except User.DoesNotExist:
75
        return HttpResponseBadRequest('No such user')
76
    
77
    url = '%s?next=%sui' %(url, settings.BASE_URL)
78
    user.state = 'ACTIVE'
79
    user.renew_token()
80
    user.save()
81
    response = HttpResponse()
82
    response['Location'] = url
83
    response.status_code = 302
84
    return response