2016-05-13 00:04:28 +05:30
|
|
|
##########################################################################
|
|
|
|
|
#
|
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
|
#
|
2024-01-01 14:13:48 +05:30
|
|
|
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2016-05-13 00:04:28 +05:30
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
|
#
|
|
|
|
|
#########################################################################
|
|
|
|
|
|
|
|
|
|
"""This file contains functions fetching different utility paths."""
|
|
|
|
|
|
|
|
|
|
import os
|
2023-03-14 18:40:53 +05:30
|
|
|
|
2019-03-22 09:36:13 +00:00
|
|
|
from flask import current_app, url_for
|
2022-01-09 14:47:32 +05:30
|
|
|
from flask_security import current_user
|
2020-08-07 12:37:00 +05:30
|
|
|
from werkzeug.exceptions import InternalServerError
|
2023-03-06 17:03:47 +05:30
|
|
|
from pgadmin.utils.constants import MY_STORAGE
|
2023-03-14 18:21:18 +05:30
|
|
|
from pgadmin.model import User
|
|
|
|
|
|
2023-06-12 18:44:31 +05:30
|
|
|
PGADMIN_PATH = '~/.pgadmin/'
|
|
|
|
|
|
2023-03-14 18:21:18 +05:30
|
|
|
|
|
|
|
|
def preprocess_username(un):
|
|
|
|
|
ret_un = un
|
|
|
|
|
if len(ret_un) == 0 or ret_un[0].isdigit():
|
|
|
|
|
ret_un = 'pga_user_' + un
|
|
|
|
|
|
|
|
|
|
ret_un = ret_un.replace('@', '_') \
|
|
|
|
|
.replace('/', 'slash') \
|
|
|
|
|
.replace('\\', 'slash')
|
|
|
|
|
|
|
|
|
|
return ret_un
|
2016-06-21 14:12:14 +01:00
|
|
|
|
2016-05-13 00:04:28 +05:30
|
|
|
|
2023-03-06 17:03:47 +05:30
|
|
|
def get_storage_directory(user=current_user, shared_storage=''):
|
2023-03-14 18:40:53 +05:30
|
|
|
# Don't move this import statement to the top of the file,
|
|
|
|
|
# it throws circular import error.
|
|
|
|
|
import config
|
2016-05-13 00:04:28 +05:30
|
|
|
if config.SERVER_MODE is not True:
|
2016-06-17 18:19:51 +01:00
|
|
|
return None
|
2016-05-13 00:04:28 +05:30
|
|
|
|
2023-03-06 17:03:47 +05:30
|
|
|
is_shared_storage = False
|
|
|
|
|
if shared_storage != MY_STORAGE and shared_storage:
|
|
|
|
|
is_shared_storage = True
|
2023-06-12 18:44:31 +05:30
|
|
|
selected_dir = [sdir for sdir in config.SHARED_STORAGE if
|
|
|
|
|
sdir['name'] == shared_storage]
|
2023-03-06 17:03:47 +05:30
|
|
|
storage_dir = None
|
2023-06-12 18:44:31 +05:30
|
|
|
if len(selected_dir) > 0:
|
|
|
|
|
the_dir = selected_dir[0]['path']
|
2023-03-06 17:03:47 +05:30
|
|
|
storage_dir = the_dir
|
|
|
|
|
else:
|
|
|
|
|
storage_dir = getattr(
|
|
|
|
|
config, 'STORAGE_DIR',
|
|
|
|
|
os.path.join(
|
|
|
|
|
os.path.realpath(
|
2023-06-12 18:44:31 +05:30
|
|
|
os.path.expanduser(PGADMIN_PATH)
|
2023-03-06 17:03:47 +05:30
|
|
|
), 'storage'
|
|
|
|
|
)
|
2016-05-13 00:04:28 +05:30
|
|
|
)
|
2016-05-14 00:48:11 +05:30
|
|
|
|
|
|
|
|
if storage_dir is None:
|
2016-06-17 18:19:51 +01:00
|
|
|
return None
|
2016-05-14 00:48:11 +05:30
|
|
|
|
2023-03-14 18:21:18 +05:30
|
|
|
username = preprocess_username(user.username.split('@')[0])
|
2016-05-13 00:04:28 +05:30
|
|
|
|
2019-03-22 09:36:13 +00:00
|
|
|
# Figure out the old-style storage directory name
|
|
|
|
|
old_storage_dir = os.path.join(
|
2018-01-31 13:58:55 +00:00
|
|
|
storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
|
|
|
|
|
else storage_dir,
|
2017-03-07 15:30:57 +05:30
|
|
|
username
|
|
|
|
|
)
|
2016-05-13 00:04:28 +05:30
|
|
|
|
2023-03-14 18:21:18 +05:30
|
|
|
username = preprocess_username(user.username)
|
2020-09-11 19:55:19 +05:30
|
|
|
|
2023-03-06 17:03:47 +05:30
|
|
|
if is_shared_storage:
|
|
|
|
|
# Figure out the new style storage directory name
|
|
|
|
|
storage_dir = os.path.join(
|
|
|
|
|
storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
|
|
|
|
|
else storage_dir
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# Figure out the new style storage directory name
|
|
|
|
|
storage_dir = os.path.join(
|
|
|
|
|
storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
|
|
|
|
|
else storage_dir,
|
|
|
|
|
username
|
|
|
|
|
)
|
2019-03-22 09:36:13 +00:00
|
|
|
|
|
|
|
|
# Rename an old-style storage directory, if the new style doesn't exist
|
|
|
|
|
if os.path.exists(old_storage_dir) and not os.path.exists(storage_dir):
|
|
|
|
|
current_app.logger.warning(
|
|
|
|
|
'Renaming storage directory %s to %s.',
|
|
|
|
|
old_storage_dir, storage_dir
|
|
|
|
|
)
|
|
|
|
|
os.rename(old_storage_dir, storage_dir)
|
|
|
|
|
|
2016-05-13 00:04:28 +05:30
|
|
|
if not os.path.exists(storage_dir):
|
|
|
|
|
os.makedirs(storage_dir, int('700', 8))
|
|
|
|
|
|
|
|
|
|
return storage_dir
|
|
|
|
|
|
|
|
|
|
|
2022-09-09 18:36:51 +05:30
|
|
|
def init_app():
|
2023-03-14 18:40:53 +05:30
|
|
|
# Don't move this import statement to the top of the file,
|
|
|
|
|
# it throws circular import error.
|
|
|
|
|
import config
|
2016-05-13 00:04:28 +05:30
|
|
|
if config.SERVER_MODE is not True:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
storage_dir = getattr(
|
|
|
|
|
config, 'STORAGE_DIR',
|
|
|
|
|
os.path.join(
|
|
|
|
|
os.path.realpath(
|
2023-06-12 18:44:31 +05:30
|
|
|
os.path.expanduser(PGADMIN_PATH)
|
2016-05-13 00:04:28 +05:30
|
|
|
), 'storage'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2016-05-14 00:48:11 +05:30
|
|
|
if storage_dir and not os.path.isdir(storage_dir):
|
2016-05-13 00:04:28 +05:30
|
|
|
if os.path.exists(storage_dir):
|
2020-08-07 12:37:00 +05:30
|
|
|
raise InternalServerError(
|
2018-01-31 13:58:55 +00:00
|
|
|
'The path specified for the storage directory is not a '
|
|
|
|
|
'directory.'
|
2016-05-13 00:04:28 +05:30
|
|
|
)
|
|
|
|
|
os.makedirs(storage_dir, int('700', 8))
|
|
|
|
|
|
2016-06-10 17:06:22 +01:00
|
|
|
if storage_dir and not os.access(storage_dir, os.W_OK | os.R_OK):
|
2020-08-07 12:37:00 +05:30
|
|
|
raise InternalServerError(
|
2018-01-31 13:58:55 +00:00
|
|
|
'The user does not have permission to read and write to the '
|
|
|
|
|
'specified storage directory.'
|
2016-05-13 00:04:28 +05:30
|
|
|
)
|
2018-03-19 17:09:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cookie_path():
|
|
|
|
|
cookie_root_path = '/'
|
|
|
|
|
pgadmin_root_path = url_for('browser.index')
|
|
|
|
|
if pgadmin_root_path != '/browser/':
|
|
|
|
|
cookie_root_path = pgadmin_root_path.replace(
|
|
|
|
|
'/browser/', ''
|
|
|
|
|
)
|
|
|
|
|
return cookie_root_path
|
2023-03-14 18:21:18 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_users_storage_directory():
|
|
|
|
|
"""
|
|
|
|
|
This function is used to iterate through all the users and
|
|
|
|
|
create users directory if not already created.
|
|
|
|
|
"""
|
2023-03-14 18:40:53 +05:30
|
|
|
# Don't move this import statement to the top of the file,
|
|
|
|
|
# it throws circular import error.
|
|
|
|
|
import config
|
2023-03-14 18:21:18 +05:30
|
|
|
if not config.SERVER_MODE:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
users = User.query.all()
|
|
|
|
|
|
|
|
|
|
for usr in users:
|
|
|
|
|
username = preprocess_username(usr.username)
|
|
|
|
|
|
|
|
|
|
storage_dir = getattr(
|
|
|
|
|
config, 'STORAGE_DIR',
|
|
|
|
|
os.path.join(
|
|
|
|
|
os.path.realpath(
|
2023-06-12 18:44:31 +05:30
|
|
|
os.path.expanduser(PGADMIN_PATH)
|
2023-03-14 18:21:18 +05:30
|
|
|
), 'storage'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if storage_dir is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
storage_dir = os.path.join(
|
|
|
|
|
storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
|
|
|
|
|
else storage_dir, username
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(storage_dir):
|
|
|
|
|
os.makedirs(storage_dir, int('700', 8))
|