Exemplo n.º 1
0
        def ug_wrapper(request, g_id):
            
            if not request.POST:
                return redirect('django_ug.views.viewgateway', g_id=g_id)

            session = request.session
            username = session['login_email']

            try:
                g = db.read_user_gateway(g_id)
                if not g:
                    raise Exception("No gateway exists.")
            except Exception as e:
                logging.error("Error reading gateway %s : Exception: %s" % (g_id, e))
                message = "No user gateway by the name of %s exists." % g_id
                t = loader.get_template("gateway_templates/viewgateway_failure.html")
                c = Context({'message':message, 'username':username})
                return HttpResponse(t.render(c))

            form = libforms.Password(request.POST)
            if not form.is_valid():
                session['message'] = "Password required."
                return redirect(redirect_view, g_id)
            # Check password hash
            if not UG.authenticate(g, form.cleaned_data['password']):
                session['message'] = "Incorrect password."
                return redirect(redirect_view, g_id)

            return f(request, g_id)
Exemplo n.º 2
0
def changewrite(request, g_id):
    '''
    View to change write capabilities of UG
    '''

    session = request.session
    g_id = int(g_id)

    g = db.read_user_gateway(g_id)
    if not g:
        return redirect('django_ug.views.viewgateway', g_id)
    if g.read_write:
        attrs = {'read_write': False}
    else:
        attrs = {'read_write': True}
    try:
        db.update_user_gateway(g_id, **attrs)
    except Exception as e:
        logging.error("Unable to update UG with ID %s. Error was %s." %
                      (g_id, e))
        session['message'] = "Error. Unable to change user gateway."
        return redirect('django_ug.views.viewgateway', g_id)
    session['new_change'] = "We've updated your UG."
    session['next_url'] = '/syn/UG/viewgateway/' + str(g_id)
    session['next_message'] = "Click here to go back to your gateway."
    return HttpResponseRedirect('/syn/thanks')
Exemplo n.º 3
0
        def ug_wrapper(request, g_id):

            if not request.POST:
                return redirect('django_ug.views.viewgateway', g_id=g_id)

            session = request.session
            username = session['login_email']

            try:
                g = db.read_user_gateway(g_id)
                if not g:
                    raise Exception("No gateway exists.")
            except Exception as e:
                logging.error("Error reading gateway %s : Exception: %s" %
                              (g_id, e))
                message = "No user gateway by the name of %s exists." % g_id
                t = loader.get_template(
                    "gateway_templates/viewgateway_failure.html")
                c = Context({'message': message, 'username': username})
                return HttpResponse(t.render(c))

            form = libforms.Password(request.POST)
            if not form.is_valid():
                session['message'] = "Password required."
                return redirect(redirect_view, g_id)
            # Check password hash
            if not UG.authenticate(g, form.cleaned_data['password']):
                session['message'] = "Incorrect password."
                return redirect(redirect_view, g_id)

            return f(request, g_id)
Exemplo n.º 4
0
def viewgateway(request, g_id=0):
    '''
    The view for viewing and changing any of the main settings on any UG. Passes
    forms for changing different settings, and the volumes attached to the gateway.
    '''
    session = request.session
    username = session['login_email']
    message = session.pop('message', "")
    g_id = int(g_id)

    g = db.read_user_gateway(g_id)
    if not g:
        logging.error("Error reading gateway %d : Does note exist" % g_id)
        message = "No user gateway with the ID %d exists." % g_id
        t = loader.get_template("gateway_templates/viewgateway_failure.html")
        c = Context({'message': message, 'username': username})
        return HttpResponse(t.render(c))

    # Create necessary forms
    location_form = gatewayforms.ModifyGatewayLocation(initial={
        'host': g.host,
        'port': g.port
    })
    change_form = gatewayforms.ChangeVolume()
    password_form = libforms.Password()
    change_password_form = libforms.ChangePassword()

    vol = db.read_volume(g.volume_id)
    if not vol:
        vol = None
        owner = None
        """
        if g.volume_id != 0:
            logging.error("Volume ID %s in gateways volume_ids does not map to volume. Gateway: %s" % (g.volume_id, g_id))
            return redirect('django_ug.views.allgateways')
        else:
            vol = None
            owner = None
        """
    else:
        attrs = {"SyndicateUser.owner_id ==": vol.owner_id}
        owner = db.get_user(attrs)
        logging.info(owner)
        logging.info(vol.owner_id)

    t = loader.get_template("gateway_templates/viewusergateway.html")
    c = RequestContext(
        request, {
            'username': username,
            'gateway': g,
            'message': message,
            'vol': vol,
            'owner': owner,
            'location_form': location_form,
            'change_form': change_form,
            'password_form': password_form,
            'change_password_form': change_password_form
        })
    return HttpResponse(t.render(c))
