def changepassword(request, g_id): ''' Handler for changing gateway password. Since it can't use precheck because of password reasons, must verify POST-ness itself. ''' session = request.session username = session['login_email'] g_id = int(g_id) # Precheck if request.method != "POST": return redirect('/syn/RG/viewgateway/' + str(g_id)) try: g = db.read_replica_gateway(g_id) if not g: raise Exception("No gateway exists.") except Exception as e: logging.error("Error reading gateway %d : Exception: %s" % (g_id, e)) message = "No replica 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)) form = libforms.ChangePassword(request.POST) if not form.is_valid(): session['message'] = "You must fill out all password fields." return redirect('django_rg.views.viewgateway', g_id) else: # Check password hash if not RG.authenticate(g, form.cleaned_data['oldpassword']): session['message'] = "Incorrect password." return redirect('django_rg.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_rg.views.viewgateway', g_id) # Ok to change password else: new_hash = RG.generate_password_hash( form.cleaned_data['newpassword_1']) fields = {'ms_password_hash': new_hash} try: db.update_replica_gateway(g_id, **fields) except Exception as e: logging.error( "Unable to update replica gateway %d. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've changed your gateways's password." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your volume." return redirect('/syn/thanks')
def changepassword(request, g_id): ''' Handler for changing gateway password. Since it can't use precheck because of password reasons, must verify POST-ness itself. ''' session = request.session username = session['login_email'] g_id = int(g_id) # Precheck if request.method != "POST": return redirect('/syn/RG/viewgateway/' + str(g_id)) try: g = db.read_replica_gateway(g_id) if not g: raise Exception("No gateway exists.") except Exception as e: logging.error("Error reading gateway %d : Exception: %s" % (g_id, e)) message = "No replica 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)) form = libforms.ChangePassword(request.POST) if not form.is_valid(): session['message'] = "You must fill out all password fields." return redirect('django_rg.views.viewgateway', g_id) else: # Check password hash if not RG.authenticate(g, form.cleaned_data['oldpassword']): session['message'] = "Incorrect password." return redirect('django_rg.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_rg.views.viewgateway', g_id) # Ok to change password else: new_hash = RG.generate_password_hash(form.cleaned_data['newpassword_1']) fields = {'ms_password_hash':new_hash} try: db.update_replica_gateway(g_id, **fields) except Exception as e: logging.error("Unable to update replica gateway %d. Exception %s" % (g_id, e)) session['message'] = "Unable to update gateway." return redirect('django_rg.views.viewgateway', g_id) session['new_change'] = "We've changed your gateways's password." session['next_url'] = '/syn/RG/viewgateway/' + str(g_id) session['next_message'] = "Click here to go back to your volume." return redirect('/syn/thanks')
def rg_wrapper(request, g_id): if not request.POST: return redirect('django_rg.views.viewgateway', g_id=g_id) session = request.session username = session['login_email'] try: g = db.read_replica_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 replica 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 RG.authenticate(g, form.cleaned_data['password']): session['message'] = "Incorrect password." return redirect(redirect_view, g_id) return f(request, g_id)
def rg_wrapper(request, g_id): if not request.POST: return redirect('django_rg.views.viewgateway', g_id=g_id) session = request.session username = session['login_email'] try: g = db.read_replica_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 replica 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 RG.authenticate(g, form.cleaned_data['password']): session['message'] = "Incorrect password." return redirect(redirect_view, g_id) return f(request, g_id)
def delete(request, g_id): ''' View for deleting AG. ''' # Helper method used to simplify error-handling. When fields are entered incorrectly, # a session message is set and this method is called. def give_delete_form(username, g, session): form = gatewayforms.DeleteGateway() t = loader.get_template( 'gateway_templates/delete_replica_gateway.html') c = RequestContext( request, { 'username': username, 'g': g, 'form': form, 'message': session.pop('message', "") }) return HttpResponse(t.render(c)) # Once again isolating transactional for views that update multiple entities @transactional(xg=True) def delete_and_update(rg_id, attached_volume_ids): db.delete_replica_gateway(rg_id) for v in attached_volume_ids: vol = db.read_volume(v) if not vol: continue new_rg_ids = vol.rg_ids new_rg_ids.remove(rg_id) attrs = {"rg_ids": new_rg_ids} db.update_volume(v, **attrs) session.pop("rg_initial_data" + str(g_id), None) session = request.session username = session['login_email'] g_id = int(g_id) rg = db.read_replica_gateway(g_id) if not rg: t = loader.get_template( 'gateway_templates/delete_replica_gateway_failure.html') c = RequestContext(request, {'username': username, 'g_name': g_name}) return HttpResponse(t.render(c)) if request.POST: # Validate input forms form = gatewayforms.DeleteGateway(request.POST) if form.is_valid(): if not RG.authenticate(rg, form.cleaned_data['g_password']): session['message'] = "Incorrect Replica Gateway password" return give_delete_form(username, rg, session) if not form.cleaned_data['confirm_delete']: session[ 'message'] = "You must tick the delete confirmation box." return give_delete_form(username, rg, session) delete_and_update(g_id, rg.volume_ids) session['new_change'] = "Your gateway has been deleted." session['next_url'] = '/syn/RG/allgateways' session['next_message'] = "Click here to see all replica gateways." return redirect('/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, rg, session) else: # Not a POST, give them blank form return give_delete_form(username, rg, session)
def delete(request, g_id): ''' View for deleting AG. ''' # Helper method used to simplify error-handling. When fields are entered incorrectly, # a session message is set and this method is called. def give_delete_form(username, g, session): form = gatewayforms.DeleteGateway() t = loader.get_template('gateway_templates/delete_replica_gateway.html') c = RequestContext(request, {'username':username, 'g':g, 'form':form, 'message':session.pop('message', "")}) return HttpResponse(t.render(c)) # Once again isolating transactional for views that update multiple entities @transactional(xg=True) def delete_and_update(rg_id, attached_volume_ids): db.delete_replica_gateway(rg_id) for v in attached_volume_ids: vol = db.read_volume(v) if not vol: continue new_rg_ids = vol.rg_ids new_rg_ids.remove(rg_id) attrs = {"rg_ids":new_rg_ids} db.update_volume(v, **attrs) session.pop("rg_initial_data" + str(g_id), None) session = request.session username = session['login_email'] g_id = int(g_id) rg = db.read_replica_gateway(g_id) if not rg: t = loader.get_template('gateway_templates/delete_replica_gateway_failure.html') c = RequestContext(request, {'username':username, 'g_name':g_name}) return HttpResponse(t.render(c)) if request.POST: # Validate input forms form = gatewayforms.DeleteGateway(request.POST) if form.is_valid(): if not RG.authenticate(rg, form.cleaned_data['g_password']): session['message'] = "Incorrect Replica Gateway password" return give_delete_form(username, rg, session) if not form.cleaned_data['confirm_delete']: session['message'] = "You must tick the delete confirmation box." return give_delete_form(username, rg, session) delete_and_update(g_id, rg.volume_ids) session['new_change'] = "Your gateway has been deleted." session['next_url'] = '/syn/RG/allgateways' session['next_message'] = "Click here to see all replica gateways." return redirect('/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, rg, session) else: # Not a POST, give them blank form return give_delete_form(username, rg, session)