A simpler solution.
The autopilot system having the ultimate control over the door lock - whenever pre-set deviation limits for the flight controls are exceeded, the door is unlocked. The door lock should also be designed as fail-safe - requires power to retain locked state.
These deviation limits should be set according to the flight plan/route (automated calculation of % deviation, or similar), and if they are to be over-ridden, require two valid pass-codes from the assigned crew.