[permission model] Redesign

Conflicts:

	patches/patch_list.py
diff --git a/accounts/doctype/account/account.txt b/accounts/doctype/account/account.txt
index 459e102..c76a410 100644
--- a/accounts/doctype/account/account.txt
+++ b/accounts/doctype/account/account.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-30 12:49:46", 
   "docstatus": 0, 
-  "modified": "2013-09-24 11:22:18", 
+  "modified": "2013-11-26 17:35:12", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -138,6 +138,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_account", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Account", 
   "oldfieldname": "parent_account", 
   "oldfieldtype": "Link", 
@@ -154,7 +155,6 @@
   "oldfieldname": "account_type", 
   "oldfieldtype": "Select", 
   "options": "\nFixed Asset Account\nBank or Cash\nExpense Account\nTax\nIncome Account\nChargeable\nWarehouse", 
-  "permlevel": 0, 
   "search_index": 0
  }, 
  {
diff --git a/accounts/doctype/cost_center/cost_center.txt b/accounts/doctype/cost_center/cost_center.txt
index a9c7add..2c6a5e6 100644
--- a/accounts/doctype/cost_center/cost_center.txt
+++ b/accounts/doctype/cost_center/cost_center.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-23 19:57:17", 
   "docstatus": 0, 
-  "modified": "2013-07-22 15:23:10", 
+  "modified": "2013-11-26 17:34:50", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -74,6 +74,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_cost_center", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Cost Center", 
   "oldfieldname": "parent_cost_center", 
   "oldfieldtype": "Link", 
@@ -171,6 +172,7 @@
   "fieldname": "old_parent", 
   "fieldtype": "Link", 
   "hidden": 1, 
+  "ignore_restriction": 1, 
   "label": "old_parent", 
   "no_copy": 1, 
   "oldfieldname": "old_parent", 
diff --git a/hr/doctype/employee/employee.py b/hr/doctype/employee/employee.py
index 7129739..a7deb0f 100644
--- a/hr/doctype/employee/employee.py
+++ b/hr/doctype/employee/employee.py
@@ -43,9 +43,11 @@
 			self.update_profile()
 				
 	def update_user_default(self):
+		if not "HR User" in webnotes.local.user.get_roles():
+			webnotes.conn.set_default("Employee", self.doc.name, self.doc.user_id, "Restriction")
+
 		webnotes.conn.set_default("employee", self.doc.name, self.doc.user_id)
 		webnotes.conn.set_default("employee_name", self.doc.employee_name, self.doc.user_id)
-		webnotes.conn.set_default("company", self.doc.company, self.doc.user_id)
 		self.set_default_leave_approver()
 	
 	def set_default_leave_approver(self):
diff --git a/hr/doctype/employee/employee.txt b/hr/doctype/employee/employee.txt
index 856f26e..c5b5018 100644
--- a/hr/doctype/employee/employee.txt
+++ b/hr/doctype/employee/employee.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-03-07 09:04:18", 
   "docstatus": 0, 
-  "modified": "2013-11-03 11:19:10", 
+  "modified": "2013-11-26 16:36:34", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -415,6 +415,7 @@
   "doctype": "DocField", 
   "fieldname": "reports_to", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Reports to", 
   "oldfieldname": "reports_to", 
   "oldfieldtype": "Link", 
@@ -749,7 +750,6 @@
   "cancel": 0, 
   "create": 0, 
   "doctype": "DocPerm", 
-  "match": "employee", 
   "role": "Employee", 
   "write": 0
  }, 
@@ -757,7 +757,6 @@
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
-  "match": "company", 
   "role": "HR User", 
   "write": 1
  }, 
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",
diff --git a/setup/doctype/customer_group/customer_group.txt b/setup/doctype/customer_group/customer_group.txt
index f76a2e1..f90c14d 100644
--- a/setup/doctype/customer_group/customer_group.txt
+++ b/setup/doctype/customer_group/customer_group.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:23", 
   "docstatus": 0, 
-  "modified": "2013-11-02 16:52:51", 
+  "modified": "2013-11-26 17:35:45", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -66,6 +66,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_customer_group", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Customer Group", 
   "oldfieldname": "parent_customer_group", 
   "oldfieldtype": "Link", 
@@ -127,6 +128,7 @@
   "fieldname": "old_parent", 
   "fieldtype": "Link", 
   "hidden": 1, 
+  "ignore_restriction": 1, 
   "label": "old_parent", 
   "no_copy": 1, 
   "oldfieldname": "old_parent", 
diff --git a/setup/doctype/item_group/item_group.txt b/setup/doctype/item_group/item_group.txt
index b365893..5af9e27 100644
--- a/setup/doctype/item_group/item_group.txt
+++ b/setup/doctype/item_group/item_group.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-03-28 10:35:29", 
   "docstatus": 0, 
-  "modified": "2013-11-02 19:41:37", 
+  "modified": "2013-11-26 17:36:08", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -71,6 +71,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_item_group", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Item Group", 
   "no_copy": 0, 
   "oldfieldname": "parent_item_group", 
@@ -166,6 +167,7 @@
   "fieldname": "old_parent", 
   "fieldtype": "Link", 
   "hidden": 1, 
+  "ignore_restriction": 1, 
   "label": "old_parent", 
   "no_copy": 1, 
   "oldfieldname": "old_parent", 
diff --git a/setup/doctype/sales_person/sales_person.txt b/setup/doctype/sales_person/sales_person.txt
index 037c6de..8f08626 100644
--- a/setup/doctype/sales_person/sales_person.txt
+++ b/setup/doctype/sales_person/sales_person.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:24", 
   "docstatus": 0, 
-  "modified": "2013-09-10 10:53:28", 
+  "modified": "2013-11-26 17:35:29", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -65,6 +65,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_sales_person", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Sales Person", 
   "oldfieldname": "parent_sales_person", 
   "oldfieldtype": "Link", 
diff --git a/setup/doctype/territory/territory.txt b/setup/doctype/territory/territory.txt
index 200f24f..b764db5 100644
--- a/setup/doctype/territory/territory.txt
+++ b/setup/doctype/territory/territory.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:24", 
   "docstatus": 0, 
-  "modified": "2013-11-02 16:59:08", 
+  "modified": "2013-11-26 17:36:23", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -68,6 +68,7 @@
   "doctype": "DocField", 
   "fieldname": "parent_territory", 
   "fieldtype": "Link", 
+  "ignore_restriction": 1, 
   "label": "Parent Territory", 
   "oldfieldname": "parent_territory", 
   "oldfieldtype": "Link", 
@@ -136,6 +137,7 @@
   "fieldname": "old_parent", 
   "fieldtype": "Link", 
   "hidden": 1, 
+  "ignore_restriction": 1, 
   "label": "old_parent", 
   "no_copy": 1, 
   "oldfieldname": "old_parent",