Merge "Factor out and speed up permission-checking code"