Check study access rights and study exists

 Fix #445
I think so. This isn't actually my code and just got rearranged when I added access checks.