From 55956fe2ca49fe9a1d1a8fc52b221e6fc05e07a6 Mon Sep 17 00:00:00 2001 From: Tim Thompson Date: Wed, 28 Jun 2017 03:16:41 -0600 Subject: [PATCH 1/3] Expose 4 more rows from the galaxy_user table to help out sys admins doing user management via the API --- lib/galaxy/model/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index 48ec6c9bea0..c8a73ed0c28 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -152,7 +152,7 @@ class User( object, Dictifiable ): histories, credentials, and roles. """ # attributes that will be accessed and returned when calling to_dict( view='collection' ) - dict_collection_visible_keys = ( 'id', 'email', 'username' ) + dict_collection_visible_keys = ( 'id', 'email', 'username', 'deleted', 'purged', 'active', 'last_password_change' ) # attributes that will be accessed and returned when calling to_dict( view='element' ) dict_element_visible_keys = ( 'id', 'email', 'username', 'total_disk_usage', 'nice_total_disk_usage' ) From e320fd4a2009aa4c155d0cbbdd549a9f07295108 Mon Sep 17 00:00:00 2001 From: XDtim Date: Mon, 3 Jul 2017 17:30:00 -0600 Subject: [PATCH 2/3] Impliment review comments from @nsoranzo --- lib/galaxy/model/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index c8a73ed0c28..1484a6eb8b3 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -152,9 +152,9 @@ class User( object, Dictifiable ): histories, credentials, and roles. """ # attributes that will be accessed and returned when calling to_dict( view='collection' ) - dict_collection_visible_keys = ( 'id', 'email', 'username', 'deleted', 'purged', 'active', 'last_password_change' ) + dict_collection_visible_keys = ( 'id', 'email', 'username', 'deleted', 'active', 'last_password_change' ) # attributes that will be accessed and returned when calling to_dict( view='element' ) - dict_element_visible_keys = ( 'id', 'email', 'username', 'total_disk_usage', 'nice_total_disk_usage' ) + dict_element_visible_keys = ( 'id', 'email', 'username', 'total_disk_usage', 'nice_total_disk_usage', 'deleted', 'active', 'last_password_change' ) def __init__( self, email=None, password=None ): self.email = email From d9607684ef7d880e213d4e08d07ed2582875e286 Mon Sep 17 00:00:00 2001 From: John Chilton Date: Tue, 4 Jul 2017 06:37:36 -0400 Subject: [PATCH 3/3] Tweak exposing user attributes to use an include list instead of exclude list. This allows for the addition of new dictifiable attributes on user like in 55956fe2ca49fe9a1d1a8fc52b221e6fc05e07a6 without exposing these attributes to non-admin users. --- lib/galaxy/webapps/galaxy/api/users.py | 16 +++-- test/integration/test_config_options_users.py | 58 +++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 test/integration/test_config_options_users.py diff --git a/lib/galaxy/webapps/galaxy/api/users.py b/lib/galaxy/webapps/galaxy/api/users.py index 2236969ebea..261a50f934a 100644 --- a/lib/galaxy/webapps/galaxy/api/users.py +++ b/lib/galaxy/webapps/galaxy/api/users.py @@ -113,10 +113,18 @@ class UserAPIController( BaseAPIController, UsesTagsMixin, CreatesUsersMixin, Cr item = user.to_dict( value_mapper={ 'id': trans.security.encode_id } ) # If NOT configured to expose_email, do not expose email UNLESS the user is self, or # the user is an admin - if not trans.app.config.expose_user_name and user is not trans.user and not trans.user_is_admin(): - del item['username'] - if not trans.app.config.expose_user_email and user is not trans.user and not trans.user_is_admin(): - del item['email'] + if user is not trans.user and not trans.user_is_admin(): + expose_keys = ["id"] + if trans.app.config.expose_user_name: + expose_keys.append("username") + if trans.app.config.expose_user_email: + expose_keys.append("email") + new_item = {} + for key, value in item.items(): + if key in expose_keys: + new_item[key] = value + item = new_item + # TODO: move into api_values rval.append( item ) return rval diff --git a/test/integration/test_config_options_users.py b/test/integration/test_config_options_users.py new file mode 100644 index 00000000000..ff74a4eff81 --- /dev/null +++ b/test/integration/test_config_options_users.py @@ -0,0 +1,58 @@ +"""Integration tests for user config options.""" + +from base import integration_util + + +class _BaseUserExposeIntegerationTestCase(integration_util.IntegrationTestCase): + + def original_user_ids(self): + return [u["id"] for u in self.galaxy_interactor.get("users").json()] + + def new_users(self, original_ids): + users = [u for u in self.galaxy_interactor.get("users").json() if u["id"] not in original_ids] + return users + + +class DefaultUserExposeIntegrationTestCase(_BaseUserExposeIntegerationTestCase): + + def test_defaults(self): + original_user_ids = self.original_user_ids() + self.galaxy_interactor.ensure_user_with_email("defaultuserexposetest@galaxyproject.org") + new_users = self.new_users(original_user_ids) + # If expose username or expose email isn't enabled - user indexing is + # empty by default for non-admin users. + assert len(new_users) == 0 + + +class EmailUserExposeIntegrationTestCase(_BaseUserExposeIntegerationTestCase): + + @classmethod + def handle_galaxy_config_kwds(cls, config): + config["expose_user_email"] = True + + def test_only_email_exposed(self): + original_user_ids = self.original_user_ids() + self.galaxy_interactor.ensure_user_with_email("emailuserexposetest@galaxyproject.org") + new_users = self.new_users(original_user_ids) + assert len(new_users) > 0 + user = new_users[0] + assert "email" in user + assert "username" not in user + assert "last_password_change" not in user + + +class UsernameUserExposeIntegrationTestCase(_BaseUserExposeIntegerationTestCase): + + @classmethod + def handle_galaxy_config_kwds(cls, config): + config["expose_user_name"] = True + + def test_only_username_exposed(self): + original_user_ids = self.original_user_ids() + self.galaxy_interactor.ensure_user_with_email("usernameuserexposetest@galaxyproject.org") + new_users = self.new_users(original_user_ids) + assert len(new_users) > 0 + user = new_users[0] + assert "email" not in user + assert "username" in user + assert "last_password_change" not in user