Restricting all suppliers to their Purchase Orders for Desk Access

@frappe.whitelist()
def restrict_supplier():
	for d in frappe.get_list("User",{'role':'Vendor'}):
		linked_contacts = frappe.get_all('Contact',filters = dict(user=d.get('name')), fields=['*'])
		for j in linked_contacts:
			doc = frappe.get_doc("Contact", j.get('name'))
			if not doc.get('links'): continue
			print("Working on {0} = {1}".format(d.get('name'), doc.links[0].get('link_name')))
			if frappe.get_value("User Permission",{"user":d.get('name')}): continue
			frappe.get_doc(
					dict(
						doctype="User Permission",
						user=d.get("name"),
						allow="Supplier",
						for_value=doc.links[0].get('link_name'),
						apply_to_all_doctypes=0,
						applicable_for="Purchase Order",
					)
			).insert(ignore_permissions=True)

Good stuff @saleemdev