# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
from frappe import _, throw
import frappe.utils.user
from frappe.permissions import check_admin_or_system_manager
from frappe.model.db_schema import type_map

def execute(filters=None):
	user, doctype = filters.get("user"), filters.get("doctype")
	validate(user, doctype)

	columns, fields = get_columns_and_fields(doctype)
	data = frappe.get_list(doctype, fields=fields, as_list=True, user=user)

	return columns, data

def validate(user, doctype):
	# check if current user is System Manager
	check_admin_or_system_manager()

	if not user:
		throw(_("Please specify user"))

	if not doctype:
		throw(_("Please specify doctype"))

def get_columns_and_fields(doctype):
	columns = ["Name:Link/{}:200".format(doctype)]
	fields = ["`name`"]
	for df in frappe.get_meta(doctype).fields:
		if df.in_list_view and df.fieldtype in type_map:
			fields.append("`{0}`".format(df.fieldname))
			fieldtype = "Link/{}".format(df.options) if df.fieldtype=="Link" else df.fieldtype
			columns.append("{label}:{fieldtype}:{width}".format(label=df.label, fieldtype=fieldtype, width=df.width or 100))

	return columns, fields

def query_doctypes(doctype, txt, searchfield, start, page_len, filters):
	user = filters.get("user")
	user_perms = frappe.utils.user.UserPermissions(user)
	user_perms.build_permissions()
	can_read = user_perms.can_read

	single_doctypes = [d[0] for d in frappe.db.get_values("DocType", {"issingle": 1})]

	out = []
	for dt in can_read:
		if txt.lower().replace("%", "") in dt.lower() and dt not in single_doctypes:
			out.append([dt])

	return out

@@ -348,16 +348,34 @@
  doctype
+, show_permissions
  = filte
@@ -412,16 +412,49 @@
 octype%22)
+, filters.get(%22show_permissions%22)
 %0A%09valida
@@ -597,16 +597,623 @@
 =user)%0A%0A
+%09if show_permissions:%0A %09%09columns = columns + %5B%22Read%22, %22Write%22, %22Create%22, %22Delete%22, %22Submit%22, %22Cancel%22, %22Amend%22, %22Print%22, %22Email%22,%0A %09%09                     %22Report%22, %22Import%22, %22Export%22, %22Share%22%5D%0A %09%09data = list(data)%0A %09%09for i,item in enumerate(data):%0A %09%09%09temp = frappe.permissions.get_doc_permissions(frappe.get_doc(doctype, item%5B0%5D), False,user)%0A %09%09%09data%5Bi%5D = item+(temp.get(%22read%22),temp.get(%22write%22),temp.get(%22create%22),temp.get(%22delete%22),temp.get(%22submit%22),temp.get(%22cancel%22),temp.get(%22amend%22),temp.get(%22print%22),temp.get(%22email%22),temp.get(%22report%22),temp.get(%22import%22),temp.get(%22export%22),temp.get(%22share%22),)%0A%0A
 %09return 

Update permitted_documents_for_user.py