blob: 36879d216d0d50633927d4afc01a29809b0adc62 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
6
7def load_address_and_contact(doc, key):
8 """Loads address list and contact list in `__onload`"""
9 from erpnext.utilities.doctype.address.address import get_address_display
10
Saurabhbce02c42016-07-04 18:27:58 +053011 doc.get("__onload")["addr_list"] = [a.update({"display": get_address_display(a)}) \
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053012 for a in frappe.get_all("Address",
13 fields="*", filters={key: doc.name},
14 order_by="is_primary_address desc, modified desc")]
15
16 if doc.doctype != "Lead":
Saurabhbce02c42016-07-04 18:27:58 +053017 doc.get("__onload")["contact_list"] = frappe.get_all("Contact",
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053018 fields="*", filters={key: doc.name},
19 order_by="is_primary_contact desc, modified desc")
Anand Doshi89b8d112015-09-29 20:06:53 +053020
21def has_permission(doc, ptype, user):
22 links = get_permitted_and_not_permitted_links(doc.doctype)
23 if not links.get("not_permitted_links"):
24 # optimization: don't determine permissions based on link fields
25 return True
26
27 # True if any one is True or all are empty
28 names = []
29 for df in (links.get("permitted_links") + links.get("not_permitted_links")):
30 doctype = df.options
31 name = doc.get(df.fieldname)
Anand Doshi89b8d112015-09-29 20:06:53 +053032 names.append(name)
33
34 if name and frappe.has_permission(doctype, ptype, doc=name):
35 return True
36
37 if not any(names):
38 return True
Serpent Consulting Services Pvt Ltd8b231f72016-10-18 17:30:24 +053039 return False
Anand Doshi89b8d112015-09-29 20:06:53 +053040
41def get_permission_query_conditions_for_contact(user):
42 return get_permission_query_conditions("Contact")
43
44def get_permission_query_conditions_for_address(user):
45 return get_permission_query_conditions("Address")
46
47def get_permission_query_conditions(doctype):
48 links = get_permitted_and_not_permitted_links(doctype)
49
50 if not links.get("not_permitted_links"):
51 # when everything is permitted, don't add additional condition
52 return ""
Nabin Haitdace2b62015-10-01 13:54:46 +053053
54 elif not links.get("permitted_links"):
55 conditions = []
56
57 # when everything is not permitted
58 for df in links.get("not_permitted_links"):
59 # like ifnull(customer, '')='' and ifnull(supplier, '')=''
60 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
61
62 return "( " + " and ".join(conditions) + " )"
Anand Doshi89b8d112015-09-29 20:06:53 +053063
64 else:
65 conditions = []
66
67 for df in links.get("permitted_links"):
68 # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
Nabin Haitdace2b62015-10-01 13:54:46 +053069 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
Anand Doshi89b8d112015-09-29 20:06:53 +053070
71 return "( " + " or ".join(conditions) + " )"
72
73def get_permitted_and_not_permitted_links(doctype):
74 permitted_links = []
75 not_permitted_links = []
76
77 meta = frappe.get_meta(doctype)
78
79 for df in meta.get_link_fields():
RobertSchoutenf28a5da2016-06-07 17:03:41 +080080 if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
Anand Doshi89b8d112015-09-29 20:06:53 +053081 continue
82
83 if frappe.has_permission(df.options):
84 permitted_links.append(df)
85 else:
86 not_permitted_links.append(df)
87
88 return {
89 "permitted_links": permitted_links,
90 "not_permitted_links": not_permitted_links
91 }