Exemplo n.º 5
0
def urldelete(request, g_id, g_password):
    session = request.session
    username = session['login_email']
    user = db.read_user(username)

    ug = db.read_user_gateway(g_id)
    if not ug:
        return HttpResponse("UG %d does not exist." % g_id)
    if ug.owner_id != user.owner_id:
        return HttpResponse("You must own this UG to delete it.")
    if not UG.authenticate(ug, g_password):
        return HttpResponse("Incorrect UG password.")
    db.delete_user_gateway(g_id)
    return HttpResponse("Gateway succesfully deleted.")
Exemplo n.º 6
0
def changepassword(request, g_id):
    session = request.session
    username = session['login_email']
    user = db.read_user(username)
    g_id = int(g_id)

    if request.method != "POST":
        return redirect('django_ug.views.viewgateway', g_id)

    g = db.read_user_gateway(g_id)
    if not g:
        logging.error("Error reading gateway %d : Gateway does not exist." %
                      g_id)
        message = "No user gateway by the name of %d exists." % g_id
        t = loader.get_template("gateway_templates/viewgateway_failure.html")
        c = Context({'message': message, 'username': username})
        return HttpResponse(t.render(c))

    form = libforms.ChangePassword(request.POST)
    if not form.is_valid():
        session['message'] = "You must fill out all password fields."
        return redirect('django_ug.views.viewgateway', g_id)
    else:
        # Check password hash
        if not UG.authenticate(g, form.cleaned_data['oldpassword']):
            session['message'] = "Incorrect password."
            return redirect('django_ug.views.viewgateway', g_id)
        elif form.cleaned_data['newpassword_1'] != form.cleaned_data[
                'newpassword_2']:
            session['message'] = "Your new passwords did not match each other."
            return redirect('django_ug.views.viewgateway', g_id)
        # Ok to change password
        else:
            new_hash = UG.generate_password_hash(
                form.cleaned_data['newpassword_1'])
            fields = {'ms_password_hash': new_hash}
            try:
                db.update_user_gateway(g_id, **fields)
            except Exception as e:
                logging.error(
                    "Unable to update user gateway %s. Exception %s" %
                    (g_name, e))
                session['message'] = "Unable to update gateway."
                return redirect('django_ug.views.viewgateway', g_id)

            session['new_change'] = "We've changed your gateways's password."
            session['next_url'] = '/syn/UG/viewgateway/' + str(g_id)
            session['next_message'] = "Click here to go back to your volume."
            return HttpResponseRedirect('/syn/thanks')
