Refactor check_user_permission function