File size: 5,234 Bytes
ae01f49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | ##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
#########################################################################
"""This file contains functions fetching different utility paths."""
import os
from flask import current_app, url_for
from flask_security import current_user
from werkzeug.exceptions import InternalServerError
from pgadmin.utils.constants import MY_STORAGE
from pgadmin.model import User
PGADMIN_PATH = '~/.pgadmin/'
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
def get_storage_directory(user=current_user, shared_storage=''):
# Don't move this import statement to the top of the file,
# it throws circular import error.
import config
if config.SERVER_MODE is not True:
return None
is_shared_storage = False
if shared_storage != MY_STORAGE and shared_storage:
is_shared_storage = True
selected_dir = [sdir for sdir in config.SHARED_STORAGE if
sdir['name'] == shared_storage]
storage_dir = None
if len(selected_dir) > 0:
the_dir = selected_dir[0]['path']
storage_dir = the_dir
else:
storage_dir = getattr(
config, 'STORAGE_DIR',
os.path.join(
os.path.realpath(
os.path.expanduser(PGADMIN_PATH)
), 'storage'
)
)
if storage_dir is None:
return None
username = preprocess_username(user.username.split('@')[0])
# Figure out the old-style storage directory name
old_storage_dir = os.path.join(
storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode')
else storage_dir,
username
)
username = preprocess_username(user.username)
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
)
# 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)
if not os.path.exists(storage_dir):
os.makedirs(storage_dir, int('700', 8))
return storage_dir
def init_app():
# Don't move this import statement to the top of the file,
# it throws circular import error.
import config
if config.SERVER_MODE is not True:
return None
storage_dir = getattr(
config, 'STORAGE_DIR',
os.path.join(
os.path.realpath(
os.path.expanduser(PGADMIN_PATH)
), 'storage'
)
)
if storage_dir and not os.path.isdir(storage_dir):
if os.path.exists(storage_dir):
raise InternalServerError(
'The path specified for the storage directory is not a '
'directory.'
)
os.makedirs(storage_dir, int('700', 8))
if storage_dir and not os.access(storage_dir, os.W_OK | os.R_OK):
raise InternalServerError(
'The user does not have permission to read and write to the '
'specified storage directory.'
)
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
def create_users_storage_directory():
"""
This function is used to iterate through all the users and
create users directory if not already created.
"""
# Don't move this import statement to the top of the file,
# it throws circular import error.
import config
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(
os.path.expanduser(PGADMIN_PATH)
), '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))
|