Security Labels and the Variables are used in many modules under the Database Node.

Provide functions in the database node to handle them.
This commit is contained in:
Khushboo Vashi 2016-02-26 15:21:55 +00:00 committed by Dave Page
parent b3ff96d2db
commit d4a13eaafa
2 changed files with 106 additions and 33 deletions

View File

@ -6,6 +6,9 @@
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Implements the Database Node"""
import json
from flask import render_template, make_response, current_app, request, jsonify
from flask.ext.babel import gettext as _
@ -17,6 +20,8 @@ from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \
from pgadmin.browser.collection import CollectionNodeModule
import pgadmin.browser.server_groups.servers as servers
from pgadmin.utils.ajax import precondition_required, gone
from pgadmin.browser.server_groups.servers.databases.utils import \
parse_sec_labels_from_db, parse_variables_from_db
from pgadmin.utils.driver import get_driver
from config import PG_DEFAULT_DRIVER
from functools import wraps
@ -273,42 +278,20 @@ class DatabaseView(PGChildNodeView):
if not status:
return internal_server_error(errormsg=res1)
# sending result to formtter
frmtd_reslt = self.formatter(result, res1)
# mergeing formated result with main result again
result.update(frmtd_reslt)
# Get Formatted Security Labels
frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels'])
result.update(frmtd_sec_labels)
# Get Formatted Variables
frmtd_variables = parse_variables_from_db(res1['rows'])
result.update(frmtd_variables)
return ajax_response(
response=result,
status=200
)
@staticmethod
def formatter(result, varibles_rset):
""" We will use this function to format our output for
security label & variables"""
frmtd_result = dict()
sec_lbls = []
if 'seclabels' in result and result['seclabels'] is not None:
for sec in result['seclabels']:
sec = re.search(r'([^=]+)=(.*$)', sec)
sec_lbls.append({
'provider': sec.group(1),
'security_label': sec.group(2)
})
frmtd_result.update({"seclabels" :sec_lbls})
variablesLst = []
for row in varibles_rset['rows']:
for d in row['setconfig']:
var_name, var_value = d.split("=")
# Because we save as boolean string in db so it needs conversion
if var_value == 'false' or var_value == 'off':
var_value = False
variablesLst.append({'role': row['user_name'], 'name': var_name, 'value': var_value, 'database': row['db_name']})
frmtd_result.update({"variables" : variablesLst})
# returning final result
return frmtd_result
@staticmethod
def formatdbacl(res, dbacl):
for row in dbacl:
@ -847,8 +830,13 @@ class DatabaseView(PGChildNodeView):
if not status:
return internal_server_error(errormsg=res1)
frmtd_reslt = self.formatter(result, res1)
result.update(frmtd_reslt)
# Get Formatted Security Labels
frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels'])
result.update(frmtd_sec_labels)
# Get Formatted Variables
frmtd_variables = parse_variables_from_db(res1['rows'])
result.update(frmtd_variables)
SQL = self.get_new_sql(gid, sid, result, did)

View File

@ -0,0 +1,85 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Database helper utilities"""
def parse_sec_labels_from_db(db_sec_labels):
"""
Function to format the output for security label.
Args:
db_sec_labels : Security Label Array in (provider=label) format
Returns:
Security Label Object in below format:
{'seclabels': [{'provider': 'provider_name', 'security_label':
'security_label'},...]}
"""
sec_lbls = []
if db_sec_labels is not None:
for sec in db_sec_labels:
sec = re.search(r'([^=]+)=(.*$)', sec)
sec_lbls.append({
'provider': sec.group(1),
'security_label': sec.group(2)
})
return {"seclabels": sec_lbls}
def parse_variables_from_db(db_variables):
"""
Function to format the output for variables.
Args:
db_variables: Variable object
Expected Object Format:
[
{
'setconfig': Variable Config Parameters,
'user_name': User Name,
'db_name': Database Name
},...
]
where:
user_name and database are optional
Returns:
Variable Object in below format:
{
'variables': [
{'name': 'var_name', 'value': 'var_value',
'user_name': 'user_name', 'database': 'database_name'},
...]
}
where:
user_name and database are optional
"""
variables_lst = []
if db_variables is not None:
for row in db_varibles:
if 'setconfig' in row and row['setconfig'] is not None:
for d in row['setconfig']:
var_name, var_value = d.split("=")
# Because we save as boolean string in db so it needs
# conversion
if var_value == 'false' or var_value == 'off':
var_value = False
var_dict = {'name': var_name, 'value': var_value}
if 'user_name' in row:
var_dict['role'] = row['user_name']
if 'db_name' in row:
var_dict['database'] = row['db_name']
variables_lst.append(var_dict)
return {"variables": variables_lst}