From 5071b698718caae30115f463a184aa76f2017250 Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Wed, 6 Dec 2017 13:38:43 +0100 Subject: [PATCH 1/6] Add option to auto create user_library_import_dir directories upon user login. --- config/galaxy.ini.sample | 4 +++ lib/galaxy/config.py | 1 + lib/galaxy/webapps/galaxy/controllers/user.py | 25 +++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config/galaxy.ini.sample b/config/galaxy.ini.sample index 7a6f0d3df8e..b6a84ea53fc 100644 --- a/config/galaxy.ini.sample +++ b/config/galaxy.ini.sample @@ -919,6 +919,10 @@ use_interactive = True # sub-directories of files contained in their directory. #user_library_import_dir = None +# If user_library_import_dir is set, this option will auto create a library +# import directory for every user (based on their email) upon login. +#user_library_import_dir_auto_creation = False + # For security reasons, users may not import any files that actually lie # outside of their `user_library_import_dir` (e.g. using symbolic links). A # list of directories can be allowed by setting the following option (the list diff --git a/lib/galaxy/config.py b/lib/galaxy/config.py index 90dc5c1e293..4db645842d1 100644 --- a/lib/galaxy/config.py +++ b/lib/galaxy/config.py @@ -427,6 +427,7 @@ class Configuration(object): self.user_library_import_dir = kwargs.get('user_library_import_dir', None) self.user_library_import_symlink_whitelist = listify(kwargs.get('user_library_import_symlink_whitelist', []), do_strip=True) self.user_library_import_check_permissions = string_as_bool(kwargs.get('user_library_import_check_permissions', False)) + self.user_library_import_dir_auto_creation = string_as_bool(kwargs.get('user_library_import_dir_auto_creation', False)) if self.user_library_import_dir else False # Searching data libraries self.ftp_upload_dir = kwargs.get('ftp_upload_dir', None) self.ftp_upload_dir_identifier = kwargs.get('ftp_upload_dir_identifier', 'email') # attribute on user - email, username, id, etc... diff --git a/lib/galaxy/webapps/galaxy/controllers/user.py b/lib/galaxy/webapps/galaxy/controllers/user.py index 5d2a0eae80d..a1b015b3f74 100644 --- a/lib/galaxy/webapps/galaxy/controllers/user.py +++ b/lib/galaxy/webapps/galaxy/controllers/user.py @@ -5,6 +5,7 @@ Contains the user interface in the Universe class import logging import random import socket +import os from datetime import datetime, timedelta from markupsafe import escape @@ -21,6 +22,7 @@ from galaxy import ( util, web ) +from galaxy.exceptions import ConfigurationError from galaxy.queue_worker import send_local_control_task from galaxy.security.validate_user_input import ( transform_publicname, @@ -510,7 +512,7 @@ class User(BaseUIController, UsesFormDefinitionsMixin, CreatesUsersMixin, Create if success: # The handle_user_login() method has a call to the history_set_default_permissions() method # (needed when logging in with a history), user needs to have default permissions set before logging in - trans.handle_user_login(user) + self.handle_user_login(trans, user) trans.log_event("User (auto) created a new account") trans.log_event("User logged in") else: @@ -555,12 +557,31 @@ class User(BaseUIController, UsesFormDefinitionsMixin, CreatesUsersMixin, Create status = 'warning' return (message, status, user, success) + def check_user_library_import_dir(self, user): + user_base_dir = self.app.config.user_library_import_dir + if self.app.config.user_library_import_dir_auto_creation: + # try to create a user library import directory + try: + self.app.config._ensure_directory(os.path.join(user_base_dir, user.email)) + except ConfigurationError as e: + self.log_event(str(e)) + + def user_checks(self, user): + """ + This could contain more checks around a user upon login + """ + self.check_user_library_import_dir(user) + + def handle_user_login(self, trans, user): + self.user_checks(user) + trans.handle_user_login(user) + def proceed_login(self, trans, user, redirect): """ Function processes user login. It is called in case all the login requirements are valid. """ message = '' - trans.handle_user_login(user) + self.handle_user_login(trans, user) if trans.webapp.name == 'galaxy': trans.log_event("User logged in") message = 'You are now logged in as %s.
You can go back to the page you were visiting or go to the home page.' % \ From 74e551d34cfb5bb50ca061f682de97084ade5a2e Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Wed, 6 Dec 2017 13:45:01 +0100 Subject: [PATCH 2/6] one less variable.. --- lib/galaxy/webapps/galaxy/controllers/user.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/galaxy/webapps/galaxy/controllers/user.py b/lib/galaxy/webapps/galaxy/controllers/user.py index a1b015b3f74..c30f3fc23b7 100644 --- a/lib/galaxy/webapps/galaxy/controllers/user.py +++ b/lib/galaxy/webapps/galaxy/controllers/user.py @@ -558,11 +558,10 @@ class User(BaseUIController, UsesFormDefinitionsMixin, CreatesUsersMixin, Create return (message, status, user, success) def check_user_library_import_dir(self, user): - user_base_dir = self.app.config.user_library_import_dir if self.app.config.user_library_import_dir_auto_creation: # try to create a user library import directory try: - self.app.config._ensure_directory(os.path.join(user_base_dir, user.email)) + self.app.config._ensure_directory(os.path.join(self.app.config.user_library_import_dir, user.email)) except ConfigurationError as e: self.log_event(str(e)) From ae3afae8f002fccb1b236271da4204b1115eef23 Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Wed, 6 Dec 2017 14:22:40 +0100 Subject: [PATCH 3/6] sort import order alphabetically --- lib/galaxy/webapps/galaxy/controllers/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/webapps/galaxy/controllers/user.py b/lib/galaxy/webapps/galaxy/controllers/user.py index c30f3fc23b7..117d831332b 100644 --- a/lib/galaxy/webapps/galaxy/controllers/user.py +++ b/lib/galaxy/webapps/galaxy/controllers/user.py @@ -3,9 +3,9 @@ Contains the user interface in the Universe class """ import logging +import os import random import socket -import os from datetime import datetime, timedelta from markupsafe import escape From 8553ea2c6e9fedb88579f1a7e9e8727fc2a977a7 Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Thu, 7 Dec 2017 15:41:47 +0100 Subject: [PATCH 4/6] move logic to webapps.py --- lib/galaxy/web/framework/webapp.py | 19 +++++++++++++-- lib/galaxy/webapps/galaxy/controllers/user.py | 24 ++----------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/galaxy/web/framework/webapp.py b/lib/galaxy/web/framework/webapp.py index 91491195bf4..a06e09db0cf 100644 --- a/lib/galaxy/web/framework/webapp.py +++ b/lib/galaxy/web/framework/webapp.py @@ -24,7 +24,7 @@ from sqlalchemy.orm import joinedload from sqlalchemy.orm.exc import NoResultFound from galaxy import util -from galaxy.exceptions import MessageException +from galaxy.exceptions import ConfigurationError, MessageException from galaxy.managers import context from galaxy.util import ( asbool, @@ -630,16 +630,31 @@ class GalaxyWebTransaction(base.DefaultWebTransaction, self.set_cookie(self.security.encode_guid(self.galaxy_session.session_key), name=name, path=self.app.config.cookie_path) + def check_user_library_import_dir(self, user): + if self.app.config.user_library_import_dir_auto_creation: + # try to create a user library import directory + try: + self.app.config._ensure_directory(os.path.join(self.app.config.user_library_import_dir, user.email)) + except ConfigurationError as e: + self.log_event(str(e)) + + def user_checks(self, user): + """ + This could contain more checks around a user upon login + """ + self.check_user_library_import_dir(user) + def handle_user_login(self, user): """ Login a new user (possibly newly created) - + - do some 'system' checks (if any) for this user - create a new session - associate new session with user - if old session had a history and it was not associated with a user, associate it with the new session, otherwise associate the current session's history with the user - add the disk usage of the current session to the user's total disk usage """ + self.user_checks(user) # Set the previous session prev_galaxy_session = self.galaxy_session prev_galaxy_session.is_valid = False diff --git a/lib/galaxy/webapps/galaxy/controllers/user.py b/lib/galaxy/webapps/galaxy/controllers/user.py index 117d831332b..5d2a0eae80d 100644 --- a/lib/galaxy/webapps/galaxy/controllers/user.py +++ b/lib/galaxy/webapps/galaxy/controllers/user.py @@ -3,7 +3,6 @@ Contains the user interface in the Universe class """ import logging -import os import random import socket from datetime import datetime, timedelta @@ -22,7 +21,6 @@ from galaxy import ( util, web ) -from galaxy.exceptions import ConfigurationError from galaxy.queue_worker import send_local_control_task from galaxy.security.validate_user_input import ( transform_publicname, @@ -512,7 +510,7 @@ class User(BaseUIController, UsesFormDefinitionsMixin, CreatesUsersMixin, Create if success: # The handle_user_login() method has a call to the history_set_default_permissions() method # (needed when logging in with a history), user needs to have default permissions set before logging in - self.handle_user_login(trans, user) + trans.handle_user_login(user) trans.log_event("User (auto) created a new account") trans.log_event("User logged in") else: @@ -557,30 +555,12 @@ class User(BaseUIController, UsesFormDefinitionsMixin, CreatesUsersMixin, Create status = 'warning' return (message, status, user, success) - def check_user_library_import_dir(self, user): - if self.app.config.user_library_import_dir_auto_creation: - # try to create a user library import directory - try: - self.app.config._ensure_directory(os.path.join(self.app.config.user_library_import_dir, user.email)) - except ConfigurationError as e: - self.log_event(str(e)) - - def user_checks(self, user): - """ - This could contain more checks around a user upon login - """ - self.check_user_library_import_dir(user) - - def handle_user_login(self, trans, user): - self.user_checks(user) - trans.handle_user_login(user) - def proceed_login(self, trans, user, redirect): """ Function processes user login. It is called in case all the login requirements are valid. """ message = '' - self.handle_user_login(trans, user) + trans.handle_user_login(user) if trans.webapp.name == 'galaxy': trans.log_event("User logged in") message = 'You are now logged in as %s.
You can go back to the page you were visiting or go to the home page.' % \ From 729b0bf63b2990849809f32da45ef078d206069b Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Thu, 18 Jan 2018 12:44:50 +0100 Subject: [PATCH 5/6] fix toolshed test with missing config attribute --- lib/galaxy/web/framework/webapp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/web/framework/webapp.py b/lib/galaxy/web/framework/webapp.py index a06e09db0cf..4d073f18fb3 100644 --- a/lib/galaxy/web/framework/webapp.py +++ b/lib/galaxy/web/framework/webapp.py @@ -631,7 +631,7 @@ class GalaxyWebTransaction(base.DefaultWebTransaction, name=name, path=self.app.config.cookie_path) def check_user_library_import_dir(self, user): - if self.app.config.user_library_import_dir_auto_creation: + if getattr(self.app.config, "user_library_import_dir_auto_creation", False): # try to create a user library import directory try: self.app.config._ensure_directory(os.path.join(self.app.config.user_library_import_dir, user.email)) From 4947ab0738a076ef7cc60856271a1585c259fa36 Mon Sep 17 00:00:00 2001 From: Jelle Scholtalbers Date: Fri, 19 Jan 2018 13:51:54 +0100 Subject: [PATCH 6/6] use safemakedirs instead of private _ensure_directory --- lib/galaxy/web/framework/webapp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/web/framework/webapp.py b/lib/galaxy/web/framework/webapp.py index 4d073f18fb3..b13b6cf6454 100644 --- a/lib/galaxy/web/framework/webapp.py +++ b/lib/galaxy/web/framework/webapp.py @@ -28,6 +28,7 @@ from galaxy.exceptions import ConfigurationError, MessageException from galaxy.managers import context from galaxy.util import ( asbool, + safe_makedirs, safe_str_cmp ) from galaxy.util.sanitize_html import sanitize_html @@ -634,7 +635,7 @@ class GalaxyWebTransaction(base.DefaultWebTransaction, if getattr(self.app.config, "user_library_import_dir_auto_creation", False): # try to create a user library import directory try: - self.app.config._ensure_directory(os.path.join(self.app.config.user_library_import_dir, user.email)) + safe_makedirs(os.path.join(self.app.config.user_library_import_dir, user.email)) except ConfigurationError as e: self.log_event(str(e))