Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / api / user.py @ 6609821f

History | View | Annotate | Download (4.3 kB)

1 bd40abfa Christos Stavrakakis
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 7a0c3713 Kostas Papadimitriou
#
3 64cd4730 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 64cd4730 Antony Chazapis
# without modification, are permitted provided that the following
5 64cd4730 Antony Chazapis
# conditions are met:
6 7a0c3713 Kostas Papadimitriou
#
7 64cd4730 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 64cd4730 Antony Chazapis
#      disclaimer.
10 7a0c3713 Kostas Papadimitriou
#
11 64cd4730 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 64cd4730 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 64cd4730 Antony Chazapis
#      provided with the distribution.
15 7a0c3713 Kostas Papadimitriou
#
16 64cd4730 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 64cd4730 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 64cd4730 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 64cd4730 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 64cd4730 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 64cd4730 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 64cd4730 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 64cd4730 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 64cd4730 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 64cd4730 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 64cd4730 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 64cd4730 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 7a0c3713 Kostas Papadimitriou
#
29 64cd4730 Antony Chazapis
# The views and conclusions contained in the software and
30 64cd4730 Antony Chazapis
# documentation are those of the authors and should not be
31 64cd4730 Antony Chazapis
# interpreted as representing official policies, either expressed
32 64cd4730 Antony Chazapis
# or implied, of GRNET S.A.
33 64cd4730 Antony Chazapis
34 64cd4730 Antony Chazapis
from time import time, mktime
35 1ae97c83 Sofia Papagiannaki
36 64cd4730 Antony Chazapis
from django.http import HttpResponse
37 64cd4730 Antony Chazapis
from django.utils import simplejson as json
38 890c2065 Sofia Papagiannaki
from django.views.decorators.csrf import csrf_exempt
39 64cd4730 Antony Chazapis
40 ee7a2b87 Christos Stavrakakis
from snf_django.lib import api
41 bd40abfa Christos Stavrakakis
from snf_django.lib.api import faults
42 890c2065 Sofia Papagiannaki
43 09e7393c Sofia Papagiannaki
from astakos.im.util import epoch
44 b095201e Sofia Papagiannaki
from astakos.im.quotas import get_user_quotas
45 79b5d61b Sofia Papagiannaki
46 7f313da1 Sofia Papagiannaki
from .util import (
47 7f313da1 Sofia Papagiannaki
    get_uuid_displayname_catalogs as get_uuid_displayname_catalogs_util,
48 7f313da1 Sofia Papagiannaki
    send_feedback as send_feedback_util,
49 7f313da1 Sofia Papagiannaki
    user_from_token)
50 79b5d61b Sofia Papagiannaki
51 ee7a2b87 Christos Stavrakakis
import logging
52 3a9f4931 Sofia Papagiannaki
logger = logging.getLogger(__name__)
53 ee7a2b87 Christos Stavrakakis
54 ee7a2b87 Christos Stavrakakis
55 ee7a2b87 Christos Stavrakakis
@api.api_method(http_method="GET", token_required=True, user_required=False,
56 79b5d61b Sofia Papagiannaki
                logger=logger)
57 ee7a2b87 Christos Stavrakakis
@user_from_token  # Authenticate user!!
58 78c3a39c Sofia Papagiannaki
def authenticate(request):
59 53f980c2 Sofia Papagiannaki
    # Normal Response Codes: 200
60 18ffbee1 Sofia Papagiannaki
    # Error Response Codes: internalServerError (500)
61 18ffbee1 Sofia Papagiannaki
    #                       badRequest (400)
62 18ffbee1 Sofia Papagiannaki
    #                       unauthorised (401)
63 78c3a39c Sofia Papagiannaki
    user = request.user
64 18ffbee1 Sofia Papagiannaki
    if not user:
65 bd40abfa Christos Stavrakakis
        raise faults.BadRequest('No user')
66 d492d8ae Kostas Papadimitriou
67 18ffbee1 Sofia Papagiannaki
    # Check if the is active.
68 18ffbee1 Sofia Papagiannaki
    if not user.is_active:
69 bd40abfa Christos Stavrakakis
        raise faults.Unauthorized('User inactive')
