Repository: conda/conda

Issue:
conda env list behavior is inconsistent between Linux/macOS and Windows
## Current Behavior

On UNIX-family operating systems (Linux and macOS), `conda env list` generates a list of all environments belonging to the current user. This behavior changes if `conda` is executed with UID 0, in which case `conda` attempts to find all user home directories on the system and list all environments belonging to all users.

This is not consistent with the behavior on Windows. On Windows, `conda env list` always attempts to find all user home directories on the system, and displays all environments belonging to all users. This occurs whether or not the current user is an administrator.

This behavior is not documented in the Conda user guide, which just says that `conda env list` is used "to see a list of all your environments." https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#viewing-a-list-of-your-environments

### Steps to Reproduce

On macOS/Linux, this will list the current user's environments only:

```
conda env list
```

Running this command with `sudo` will list all environments on the system:

```
sudo conda env list
```

On Windows, running this command from either a standard prompt or from an administrative prompts with elevated permissions will list all environments on the system (as long as the executing user can read the environment configuration file for the user that actually owns the environment):

```
conda env list
```

## Expected Behavior

I would expect one of the two following behaviors:

1. `conda env list` only lists the current user's environment on any operating system.
2. `conda env list` only lists the current user's environment, unless it is executed as an administrator. This behavior should be consistent between Windows and macOS. 

The current behavior on Windows (all users) and macOS/Linux (admins) should only be executed for administrators, if at all. In particular, the current Windows code finds all sibling directories of the current user's home folder and tries to check each one of them for an environment configuration file. In environments where Windows home directories are redirected onto a common network share, this may result in `conda env list` firing off a large number of I/O operations as it tries to access all home directories on the network share.


Discussion:

Resolving patch:
diff --git a/conda/core/envs_manager.py b/conda/core/envs_manager.py
--- a/conda/core/envs_manager.py
+++ b/conda/core/envs_manager.py
@@ -11,6 +11,7 @@
 from .prefix_data import PrefixData
 from ..base.context import context
 from ..common.compat import ensure_text_type, on_win, open
+from ..common._os import is_admin
 from ..common.path import expand
 from ..gateways.disk.read import yield_lines
 from ..gateways.disk.test import is_conda_environment
@@ -63,23 +64,21 @@ def unregister_env(location):
 
 def list_all_known_prefixes():
     all_env_paths = set()
-    if on_win:
-        home_dir_dir = dirname(expand('~'))
-        for home_dir in listdir(home_dir_dir):
-            environments_txt_file = get_user_environments_txt_file(join(home_dir_dir, home_dir))
-            if isfile(environments_txt_file):
-                all_env_paths.update(_clean_environments_txt(environments_txt_file))
-    else:
-        from os import geteuid
-        from pwd import getpwall
-        if geteuid() == 0:
-            search_dirs = tuple(pwentry.pw_dir for pwentry in getpwall()) or (expand('~'),)
+    # If the user is an admin, load environments from all user home directories
+    if is_admin():
+        if on_win:
+            home_dir_dir = dirname(expand('~'))
+            search_dirs = tuple(join(home_dir_dir, d) for d in listdir(home_dir_dir))
         else:
-            search_dirs = (expand('~'),)
-        for home_dir in search_dirs:
-            environments_txt_file = get_user_environments_txt_file(home_dir)
-            if isfile(environments_txt_file):
-                all_env_paths.update(_clean_environments_txt(environments_txt_file))
+            from pwd import getpwall
+            search_dirs = tuple(pwentry.pw_dir for pwentry in getpwall()) or (expand('~'),)
+    else:
+        search_dirs = (expand('~'),)
+    for home_dir in search_dirs:
+        environments_txt_file = get_user_environments_txt_file(home_dir)
+        if isfile(environments_txt_file):
+            all_env_paths.update(_clean_environments_txt(environments_txt_file))
+
 
     # in case environments.txt files aren't complete, also add all known conda environments in
     # all envs_dirs


Test patch: