Check if user permission already exists (#5676)

* Check if user permission already exists

* Optimize code to check duplicate

* Replace frappe.db.exists with frappe.db.getall

since ['!=', name] filter was not working with frappe.db.exists
This commit is contained in:
Suraj Shetty 2018-06-14 10:53:58 +05:30 committed by Rushabh Mehta
parent 705bb43ee8
commit 2489bf4c7a

View file

@ -9,6 +9,16 @@ from frappe.permissions import (get_valid_perms, update_permission_property)
from frappe import _
class UserPermission(Document):
def validate(self):
duplicate_exists = frappe.db.get_all(self.doctype, filters={
'allow': self.allow,
'for_value': self.for_value,
'user': self.user,
'name': ['!=', self.name]
}, limit=1)
if duplicate_exists:
frappe.msgprint(_("User permission already exists"), raise_exception=True)
def on_update(self):
frappe.cache().delete_value('user_permissions')
frappe.publish_realtime('update_user_permissions')