fix: correctly fetch list of users with role (#17363)

fix: correctly fetch user list

`HasRole` table is attached to many doctypes, only User should be
filtered out.
This commit is contained in:
Ankush Menat 2022-06-30 15:29:32 +05:30 committed by GitHub
parent e8f46015f0
commit befabac17e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -67,7 +67,10 @@ class Role(Document):
def get_info_based_on_role(role, field="email"):
"""Get information of all users that have been assigned this role"""
users = frappe.get_list(
"Has Role", filters={"role": role}, parent_doctype="User", fields=["parent as user_name"]
"Has Role",
filters={"role": role, "parenttype": "User"},
parent_doctype="User",
fields=["parent as user_name"],
)
return get_user_info(users, field)

View file

@ -3,6 +3,7 @@
import unittest
import frappe
from frappe.core.doctype.role.role import get_info_based_on_role
test_records = frappe.get_test_records("Role")
@ -43,3 +44,11 @@ class TestUser(unittest.TestCase):
role.save()
user.reload()
self.assertTrue(user.user_type == "Website User")
def test_get_users_by_role(self):
role = "System Manager"
sys_managers = get_info_based_on_role(role, field="name")
for user in sys_managers:
self.assertIn(role, frappe.get_roles(user))