refactor search permission code