Teachers
have access to a list of their Student
s classes. On this page (one page per Student
) the Teachers
can mark a checkbox as completed
if the Student completes that class. This is done with ajax. I'm passing the current Students
id
by making it a value on the checkbox. This is not good as a malicious Teacher
could inspect the page and change the Students
id
in the checkbox element (so they could be on Student
111's page and change the field in the checkbox to 112 and mark that other Student
s class as complete.
How can I prevent them from doing this? I know I can get current users data in POST or using a token, but the Student
whose checkbox is being changed isn't the current user, current user is a Teacher
and he is making changes to one of his Students
. The url is something like this: /website/teacher/student111/classes/
.
def post_form_api(request):
data = {}
if request.method == "POST":
class_id = request.POST.get("class_id")
student_id = request.POST.get("student_id")
student_class_data_entry = get_object_or_404(StudentClassData, student_id=student_id, class_id=class_id)
form = StudentClassDataForm(request.POST, instance=student_class_data_entry)
[.. other logic .]
if form.is_valid():
form.save()
data = {'result' : True}
if request.is_ajax():
return JsonResponse(data)
else:
return HttpResponseBadRequest()
(value
says which of the Students classes has been changed)
<form>
{% csrf_token %}
<td class='class_complete' student_id='{{ student.student.id }}' name='completed' value='{{ class.id }}'>{{ item.completed }}</td>
</form>
$(".class_complete").click(function(e){
var csrfToken = $("input[name='csrfmiddlewaretoken']");
class_id = $(this).attr('value');
student_id = $(this).attr('student_id');
var checked = $('#'+e.target.id).is(":checked");
$.ajax({ url: "/api/post_form/",
type: "POST",
dataType: "json",
data: {'completed':checked, 'student_id':student_id, 'class_id':class_id, 'csrfmiddlewaretoken':csrfToken.val()},
cache: false
}).done(function(data) {
if (data.result === true){
}
else {
}
});
});
Thank you.