示例#1
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')
示例#2
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)
示例#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)
示例#4
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.")
示例#5
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)