Statistics
| Branch: | Tag: | Revision:

root / snf-saas-app / synnefo / saas / views.py @ cfbbfe0f

History | View | Annotate | Download (4.3 kB)

1
# Copyright 2012 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
import re
35
import logging
36

    
37
from django.shortcuts import redirect, get_object_or_404, render_to_response
38
from django.template import Context, loader
39
from django.conf import settings
40
from django.views.generic.simple import direct_to_template
41
from django.core.exceptions import PermissionDenied
42
from django.http import Http404, HttpResponse, HttpResponseRedirect
43
from django.core.urlresolvers import reverse
44

    
45
from synnefo.lib.astakos import get_user
46
from synnefo.saas.forms import VMSettings
47

    
48
from itertools import chain
49

    
50
from urllib import unquote
51

    
52

    
53
def get_token_from_cookie(request, cookiename):
54
    """
55
    Extract token from the cookie name provided. Cookie should be in the same
56
    form as astakos service sets its cookie contents::
57

58
        <user_uniq>|<user_token>
59
    """
60
    try:
61
        cookie_content = unquote(request.COOKIES.get(cookiename, None))
62
        return cookie_content.split("|")[1]
63
    except AttributeError:
64
        pass
65

    
66
    return None
67

    
68

    
69
def token_check(func):
70
    """
71
    Mimic csrf security check using user auth token.
72
    """
73
    def wrapper(request, *args, **kwargs):
74
        if not hasattr(request, 'user'):
75
            raise PermissionDenied
76

    
77
        token = request.POST.get('token', None)
78
        if token and token != request.user.get('auth_token', None):
79
            return func(request, *args, **kwargs)
80

    
81
        raise PermissionDenied
82

    
83
    return wrapper
84

    
85
def user_required(func):
86
    """
87
    Django view wrapper that identifies user from token
88
    """
89

    
90
    def wrapper(request, *args, **kwargs):
91

    
92
        token = get_token_from_cookie(request, AUTH_COOKIE_NAME)
93
        get_user(request, settings.ASTAKOS_URL, fallback_token=token)
94

    
95
        if hasattr(request, 'user') and request.user:
96
            has_perm = True
97

    
98
        if not has_perm:
99
            raise PermissionDenied
100

    
101
        logging.debug("User %s accessed helpdesk view" % (request.user_uniq))
102
        return func(request, *args, **kwargs)
103

    
104
    return wrapper
105

    
106

    
107
def index(request):
108

    
109
    return direct_to_template(request, "saas/index.html")
110

    
111
def select_software(request):
112
    if request.method ==  'POST':
113
        software = request.POST.getlist('software')
114

    
115
        return HttpResponseRedirect("/saas/settings/")
116

    
117
def vm_settings(request):
118
    if request.method == 'POST':
119
        form = VMSettings(request.POST)
120
        if form.is_valid():
121
            return HttpResponseRedirect("/saas/status/")
122
    
123
        else:
124
            return render_to_response('saas/settings.html', {'form': form})
125
            
126
    else:
127
        form = VMSettings()
128
        return render_to_response('saas/settings.html', {'form': form})
129

    
130
def vm_status(request):
131
    status = True
132
    template = loader.get_template('saas/status.html')
133
    context = Context({'status': status})
134

    
135
    return HttpResponse(template.render(context))
136