Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | 5eeef7f | 2014-11-24 14:16:51 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | |
| 7 | def 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 | |
| 11 | doc.get("__onload").addr_list = [a.update({"display": get_address_display(a)}) \ |
| 12 | 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": |
| 17 | doc.get("__onload").contact_list = frappe.get_all("Contact", |
| 18 | fields="*", filters={key: doc.name}, |
| 19 | order_by="is_primary_contact desc, modified desc") |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 20 | |
| 21 | def 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) |
| 32 | |
| 33 | names.append(name) |
| 34 | |
| 35 | if name and frappe.has_permission(doctype, ptype, doc=name): |
| 36 | return True |
| 37 | |
| 38 | if not any(names): |
| 39 | return True |
| 40 | |
| 41 | else: |
| 42 | return False |
| 43 | |
| 44 | def get_permission_query_conditions_for_contact(user): |
| 45 | return get_permission_query_conditions("Contact") |
| 46 | |
| 47 | def get_permission_query_conditions_for_address(user): |
| 48 | return get_permission_query_conditions("Address") |
| 49 | |
| 50 | def get_permission_query_conditions(doctype): |
| 51 | links = get_permitted_and_not_permitted_links(doctype) |
| 52 | |
| 53 | if not links.get("not_permitted_links"): |
| 54 | # when everything is permitted, don't add additional condition |
| 55 | return "" |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 56 | |
| 57 | elif not links.get("permitted_links"): |
| 58 | conditions = [] |
| 59 | |
| 60 | # when everything is not permitted |
| 61 | for df in links.get("not_permitted_links"): |
| 62 | # like ifnull(customer, '')='' and ifnull(supplier, '')='' |
| 63 | conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname)) |
| 64 | |
| 65 | return "( " + " and ".join(conditions) + " )" |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 66 | |
| 67 | else: |
| 68 | conditions = [] |
| 69 | |
| 70 | for df in links.get("permitted_links"): |
| 71 | # like ifnull(customer, '')!='' or ifnull(supplier, '')!='' |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 72 | conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname)) |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 73 | |
| 74 | return "( " + " or ".join(conditions) + " )" |
| 75 | |
| 76 | def get_permitted_and_not_permitted_links(doctype): |
| 77 | permitted_links = [] |
| 78 | not_permitted_links = [] |
| 79 | |
| 80 | meta = frappe.get_meta(doctype) |
| 81 | |
| 82 | for df in meta.get_link_fields(): |
RobertSchouten | f28a5da | 2016-06-07 17:03:41 +0800 | [diff] [blame] | 83 | if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"): |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 84 | continue |
| 85 | |
| 86 | if frappe.has_permission(df.options): |
| 87 | permitted_links.append(df) |
| 88 | else: |
| 89 | not_permitted_links.append(df) |
| 90 | |
| 91 | return { |
| 92 | "permitted_links": permitted_links, |
| 93 | "not_permitted_links": not_permitted_links |
| 94 | } |