[permission model] Redesign

Conflicts:

	patches/patch_list.py
diff --git a/patches/1311/p06_update_user_properties.py b/patches/1311/p06_update_user_properties.py
new file mode 100644
index 0000000..5d5b508
--- /dev/null
+++ b/patches/1311/p06_update_user_properties.py
@@ -0,0 +1,38 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import webnotes
+
+def execute():
+	update_user_properties()
+	update_permissions()
+	webnotes.clear_cache()
+
+def update_user_properties():
+	webnotes.reload_doc("core", "doctype", "docfield")
+	
+	for d in webnotes.conn.sql("""select parent, defkey, defvalue from tabDefaultValue 
+		where parent not in ('__global', 'Control Panel')""", as_dict=True):
+		df = webnotes.conn.sql("""select options from tabDocField 
+			where fieldname=%s and fieldtype='Link'""", d.defkey, as_dict=True)
+		
+		if df:
+			webnotes.conn.sql("""update tabDefaultValue 
+				set defkey=%s, parenttype='Restriction' 
+				where defkey=%s and 
+				parent not in ('__global', 'Control Panel')""", (df[0].options, d.defkey))
+				
+	# remove duplicate restrictions (if they exist)
+	for d in webnotes.conn.sql("""select parent, defkey, substr(defvalue,0,10), 
+		count(*) as cnt from tabDefaultValue 
+		where parent not in ('__global', 'Control Panel') 
+		group by parent, defkey, defvalue""", as_dict=1):
+		if d.cnt > 1:
+			webnotes.conn.sql("""delete from tabDefaultValue where parent=%s, defkey=%s, 
+				defvalue=%s limit %s""", (d.parent, d.defkey, d.defvalue, d.cnt-1))
+				
+def update_permissions():
+	# clear match conditions other than owner
+	webnotes.conn.sql("""update tabDocPerm set `match`=''
+		where ifnull(`match`,'') not in ('', 'owner')""")
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 49c0779..5da1433 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -254,6 +254,7 @@
 	"patches.1311.p04_update_comments",
 	"patches.1311.p05_website_brand_html",
 	"patches.1311.p06_fix_report_columns",
+	"patches.1311.p06_update_user_properties",
 	"execute:webnotes.delete_doc('DocType', 'Documentation Tool')",
 	"execute:webnotes.delete_doc('Report', 'Stock Ledger') #2013-11-29",
 	"patches.1312.p01_delete_old_stock_reports",