mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2024-11-22 08:46:39 -06:00
Fixed multiple CLI issues. #2483
This commit is contained in:
parent
4d74f84d80
commit
fdaedacf40
@ -584,5 +584,8 @@ If auth_source is not given, Internal authentication will be consider by default
|
|||||||
|
|
||||||
/path/to/python /path/to/setup.py set-prefs user1@gmail.com sqleditor:editor:comma_first=true
|
/path/to/python /path/to/setup.py set-prefs user1@gmail.com sqleditor:editor:comma_first=true
|
||||||
|
|
||||||
|
# To specify multiple preferences options
|
||||||
|
/path/to/python /path/to/setup.py set-prefs user1@gmail.com sqleditor:editor:comma_first=true sqleditor:keyboard_shortcuts:toggle_case=true
|
||||||
|
|
||||||
# to specify an auth_source
|
# to specify an auth_source
|
||||||
/path/to/python /path/to/setup.py set-prefs user1@gmail.com sqleditor:editor:comma_first=true --auth-source=ldap
|
/path/to/python /path/to/setup.py set-prefs user1@gmail.com sqleditor:editor:comma_first=true --auth-source=ldap
|
||||||
|
@ -599,10 +599,6 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
window.open(url_for('help.static', { 'filename': 'preferences.html' }), 'pgadmin_help');
|
window.open(url_for('help.static', { 'filename': 'preferences.html' }), 'pgadmin_help');
|
||||||
};
|
};
|
||||||
|
|
||||||
const onDialogHelpCli = () => {
|
|
||||||
window.open(url_for('preferences.get_all_cli'), 'pgadmin_help');
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box height={'100%'}>
|
<Box height={'100%'}>
|
||||||
<Box className={classes.root}>
|
<Box className={classes.root}>
|
||||||
@ -628,7 +624,6 @@ export default function PreferencesComponent({ ...props }) {
|
|||||||
<Box className={classes.footer}>
|
<Box className={classes.footer}>
|
||||||
<Box>
|
<Box>
|
||||||
<PgIconButton data-test="dialog-help" onClick={onDialogHelp} icon={<HelpIcon />} title={gettext('Help for this dialog.')} />
|
<PgIconButton data-test="dialog-help" onClick={onDialogHelp} icon={<HelpIcon />} title={gettext('Help for this dialog.')} />
|
||||||
<PgIconButton data-test="dialog-help-cli" onClick={onDialogHelpCli} icon={<HelpIcon />} title={gettext('Help for this dialog.')} />
|
|
||||||
</Box>
|
</Box>
|
||||||
<Box className={classes.actionBtn} marginLeft="auto">
|
<Box className={classes.actionBtn} marginLeft="auto">
|
||||||
<DefaultButton className={classes.buttonMargin} onClick={() => { props.closeModal();}} startIcon={<CloseSharpIcon onClick={() => { props.closeModal();}} />}>
|
<DefaultButton className={classes.buttonMargin} onClick={() => { props.closeModal();}} startIcon={<CloseSharpIcon onClick={() => { props.closeModal();}} />}>
|
||||||
|
@ -28,11 +28,16 @@ export function parseApiError(error, withData=false) {
|
|||||||
// The request was made and the server responded with a status code
|
// The request was made and the server responded with a status code
|
||||||
// that falls out of the range of 2xx
|
// that falls out of the range of 2xx
|
||||||
if(error.response.headers['content-type'] == 'application/json') {
|
if(error.response.headers['content-type'] == 'application/json') {
|
||||||
let errormsg = error.response.data.errormsg;
|
let err_resp_data = error.response.data;
|
||||||
let data = error.response.data.data;
|
if (err_resp_data.response != undefined && Array.isArray(err_resp_data.response.errors)) {
|
||||||
// If we want to use data which came with error set withData
|
return err_resp_data.response.errors[0];
|
||||||
// flag to true.
|
} else {
|
||||||
return withData ? {errormsg, data} : errormsg;
|
let errormsg = err_resp_data.errormsg;
|
||||||
|
let data = error.response.data.data;
|
||||||
|
// If we want to use data which came with error set withData
|
||||||
|
// flag to true.
|
||||||
|
return withData ? {errormsg, data} : errormsg;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return error.response.statusText;
|
return error.response.statusText;
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,7 @@ function UserManagementDialog({onClose}) {
|
|||||||
setRoles(res.data);
|
setRoles(res.data);
|
||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
pgAdmin.Browser.notifier.error(err);
|
pgAdmin.Browser.notifier.error(parseApiError(err));
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
pgAdmin.Browser.notifier.error(parseApiError(error));
|
pgAdmin.Browser.notifier.error(parseApiError(error));
|
||||||
|
148
web/setup.py
148
web/setup.py
@ -36,15 +36,15 @@ if 'SERVER_MODE' in globals():
|
|||||||
else:
|
else:
|
||||||
builtins.SERVER_MODE = None
|
builtins.SERVER_MODE = None
|
||||||
|
|
||||||
from pgadmin.model import db, Version, User,\
|
from pgadmin.model import db, Version, User, \
|
||||||
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
|
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
|
||||||
from pgadmin import create_app
|
from pgadmin import create_app
|
||||||
from pgadmin.utils import clear_database_servers, dump_database_servers,\
|
from pgadmin.utils import clear_database_servers, dump_database_servers, \
|
||||||
load_database_servers
|
load_database_servers
|
||||||
from pgadmin.setup import db_upgrade, create_app_data_directory
|
from pgadmin.setup import db_upgrade, create_app_data_directory
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from typing_extensions import Annotated
|
from typing_extensions import Annotated
|
||||||
from pgadmin.utils.constants import MIMETYPE_APP_JS, INTERNAL, LDAP, OAUTH2,\
|
from pgadmin.utils.constants import MIMETYPE_APP_JS, INTERNAL, LDAP, OAUTH2, \
|
||||||
KERBEROS, WEBSERVER
|
KERBEROS, WEBSERVER
|
||||||
from pgadmin.tools.user_management import create_user, delete_user, update_user
|
from pgadmin.tools.user_management import create_user, delete_user, update_user
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@ -74,10 +74,13 @@ class ManageServers:
|
|||||||
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
||||||
print('----------')
|
print('----------')
|
||||||
|
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
try:
|
||||||
with app.test_request_context():
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
dump_database_servers(output_file, server, dump_user, True,
|
with app.test_request_context():
|
||||||
auth_source)
|
dump_database_servers(output_file, server, dump_user, True,
|
||||||
|
auth_source)
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e))
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def load_servers(input_file: str, user: Optional[str] = None,
|
def load_servers(input_file: str, user: Optional[str] = None,
|
||||||
@ -101,12 +104,15 @@ class ManageServers:
|
|||||||
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
print('SQLite pgAdmin config:', config.SQLITE_PATH)
|
||||||
print('----------')
|
print('----------')
|
||||||
|
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
try:
|
||||||
with app.test_request_context():
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
if replace:
|
with app.test_request_context():
|
||||||
clear_database_servers(load_user, True, auth_source)
|
if replace:
|
||||||
load_database_servers(input_file, None, load_user, True,
|
clear_database_servers(load_user, True, auth_source)
|
||||||
auth_source)
|
load_database_servers(input_file, None, load_user, True,
|
||||||
|
auth_source)
|
||||||
|
except Exception as e:
|
||||||
|
print(str(e))
|
||||||
|
|
||||||
|
|
||||||
class AuthExtTypes(str, Enum):
|
class AuthExtTypes(str, Enum):
|
||||||
@ -227,19 +233,26 @@ class ManageUsers:
|
|||||||
else:
|
else:
|
||||||
status, msg = update_user(uid, data)
|
status, msg = update_user(uid, data)
|
||||||
if status:
|
if status:
|
||||||
_user = ManageUsers.get_users(username=email,
|
_user = ManageUsers.get_users_from_db(username=email,
|
||||||
auth_source=INTERNAL,
|
auth_source=INTERNAL,
|
||||||
console=False)
|
console=False)
|
||||||
ManageUsers.display_user(_user[0], console, json)
|
ManageUsers.display_user(_user[0], console, json)
|
||||||
else:
|
else:
|
||||||
print('Something went wrong. ' + str(msg))
|
print('Something went wrong. ' + str(msg))
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def get_users(username:Optional[str] = None,
|
def get_users(username: Optional[str] = None,
|
||||||
auth_source: AuthType = None,
|
auth_source: AuthType = None,
|
||||||
console:Optional[bool] = True,
|
json: Optional[bool] = False
|
||||||
json:Optional[bool] = False
|
|
||||||
):
|
):
|
||||||
|
ManageUsers.get_users_from_db(username, auth_source, True, json)
|
||||||
|
|
||||||
|
@app.command()
|
||||||
|
def get_users_from_db(username: Optional[str] = None,
|
||||||
|
auth_source: AuthType = None,
|
||||||
|
console: Optional[bool] = True,
|
||||||
|
json: Optional[bool] = False
|
||||||
|
):
|
||||||
"""Get user(s) details."""
|
"""Get user(s) details."""
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
usr = None
|
usr = None
|
||||||
@ -264,9 +277,9 @@ class ManageUsers:
|
|||||||
'locked': u.locked
|
'locked': u.locked
|
||||||
}
|
}
|
||||||
users_data.append(_data)
|
users_data.append(_data)
|
||||||
if console:
|
if console:
|
||||||
ManageUsers.display_user(_data, False, json)
|
ManageUsers.display_user(users_data, console, json)
|
||||||
if not console:
|
else:
|
||||||
return users_data
|
return users_data
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
@ -312,7 +325,7 @@ class ManageUsers:
|
|||||||
def create_user(data, console, json):
|
def create_user(data, console, json):
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
username = data['username'] if 'username' in data else\
|
username = data['username'] if 'username' in data else \
|
||||||
data['email']
|
data['email']
|
||||||
uid = ManageUsers.get_user(username=username,
|
uid = ManageUsers.get_user(username=username,
|
||||||
auth_source=data['auth_source'])
|
auth_source=data['auth_source'])
|
||||||
@ -342,25 +355,30 @@ class ManageUsers:
|
|||||||
return usr.id
|
return usr.id
|
||||||
|
|
||||||
def display_user(data, _console, _json):
|
def display_user(data, _console, _json):
|
||||||
if _json:
|
if _console:
|
||||||
json_formatted_str = jsonlib.dumps(data, indent=0)
|
if _json:
|
||||||
console.print(json_formatted_str)
|
json_formatted_str = jsonlib.dumps(data, indent=0)
|
||||||
else:
|
console.print(json_formatted_str)
|
||||||
table = Table(title="User Details", box=box.ASCII)
|
else:
|
||||||
table.add_column("Field", style="green")
|
if isinstance(data, dict):
|
||||||
table.add_column("Value", style="green")
|
data = [data]
|
||||||
|
for _data in data:
|
||||||
|
table = Table(title="User Details", box=box.ASCII)
|
||||||
|
table.add_column("Field", style="green")
|
||||||
|
table.add_column("Value", style="green")
|
||||||
|
|
||||||
if 'username' in data:
|
if 'username' in _data:
|
||||||
table.add_row("Username", data['username'])
|
table.add_row("Username", _data['username'])
|
||||||
if 'email' in data:
|
if 'email' in _data:
|
||||||
table.add_row("Email", data['email'])
|
table.add_row("Email", _data['email'])
|
||||||
table.add_row("auth_source", data['auth_source'])
|
table.add_row("auth_source", _data['auth_source'])
|
||||||
table.add_row("role",
|
table.add_row("role",
|
||||||
"Admin" if data['role'] and data['role'] != 2 else
|
"Admin" if _data['role'] and
|
||||||
"Non-admin")
|
_data['role'] != 2 else
|
||||||
table.add_row("active",
|
"Non-admin")
|
||||||
'True' if data['active'] else 'False')
|
table.add_row("active",
|
||||||
console.print(table)
|
'True' if _data['active'] else 'False')
|
||||||
|
console.print(table)
|
||||||
|
|
||||||
|
|
||||||
class ManagePreferences:
|
class ManagePreferences:
|
||||||
@ -376,7 +394,10 @@ class ManagePreferences:
|
|||||||
return usr.id
|
return usr.id
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def get_prefs(id: Optional[bool] = None, json: Optional[bool] = False):
|
def get_prefs(json: Optional[bool] = False):
|
||||||
|
return ManagePreferences.fetch_prefs()
|
||||||
|
|
||||||
|
def fetch_prefs(id: Optional[bool] = None, json: Optional[bool] = False):
|
||||||
"""Get Preferences List."""
|
"""Get Preferences List."""
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
table = Table(title="Pref Details", box=box.ASCII)
|
table = Table(title="Pref Details", box=box.ASCII)
|
||||||
@ -419,13 +440,14 @@ class ManagePreferences:
|
|||||||
json_formatted_str = jsonlib.dumps(
|
json_formatted_str = jsonlib.dumps(
|
||||||
{"Preferences": all_preferences},
|
{"Preferences": all_preferences},
|
||||||
indent=0)
|
indent=0)
|
||||||
console.print(json_formatted_str)
|
print(json_formatted_str)
|
||||||
else:
|
else:
|
||||||
console.print(table)
|
print(table)
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def set_prefs(username, pref_options: List[str],
|
def set_prefs(username, pref_options: List[str],
|
||||||
auth_source: AuthType = AuthType.internal,
|
auth_source: AuthType = AuthType.internal,
|
||||||
|
console: Optional[bool] = True,
|
||||||
json: Optional[bool] = False):
|
json: Optional[bool] = False):
|
||||||
"""Set User preferences."""
|
"""Set User preferences."""
|
||||||
user_id = ManagePreferences.get_user(username, auth_source)
|
user_id = ManagePreferences.get_user(username, auth_source)
|
||||||
@ -436,24 +458,23 @@ class ManagePreferences:
|
|||||||
print("User not found.")
|
print("User not found.")
|
||||||
return
|
return
|
||||||
|
|
||||||
prefs = ManagePreferences.get_prefs(True)
|
prefs = ManagePreferences.fetch_prefs(True)
|
||||||
app = create_app(config.APP_NAME + '-cli')
|
app = create_app(config.APP_NAME + '-cli')
|
||||||
|
invalid_prefs = []
|
||||||
|
valid_prefs = []
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
from pgadmin.preferences import save_pref
|
from pgadmin.preferences import save_pref
|
||||||
for opt in pref_options:
|
for opt in pref_options:
|
||||||
val = opt.split("=")
|
val = opt.split("=")
|
||||||
|
if len(val) <= 1:
|
||||||
|
print('Preference key=value is required, example: '
|
||||||
|
'[green]sqleditor:editor:comma_first=true[/green]')
|
||||||
|
return
|
||||||
final_opt = val[0].split(":")
|
final_opt = val[0].split(":")
|
||||||
val = val[1]
|
val = val[1]
|
||||||
f = ":".join(final_opt)
|
f = ":".join(final_opt)
|
||||||
if f in prefs:
|
if f in prefs:
|
||||||
ids = prefs[f].split(":")
|
ids = prefs[f].split(":")
|
||||||
save_pref({
|
|
||||||
'mid': ids[0],
|
|
||||||
'category_id': ids[1],
|
|
||||||
'id': ids[2],
|
|
||||||
'name': final_opt[2],
|
|
||||||
'user_id': user_id,
|
|
||||||
'value': val})
|
|
||||||
_row = {
|
_row = {
|
||||||
'mid': ids[0],
|
'mid': ids[0],
|
||||||
'category_id': ids[1],
|
'category_id': ids[1],
|
||||||
@ -461,14 +482,23 @@ class ManagePreferences:
|
|||||||
'name': final_opt[2],
|
'name': final_opt[2],
|
||||||
'user_id': user_id,
|
'user_id': user_id,
|
||||||
'value': val}
|
'value': val}
|
||||||
if json:
|
save_pref(_row)
|
||||||
json_formatted_str = jsonlib.dumps(_row, indent=0)
|
valid_prefs.append(_row)
|
||||||
console.print(json_formatted_str)
|
|
||||||
else:
|
|
||||||
table.add_row(jsonlib.dumps(_row))
|
|
||||||
|
|
||||||
if not json:
|
if not json:
|
||||||
console.print(table)
|
table.add_row(jsonlib.dumps(_row))
|
||||||
|
else:
|
||||||
|
invalid_prefs.append(f)
|
||||||
|
|
||||||
|
if len(invalid_prefs) >= 1:
|
||||||
|
print("Preference(s) [red]{0}[/red] not found.".format(
|
||||||
|
(', ').join(
|
||||||
|
invalid_prefs)))
|
||||||
|
|
||||||
|
if not json and console:
|
||||||
|
print(table)
|
||||||
|
elif json and console:
|
||||||
|
print(jsonlib.dumps(valid_prefs, indent=2))
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
|
Loading…
Reference in New Issue
Block a user