diff --git a/frappe/core/doctype/user/user.py b/frappe/core/doctype/user/user.py index 395af1a1a0..32fdb38027 100644 --- a/frappe/core/doctype/user/user.py +++ b/frappe/core/doctype/user/user.py @@ -1078,42 +1078,33 @@ def reset_password(user: str) -> str: @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def user_query(doctype, txt, searchfield, start, page_len, filters): - from frappe.desk.reportview import get_filters_cond, get_match_cond - doctype = "User" - conditions = [] - user_type_condition = "and user_type != 'Website User'" - if filters and filters.get("ignore_user_type") and frappe.session.data.user_type == "System User": - user_type_condition = "" - filters and filters.pop("ignore_user_type", None) + list_filters = { + "enabled": 1, + "docstatus": ["<", 2], + "name": ["not in", STANDARD_USERS], + searchfield: ["like", f"%{txt}%"], + } - txt = f"%{txt}%" - return frappe.db.sql( - """SELECT `name`, CONCAT_WS(' ', first_name, middle_name, last_name) - FROM `tabUser` - WHERE `enabled`=1 - {user_type_condition} - AND `docstatus` < 2 - AND `name` NOT IN ({standard_users}) - AND ({key} LIKE %(txt)s - OR CONCAT_WS(' ', first_name, middle_name, last_name) LIKE %(txt)s) - {fcond} {mcond} - ORDER BY - CASE WHEN `name` LIKE %(txt)s THEN 0 ELSE 1 END, - CASE WHEN concat_ws(' ', first_name, middle_name, last_name) LIKE %(txt)s - THEN 0 ELSE 1 END, - NAME asc - LIMIT %(page_len)s OFFSET %(start)s - """.format( - user_type_condition=user_type_condition, - standard_users=", ".join(frappe.db.escape(u) for u in STANDARD_USERS), - key=searchfield, - fcond=get_filters_cond(doctype, filters, conditions), - mcond=get_match_cond(doctype), - ), - dict(start=start, page_len=page_len, txt=txt), - ) + if filters: + if not (filters.get("ignore_user_type") and frappe.session.data.user_type == "System User"): + list_filters["user_type"] = ["!=", "Website User"] + + filters.pop("ignore_user_type", None) + list_filters.update(filters) + + return [ + (user.name, user.full_name) + for user in frappe.get_list( + doctype, + filters=list_filters, + fields=["name", "full_name"], + limit_start=start, + limit_page_length=page_len, + order_by="name asc", + ) + ] def get_total_users():