mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Fixed a couple of SonarQube issues.
This commit is contained in:
parent
add5404f5e
commit
1b11a6cd4b
@ -356,6 +356,12 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function selectChildNode(item, prefTreeInit) {
|
||||||
|
if (item.isExpanded && item._children && item._children.length > 0 && prefTreeInit.current && event.code !== 'ArrowUp') {
|
||||||
|
pgAdmin.Browser.ptree.tree.setActiveFile(item._children[0], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let initTreeTimeout = null;
|
let initTreeTimeout = null;
|
||||||
let firstElement = null;
|
let firstElement = null;
|
||||||
@ -379,9 +385,7 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
}, 10);
|
}, 10);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (item.isExpanded && item._children && item._children.length > 0 && prefTreeInit.current && event.code !== 'ArrowUp') {
|
selectChildNode(item, prefTreeInit);
|
||||||
pgAdmin.Browser.ptree.tree.setActiveFile(item._children[0], true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -391,7 +395,14 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Listen added preferences tree node event to expand the newly added node on tree load.
|
// Listen added preferences tree node event to expand the newly added node on tree load.
|
||||||
pgAdmin.Browser.Events.on('preferences:tree:added', (event, item) => {
|
pgAdmin.Browser.Events.on('preferences:tree:added', addPrefTreeNode);
|
||||||
|
/* Clear the initTreeTimeout timeout if unmounted */
|
||||||
|
return () => {
|
||||||
|
clearTimeout(initTreeTimeout);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function addPrefTreeNode(event, item) {
|
||||||
if (item._parent._fileName == firstTreeElement.current && item._parent.isExpanded && !prefTreeInit.current) {
|
if (item._parent._fileName == firstTreeElement.current && item._parent.isExpanded && !prefTreeInit.current) {
|
||||||
pgAdmin.Browser.ptree.tree.setActiveFile(item._parent._children[0], true);
|
pgAdmin.Browser.ptree.tree.setActiveFile(item._parent._children[0], true);
|
||||||
}
|
}
|
||||||
@ -399,12 +410,7 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
// Check the if newely added node is Directoy and call toggle to expand the node.
|
// Check the if newely added node is Directoy and call toggle to expand the node.
|
||||||
pgAdmin.Browser.ptree.tree.toggleDirectory(item);
|
pgAdmin.Browser.ptree.tree.toggleDirectory(item);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
/* Clear the initTreeTimeout timeout if unmounted */
|
|
||||||
return () => {
|
|
||||||
clearTimeout(initTreeTimeout);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
function getControlMappedForType(type) {
|
function getControlMappedForType(type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -155,14 +155,19 @@ const dialogStyle = makeStyles((theme) => ({
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
function checkIsResizable(props) {
|
||||||
|
return props.isresizeable == 'true' ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setEnableResizing(props, resizeable) {
|
||||||
|
return props.isfullscreen == 'true' ? false : resizeable;
|
||||||
|
}
|
||||||
|
|
||||||
function PaperComponent({minHeight, minWidth, ...props}) {
|
function PaperComponent({minHeight, minWidth, ...props}) {
|
||||||
let classes = dialogStyle();
|
let classes = dialogStyle();
|
||||||
let [dialogPosition, setDialogPosition] = useState(null);
|
let [dialogPosition, setDialogPosition] = useState(null);
|
||||||
let resizeable = props.isresizeable == 'true' ? true : false;
|
let resizeable = checkIsResizable(props);
|
||||||
|
|
||||||
const setEnableResizing = () => {
|
|
||||||
return props.isfullscreen == 'true' ? false : resizeable;
|
|
||||||
};
|
|
||||||
|
|
||||||
const setConditionalPosition = () => {
|
const setConditionalPosition = () => {
|
||||||
return props.isfullscreen == 'true' ? { x: 0, y: 0 } : dialogPosition && { x: dialogPosition.x, y: dialogPosition.y };
|
return props.isfullscreen == 'true' ? { x: 0, y: 0 } : dialogPosition && { x: dialogPosition.x, y: dialogPosition.y };
|
||||||
@ -187,7 +192,7 @@ function PaperComponent({minHeight, minWidth, ...props}) {
|
|||||||
// {...(props.width && { minWidth: MIN_WIDTH })}
|
// {...(props.width && { minWidth: MIN_WIDTH })}
|
||||||
// {...(props.height && { minHeight: MIN_HEIGHT })}
|
// {...(props.height && { minHeight: MIN_HEIGHT })}
|
||||||
bounds="window"
|
bounds="window"
|
||||||
enableResizing={setEnableResizing()}
|
enableResizing={setEnableResizing(props, resizeable)}
|
||||||
position={setConditionalPosition()}
|
position={setConditionalPosition()}
|
||||||
onDragStop={(e, position) => {
|
onDragStop={(e, position) => {
|
||||||
if (props.isfullscreen !== 'true') {
|
if (props.isfullscreen !== 'true') {
|
||||||
|
@ -245,6 +245,22 @@ def _get_rows_for_type(conn, ntype, server_prop, node_id):
|
|||||||
return status, res
|
return status, res
|
||||||
|
|
||||||
|
|
||||||
|
def get_node_sql_with_type(node_id, node_type, server_prop,
|
||||||
|
get_schema_sql_url, show_sysobj):
|
||||||
|
if node_type == 'database':
|
||||||
|
sql = render_template("/".join(
|
||||||
|
[server_prop['template_path'], get_schema_sql_url]),
|
||||||
|
show_sysobj=show_sysobj)
|
||||||
|
ntype = 'schema'
|
||||||
|
else:
|
||||||
|
sql = render_template("/".join(
|
||||||
|
[server_prop['template_path'], get_schema_sql_url]),
|
||||||
|
show_sysobj=show_sysobj, nspid=node_id)
|
||||||
|
ntype = node_type
|
||||||
|
|
||||||
|
return sql, ntype
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route(
|
@blueprint.route(
|
||||||
'/<int:sid>/<int:did>/<int:node_id>/<node_type>/',
|
'/<int:sid>/<int:did>/<int:node_id>/<node_type>/',
|
||||||
methods=['GET'], endpoint='objects'
|
methods=['GET'], endpoint='objects'
|
||||||
@ -269,16 +285,9 @@ def properties(sid, did, node_id, node_type):
|
|||||||
conn = manager.connection(did=did)
|
conn = manager.connection(did=did)
|
||||||
|
|
||||||
show_sysobj = blueprint.show_system_objects().get()
|
show_sysobj = blueprint.show_system_objects().get()
|
||||||
if node_type == 'database':
|
|
||||||
sql = render_template("/".join(
|
sql, ntype = get_node_sql_with_type(node_id, node_type, server_prop,
|
||||||
[server_prop['template_path'], get_schema_sql_url]),
|
get_schema_sql_url, show_sysobj)
|
||||||
show_sysobj=show_sysobj)
|
|
||||||
ntype = 'schema'
|
|
||||||
else:
|
|
||||||
sql = render_template("/".join(
|
|
||||||
[server_prop['template_path'], get_schema_sql_url]),
|
|
||||||
show_sysobj=show_sysobj, nspid=node_id)
|
|
||||||
ntype = node_type
|
|
||||||
|
|
||||||
status, res = conn.execute_dict(sql)
|
status, res = conn.execute_dict(sql)
|
||||||
|
|
||||||
@ -351,6 +360,17 @@ def properties(sid, did, node_id, node_type):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_req_data():
|
||||||
|
return request.form if request.form else json.loads(request.data.decode())
|
||||||
|
|
||||||
|
|
||||||
|
def set_priv_for_package(server_prop, data, acls):
|
||||||
|
if server_prop['server_type'] == 'ppas':
|
||||||
|
data['priv']['package'] = parse_priv_to_db(
|
||||||
|
data['acl'],
|
||||||
|
acls['package']['acl'])
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route(
|
@blueprint.route(
|
||||||
'/sql/<int:sid>/<int:did>/',
|
'/sql/<int:sid>/<int:did>/',
|
||||||
methods=['POST'], endpoint='modified_sql'
|
methods=['POST'], endpoint='modified_sql'
|
||||||
@ -362,7 +382,7 @@ def msql(sid, did):
|
|||||||
This function will return modified SQL
|
This function will return modified SQL
|
||||||
"""
|
"""
|
||||||
server_prop = server_info
|
server_prop = server_info
|
||||||
data = request.form if request.form else json.loads(request.data.decode())
|
data = get_req_data()
|
||||||
# Form db connection
|
# Form db connection
|
||||||
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
|
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
|
||||||
conn = manager.connection(did=did)
|
conn = manager.connection(did=did)
|
||||||
@ -398,10 +418,7 @@ def msql(sid, did):
|
|||||||
acls['foreign_table']['acl'])
|
acls['foreign_table']['acl'])
|
||||||
|
|
||||||
# Logic for setting privileges only for ppas
|
# Logic for setting privileges only for ppas
|
||||||
if server_prop['server_type'] == 'ppas':
|
set_priv_for_package(server_prop, data, acls)
|
||||||
data['priv']['package'] = parse_priv_to_db(
|
|
||||||
data['acl'],
|
|
||||||
acls['package']['acl'])
|
|
||||||
|
|
||||||
# Pass database objects and get SQL for privileges
|
# Pass database objects and get SQL for privileges
|
||||||
sql_data = ''
|
sql_data = ''
|
||||||
@ -466,36 +483,7 @@ def msql(sid, did):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route(
|
def parse_priv(data, acls, server_prop):
|
||||||
'/<int:sid>/<int:did>/', methods=['POST'], endpoint='apply'
|
|
||||||
)
|
|
||||||
@login_required
|
|
||||||
@check_precondition
|
|
||||||
def save(sid, did):
|
|
||||||
"""
|
|
||||||
This function will apply the privileges to the selected
|
|
||||||
Database Objects
|
|
||||||
"""
|
|
||||||
server_prop = server_info
|
|
||||||
data = request.form if request.form else json.loads(request.data.decode())
|
|
||||||
|
|
||||||
# Form db connection and we use conn to execute sql
|
|
||||||
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
|
|
||||||
conn = manager.connection(did=did)
|
|
||||||
|
|
||||||
acls = []
|
|
||||||
try:
|
|
||||||
acls = render_template(
|
|
||||||
"/".join([server_prop['template_path'], 'acl.json']),
|
|
||||||
)
|
|
||||||
acls = json.loads(acls)
|
|
||||||
except Exception as e:
|
|
||||||
current_app.logger.exception(e)
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
# Parse privileges
|
|
||||||
data['priv'] = {}
|
|
||||||
if 'acl' in data:
|
if 'acl' in data:
|
||||||
# Get function acls
|
# Get function acls
|
||||||
data['priv']['function'] = parse_priv_to_db(
|
data['priv']['function'] = parse_priv_to_db(
|
||||||
@ -515,11 +503,40 @@ def save(sid, did):
|
|||||||
acls['foreign_table']['acl'])
|
acls['foreign_table']['acl'])
|
||||||
|
|
||||||
# Logic for setting privileges only for ppas
|
# Logic for setting privileges only for ppas
|
||||||
if server_prop['server_type'] == 'ppas':
|
set_priv_for_package(server_prop, data, acls)
|
||||||
data['priv']['package'] = parse_priv_to_db(
|
|
||||||
data['acl'],
|
|
||||||
acls['package']['acl'])
|
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route(
|
||||||
|
'/<int:sid>/<int:did>/', methods=['POST'], endpoint='apply'
|
||||||
|
)
|
||||||
|
@login_required
|
||||||
|
@check_precondition
|
||||||
|
def save(sid, did):
|
||||||
|
"""
|
||||||
|
This function will apply the privileges to the selected
|
||||||
|
Database Objects
|
||||||
|
"""
|
||||||
|
server_prop = server_info
|
||||||
|
data = get_req_data()
|
||||||
|
|
||||||
|
# Form db connection and we use conn to execute sql
|
||||||
|
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
|
||||||
|
conn = manager.connection(did=did)
|
||||||
|
|
||||||
|
acls = []
|
||||||
|
try:
|
||||||
|
acls = render_template(
|
||||||
|
"/".join([server_prop['template_path'], 'acl.json']),
|
||||||
|
)
|
||||||
|
acls = json.loads(acls)
|
||||||
|
except Exception as e:
|
||||||
|
current_app.logger.exception(e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# Parse privileges
|
||||||
|
data['priv'] = {}
|
||||||
|
parse_priv(data, acls, server_prop)
|
||||||
# Pass database objects and get SQL for privileges
|
# Pass database objects and get SQL for privileges
|
||||||
# Pass database objects and get SQL for privileges
|
# Pass database objects and get SQL for privileges
|
||||||
sql_data = ''
|
sql_data = ''
|
||||||
|
@ -8,14 +8,13 @@
|
|||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import select
|
import select
|
||||||
import struct
|
import struct
|
||||||
import config
|
import config
|
||||||
from sys import platform as _platform
|
from sys import platform as _platform
|
||||||
import eventlet.green.subprocess as subprocess
|
import eventlet.green.subprocess as subprocess
|
||||||
from config import PG_DEFAULT_DRIVER
|
from config import PG_DEFAULT_DRIVER
|
||||||
from flask import Response, url_for, request
|
from flask import Response, request
|
||||||
from flask import render_template, copy_current_request_context, \
|
from flask import render_template, copy_current_request_context, \
|
||||||
current_app as app
|
current_app as app
|
||||||
from flask_babel import gettext
|
from flask_babel import gettext
|
||||||
@ -215,20 +214,7 @@ def read_stdout(process, sid, max_read_bytes, win_emit_output=True):
|
|||||||
sio.sleep(0)
|
sio.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
@sio.on('start_process', namespace='/pty')
|
def windows_platform(connection_data, sid, max_read_bytes):
|
||||||
def start_process(data):
|
|
||||||
"""
|
|
||||||
Start the pty terminal and execute psql command and emit results to user.
|
|
||||||
:param data:
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
@copy_current_request_context
|
|
||||||
def read_and_forward_pty_output(sid, data):
|
|
||||||
|
|
||||||
max_read_bytes = 1024 * 20
|
|
||||||
import time
|
|
||||||
if _platform == 'win32':
|
|
||||||
|
|
||||||
os.environ['PYWINPTY_BACKEND'] = '1'
|
os.environ['PYWINPTY_BACKEND'] = '1'
|
||||||
process = PtyProcess.spawn('cmd.exe')
|
process = PtyProcess.spawn('cmd.exe')
|
||||||
|
|
||||||
@ -242,10 +228,9 @@ def start_process(data):
|
|||||||
while True:
|
while True:
|
||||||
read_stdout(process, sid, max_read_bytes,
|
read_stdout(process, sid, max_read_bytes,
|
||||||
win_emit_output=True)
|
win_emit_output=True)
|
||||||
else:
|
|
||||||
|
|
||||||
p, parent, fd = create_pty_terminal(connection_data)
|
|
||||||
|
|
||||||
|
def non_windows_platform(parent, p, fd, data, max_read_bytes, sid):
|
||||||
while p and p.poll() is None:
|
while p and p.poll() is None:
|
||||||
if request.sid in app.config['sessions']:
|
if request.sid in app.config['sessions']:
|
||||||
# This code is added to make this unit testable.
|
# This code is added to make this unit testable.
|
||||||
@ -264,6 +249,27 @@ def start_process(data):
|
|||||||
|
|
||||||
read_terminal_data(parent, data_ready, max_read_bytes, sid)
|
read_terminal_data(parent, data_ready, max_read_bytes, sid)
|
||||||
|
|
||||||
|
|
||||||
|
def pty_handel_io(connection_data, data, sid):
|
||||||
|
max_read_bytes = 1024 * 20
|
||||||
|
if _platform == 'win32':
|
||||||
|
windows_platform(connection_data, sid, max_read_bytes)
|
||||||
|
else:
|
||||||
|
p, parent, fd = create_pty_terminal(connection_data)
|
||||||
|
non_windows_platform(parent, p, fd, data, max_read_bytes, sid)
|
||||||
|
|
||||||
|
|
||||||
|
@sio.on('start_process', namespace='/pty')
|
||||||
|
def start_process(data):
|
||||||
|
"""
|
||||||
|
Start the pty terminal and execute psql command and emit results to user.
|
||||||
|
:param data:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
@copy_current_request_context
|
||||||
|
def read_and_forward_pty_output(sid, data):
|
||||||
|
pty_handel_io(connection_data, data, sid)
|
||||||
|
|
||||||
# Check user is authenticated and PSQL is enabled in config.
|
# Check user is authenticated and PSQL is enabled in config.
|
||||||
if current_user.is_authenticated and config.ENABLE_PSQL:
|
if current_user.is_authenticated and config.ENABLE_PSQL:
|
||||||
connection_data = []
|
connection_data = []
|
||||||
@ -555,6 +561,13 @@ def disconnect_socket():
|
|||||||
del app.config['sessions'][request.sid]
|
del app.config['sessions'][request.sid]
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection_status(conn):
|
||||||
|
if conn.connected():
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _get_database(sid, did):
|
def _get_database(sid, did):
|
||||||
"""
|
"""
|
||||||
This method is used to get database based on sid, did.
|
This method is used to get database based on sid, did.
|
||||||
@ -564,10 +577,9 @@ def _get_database(sid, did):
|
|||||||
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
|
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(int(sid))
|
||||||
conn = manager.connection()
|
conn = manager.connection()
|
||||||
db_name = None
|
db_name = None
|
||||||
if conn.connected():
|
|
||||||
is_connected = True
|
is_connected = get_connection_status(conn)
|
||||||
else:
|
|
||||||
is_connected = False
|
|
||||||
if is_connected:
|
if is_connected:
|
||||||
|
|
||||||
if conn.manager and conn.manager.db_info \
|
if conn.manager and conn.manager.db_info \
|
||||||
|
@ -17,6 +17,7 @@ import {getRandomInt, hasBinariesConfiguration, registerDetachEvent} from 'sourc
|
|||||||
import {retrieveAncestorOfTypeServer} from 'sources/tree/tree_utils';
|
import {retrieveAncestorOfTypeServer} from 'sources/tree/tree_utils';
|
||||||
import pgWindow from 'sources/window';
|
import pgWindow from 'sources/window';
|
||||||
import Notify from '../../../../static/js/helpers/Notifier';
|
import Notify from '../../../../static/js/helpers/Notifier';
|
||||||
|
import { copyToClipboard } from '../../../../static/js/clipboard';
|
||||||
|
|
||||||
import {generateTitle, refresh_db_node} from 'tools/sqleditor/static/js/sqleditor_title';
|
import {generateTitle, refresh_db_node} from 'tools/sqleditor/static/js/sqleditor_title';
|
||||||
|
|
||||||
@ -328,7 +329,14 @@ export function initialize(gettext, url_for, $, _, pgAdmin, csrfToken, Browser)
|
|||||||
term.attachCustomKeyEventHandler(e => {
|
term.attachCustomKeyEventHandler(e => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
if(e.type=='keydown' && (e.metaKey || e.ctrlKey) && (e.key == 'c' || e.key == 'C')) {
|
if(e.type=='keydown' && (e.metaKey || e.ctrlKey) && (e.key == 'c' || e.key == 'C')) {
|
||||||
document.execCommand('copy');
|
let selected_text = term.getSelection();
|
||||||
|
navigator.permissions.query({ name: 'clipboard-write' }).then(function(result) {
|
||||||
|
if(result.state === 'granted' || result.state === 'prompt') {
|
||||||
|
copyToClipboard(selected_text);
|
||||||
|
} else{
|
||||||
|
Notify.alert(gettext('Clipboard write permission required'), gettext('To copy data from PSQL terminal, Clipboard write permission required.'));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return !(e.ctrlKey && platform == 'win32');
|
return !(e.ctrlKey && platform == 'win32');
|
||||||
|
Loading…
Reference in New Issue
Block a user