Create OC_JSON class, for single point of creating json responses. No real logic change, this just cleans up the code a bit.
Also use OC_User::isLoggedIn to check if new authentication is needed for grouplist.php and userlist.php For validateuser.php, credentials are always needed.