70 18ffbee1 Sofia Papagiannaki
71 18ffbee1 Sofia Papagiannaki
    # Check if the token has expired.
72 18ffbee1 Sofia Papagiannaki
    if (time() - mktime(user.auth_token_expires.timetuple())) > 0:
73 bd40abfa Christos Stavrakakis
        raise faults.Unauthorized('Authentication expired')
74 d492d8ae Kostas Papadimitriou
75 fcf90160 Sofia Papagiannaki
    if not user.signed_terms:
76 bd40abfa Christos Stavrakakis
        raise faults.Unauthorized('Pending approval terms')
77 d492d8ae Kostas Papadimitriou
78 18ffbee1 Sofia Papagiannaki
    response = HttpResponse()
79 5a8eb6e7 Sofia Papagiannaki
    user_info = {
80 5a8eb6e7 Sofia Papagiannaki
        'id': user.id,
81 c700f742 Sofia Papagiannaki
        'username': user.username,
82 e0fa2a89 Kostas Papadimitriou
        'uuid': user.uuid,
83 5a8eb6e7 Sofia Papagiannaki
        'email': [user.email],
84 5a8eb6e7 Sofia Papagiannaki
        'name': user.realname,
85 210f4d0e Kostas Papadimitriou
        'groups': list(user.groups.all().values_list('name', flat=True)),
86 210f4d0e Kostas Papadimitriou
        'auth_token': request.META.get('HTTP_X_AUTH_TOKEN'),
87 5a8eb6e7 Sofia Papagiannaki
        'auth_token_created': epoch(user.auth_token_created),
88 890c2065 Sofia Papagiannaki
        'auth_token_expires': epoch(user.auth_token_expires)}
89 7bffb0bd Kostas Papadimitriou
90 18ffbee1 Sofia Papagiannaki
    response.content = json.dumps(user_info)
91 18ffbee1 Sofia Papagiannaki
    response['Content-Type'] = 'application/json; charset=UTF-8'
92 18ffbee1 Sofia Papagiannaki
    response['Content-Length'] = len(response.content)
93 18ffbee1 Sofia Papagiannaki
    return response
94 890c2065 Sofia Papagiannaki
95 bd40abfa Christos Stavrakakis
96 890c2065 Sofia Papagiannaki
@csrf_exempt
97 ee7a2b87 Christos Stavrakakis
@api.api_method(http_method="POST", token_required=True, user_required=False,
98 79b5d61b Sofia Papagiannaki
                logger=logger)
99 ee7a2b87 Christos Stavrakakis
@user_from_token  # Authenticate user!!
100 78c3a39c Sofia Papagiannaki
def get_uuid_displayname_catalogs(request):
101 890c2065 Sofia Papagiannaki
    # Normal Response Codes: 200
102 890c2065 Sofia Papagiannaki
    # Error Response Codes: internalServerError (500)
103 890c2065 Sofia Papagiannaki
    #                       badRequest (400)
104 890c2065 Sofia Papagiannaki
    #                       unauthorised (401)
105 890c2065 Sofia Papagiannaki
106 6f1d4d03 Sofia Papagiannaki
    return get_uuid_displayname_catalogs_util(request)
107 890c2065 Sofia Papagiannaki
108 bd40abfa Christos Stavrakakis
109 890c2065 Sofia Papagiannaki
@csrf_exempt
110 ee7a2b87 Christos Stavrakakis
@api.api_method(http_method="POST", token_required=True, user_required=False,
111 79b5d61b Sofia Papagiannaki
                logger=logger)
112 ee7a2b87 Christos Stavrakakis
@user_from_token  # Authenticate user!!
113 78c3a39c Sofia Papagiannaki
def send_feedback(request, email_template_name='im/feedback_mail.txt'):
114 890c2065 Sofia Papagiannaki
    # Normal Response Codes: 200
115 890c2065 Sofia Papagiannaki
    # Error Response Codes: internalServerError (500)
116 890c2065 Sofia Papagiannaki
    #                       badRequest (400)
117 890c2065 Sofia Papagiannaki
    #                       unauthorised (401)
118 890c2065 Sofia Papagiannaki
119 78c3a39c Sofia Papagiannaki
    return send_feedback_util(request, email_template_name)