Procházet zdrojové kódy

Filter query reports based on permissions using doctype name instead of fieldnames

version-14
Anand Doshi před 11 roky
rodič
revize
ae38bcf40f
3 změnil soubory, kde provedl 26 přidání a 30 odebrání
  1. +1
    -1
      frappe/model/db_query.py
  2. +14
    -6
      frappe/public/js/frappe/views/query_report.js
  3. +11
    -23
      frappe/widgets/query_report.py

+ 1
- 1
frappe/model/db_query.py Zobrazit soubor

@@ -256,7 +256,7 @@ class DatabaseQuery(object):
fieldname=df.fieldname, fieldname=df.fieldname,
values=", ".join([('"'+v.replace('"', '\"')+'"') for v in user_permissions[df.options]]) values=", ".join([('"'+v.replace('"', '\"')+'"') for v in user_permissions[df.options]])
)) ))
match_filters[df.fieldname] = user_permissions[df.options]
match_filters[df.options] = user_permissions[df.options]


if match_conditions: if match_conditions:
self.match_conditions.append(" and ".join(match_conditions)) self.match_conditions.append(" and ".join(match_conditions))


+ 14
- 6
frappe/public/js/frappe/views/query_report.js Zobrazit soubor

@@ -475,14 +475,22 @@ frappe.views.QueryReport = Class.extend({
// apply inline filters // apply inline filters
if (!me.inline_filter(item)) return false; if (!me.inline_filter(item)) return false;


var parent_name = item[me.parent_field];
while (parent_name) {
if (me.item_by_name[parent_name]._collapsed) {
return false;
try {
var parent_name = item[me.parent_field];
while (parent_name) {
if (me.item_by_name[parent_name]._collapsed) {
return false;
}
parent_name = me.item_by_name[parent_name][me.parent_field];
} }
parent_name = me.item_by_name[parent_name][me.parent_field];
return true;
} catch (e) {
if (e.message.indexOf("[parent_name] is undefined")!==-1) {
msgprint(__("Unable to display this tree report, due to missing data. Most likely, it is being filtered out due to permissions."));
}

throw e;
} }
return true;
}, },
tree_formatter: function(row, cell, value, columnDef, dataContext) { tree_formatter: function(row, cell, value, columnDef, dataContext) {
var me = frappe.query_report; var me = frappe.query_report;


+ 11
- 23
frappe/widgets/query_report.py Zobrazit soubor

@@ -138,7 +138,6 @@ def add_total_row(result, columns):


def get_filtered_data(ref_doctype, columns, data): def get_filtered_data(ref_doctype, columns, data):
result = [] result = []

linked_doctypes = get_linked_doctypes(columns) linked_doctypes = get_linked_doctypes(columns)
match_filters_per_doctype = get_user_match_filters(linked_doctypes, ref_doctype) match_filters_per_doctype = get_user_match_filters(linked_doctypes, ref_doctype)


@@ -153,17 +152,19 @@ def get_filtered_data(ref_doctype, columns, data):
return result return result


def has_match(row, linked_doctypes, doctype_match_filters): def has_match(row, linked_doctypes, doctype_match_filters):
filter_column_cache = {}

resultant_match = True resultant_match = True

if not row:
# allow empty rows :)
return resultant_match

for doctype, filter_list in doctype_match_filters.items(): for doctype, filter_list in doctype_match_filters.items():
matched_for_doctype = False matched_for_doctype = False


for match_filters in filter_list: for match_filters in filter_list:
match = True match = True
matched_columns = get_matched_columns(linked_doctypes, match_filters, filter_column_cache)
for col, idx in matched_columns.items():
if row[idx] not in match_filters[col]:
for dt, idx in linked_doctypes.items():
if dt in match_filters and row[idx] not in match_filters[dt]:
match = False match = False
break break


@@ -194,7 +195,7 @@ def get_linked_doctypes(columns):


# dict # dict
elif col.get("fieldtype")=="Link" and col.get("options"): elif col.get("fieldtype")=="Link" and col.get("options"):
linked_doctypes[col["options"]] = idx
linked_doctypes[col["options"]] = col["fieldname"]


return linked_doctypes return linked_doctypes


@@ -202,21 +203,8 @@ def get_user_match_filters(doctypes, ref_doctype):
match_filters = {} match_filters = {}


for dt in doctypes: for dt in doctypes:
match_filters[dt] = frappe.widgets.reportview.build_match_conditions(dt, False)
filter_list = frappe.widgets.reportview.build_match_conditions(dt, False)
if filter_list:
match_filters[dt] = filter_list


return match_filters return match_filters

def get_matched_columns(linked_doctypes, match_filters, filter_column_cache):
if not filter_column_cache.get(match_filters.keys()):
if "owner" in match_filters:
match_filters["user"] = match_filters["owner"]

col_idx_map = {}
for dt, idx in linked_doctypes.items():
link_field = dt.lower().replace(" ", "_")
if link_field in match_filters:
col_idx_map[link_field] = idx

filter_column_cache[match_filters.keys()] = col_idx_map

return filter_column_cache[match_filters.keys()]

Načítá se…
Zrušit
Uložit