Exemplo n.º 7
0
def delete(request, g_id):
    '''
    View for deleting UGs
    '''

    # Helper method that simplifies returning forms after user error.
    def give_delete_form(username, g_name, session):
        message = session.pop('message' "")
        form = gatewayforms.DeleteGateway()
        t = loader.get_template('gateway_templates/delete_user_gateway.html')
        c = RequestContext(
            request, {
                'username': username,
                'g_name': g_name,
                'form': form,
                'message': message
            })
        return HttpResponse(t.render(c))

    session = request.session
    username = session['login_email']
    g_id = int(g_id)

    ug = db.read_user_gateway(g_id)
    if not ug:
        t = loader.get_template(
            'gateway_templates/delete_user_gateway_failure.html')
        c = RequestContext(request, {'username': username})
        return HttpResponse(t.render(c))

    g_name = ug.ms_username
    if ug.owner_id != user.owner_id:
        t = loader.get_template(
            'gateway_templates/delete_user_gateway_failure.html')
        c = RequestContext(request, {'username': username})
        return HttpResponse(t.render(c))

    if request.POST:
        # Validate input forms
        form = gatewayforms.DeleteGateway(request.POST)
        if form.is_valid():
            if not UG.authenticate(ug, form.cleaned_data['g_password']):
                session['message'] = "Incorrect User Gateway password"
                return give_delete_form(username, g_name, session)
            if not form.cleaned_data['confirm_delete']:
                session[
                    'message'] = "You must tick the delete confirmation box."
                return give_delete_form(username, g_name, session)

            db.delete_user_gateway(g_id)
            session['new_change'] = "Your gateway has been deleted."
            session['next_url'] = '/syn/UG/mygateways'
            session['next_message'] = "Click here to see your gateways."
            return HttpResponseRedirect('/syn/thanks/')

        # invalid forms
        else:
            # Prep error message
            session['message'] = "Invalid form entry: "

            for k, v in form.errors.items():
                session[
                    'message'] = session['message'] + "\"" + k + "\"" + " -> "
                for m in v:
                    session['message'] = session['message'] + m + " "

            return give_delete_form(username, g_name, session)
    else:
        # Not a POST, give them blank form
        return give_delete_form(username, g_name, session)
Exemplo n.º 8
0
def changevolume(request, g_id):
    '''
    View to enable changing volume attached to UG
    '''
    session = request.session
    username = session['login_email']
    user = db.read_user(username)
    g_id = int(g_id)
    g = db.read_user_gateway(g_id)

    # Isolate DB calls to allow roll-backs via transactions
    @transactional(xg=True)
    def update_ug_and_vol(g_id, gfields, vol1_id, vol2_id):
        db.update_user_gateway(g_id, **gfields)
        attrs = {"UG_version": 1}

        # Force update of UG version
        db.update_volume(vol1_id, **attrs)
        db.update_volume(vol2_id, **attrs)

    form = gatewayforms.ChangeVolume(request.POST)
    if form.is_valid():
        attrs = {
            "Volume.name ==":
            form.cleaned_data['volume_name'].strip().replace(" ", "_")
        }
        vols = db.list_volumes(attrs, limit=1)
        if vols:
            new_vol = vols[0]
        else:
            session['message'] = "No volume %s exists." % form.cleaned_data[
                'volume_name']
            return redirect('django_ug.views.viewgateway', g_id)
        if (new_vol.volume_id
                not in user.volumes_r) and (new_vol.volume_id
                                            not in user.volumes_rw):
            session[
                'message'] = "Must have read rights to volume %s to assign UG to it." % form.cleaned_data[
                    'volume_name']
            return redirect('django_ug.views.viewgateway', g_id)

        old_vol = g.volume_id
        #fields = {"volume_id":new_vol.volume_id, "cert_version": True}
        try:

            db.update_user_gateway(g_id,
                                   volume_id=new_vol.volume_id,
                                   cert_version=True)

            db.update_volume(new_vol.volume_id,
                             version=True,
                             cert_version=True)

            if g.is_bound_to_volume():
                # update the old Volume
                db.update_volume(old_vol, version=True, cert_version=True)

            #update_ug_and_vol(g_id, fields, old_vol, new_vol.volume_id)
        except Exception, e:
            logging.error("Unable to update UG with ID %s. Error was %s." %
                          (g_id, e))
            session['message'] = "Error. Unable to change user gateway."
            return redirect('django_ug.views.viewgateway', g_id)
        session['new_change'] = "We've updated your UG."
        session['next_url'] = '/syn/UG/viewgateway/' + str(g_id)
        session['next_message'] = "Click here to go back to your gateway."
        return HttpResponseRedirect('/syn/thanks')